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

What are the debugging tools for Linux user space

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This issue of the content of the editor will bring you about the Linux user space debugging tools, the article is rich in content and professional analysis and description for you, after reading this article, I hope you can get something.

Using debugging tools in Linux system can help us to monitor, control and correct the programs of other programs.

1.print statement this is a basic primitive method of debugging problems. We can insert print statements into the program to understand the control flow and variable values. Although this is a simple technology, it has some disadvantages. The program needs to be edited to add print statements, and then must be recompiled and rerun to get the output. This is a time-consuming method if the program you are debugging is quite large.

two。 Query in some cases, we need to figure out the state and memory mapping of a process running in the kernel. In order to get this information, we don't need to insert any code into the kernel. Instead, you can use the / proc file system.

/ proc is a pseudo file system, which collects information about the runtime system (cpu information, memory capacity, etc.) as soon as the system is started.

Output of ls / proc

As you can see, every process running on the system has an entry named after the process id in the / proc file system. The details of each process can be obtained in the file in the directory corresponding to the process id.

Output of ls / proc/pid

Explaining all entries in the / proc file system is beyond the scope of this article. Some useful ones are listed as follows:

/ proc/cmdline-> kernel command line

/ proc/cpuinfo-> about the brand and model of the processor, etc.

/ proc/filesystems-> information supported by the kernel of the file system

/ proc//cmdline-> Command line arguments are passed to the current process

/ proc//mem-> memory held by the current process

/ proc//status-> status of the current process

3. Tracking strace and ltrace are two tracking tools used in Linux to track the execution details of a program.

Strace:strace intercepts and records system calls and the signals they receive. For users, it displays system calls, parameters passed to them, and return values. Strace can be attached to an already running process or to a new process. It is very useful as a diagnostic and debugging tool for developers and system administrators. It can also be used as a tool to understand the system by tracking different program calls. The advantage of this tool is that no source code is required and the program does not need to be recompiled.

The basic syntax for using strace is:

Strace command

Strace has a variety of parameters. You can check strace's manual page for more details.

The output of strace is so long that we are not usually interested in every line displayed. We can use the'- e expr' option to filter unwanted data.

Use the-p pid option to bind to a running process.

With the-o option, the output of the command can be redirected to a file.

Strace is filtered to output with only system calls

Ltrace:ltrace tracks and records calls to a process's dynamic (runtime) library and the signals it receives. It can also track system calls made by a process. Its usage is similar to strace.

Ltrace command

The-I option prints the instruction pointer when the library is called.

The-S option is used to reality system calls and library calls

Refer to the ltrace manual for all available options.

Ltrace captures the output of STRCMP library calls

4. ValgrindValgrind is a set of debugging and analysis tools. One of its widely used default tools, Memcheck, can intercept malloc (), new (), free (), and delete () calls. In other words, it is very useful in detecting the following problems:

Memory leak

Re-release

Access out of bounds

Use uninitialized memory

Use the memory that has been released, etc.

It runs directly from an executable file.

Valgrind also has some disadvantages because it increases the memory footprint and slows down your program. It sometimes causes false positives and false positives. It cannot detect access out of bounds for statically allocated arrays.

In order to use it, please download and install it on your system first. You can use the package manager on the operating system to install.

Use the command line to install files that need to be unzipped and unpackaged.

Tar-xjvf valgring-x.y.z.tar.bz2 (where x.y.z is the version number you are trying to install)

Enter the newly created directory (valgrind-XYZ of) and run the following command:

/ configuremakemake install lets us understand how valgrind works through a Mini Program (test.c):

# includevoid f (void) {int x = malloc (10 * sizeof (int)); x [10] = 0;} int main () {f (); return 0;} compiler:

Gcc-o test-g test.c

Now we have an executable file called 'test'. We can now use valgrind to detect memory errors:

Valgrind-tool=memcheck-leak-check=yes test

This is the output of the valgrind rendering error:

Valgrind displays output from heap overflows and memory leaks

As we saw above, we are trying to access the unallocated memory of the function f and allocate the unfreed memory.

GDBGDB is a debugger from the Free Software Foundation. It is very helpful in locating and fixing problems in your code. When the debugged program runs, it gives the user control to perform various actions, such as:

Startup program

Stop at a specified position

Stop at the specified condition

Check the required information

Change the data in the program, etc.

You can also attach a crashed program coredump to GDB and analyze the cause of the failure.

GDB provides a number of options for debugging programs. However, we will introduce some important options to feel how to get started with GDB.

If you haven't installed GDB yet, you can download it here: the official website of GDB.

Compiler: in order to debug a program with GDB, you must compile using the'- g 'option of gcc. This will generate debugging information in the native format of the operating system, which GDB uses to work.

Here is a simple program (example1.c) that divides by zero to show the use of GDB:

# includeint divide () {int xero5, yellow0utterreturn x / y;} int main () {divide ();}

Show examples of GDB usage

Call GDB: start gdb by executing gdb on the command line:

Call gdb

After the call, it waits for the terminal command and executes until it exits.

If a process is already running, you need to connect GDB to it, which can be achieved by specifying the process ID. Assuming that the program has crashed, to analyze the cause of the problem, use GDB to analyze the core file.

Startup program: once you are in GDB, use the run command to start the program for debugging.

Pass parameters to the program: use set args to pass parameters to your program, which will be obtained the next time the program runs.' Show args' displays the parameters passed to the program.

Check the stack: whenever the program stops, the first thing anyone wants to figure out is why it stopped and how it stopped there. This information is called reverse tracking. Each function call and local variable, passed parameters, call location and other information generated by the program are stored together in the stack of data blocks, which is called a frame. We can use GDB to examine all of this data. GDB numbers the frames starting at the lowest level.

Bt: print backtracking of the entire stack

Bt prints backtracking of n frames

Frame: switch to the specified frame and print that frame

Up: move up'n 'Fram

Down: move down'n 'frames (n defaults to 1)

Check data: the data of the program can be checked by GDB using the 'print' command. For example, if'x'is a variable within the debugger, 'print x' prints the value of x.

Check source code: the source code can be printed in GDB. By default, the 'list' command prints 10 lines of code.

List: list the source code around the 'linenum' line

List: list the source code starting with 'function'

Disas: displays the machine code of this function

Stop and restore programs: with GDB, we can set breakpoints, observation points, etc., where necessary to stop the program.

Break: set a breakpoint in 'location'. When the program executes here, the breakpoint will be hit and control will be given to the user.

Watch: GDB will stop when 'expr' is written by the program and its value changes

Catch: GDB stops when 'event' occurs

Disable: disables specified breakpoint

Enable: enable specified breakpoint

Delete: delete breakpoints / observation points / capture points. If no parameters are passed, the default operation is at all breakpoints

Step: execute the program step by step

Continue: continue to execute the program until it is finished

Exit GDB: also exit from GDB with the 'quit' command.

There are more options available for GDB. GDB uses the help option to learn more.

Get help in GDB

What are the versions of Linux? the versions of Linux are Deepin, UbuntuKylin, Manjaro, LinuxMint, Ubuntu and so on. Among them, Deepin is one of the best-developed Linux distributions in China; UbuntuKylin is a derivative release based on Ubuntu; Manjaro is a Linux release based on Arch; LinuxMint's default Cinnamon desktop is similar to Windows XP's easy-to-use; Ubuntu is the Linux operating system based on desktop applications.

These are the Linux user space debugging tools that Xiaobian shared with you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report