Line number of segmentation fault

前端 未结 8 1000
误落风尘
误落风尘 2020-12-13 08:56

Is there any gcc option I can set that will give me the line number of the segmentation fault?

I know I can:

  1. Debug line by line
  2. Put printfs in
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 09:08

    You can get gcc to print you a stacktrace when your program gets a SEGV signal, similar to how Java and other friendlier languages handle null pointer exceptions. See my answer here for more details:

    • how to generate a stacktace when my C++ app crashes ( using gcc compiler )

    The nice thing about this is you can just leave it in your code; you don't need to run things through gdb to get the nice debug output.

    If you compile with -g and follow the instructions there, you can use a command-line tool like addr2line to get file/line information from the output.

提交回复
热议问题