Linux learning --Gdb basic debugging multithreaded debugging method &&

The basic debugging 1.Gdb

Sample Code

//e.c
 #include <stdio.h>
 void debug(char *str)
{
    printf("debug info :%s\n",str );
}

int main(int argc,char *argv[]){
    int i,j;
    j=0;
    for(i=0;i<10;i++){
        j+=5;
        printf("now a=%d\n", j);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

gcc -g -oe ec
debugging gdb E
or type gdb
and file e

1. list Command Usage

list command to display multiple lines of source code, start from the last position of the display, by default, display 10 rows, when for the first time, in fact, the position of the display from the code.

list n显示已第n行未中心的10行代码
list functionname显示以functionname的函数为中心的10行代码
  • 1
  • 2

2. breakpoint commands BREAK
BREAK location: setting a breakpoint at the location position, the change of position may be a row address, a function name or other structures. gdb will stop before executing the code to that location.

使用delete breakpoints 断点号 删除断点
这里的断点号表示的是第几个断点,刚才执行break 10返回 
reakpoint 1 at 0x40050a: file e.c, line 10.
中的1表示该断点的标号,因此使用 delete breakpoints 1表示删除第10行所定义的断点

clear n表示清除第n行的断点,因此clear 10等同于delete breakpoints 1
disable/enable n表示使得编号为n的断点暂时失效或有效
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Breakpoints can use the info to view information related
info breakpoints

c (continue), the program continues to run until the next breakpoint

3.display command
to view the value of the parameter

4.step and next command
step may be executed one by one so that the program, i.e., after executing a jump statement before the next statement is then stopped, waiting for the user's command. General use step command, use the display command to view or watch the change of variables in order to determine whether the behavior of the program to meet the requirements. When the next instruction is a function, s function into the interior, which stop before the first statement. next single step, but does not enter the internal function.
step n, next n represent continuous but not execute n instructions, if a breakpoint is encountered during the stop
5.print internal print variable values
6.bt  review the stack information
changes 7.watch monitor variable values

watch usually requires and break, run, continue joint use.

The following example:

Code is as follows:
#include <stdio.h>

main int ()
{
int A = 0;
for (int I = 0; I <10; I ++)
A I + =;
}
when debugging process is as follows:

(GDB) L
. 1 #include <stdio.h>
2
. 3 int main ()
. 4 {
. 5 A int = 0;
. 6 for (int I = 0; I <10; I ++)
. 7 A I + =;
. 8}
(GDB) b 5 ------- 5 disposed off in line
breakpoint aT 0x80483ba. 1: File a.cpp, line 5. the
(GDB) R & lt ------- stop execution at the breakpoint
Starting program: / ao

. 1 Breakpoint, main () AT a.cpp:. 5
. 5 int a = 0;
(GDB) ------- Watch a value of a observed, when there is a change, stopping
Hardware the watchpoint 2: a
(GDB) C ------- continued, stopped when the change in the value of a
continuing.
Hardware the watchpoint 2: a

Old value = 0
New value = 1
main () at a.cpp:6
6 for(int i=0; i<10; i++)
(gdb)
Continuing.
Hardware watchpoint 2: a

Old value = 1
New value = 3
main () at a.cpp:6
6 for(int i=0; i<10; i++)
(gdb)
Continuing.
Hardware watchpoint 2: a

That is, when the watch using the following steps:

  1. Using break set breakpoints located at variable to be observed;

  2. Use run execution until a breakpoint;

  3. Use watch setting observation points;

  4. Continue to observe whether the set of observation points have changed.

8.set variable value = x dynamically change the value of the variable
1, when debugging the need to modify the value of the temporary variable, you can use the set command
syntax:

set variable key = value
set var key = value
示例:
(gdb) set variable array[1] = 12

2, another simpler way, using the modified print command
syntax:
print Key value =
Here Insert Picture Description

2. Multi-thread debugging

1. Check the thread
first create two threads:

#include <stdio.h>
#include <unistd.h> #include <pthread.h> #include <stdlib.h> #include <string.h> void* pthread_run1(void* arg) { (void)arg; while(1) { printf("I am thread1,ID: %d\n",pthread_self()); sleep(1); } } void* pthread_run2(void* arg) { (void)arg; while(1) { printf("I am thread2,ID: %d\n",pthread_self()); sleep(1); } } int main() { pthread_t tid1; pthread_t tid2; pthread_create(&tid1,NULL,pthread_run1,NULL); pthread_create(&tid2,NULL,pthread_run2,NULL); printf("I am main thread\n"); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

Analysis: The above program created two threads, to implement the program, main function of the program where the main thread, there are two new threads running in the main thread.
View command line:

//查看当前运行的进程
ps aux|grep a.out
//查看当前运行的轻量级进程
ps -aL|grep a.out
//查看主线程和新线程的关系
pstree -p 主线程id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Here Insert Picture Description
2. Check the thread stack structure

  1. Get Thread ID
  2. View stack structure ps stack thread ID command
    Here Insert Picture Description
    3. Use gdb to view the thread information
    to attach gdb debugger which process to see whether to create a new thread: gdb attach the main thread ID
    Here Insert Picture Description

Some threads View

// 1 viewing process. : Info inferiors
. // 2 View Thread : Threads info
// 3 View thread stack structure. : Bt
// 4 switching threads. : The Thread n (n represents the first of several threads)
Here Insert Picture Description

4. Use gdb debugging multithreaded
  when the program does not start, the thread has not been performed, this time using the gdb debugger to debug multi-threaded and normal program, by setting breakpoints, run, view the information, etc., is not demo here will eventually add thread debugging command

Set a breakpoint

//1. 设置断点:break 行号/函数名
//2. 查看断点:info b
  • 1
  • 2

Here Insert Picture Description
Execution thread function 2, referring to the line continues to run to completion breakpoint

1. 继续使某一线程运行:thread apply 1-n(第几个线程) n
2. 重新启动程序运行到断点处:r
  • 1
  • 2

Here Insert Picture Description
3. Run only the current thread

. 设置:set scheduler-locking on 2
. 运行:n
  • 1
  • 2

Here Insert Picture Description
4. All threads execute concurrently

    1. 设置:set scheduler-locking off
    2. 运行:n
  • 1
  • 2

Here Insert Picture Description

Precautions:
Ctrl Ctrl + C + D + Z Ctrl distinction usage scenarios and

Ctrl + C: trap routine, the program runs no matter where are stopped.

Ctrl + D: transmitting a signal exit, exit the current user or client.

Ctrl + Z: suspend the program, maintaining the suspended state in the process.

Others quoted saying:

1, Ctrl + C relatively violence, is to send Terminal to the current program, for example, you are running a search function, the file is looking in, Ctrl + C will force the end of the current process.
2, Ctrl + Z is to suspend the current program, to suspend the implementation of this program, such as mysql terminal you are, you need to point out to engage other file operations, do not want to quit mysql terminal (because the next will have to enter a user name and password to enter , very troublesome), will then be ctrl + z mysql suspended, and other operations, and then enter a carriage return after fg can come back, of course, a lot of the process may be suspended to the background, then add number fg able to suspend the process of return to the front desk. Of course, with the bg (background) and fg commands before and after the switching station will be very convenient.
3, Ctrl + D exit is to send a signal, not so strong, similar operations ctrl + C, such as you return from your administrator root to ordinary users can use it.

Guess you like

Origin www.cnblogs.com/jack-hzm/p/11372285.html