首页 > 系统相关 >【gdb】调试已经运行的进程

【gdb】调试已经运行的进程

时间:2023-10-15 20:47:22浏览次数:41  
标签:Thread lib symbols so.1 gdb 进程 New 调试

 调试已经运行的进程

1.例子:

#include <stdio.h>
#include <pthread.h>
void *thread_func(void *p_arg)
{
  while (1)
  {
    printf("%s\n", (char *)p_arg);
    sleep(10);
  }
}
int main(void)
{
  pthread_t t1, t2;

  pthread_create(&t1, NULL, thread_func, "Thread 1");
  pthread_create(&t2, NULL, thread_func, "Thread 2");

  sleep(1000);
  return;
}

调试已经运行的进程有两种方法:一种是gdb启动时,指定进程的ID:gdb program processID(也可以用-p或者--pid指定进程ID,例如:gdb program -p=10210)。以上面代码为例,用“ps”命令已经获得进程ID为10210:

bash-3.2# gdb -q a.out 10210
Reading symbols from /data/nan/a.out ...done.
Attaching to program `/data/nan/a.out', process 10210
[New process 10210]
Retry #1:
Retry #2:
Retry #3:
Retry #4:
Reading symbols from /usr/lib/libc.so.1...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New LWP    3        ]
[New LWP    2        ]
[New Thread 1 (LWP 1)]
[New Thread 2 (LWP 2)]
[New Thread 3 (LWP 3)]
Loaded symbols for /usr/lib/libc.so.1
Reading symbols from /lib/ld.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/ld.so.1
[Switching to Thread 1 (LWP 1)]
0xfeeeae55 in ___nanosleep () from /usr/lib/libc.so.1
(gdb) bt
#0  0xfeeeae55 in ___nanosleep () from /usr/lib/libc.so.1
#1  0xfeedcae4 in sleep () from /usr/lib/libc.so.1
#2  0x080509ef in main () at a.c:17

另一种是先启动gdb,然后用“attach”命令“附着”在进程上:

bash-3.2# gdb -q a
Reading symbols from /data/nan/a...done.
(gdb) attach 10210
Attaching to program `/data/nan/a', process 10210
[New process 10210]
Retry #1:
Retry #2:
Retry #3:
Retry #4:
Reading symbols from /usr/lib/libc.so.1...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New LWP    3        ]
[New LWP    2        ]
[New Thread 1 (LWP 1)]
[New Thread 2 (LWP 2)]
[New Thread 3 (LWP 3)]
Loaded symbols for /usr/lib/libc.so.1
Reading symbols from /lib/ld.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/ld.so.1
[Switching to Thread 1 (LWP 1)]
0xfeeeae55 in ___nanosleep () from /usr/lib/libc.so.1
(gdb) bt
#0  0xfeeeae55 in ___nanosleep () from /usr/lib/libc.so.1
#1  0xfeedcae4 in sleep () from /usr/lib/libc.so.1
#2  0x080509ef in main () at a.c:17

如果不想继续调试了,可以用“detach”命令“脱离”进程:

(gdb) detach
Detaching from program: /data/nan/a, process 10210
(gdb) bt
No stack.

 

参考资料

1. 调试已经运行的进程

标签:Thread,lib,symbols,so.1,gdb,进程,New,调试
From: https://www.cnblogs.com/sunbines/p/17766131.html

相关文章

  • 【gdb】让catchpoint只触发一次
    让catchpoint只触发一次1.例子:#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>intmain(void){pid_tpid;inti=0;for(i=0;i<2;i++){ pid=fork(); if(pid<0)......
  • 【gdb】打印内存的值
    打印内存的值1.例子#include<stdio.h>intmain(void){inti=0;chara[100];for(i=0;i<sizeof(a);i++){a[i]=i;}return0;}gdb中使用“x”命令来打印内存的值,格式为“x/nfuaddr”......
  • 【gdb】进入和退出图形化调试界面
    进入和退出图形化调试界面1.例子#include<stdio.h>voidfun1(void){inti=0;i++;i=i*2;printf("%d\n",i);}voidfun2(void){intj=0;fun1();j++;j=j*2;print......
  • 【gdb】打印数组的索引下标
    打印数组的索引下标1.例子#include<stdio.h>intnum[10]={1<<0,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7,1<<8,1<<9};intmain(void){inti;for......
  • 【gdb】gdb目录索引
    gdb目录索引 打印1打印ASCII和宽字符字符串打印数组中任意连续元素......
  • 【gdb】打印ASCII和宽字符字符串
    打印ASCII和宽字符字符串1.例子:#include<stdio.h>#include<wchar.h>intmain(void){charstr1[]="abcd";wchar_tstr2[]=L"abcd";return0;}用gdb调试程序时,可以使用“x/s”命令打印ASCII字符串。以上面程序为例:[root@node0......
  • 【gdb】设置观察点
    设置观察点1.例子:#include<stdio.h>#include<pthread.h>typedefstruct{inta;intb;intc;intd;pthread_mutex_tmutex;}ex_st;intmain(void){ex_stst={1,2,3,4,PTHREAD_MUTEX_INITIALIZER};printf("%d,%d,%d,%d\n&......
  • 进程的三态模型
        ......
  • 【gdb】run和start区别
    run和start区别gdb调试器提供了多种方式来启动目标程序,其中最常用的就是run指令,其次为start指令。也就是说,run和start指令都可以用来在gdb调试器中启动程序,它们之间的区别是:1、默认情况下,run指令会一直执行程序,直到执行结束。如果程序中手动设置有断点,则run指令会执行......
  • 【CSS3】CSS3 3D 转换 ③ ( 3D 透视视图 | translateZ 转换分析 | 网页调试工具调试 t
    文章目录一、translateZ转换分析1、translateZ转换对应的空间分析2、网页调试工具调试translateZ属性值二、代码示例-translateZ转换分析1、代码示例-translateZ为0的参照示例2、代码示例-translateZ为200示例一、translateZ转换分析1、translateZ转换对应的......