Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop a Linux debugger and what the preparation environment is like

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to develop a Linux debugger and prepare the environment is like, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Anyone who has written a program that is more complex than hello world should have used a debugger (if you haven't already, stop your work and learn). However, although these tools have been widely used, there are not many resources to tell you how they work and how to develop them, especially compared to other tool chain technologies such as compilers.

Here are some other resources to refer to:

Http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1

Https://t-a-w.blogspot.co.uk/2007/03/how-to-code-debuggers.html

Https://www.codeproject.com/Articles/43682/Writing-a-basic-Windows-debugger

Http://system.joekain.com/debugger/

We will support the following features:

Start, pause, resume execution

Set breakpoints in different places

Memory address

Source code line

Function entry

Read and write registers and memory

Single-step execution

Instruction

Entry function

Jump-out function

Skip function

Print the current code address

Print function call stack

Print the value of a simple variable

In the * section, I'll also outline how to add the following features to your debugger:

Remote debugging

Shared library and dynamic library support

Expression calculation

Multithreaded debugging support

In this project, I will focus on C # and custom codes, but it should also work for languages that compile source code into machine code and output standard DWARE debugging information (if you don't know what these things are, don't worry, we'll get to that in a minute). In addition, I only focus on how to get the program running and work in most cases, avoiding things like robust error handling for simplicity.

Index of series of articles

With the release of later articles, these links will gradually take effect.

Prepare the environment

Breakpoint

Register and memory

Elves and dwarves

Source code and signal

Step by step execution of the source code layer

Source code layer breakpoint

Call stack

Read variable

Subsequent steps

LCTT translation note: ELF-executable file format (Executable and Linkable Format); DWARF (a widely used debug data format, see WIKI).

Prepare the environment

Before we officially begin, we first need to set up the environment. In this article I will rely on two tools: Linenoise for processing command-line input and libelfin for parsing debugging information. You can also use the more traditional libdwarf instead of libelfin, but the interface is less friendly, and libelfin also provides a basic complete DWARF expression evaluator, which can save you a lot of time when you want to read the value of a variable. Make sure you are using libelfin my fbreg branch because it provides additional support for reading variables on x86.

Once you have installed or compiled these dependent tools on the system using your favorite compilation system, you are ready to start. I set them up in the CMake file to compile with the rest of my code.

Start the executable program

Before we can actually debug any program, we need to start the program being debugged. We will use the classic fork/exec mode.

Int main (int argc, char* argv []) {if (argc)

< 2) { std::cerr = 1) { //parent debugger dbg{prog, pid}; dbg.run(); }class debugger { public: debugger (std::string prog_name, pid_t pid) : m_prog_name{std::move(prog_name)}, m_pid{pid} {} void run(); private: std::string m_prog_name; pid_t m_pid; }; 在 run 函数中,我们需要等待,直到子进程完成启动,然后一直从 linenoise 获取输入直到收到 EOF(CTRL+D)。 void debugger::run() { int wait_status; auto options = 0; waitpid(m_pid, &wait_status, options); char* line = nullptr; while((line = linenoise("minidbg>

")! = nullptr) {handle_command (line); linenoiseHistoryAdd (line); linenoiseFree (line);}}

When the tracked process starts, a SIGTRAP signal is sent to it, which is a trace or breakpoint interrupt. We can use the waitpid function to wait for this signal to be sent.

When we know that the process can be debugged, we listen for user input. The linenoise function itself uses a window to display and process user input. This means that we don't need to do much work to have a command line that supports history and navigation commands. When we get the input, we send the command to the Mini Program handle_command we wrote, and then we add the command to the linenoise history and release the resources.

Processing input

Our commands are similar to gdb and lldb formats. To continue executing the program, the user needs to type continue or cont or even c. If they want to set a breakpoint in an address, they enter break 0xDEADBEEF, where 0xDEADBEEF is the hexadecimal format of the desired address. Let's increase our support for these commands.

Void debugger::handle_command (const std::string& line) {auto args = split (line,''); auto command = args [0]; if (is_prefix (command, "continue")) {continue_execution ();} else {std::cerr of.size () return false; return std::equal (s.begin (), s.end (), of.begin ());}

We will add the continue_execution function to the debuger class.

Void debugger::continue_execution () {ptrace (PTRACE_CONT, m_pid, nullptr, nullptr); int wait_status; auto options = 0; waitpid (m_pid, & wait_status, options);}

Now our continue_execution function tells the process to continue with ptrace, and then uses waitpid to wait until the signal is received.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report