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

Tutorial for getting started with gdb in linux

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

Gdb is a very easy to use debugging tool under linux. Although it is a debugging tool in command line mode, it is so powerful that you can't imagine. Here is a brief introduction to the use of gdb. Friends who need it, let's take a look at the detailed introduction.

Breakpoint

Break at the specified location in the code, so that the program is interrupted here.

Break stops break at the specified line number when entering the specified function. Break + /-offset stops on the offset line before or after the current line number. Offiset is a natural number. Break filename:linenum stops at the linenum line of the source file filename. Break... If... Can be the above parameters, condition represents the condition, stop when the condition is established. For example, in a loop, you can set break if iTunes 100, which means to stop the program when I is 100.

Case study:

(gdb) break sc_smartcontract_handler.cpp:45Breakpoint 4 at 0x424d42: file sc_smartcontract_handler.cpp, line 45.

Related operations on breakpoints

Delete deletes all breakpoints delete breakpoint [n] deletes a breakpoint disable breakpoint [n] disables a breakpoint enable breakpoint [n] enables a breakpoint info breakpoints [n] to view current breakpoint information

Observation point

The capture point is used to catch some events while the program is running. Such as: loading shared libraries (dynamic link libraries), C++ exceptions, and so on. It is also usually used to locate bug.

The command format for capture points is: catch, and event can be the following

Interrupt when watch variable changes rwatch variable is read interrupt awatch variable value is read or written

You can view the current observation point information through the info watchpoints [n] command

View variabl

The most common way to view variables is

(gdb) print {variable name}

(gdb) print argc$1 = 1

If you print an array, the tail of the array that may be printed is not displayed due to the default setting. You can set the maximum length of the printed array by using the following command

(gdb) set print elements 300

View the code in debugging

List function such as list main: display the code near the main function list file:function such as list main.c:main: display the code near the main function in main.c list N1 Magi N2 such as list 10 Magi 20, display 10 to 20 lines of the current file

Although list is already very convenient, it is still not satisfactory. If only I could display the code while running, the answer is yes.

Start gdb with the following command: gdb-tui project name or after starting gdb, enter the command focus, as shown in the figure:

Resume program running and single-step debugging

In gdb, the main commands related to debugging steps are as follows:

Continue continues to run the program until the next breakpoint (similar to F5 in VS) next steps step by step, does not enter the sub-function (similar to F10 in VS) setp step by step, will enter the sub-function (similar to F11 in VS) until runs until the end of the current statement block finish runs to the end of the function and jumps out, and prints the return value of the function (similar to VS's Shift+F11)

Interrupt in a specific thread

You can define whether your breakpoint is on all threads or on a particular thread. GDB can easily help you with the work.

Break thread break thread if...

Linespec specifies the line number of the source program where the breakpoint is set. Threadno specifies the ID of the thread. Note that this ID is assigned by GDB, and you can view the thread information in the running program through the "info threads" command. If you do not specify thread, your breakpoint is set on all threads. You can also specify breakpoint conditions for a thread. Such as:

(gdb) break frik.c:13 thread 28 if bartab > lim

When your program is stopped by GDB, all running threads will be stopped. This makes it easy for you to see the overall situation of the running program. When you resume running the program, all threads will be resumed. Even when the main process is being debugged in a single step.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, 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: 280

*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