简体   繁体   English

分段错误 - 使用 gdb 进行奇怪的调试

[英]Segmentation fault - weird debugging with gdb

I'm working in C, Linux terminal.我在 C、Linux 终端上工作。 I need to find a pattern in a text and recolor it.我需要在文本中找到一个模式并重新着色。 GDB debugging can locate the function that is causing the problem via (gdb) backtrace, but it shows me a terrible message when I try to find the exact line: GDB 调试可以通过 (gdb) 回溯找到导致问题的函数,但是当我试图找到确切的行时,它向我显示了一条可怕的消息:

Error错误

Program received signal SIGSEGV, Segmentation fault.
strstr_sse2_unaligned ()
at ../sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S:40
40 ../sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S: No such file or dir
ectory.
(gbd)

The broken function is find_and_recolor:损坏的函数是 find_and_recolor:

char* my_replace(char *text, char* replacement)
{
      int lgreplacement = strlen(replacement);
      int lgtext = strlen(text);
      char *aux = (char*)malloc((lgreplacement + lgtext + 10) * sizeof(char));
      strcpy(aux, replacement);
      strcat(aux, text);
      return(aux);
}

char* find_and_recolor(char* text, char* pattern)
{
      int lgpattern = strlen(pattern);
      int lgreplace = lgpattern + 10;//there are exactly 10 characters that must be inserted along the pattern word
      int dif = 0;
      char *p;
      char *replacement = (char*)malloc(lgreplace * sizeof(char));
      strcpy(replacement, "\e[0;31m");
      strcat(replacement, pattern);
      strcat(replacement, "\e[m");//to recolor a word in red, that word must be surrounded by those characters
      while(p = strstr(text + dif, pattern))
      {
            p = my_replace(p, replacement);
            p += lgreplace;
            dif = p - text;
      }
      free(replacement);
      return strdup(text);
}

it shows me a terrible message when I try to find the exact line:当我试图找到确切的行时,它向我显示了一条可怕的消息:

There is nothing terrible, weird or unusual about this message, you just need to learn proper debugging technique.这个消息没有什么可怕的、奇怪的或不寻常的,你只需要学习正确的调试技术。

What's happening is that the segmentation fault doesn't happen in your code, it happens inside GLIBC code (inside strstr ), because you called strstr with bad arguments.发生的情况是分段错误不会发生在您的代码中,而是发生在 GLIBC 代码(在strstr )中,因为您使用错误的参数调用了strstr

To find which call to strstr that was, use GDB up command to step out of GLIBC code, and into your code.要查找对strstr调用,请使用 GDB up命令跳出 GLIBC 代码,进入您的代码。 Once you are inside find_and_recolor , you would be able to see the exact line, and print values of text , dif and pattern which caused your crash (assuming you compiled your code for debugging, ie with the -g flag).一旦您进入find_and_recolor ,您将能够看到确切的行,并打印导致崩溃的textdifpattern值(假设您编译了用于调试的代码,即使用-g标志)。

Updating diff to p-text in while loop where both pointer points to different array doesn't make sense.在 while 循环中将 diff 更新为p-text ,其中两个指针都指向不同的数组是没有意义的。 It is undefined behavior.这是未定义的行为。

Also code has other issues.代码还有其他问题。

  1. Uninitialized variable.未初始化的变量。
  2. Less optimized as number of call can be reduced.由于可以减少调用次数,因此优化程度较低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM