如何调试分段错误? [英] How to debug segmentation fault?

查看:169
本文介绍了如何调试分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它的工作原理是,在循环中,我将每个元素设置为0或entry_count-1。
当我设置它以使entry_count很小,并且我手工编写它而不是通过循环(sorted_order [0] = 0; sorted_order [1] = 1; ... etc)时,它工作。 p>

请不要告诉我如何解决我的代码。我不会使用智能指针或向量的非常具体的原因。反而集中在这个问题:
什么样的条件可以导致这种segfault?
谢谢。



---- OLD -----



调试不工作在unix机器上的代码。代码的要点是:

  int * sorted_array =(int *)memory; 
//我知道这个块足够大
//它由malloc早先分配

for(int i = 0; i sorted_array [i] = i;
}

循环中似乎有一个segfault。切换到调试模式,不幸的是,使得segfault停止。使用cout调试,我发现它必须在循环中。



接下来我想知道segfault发生了多远,因此我添加了:

  std :: cout< i<< '\\\
';

它显示了整个范围,假设是循环,没有segfault。



有了更多的实验,我最终在循环之前创建了一个字符串流,并为循环的每次迭代写入一个空字符串,没有segfault。



我尝试了一些其他分类操作试图找出发生了什么。我试着设置一个变量j = i;



运行valgrind我在segfault上得到的唯一的信息是,它是一个一般保护故障还有一些关于默认响应为11.它还提到有条件跳转或移动取决于未初始化的值,但看代码我不能弄清楚如何可能。



这是什么?

解决方案

这显然是您程序中无效内存使用的症状。这将是位通过查看你的代码片段很难找到,因为它很可能是已经发生的其他坏事的副作用。



但是正如你在你的问题中提到的您可以使用 Valgrind 附加您的程序,因为可重现。因此,您可能需要附加您的程序(a.out)。


$ valgrind --tool = memcheck --db-attach = yes ./a.out


这样当检测到第一个内存错误时,Valgrind会将程序附加到调试器中,实时调试(GDB)。这应该是了解和解决您的问题的最佳方式。



一旦你能够弄清楚你的第一个错误,修复它,然后重新运行它,看看什么是其他错误你得到。这一步应该做,直到没有报告Valgrind的错误。



但是你应该避免使用现代C ++程序中的原始指针,并开始使用 std :: vector std :: unique_ptr 以及其他人建议。


It works when, in the loop, I set every element to 0 or to entry_count-1. It works when I set it up so that entry_count is small, and I write it by hand instead of by loop (sorted_order[0] = 0; sorted_order[1] = 1; ... etc).

Please do not tell me what to do to fix my code. I will not be using smart pointers or vectors for very specific reasons. Instead focus on the question: What sort of conditions can cause this segfault? Thank you.

---- OLD -----

I am trying to debug code that isn't working on a unix machine. The gist of the code is:

int *sorted_array = (int*)memory;
// I know that this block is large enough
// It is allocated by malloc earlier

for (int i = 0; i < entry_count; ++i){
  sorted_array[i] = i;
}

There appears to be a segfault somewhere in the loop. Switching to debug mode, unfortunately, makes the segfault stop. Using cout debugging I found that it must be in the loop.

Next I wanted to know how far into the loop the segfault happend so I added:

std::cout << i << '\n';

It showed the entire range it was suppose to be looping over and there was no segfault.

With a little more experimentation I eventually created a string stream before the loop and write an empty string into it for each iteration of the loop and there is no segfault.

I tried some other assorted operations trying to figure out what is going on. I tried setting a variable j = i; and stuff like that, but I haven't found anything that works.

Running valgrind the only information I got on the segfault was that it was a "General Protection Fault" and something about default response to 11. It also mentions that there's a Conditional jump or move depends on uninitialized value(s), but looking at the code I can't figure out how that's possible.

What can this be? I am out of ideas to explore.

解决方案

This is clearly a symptoms of invalid memory uses within your program.This would be bit difficult to find by looking out your code snippet as it is most likely be the side effect of something else bad which has already happened.

However as you have mentioned in your question that you are able to attach your program using Valgrind. as it is reproducible. So you may want to attach your program(a.out).

$ valgrind --tool=memcheck --db-attach=yes ./a.out

This way Valgrind would attach your program in the debugger when your first memory error is detected so that you can do live debugging(GDB). This should be the best possible way to understand and resolve your problem.

Once you are able to figure it out your first error, fix it and rerun it and see what are other errors you are getting.This steps should be done till no error is getting reported by Valgrind.

However you should avoid using the raw pointers in modern C++ programs and start using std::vector std::unique_ptr as suggested by others as well.

这篇关于如何调试分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆