栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux gdb调试三(多线程)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux gdb调试三(多线程)

创作人QQ:851301776,邮箱:lfr890207@163.com
        欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点!

个人座右铭:
         1.没有横空出世,只要厚积一定发。
         2.你可以学历不高,你可以不上学,但你不能不学习
 

一、代码
​


#include 
#include 
int i;

void *pthread_proc_1(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %dn", __func__, i);
        sleep(5);
    }


    return NULL;
}
void *pthread_proc_2(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %dn", __func__, i);
        sleep(5);
    }


    return NULL;
}
void *pthread_proc_3(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %dn", __func__, i);
        sleep(5);
    }


    return NULL;
}


int main(void)
{
    pthread_t thread1;
    pthread_create(&thread1, NULL, pthread_proc_1, NULL);
    pthread_t thread2;
    pthread_create(&thread2, NULL, pthread_proc_2, NULL);
    pthread_t thread3;
    pthread_create(&thread3, NULL, pthread_proc_3, NULL);


    sleep(500);
    return 0;
}

[点击并拖拽以移动]
​

代码不做分析

二、调试

        主要目的熟悉多线程:

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.

        (1)打断点在主函数中,断点必须是在创建线程完成后。

(gdb) b pthread_test.c:57
Breakpoint 1 at 0x400872: file pthread_test.c, line 57.
(gdb) r
Starting program: /home/mrlee/test/test/test/test/test/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77ef700 (LWP 74724)]
pthread_proc_1 1
[New Thread 0x7ffff6fee700 (LWP 74725)]
pthread_proc_2 2
[New Thread 0x7ffff67ed700 (LWP 74726)]

Thread 1 "a.out" hit Breakpoint 1, main () at pthread_test.c:57
warning: Source file is more recent than executable.
57        sleep(500);

        (2)查看线程数量与我们代码是否相同

(gdb) info threads
  Id   Target Id         frame
* 1    Thread 0x7ffff7fd0700 (LWP 74720) "a.out" main () at pthread_test.c:57
  2    Thread 0x7ffff77ef700 (LWP 74724) "a.out" 0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
  3    Thread 0x7ffff6fee700 (LWP 74725) "a.out" 0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
  4    Thread 0x7ffff67ed700 (LWP 74726) "a.out" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:81

备注:*号表示现在对应的线程,前面的数字表示线程对应的id

        (3)进入对应线程

(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff77ef700 (LWP 74724))]
#0  0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
84    ../sysdeps/unix/syscall-template.S: 没有那个文件或目录.

        (4)锁定在此线程

(gdb) set scheduler-locking on
 

        (5)查看此线程运行状态

(gdb) c
Continuing.
pthread_proc_1 3
pthread_proc_1 4
set sch    pthread_proc_1 5

        (6)解除线程锁定

(gdb) set scheduler-locking off
(gdb) c
Continuing.
pthread_proc_1 6
pthread_proc_3 7
pthread_proc_2 8

       

        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/757527.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号