In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the way of debugging core dump under Linux". In the daily operation, I believe that many people have doubts about the way of debugging core dump under Linux. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the way of debugging core dump under Linux". Next, please follow the editor to study!
What is core dump?
For programs, due to various exceptions or bug, a file called core is generated during operation and under certain conditions.
Normally, the core file will contain the memory, register status, stack pointer, memory management information and all kinds of function call stack information when the program is running.
When many programs go wrong, a core file is generated. Through the analysis of this file by the tool, we can locate the information such as the corresponding stack call when the program exits abnormally.
Turn on the core dump switch: ulimit-c unlimited
Look at a problematic piece of code:
# include int main () {int * pendant null; * pendant 0; printf ("bad\ n"); return 0;}
Compile and execute under linux:
[root@VM-16-9-centos C++] # Gmail +-g main.cpp [root@VM-16-9-centos C++] #. / a.out Segmentation fault (core dumped) [root@VM-16-9-centos C++] # ls a.out core.1989 main.cpp
At first glance, there are errors in the above code, and execution will generate core dump. But in large-scale projects, it is ugly with the naked eye. Here is how to debug core dump under linux.
Dmesg+addr2line debugging
Let's start with two linux commands:
Dmesg, a program that detects and controls kernel buffers. The program is used to help users understand the startup information of the system and obtain the error stack address.
Addr2line, a tool that converts the address and executable image of an instruction to a file name, function name, or source code. This feature is useful for translating tracking addresses into more meaningful content.
When calling the addr2line tool, use the-e option to specify the executable image, and the-f option to tell the tool to output the function name.
Operation procedure under linux:
[root@VM-16-9-centos C++] # dmesg | grep a.out [212.330289] a.out [1946]: segfault at 0 ip 00000000400571 sp 00007ffdf0aafbb0 error 6 in a.out [400000000000400571] a.out: segfault at 0 ip 00000000400571 sp 00007ffcfd01c8c0 error 6 in a.out [400000y1000] [root@VM-16-9-centos C++] # [root@VM-16-9-centos C++] # addr2line-e a.out 00000000400571 / root/c++/main.cpp:6
First find the address corresponding to the error through dmesg, and then use addr2line-e to resolve the address to the corresponding line of code.
Gdb debugging
Gdb you must have heard that Linux is a commonly used debugging tool.
The gdb compiler is usually used in terminals in the form of gdb commands, so let's learn about common debugging options.
Bt: viewing stack information
I locals: view the local variables of the current program stack
I args: view the parameters of the current program stack
I catch: view the exception handler of the stack frame in the current program
P a: print the value of the variable
I register: view the value of the current register
R: from running the program to the first breakpoint, if there is no breakpoint, it will run all the time.
Quit: exit
During gdb debugging, enter r, bt. R is to run the a.out file, and bt to check the stack.
We don't need to execute gdb a.out, which is equivalent to rerunning the a.out file. However, in actual development, there are many problems are probabilistic, so this method is not very practical.
Operation procedure under linux (omitting some gdb introduction information):
[root@VM-16-9-centos C++] # gdb a.out core.1989 Reading symbols from / root/c++/a.out...done. [New LWP 1989] bCore was generated by `. / a.outbound. Program terminated with signal 11, Segmentation fault. # 0 0x0000000000400571 in main () at main.cpp:6 6 * pendant; Missing separate debuginfos, use: debuginfo-install glibc-2.17-307.el7.1.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64 (gdb) bt # 0 0x0000000000400571 in main () at main.cpp:6 (gdb)
Execute gdb a.out core.1989 directly without the r command to avoid repeated execution of the program. Using the bt command, you can see the line of code that went wrong with the program.
Strace+addr2line debugging
Strace is a tool that integrates diagnosis, debugging, statistics and integration. We can use strace to analyze the tracking results of system calls and signal transmission of the application in order to solve problems or understand the working process of the application.
The simple use of strace is to execute a specified command that exits after the specified command ends.
During the execution of the command, strace records and parses all system calls made by the command process, as well as all signal values received by the process.
-c, count the execution time, number of times and errors of each system call
-p, specify the process pid
-I, which outputs the entry pointer of the system call
Operation procedure under linux (omitting some loading information):
[root@VM-16-9-centos C++] # strace-I. / a.out [00007f79d3573847] munmap (0x7f79d3772000, 31038) = 0 [0000000000400571]-SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL}-[?] + + killed by SIGSEGV (core dumped) + + Segmentation fault [root@VM-16-9-centos C++] # addr2line-e a.out 00000000400571 / root/c++/main.cpp:6 so far The study on "the way to debug core dump under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.