How do you debug a Linux core dump using VSCode?

孤者浪人 提交于 2019-12-24 19:42:53

问题


I am purposely generating a core dump from a C++ application I'm writing using VSCode. I cannot figure out how to debug the core dump. Has anyone had any experience with this they'd be willing to share?

***** UPDATE ***** I believe I have it working now. I created a second debug configuration for core files. I needed to add the "coreDumpPath" option that pointed to the dump file generated. I also needed to remove preLaunchTask option that would always build a new executable.



回答1:


From the VScode docs

Memory dump debugging

The C/C++ extension for VS Code also has the ability to debug memory dumps. To debug a memory dump, open your launch.json file and add the coreDumpPath (for GDB or LLDB) or dumpPath (for the Visual Studio Windows Debugger) property to the C++ Launch configuration, set its value to be a string containing the path to the memory dump. This will even work for x86 programs being debugged on an x64 machine.

P.S. The asker has already updated the question with the solution. But adding this answer to help those people who jump directly into the answer section ;)




回答2:


You don't use a source code editor (even VSCode) to debug a core dump (because a core file has not a textual format). You use gdb (or perhaps some other debugger, such as lldb). GDB has a very nice user manual that I strongly recommend to read. You also don't use VSCode to compile your C++ code, but a compiler such as GCC or Clang (probably VSCode could be configured to start g++ for you).

You also don't need to generate a core dump on purpose. It is often simpler to set a breakpoint under gdb. You could generate a core of a running process using gcore(1). You could attach gdb to a running process using the -p option of gdb(1). I see very few cases where generating a core dump on purpose is useful. But of course abort(3) (also used by assert(3)) generates a core dump.

Your application should better be compiled (using the -g option to GCC or Clang) with DWARF debug information. Assume your application executable is some yourapp executable file. Then you debug the core dump (for a core file, see core(5) for more; notice that gdb(1) is mentioned in core(5) man page, given by the man core command) using gdb yourapp core

Some source code editors are capable of running gdb (my editor is emacs and it is capable of running gdb with M-x gdb). You should dive into the documentation of your source code editor to understand how to do that.

But I recommend using gdb on the command line in a terminal. It is a very handy tool. See this answer to a related question.

gdb is using the very low level ptrace(2) system call to set breakpoints etc etc.. You almost never need ptrace (except if you write your own debugger, which could take years of work), but you use gdb which uses ptrace.

PS. How to run gdb from VSCode is a different question. Since I don't use VSCode, I cannot answer it. And it might not even worth doing. Even with 30 years of emacs experience, I often run gdb in a terminal. Since it is simpler than running it from emacs (or VSCode).

NB. These days, in 2019, "source code editor" is a near synonym for "IDE". Both locutions in practice refer to the same products, but they differ in the way they present them. You can call emacs an IDE, I (and the GNU community) prefer to call it a source code editor, but we both will use it for the same things: nicely writing and browsing and working on source code and building and debugging it.



来源:https://stackoverflow.com/questions/55736235/how-do-you-debug-a-linux-core-dump-using-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!