首页 > 编程语言 >【C++】Debugging Segmentation Faults

【C++】Debugging Segmentation Faults

时间:2022-09-01 12:34:07浏览次数:76  
标签:Debugging Segmentation code core C++ GDB print line method

 

背景

linux下的程序,在遇到空指针解引用、栈错误等原因崩溃时,bash会输出一条:

Segmentation fault(core dump)

如果你看到core dumped字样,并且在目录下也找到了一个叫core的文件,那你可以直接用gdb定位到程序崩溃的位置。但是,我在实践中发现,在我的ubuntu 20.04环境下,程序段错误后找不到core文件。

  • 检查是否开启core dump?

  先用ulimit -c,如果看到0,说明没有开core dump。
  所以我们输入ulimit -c unlimited,打开core dump。
  再次用ulimit -c,看到unlimited了,说明core dump打开了。 

  • ulimit -c已经是unlimited了,怎么还是看不到core文件?

  输入命令:

man 5 core

  查看手册中的core内容,然后往下翻:

 

它这里面列举了几条不产生core文件的原因,你可以逐条对照。最有可能的原因是/proc/sys/kernel/core_pattern文件里面把你的core文件发送到别处去了。我的ubuntu系统默认就是这样干的。

然后打开/proc/sys/kernel/core_pattern文件,里面如果不是core的内容,表明它可能把你的core文件发到别的地方去了。那么则可以编辑这个文件,把内容改成core,然后保存。

另外有一种更简洁的方式,执行以下命令:

sudo service apport stop

然后你会发现,core_pattern的内容变为core了,这样core文件就可以正常生成了。

GDB

GDB is a powerful debugger that allows a programmer to step through their code line by line and probe any variable for its value at that step of execution. It has a lot of capability beyond what can be addressed in a simple primer. A very useful cheat sheet that I always have a printed copy of on my desk can be found here https://darkdust.net/files/GDB Cheat Sheet.pdf.

 

First and foremost, GDB will need some specific information injected into the executable that needs to be debugged. This requires compiling all of our code with the -g flag. The best way to do this is by adding it to your CXXFLAGS variable in your Makefile. That ensures that all automatically created *.o files are also built using the -g flag. You should also delete the old *.o files before rebuilding. This is a great time to run make clean, assuming you have a well-defined clean rule in your Makefile.

Now that you have recompiled with -g flags, you can fire up the debugger. The -tui flag below opens the source code in the top half of the screen which is great for adding some context to where the program is in the execution. gdb -tui a.out

If files were built with -g, the symbol table should load and GDB is ready to use. The first thing that is typically needed is to add one or more breakpoints. These can be added to method names, or to lines of code (if you specify a line that cannot break, the next breakable line is used).

Breaking on a specific line of source code

To halt execution at line 12 of test.cpp, you would simply type break test.cpp:12. Now when you type run, GDB starts executing and will halt at line 12 (or the next breakable line) so that you can inspect the status of your program.

Breaking on a method

If you suspect a specific method or function in your code, you can halt execution and inspect whenever that method is called. In order to inspect the method Search within the BinarySearch class, I would type break BinarySearch::Search(int*, int, int, int). As you might have guessed, this is a great time for tab-completion which GDB is great at. Just start typing the class or method name and hit tab in order to fill in the rest. Now when you type run, GDB starts executing and will halt the first (and every) time that method is called.

Stepping through

The commands needed to restart execution after a breakpoint has been reached are:

  • next - This goes to the next line of code, but will not dive into functions.
  • step - This goes to the next line of instruction. This might be inside of a function call or elsewhere in the code tree.
  • continue - This runs the program until the end of execution, or until a breakpoint is reached.

Looking at values

The simple act of stepping through code often helps figure out the problem. “This should not go inside that if statement”…well, it does, so figure out why.

Sometimes, it is not enough to just know the current location of your code execution. Luckily, we can print out values of our variables. print myVar would print the value of myVarprint &myVar would print the address of myVar. Pointers will print the address by default. To see their value, you would need to dereference, e.g. print *myPointer. As you might have noticed, the print statement is identical to sticking in std::cout statements, except that you don’t need to guess in advance, you can poke around until you find the values you need. Print will also let you call methods, so print myObject.GetSomeData() would print the results of the method call.

When breaking on a method call, the arguments passed to the call are printed automatically. For the BinarySearch breakpoint we mentioned earlier, when the breakpoint is triggered, the output might read:

Breakpoint 1, BinarySearch::Search (listOfNumbers=0x7fffffffc720, left=7, right=10, searchKey=10) at binary-search.cpp:5

 

参考:

https://stackoverflow.com/questions/2065912/core-dumped-but-core-file-is-not-in-the-current-directory

 

标签:Debugging,Segmentation,code,core,C++,GDB,print,line,method
From: https://www.cnblogs.com/carsonzhu/p/16646087.html

相关文章

  • 在 C# CLR 中学习 C++ 之了解 namespace
    一:背景相信大家在分析dump时,经常会看到WKS和SRV这样的字眼,如下代码所示:00007ffa`778a07b8coreclr!WKS::gc_heap::segment_standby_list=0x00000000`000000000......
  • # C++ 简单的程序段记时工具
    基于宏定义的几个C++记时工具,实现类似于MATLAB中tic().toc()的功能代码#include<ctime>#definedef_tic(name)clock_tt_##name#definerec_tic(name)t_##name=c......
  • c++的类型转换
    1.int转string,函数to_string()x=10;stringm=to_string(x);经测试gccv5.4.0版本不支持,版本v7.5.0支持。判断版本号命令:g++-v同样适用于double,float2.string转int,......
  • C++ 地形导航系统之确定峰点的位置
    #include<iostream>#include<string>#include<fstream>#defineN64boolisPeak(intgrid[][N],intr,intc);intmain(){intnrows,ncols;intm......
  • c++基础思维导图2
    c++基础思维导图2结构体结构体的基本概念:用户自定义的数据类型结构体定义和使用struct结构体名{结构体成员}struct结构体名变量名;struct结构体名变量名={成......
  • 【C++】引用与指针的区别
    安全性首先引用要比指针要安全,因为引用不能更改绑定,但是指针可以更改指向,此外指针可以有多级,但是引用一般只有一级。在使用指针的时候,我们往往需要使用断言,判断指针是不是......
  • VectorCAST在汽车电子C++代码测试的应用
    随着汽车行业的发展,软件定义汽车已成为汽车产业达成的共识发展趋势,软件在汽车产品中也承担着越来越重要的角色,车身域各功能的实现,少则几千行代码,动辄百万行代码。随着汽车......
  • 新发现的几个不错的c++库
    1.coost包含了各种常用的库,比boost轻量级的基于c++11的库https://github.com/idealvin/coost2.ImGui一个较少依赖的gui界面库https://github.com/ocornut/imgui ......
  • 【C++】ceil floor round 函数
    https://blog.csdn.net/dangzhangjing97/article/details/81279862?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRL......
  • CCF 201503-1 图像旋转(C++)
    好像旋转矩阵有更好的做法,但是我觉得这样也足够了,如果需要更好的做法,大家得自己在去找一下。我主要是找了下规律,然后做出来的#include<iostream>#include<bits/stdc+......