Linux debugging includes user-layer debugging and kernel debugging. The main tools used include GDB and kgdb.
This article focuses on the working principle, key technologies, and differences in platform dependencies of the debugger in Linux programming environment. If the time is sufficient, the multi-thread debugging will be updated.
++ ++
I. Debugger Basics
The mysterious debugger actually works on some simple technologies. Although there are some differences between different platforms, its basic principles are similar, the following is a memorandum using Linux as an example (I learned from a friend that Windows is also running an open-source system, which is very powerful, but I am really worried about the unexpected situation .....) right:
1) ptrace. Ptrace provides a way for the parent process to monitor and control other processes. It can also change the registers and kernel images in sub-processes, so that it can implement breakpoint debugging and tracing of system calls. With ptrace, You can intercept and modify system calls at the user layer ).
2) ELF format. The ELF file format provides a lot of useful information for debugging.
3) Dwarf format. This is a format in which debugging information is stored in a binary file. It is not hierarchical with elf. When the-G parameter is used during compilation, the compiler collects a large amount of information from the source file, for example, the variable name, the variable type, the row number where the variable is located, the function name, the function parameter, the address range of the function, and the correspondence between the row number and the address, then write the data to the compiled file in a specific format. During debugging, the debugger reads and parses the information from the file to produce readable information. To put it simply, the debugging information is to build a bridge between the machine code and the corresponding source code, which greatly facilitates and improves the debugging program capability. The full name of dwarf is "debugging with attributed record Formats", which complies with the gnu fdl authorization.
4)/proc. The Linux proc file system contains a lot of information about running programs.
Adding an explanation of the system call is also required to complete Debug: the operating system provides a standard service that allows programmers to control underlying hardware and services (such as file systems), called system calls ). When a program needs to be called by the system, it puts the relevant parameters into the registers related to the system call, and then calls the Soft Interrupt 0x80, this interrupt is like a window that allows a program to access the kernel mode. The program submits the parameters and system call numbers to the kernel to complete the execution of system calls.
Ii. How does a debugger work?
After you establish a debugging relationship using a ptrace system call with the ptrace_traceme or ptrace_attach parameter, any signal (except sigkill) delivered to the target program will be intercepted by GDB first, or gdbserver intercepts and notifies GDB during remote debugging.
In GDB, you can use handle signal keywords... to set the behavior, including:
Nostop/stop, noprint/print, noignore/ignor.
GDB has the opportunity to process the signal accordingly, and determines whether to deliver the previously intercepted signal to the target program or how to process it when continuing the target program.
For the signals in GDB, note the following:
1) You can use the signal command to generate a signal for the program to be debugged. But here, unlike using kill in shell to send a signal to the program, the signal sent by kill is intercepted by GDB, the signal sent by the signal command is directly sent to the program to be debugged.
2) When a task is in the tracing status, the process stops when it receives any signal (except sigkill), even if you set nostop, it is especially easy to misunderstand from the literal point of view. This is sometimes a program with many signal interactions that slows down when running GDB.
Next we will take GDB as an example to briefly introduce the implementation of several basic behaviour of debugger (many of the content comes from Liu Dong (yusi wind film ):
1)Run
When we execute run in GDB, GDB will complete the following operations:
1. Create a new process through the fork system call
2. Perform the traceme operation through ptrace in the newly created sub-process.
3. Load the executable file specified by the user through the execv system call in the process.
0. The working mode of command response to GDB is motivated by external events. There are two external event sources: standard input (User-entered Debugging commands) and remote connection-reported events.
2)Gdbsever
Most of the time, we need to perform remote debugging on the target machine. At this time, we need to use gdbsever on tagert. On the host, we continue to use GDB to connect to gdbsever for debugging.
1. The communication data format between GDB and gdbsever is defined by the gdb Remote Serial protocol. 2. The gdbsever startup method is equivalent to running a process on the target, tracing it, And then executing the specified program using exec.
3)Breakpoint
In GDB, most operations are driven by signal. The signal is also the basis for implementing the breakpoint function. For example, on the X86 platform, the breakpoint command int3. After the target program runs this command, it will trigger the sigtrap signal. GDB captures this signal and queries the checkpoint linked list maintained by GDB based on the current position of the target program. If it is found that the address does have a breakpoint, it can be determined as a breakpoint hit.
4)Single Step
- Command-level single step
Before learning about step, you must understand the single step at the command level. A single step at the command level means that GDB controls the target program to stop immediately after only one command is run. The command-level single step is the basis for running class Debugging commands such as next step.
Commands can be divided into two types: hardware and software. A hardware step indicates that the CPU architecture directly supports a single step at the command level, and the target program can automatically stop after running a command. A single software step indicates that the CPU does not support a single command-level step. GDB must use a software method to implement a single command-level step.
Both x86 and PPC support one hardware step. In this mode, each time the CPU executes a command, it will generate a single-step exception and notify GDB to handle it.
Arm and MIPS do not support hardware single step. In this case, GDB uses a temporary software breakpoint to simulate a single step. Insert a breakpoint temporarily at the next command to execute the command, and then let the target program continue to run. It will encounter a temporary breakpoint at the next command after the current command is executed, as a result, the target program stops, notifying GDB to hit the breakpoint, and GDB then deletes the breakpoint to complete the instruction-level single step process.
The next command implements a single step at the C code level. The implementation mechanism introduces the concepts of step_range, step_range_start and step_range_end. When executing the next command, GDB calculates the address of the First Command of the C statement at the current stop position as step_range_start, and then calculates the address of the First Command of the next C statement at the current stop position as step_rang_end, then control the target program from the current stop position to take the command-level single step until the PC exceeds step_range.
Similar to the next command, the step command is also a single step for implementing C Code. For simple statements, the step command is equivalent to next. The difference is that if a function is called during a single step, the step command stops at the beginning of the subfunction rather than crossing it (except for subfunctions without debugging information ).
Iii. Key Technologies
I would like to introduce the debug dependency technology, but there are already a lot of online resources, and these key technologies need to be forgotten, it takes too long, here only list the reference resources, some of the above content also comes from these reference resources. Thanks to these selfless authors.
Ptrace http://blogold.chinaunix.net/u/19651/showart_362901.html
Dwarf http://hi.baidu.com/delovery/blog/item/5827f124e46a853b8744f933.html
Elf http://cs.mipt.ru/docs/comp/eng/ OS /linux/howto/howto_english/elf/elf-howto.html#toc1
I also saved a very good elf document, which I will share with you on csdn.
The Multi content does not know how to write and grasp the key points. Please add it later.