Python 除非通过gdb运行,否则如何调试错误代码?
这是一个单线程代码 特别是:ahocarasick Python扩展模块(易于安装ahocarasick) 我把这个问题归结为一个简单的例子:Python 除非通过gdb运行,否则如何调试错误代码?,python,gdb,segmentation-fault,Python,Gdb,Segmentation Fault,这是一个单线程代码 特别是:ahocarasick Python扩展模块(易于安装ahocarasick) 我把这个问题归结为一个简单的例子: import ahocorasick t = ahocorasick.KeywordTree() t.add("a") 当我在gdb中运行它时,一切正常,当我将这些指令输入到pythoncli中时也会发生同样的情况。然而,当我试图正常运行脚本时,我会遇到一个错误 更奇怪的是,导致segfault(通过堆芯转储分析确定)的行是一个常规的int增量(参见函
import ahocorasick
t = ahocorasick.KeywordTree()
t.add("a")
当我在gdb中运行它时,一切正常,当我将这些指令输入到pythoncli中时也会发生同样的情况。然而,当我试图正常运行脚本时,我会遇到一个错误
更奇怪的是,导致segfault(通过堆芯转储分析确定)的行是一个常规的int增量(参见函数体的底部)
我完全被这一刻困住了,我能做什么
int
aho_corasick_addstring(aho_corasick_t *in, unsigned char *string, size_t n)
{
aho_corasick_t* g = in;
aho_corasick_state_t *state,*s = NULL;
int j = 0;
state = g->zerostate;
// As long as we have transitions follow them
while( j != n &&
(s = aho_corasick_goto_get(state,*(string+j))) != FAIL )
{
state = s;
++j;
}
if ( j == n ) {
/* dyoo: added so that if a keyword ends up in a prefix
of another, we still mark that as a match.*/
aho_corasick_output(s) = j;
return 0;
}
while( j != n )
{
// Create new state
if ( (s = xalloc(sizeof(aho_corasick_state_t))) == NULL )
return -1;
s->id = g->newstate++;
debug(printf("allocating state %d\n", s->id)); /* debug */
s->depth = state->depth + 1;
/* FIXME: check the error return value of
aho_corasick_goto_initialize. */
aho_corasick_goto_initialize(s);
// Create transition
aho_corasick_goto_set(state,*(string+j), s);
debug(printf("%u -> %c -> %u\n",state->id,*(string+j),s->id));
state = s;
aho_corasick_output(s) = 0;
aho_corasick_fail(s) = NULL;
++j; // <--- HERE!
}
aho_corasick_output(s) = n;
return 0;
}
int
aho_corasick_addstring(aho_corasick_t*in,无符号字符*字符串,大小\u t n)
{
aho_corasick_t*g=in;
aho_corasick_state_t*state,*s=NULL;
int j=0;
state=g->zerostate;
//只要我们有过渡,就跟着他们走
而(j!=n&&
(s=aho_corasick_goto_get(state,*(string+j))!=FAIL)
{
状态=s;
++j;
}
如果(j==n){
/*dyoo:添加,以便在关键字以前缀结尾时
另一方面,我们仍然认为这是一场比赛*/
aho_corasick_输出=j;
返回0;
}
while(j!=n)
{
//创建新状态
if((s=xalloc(sizeof(aho_corasick_state_t)))==NULL)
返回-1;
s->id=g->newstate++;
调试(printf(“分配状态%d\n”,s->id));/*debug*/
s->深度=状态->深度+1;
/*FIXME:检查的错误返回值
阿霍·科拉西克·后藤*/
阿霍·科拉西克·后藤·初始化;
//创建转换
aho_corasick_goto_集(state,*(string+j),s);
调试(printf(“%u->%c->%u\n”,state->id,*(string+j),s->id));
状态=s;
aho_corasick_输出=0;
aho_corasick_失败=空;
++j、 //您还可以使用其他工具来查找不一定会使程序崩溃的故障。
和类似的工具可能会帮助您
在某些情况下,您可能需要构建自己的python才能使用它允许python使用直接内存分配而不是python自己的内存分配的可能性。您是否尝试过将while循环转换为for循环?如果您使用更直观的方法,可能会对++j
产生一些微妙的误解。尝试注释调试()行,可能通过GDB运行它?我也可能尝试使用核心文件来研究崩溃时机器的状态。它可能显示它不是真正在“代码> > ++J < /代码>行……堆栈跟踪在哪里?我正在经历一个与我正在编写的C++模块类似的问题。如果我从命令行运行,它会跳过。ngs,如果我通过gdb运行它,一切都很顺利。