This week, I spent a week, I tried to debug a paragraph error. I have never done this before. I spent a long time to figure out some of the basic things involved (get the core dump, find the line number that caused the paragraph error). So I have this blog to explain how to do those things!
After reading this blog, you should know how from "Oh, my program has a paragraph error, but I don't know what happens" to "I know that it appears when the stack is in the wrong, the line number!"
What is the paragraph error?
"Segment error segmentation fault" means that your program attempts to access the memory address that does not allow access. This may be due to:
Try to solve the null pointer (you are not allowed to access the memory address 0);
Trying to solve the other things that are not in your memory (LCTT translation: refer to within a legal memory address interval);
A C ++++ virtual table pointer C ++ VTable Pointer that has been destroyed and points to the wrong place, which results in the program to execute instructions in memory that do not perform permission;
Some of the things I don't understand, such as what I think that the unlined memory address may also lead to the paragraph error (LCTT translation: architecture, such as MIPS, ARM), such as MIPS, ARM is more likely to generate paragraph error ).
This "C ++ dummy pointer" is the case where my program is incorrect. I may explain this in the future blog, because I originally didn't know any knowledge about C ++, and this virtual table lookup caused the case where the block was wrong.
but! This blog is not about C ++ issues. Let us talk about the basic things, for example, how do we get a core dump?
Run Valgrind
I found out why my program has the easiest way to use Valgrind: I run
VALGRIND -VYOUR-PROGRAM
This gives me a stack call sequence when a fault. concise!
But I want to do a more in-depth investigation and find some Valgrind not telling me information! So I want to get a core dump and explore it.
How to get a core dump
The core dump Core Dump is a copy of your program memory, and it is very useful when you try to debug your problem.
When your program has a paragraph error, Linux's kernel sometimes writes a core to disk. When I initially tried to get a core dump, I was very depressed for a long time, because - Linux did not generate a core dump! Where is my core dump?
This is what I finally did:
Run ULIMIT -C Unlimited before starting my program
Run sudo sysctl -w kernel.core_pattern = / tmp / core-% e.% P.% H.% T
Ulimit: Set the maximum size of core dumps
Ulimit -c Set the maximum size of the core dump. It is often set to 0, which means that the kernel does not write core dumps at all. It is in kilobytes. Ulimit is set separately by each process - you can see a variety of resource restrictions on a process by running CAT / Proc / Pid / LIMIT.
For example, these are the resource restrictions of a casual Firefox process on my system:
The kernel uses a soft restriction Soft Limit when it is decided to write how large core dump files (in this case, Max core file size = 0). You can use the shell's built-in command ulimit (ulimit -c unlimited) to add soft restrictions to hard limit Hard Limit.
KERNEL.CORE_PATTERN: Where is the core dump?
Kernel.core_pattern is a kernel parameter, or called "Sysctl Settings", which controls where the Linux kernel writes the core dump file to disk.
The kernel parameters are a way to set your system global settings. You can get a list that contains each kernel parameter by running sysctl -a, or using sysctl kernel.core_pattern to specifically view the kernel.core_pattern setting.
So sysctl -w kernel.core_pattern = / tmp / core-% e.% P.% H.% T Save the core dump under directory / TMP, and add a series of enabled (outable) processes in Core The suffix made of parameters is the file name.
If you want to know what these shaped messages, please, please refer to Man Core.
One thing is important, kernel.core_pattern is a global setting - it is best to be careful when modifying it, because it is possible that other system functions depend on how to set it into a specific way (to work normally).
Kernel.core_pattern and ubuntu
By default, in the Ubuntu system, kernel.core_pattern is set to the following value:
$ sysctl kernel.core_pattern
KERNEL.CORE_PATTERN = | / usr / share / apport / app% p% s% C% D% P
This caused my confusion (what is this apport, what did it dump my core dump?). The following is what I learned:
Ubuntu uses a system called Apport to report crash information related to the APT package.
Set kernel.core_pattern = | / usr / share / apport / app% P% S% C% D% P means that the core dump will be given to the Apport program via a pipe.
The app's log is saved in the file /var/log/apport.log.
AppR The default will ignore crash information from binary files that are part of the Ubuntu package.
I finally jumped about Apport and reset kernel.core_pattern to sysctl -w kernel.core_pattern = / tmp / core-% e.% P.% H.% T, because I am on a development machine, I don't care Whether Apport works, I don't want to try to keep Apport on the disk.
Now that you have a core dump, what is next?
Ok, now we understand ulimit and kernel.core_pattern, and actually have a core dump file in the disk / TMP directory. Great! What are you doing next? We still don't know why the program will have a paragraph error!
The next step will open the core dump file using GDB and get the stack call sequence.
Get stack calling sequence from GDB
You can open a core dump file with GDB:
$ GDB -CMY_CORE_FILE
Next, we want to know what the stack when the program crashes. Run BT at the GDB prompt will give you a call sequence backtrace. In my example, GDB does not load symbol information for binary files, so these functions are like "??????". Fortunately, (we pass) the load symbol fixes it.
Here's how to load debug symbols.
Symbol-file / path / to / my / binary
Sharedlibrary
This loads the symbol from any shared library of binary files and its references. Once I did this, when I executed BT, GDB gave me a beautiful stack track with the line number!
If you want it to work, the binary should be compiled in a way with debug symbol information. When trying to find out the reason for the crash, the line number in the stack track is very helpful. :)
View the stack of each thread
Get the call stack of each thread in the GDB in the following manner!
Thread Apply All Bt Full
GDB + core dump = surprise
If you have a core dump with a debug symbol and GDB, it's great! You can view the call stack up and down (LCTT Demo: Refers to the Different functions of the jump call sequence to facilitate the local variables), print variables, and see what happened. this is the best.
If you are still working on the GDB wizard, you only print out the stack tracking and BT. :)
Asan
Another way to figure out your paragraphs is to use the AddressSanitizer option compiler ("Asan", ie $ cc -fsanitize = address and run it. I am not preparing to discuss this article, because this article is quite long, and in my example opens the Asan post-end error disappears, it may be because ASAN uses a different memory distributor (system memory distributor, not Tcmalloc) ).
In the future, if I can make Asan work, I may write something about it. (LCTT translation: Here, refers to the use of Asan to reproduce the paragraph error)
Getting a stack track from a core dump is really friendly!
This blog sounds a lot, when I do this, I am very confused, but I really get a stack call from a paragraph wrong program without that multi-step:
Try with Valgrind
If you use it, or you want to get a core dump to investigate:
Make sure that binary file compiles with debug symbol information;
Correct setting ulimit and kernel.core_pattern;
Run the program;
Once you use the GDB debug core to dump, load the symbol and run the BT;
Try to find what happened!
I can use GDB to figure out that there is a c ++ virtual table entry pointing to some broken memory, this is a bit helpful, and I feel like I understand C ++. Maybe one day we will discuss how to use GDB to find problems! , Read the full article, original title: How to get a core dump of a paragraph error on Linux
Article Source: [Micro Signal: Linuxhub, WeChat public number: Linux enthusiasts] Welcome to add attention! Please indicate the source of the article.
Our other product: