精华内容
下载资源
问答
  • GSTACK(1) Linux Programmer's Manual GSTACK(1) NAME  gstack - print a stack trace of a running process SYNOPSIS  gstack pid DESCRIPTION  
    GSTACK(1)                  Linux Programmer's Manual                 GSTACK(1)


    NAME
           gstack - print a stack trace of a running process


    SYNOPSIS
           gstack pid


    DESCRIPTION
           gstack  attaches  to  the active process named by the pid on the command line, and prints out an execution stack
           trace.  If ELF symbols exist in the binary (usually the case  unless  you  have  run  strip(1)),  then  symbolic
           addresses are printed as well.


           If  the  process  is part of a thread group, then gstack will print out a stack trace for each of the threads in
           the group.


    SEE ALSO

           nm(1), ptrace(2), gdb(1)


    实例:


    展开全文
  • gstack命令的说明如下: NAME gstack - print a stack trace of a running process SYNOPSIS gstack pid DESCRIPTION gstack attaches to the active process named by the pid on the c...

    gstack命令的说明如下:

    NAME
           gstack - print a stack trace of a running process
    
    SYNOPSIS
           gstack pid
    
    DESCRIPTION
           gstack  attaches  to the active process named by the pid on the command line, and prints out an execution stack trace.  If ELF sym‐bols exist in the binary (usually the case unless you have run strip(1)), then symbolic addresses are printed as well.
    
           If the process is part of a thread group, then gstack will print out a stack trace for each of the threads in the group.

    它的用法很简单,只要在gstack命令后面跟上某个进程pid就可以,例如查询一个newthread进程的运行堆栈信息:

    [yuanping@Linux ~]$ gstack `pgrep newthread | head -1`
    Thread 2 (Thread 0xb77d7b40 (LWP 4165)):
    #0  0x00e13424 in __kernel_vsyscall ()
    #1  0x4ebf79c6 in nanosleep () from /lib/libc.so.6
    #2  0x4ebf77df in sleep () from /lib/libc.so.6
    #3  0x0804853a in thread ()
    #4  0x4ed27cd3 in start_thread () from /lib/libpthread.so.0
    #5  0x4ec324de in clone () from /lib/libc.so.6
    Thread 1 (Thread 0xb77d8900 (LWP 4164)):
    #0  0x00e13424 in __kernel_vsyscall ()
    #1  0x4ed28dd5 in pthread_join () from /lib/libpthread.so.0
    #2  0x080485bd in main ()
    [yuanping@Linux ~]$ 

     

    转载于:https://www.cnblogs.com/yuanping/archive/2013/01/07/2850316.html

    展开全文
  • linux环境下查看C/C++程序的堆栈信息

    千次阅读 2019-05-08 16:16:06
    在源代码编辑框上点击就可以添加断点,在调用堆栈窗口就可以看到程序运行的堆栈信息,但是在 linux 环境下,面对命令行天下,我们需要掌握一些命令,才能够查看C/C++程序的堆栈信息。 测...

    前言

    经常在Windows上开发的工程师们可能已经习惯了图形化的调试界面,在源代码的编辑框上点击就可以添加断点,在调用堆栈的窗口就可以看到程序运行的堆栈信息,但是在 linux 环境下,面对命令行的天下,我们需要掌握一些命令,才能够查看C/C++程序的堆栈信息。

    测试环境

    [albert@localhost#13:58:34#/home/albert]$cat /etc/issue
    CentOS release 6.3 (Final)
    Kernel \r on an \m
    
    [albert@localhost#13:58:43#/home/albert]$g++ --version
    g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)
    Copyright ?? 2010 Free Software Foundation, Inc.
    
    

    查看方法

    1. 使用gdb程序调试core文件,格式为 gdb test_proc core.proc_id

    2. 使用gdb程序附加到调试程序的进程上,格式为 gdb attach proc_id

    3. 使用pstack程序输出调试程序的堆栈信息,格式为 pstack proc_id

    4. 使用strace程序打印调试程序的运行信息,格式为 strace -p proc_id

    具体实践

    • 一般查看堆栈信息时常常面对的都是多线程的程序,所以我们也来写一个简单的多线程小程序,代码如下:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <unistd.h>
    
    static void* thread_proc(void* arg)
    {
        unsigned int sum = 3;
        while(true)
        {
            for (int idx = 0; idx < 1000000000; ++idx)
                sum += idx;
    
            printf("thread sum = %u\n", sum);
            sleep(2);
        }
    
        return 0;
    }
    
    int main()
    {
        pthread_t thread_id;
        pthread_create(&thread_id, NULL, thread_proc, NULL);
        unsigned int sum = 0;
    
        while(true)
        {
            for (int idx = 0; idx < 1000000000; ++idx)
                sum += idx;
    
            printf("main sum = %u\n", sum);
            sleep(1);
        }
    
        return 0;
    }
    
    • 编译程序并运行,程序开始不断的打印计算结果
    [albert@localhost#15:06:54#/home/albert/test/threadtest]$g++ threadtest.cpp -O0 -pthread -o threadtest
    [albert@localhost#15:08:27#/home/albert/test/threadtest]$./threadtest
    thread sum = 3051657987
    main sum = 3051657984
    thread sum = 1808348675
    main sum = 1808348672
    main sum = 565039360
    thread sum = 565039363
    main sum = 3616697344
    thread sum = 3616697347
    ...
    
    • 现在可以通过上面描述的方法来查看threadtest程序堆栈信息了,几乎所有的命令都需要进程id,所以我们可以再开一个终端先通过pidof命令来获得:
    [albert@localhost#15:39:35#/home/albert/test/threadtest]$pidof threadtest
    21473
    

    gdb调试core文件

    1. 通过kill命令产生core文件

    使用命令 kill -11 21473可以将正在运行的程序杀死,并且产生core文件core.21473,-11表示段错误信号,通常是访问了无效的内存导致

    1. 通过gcore命令产生core文件

    使用命令 gcore 21473可以产生core文件core.21473,但是不会杀死程序,适用于调试线上程序,又不影响用户使用的情况,可以测试一下:

    [albert@localhost#15:39:43#/home/albert/test/threadtest]$gcore 21473
    warning: the debug information found in "/usr/lib/debug//lib64/libm-2.12.so.debug" does not match "/lib64/libm.so.6" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug/lib64/libm-2.12.so.debug" does not match "/lib64/libm.so.6" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug//lib64/libpthread-2.12.so.debug" does not match "/lib64/libpthread.so.0" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug/lib64/libpthread-2.12.so.debug" does not match "/lib64/libpthread.so.0" (CRC mismatch)
    [New LWP 21474]
    [Thread debugging using libthread_db enabled]
    warning: the debug information found in "/usr/lib/debug//lib64/libc-2.12.so.debug" does not match "/lib64/libc.so.6" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug/lib64/libc-2.12.so.debug" does not match "/lib64/libc.so.6" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug//lib64/ld-2.12.so.debug" does not match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch)
    warning: the debug information found in "/usr/lib/debug/lib64/ld-2.12.so.debug" does not match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch)
    0x00000000004006eb in main ()
    Saved corefile core.21473
    

    然后使用gdb调试core文件:

    [albert@localhost#15:47:13#/home/albert/test/threadtest]$gdb threadtest core.21473
    GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    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-redhat-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/albert/test/threadtest/threadtest...(no debugging symbols found)...done.
    [New Thread 21474]
    [New Thread 21473]
    Missing separate debuginfo for
    Try: yum --enablerepo='*-debug*' install /usr/lib/debug/.build-id/80/1b9608daa2cd5f7035ad415e9c7dd06ebdb0a2
    Reading symbols from /usr/lib64/libstdc++.so.6...(no debugging symbols found)...done.
    Loaded symbols for /usr/lib64/libstdc++.so.6
    Reading symbols from /lib64/libm.so.6...
    
    ...省略无关信息
    
    (no debugging symbols found)...done.
    Loaded symbols for /lib64/ld-linux-x86-64.so.2
    Core was generated by `./threadtest'.
    #0  0x0000000000400691 in thread_proc(void*) ()
    Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.209.el6_9.2.x86_64 libstdc++-4.4.7-18.el6_9.2.x86_64
    (gdb)
    

    显示所有线程信息,可以使用gdb命令thread apply all bt

    (gdb) thread apply all bt
    
    Thread 2 (Thread 0x7f1b4e1d2720 (LWP 21473)):
    #0  0x00000000004006eb in main ()
    
    Thread 1 (Thread 0x7f1b4d270700 (LWP 21474)):
    #0  0x0000000000400691 in thread_proc(void*) ()
    #1  0x00007f1b4d60caa1 in start_thread () from /lib64/libpthread.so.0
    #2  0x00007f1b4d359bcd in clone () from /lib64/libc.so.6
    

    gdb附加到进程

    可以通过 gdb attach pid 直接附加到正在运行的程序上,然后查看线程信息thread apply all bt

    [albert@localhost#15:54:59#/home/albert/test/threadtest]$gdb attach 21473
    GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    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-redhat-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    attach: 没有那个文件或目录.
    Attaching to process 21473
    Reading symbols from /home/albert/test/threadtest/threadtest...(no debugging symbols found)...done.
    Reading symbols from /usr/lib64/libstdc++.so.6...(no debugging symbols found)...done.
    Loaded symbols for /usr/lib64/libstdc++.so.6
    Reading symbols from /lib64/libm.so.6...
    
    ...省略无关信息
    
    (no debugging symbols found)...done.
    Loaded symbols for /lib64/ld-linux-x86-64.so.2
    0x00007f1b4d31dc4d in nanosleep () from /lib64/libc.so.6
    Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.209.el6_9.2.x86_64 libstdc++-4.4.7-18.el6_9.2.x86_64
    (gdb) thread apply all bt
    
    Thread 2 (Thread 0x7f1b4d270700 (LWP 21474)):
    #0  0x00007f1b4d31dc4d in nanosleep () from /lib64/libc.so.6
    #1  0x00007f1b4d31dac0 in sleep () from /lib64/libc.so.6
    #2  0x00000000004006b6 in thread_proc(void*) ()
    #3  0x00007f1b4d60caa1 in start_thread () from /lib64/libpthread.so.0
    #4  0x00007f1b4d359bcd in clone () from /lib64/libc.so.6
    
    Thread 1 (Thread 0x7f1b4e1d2720 (LWP 21473)):
    #0  0x00007f1b4d31dc4d in nanosleep () from /lib64/libc.so.6
    #1  0x00007f1b4d31dac0 in sleep () from /lib64/libc.so.6
    #2  0x0000000000400721 in main ()
    

    pstack输出堆栈信息

    如果不需要调试,只想查看运行程序当前的堆栈信息,可以使用pstack命令,输出信息很简洁:

    [albert@localhost#15:57:53#/home/albert/test/threadtest]$pstack 21473
    Thread 2 (Thread 0x7f1b4d270700 (LWP 21474)):
    #0  0x0000000000400683 in thread_proc(void*) ()
    #1  0x00007f1b4d60caa1 in start_thread () from /lib64/libpthread.so.0
    #2  0x00007f1b4d359bcd in clone () from /lib64/libc.so.6
    Thread 1 (Thread 0x7f1b4e1d2720 (LWP 21473)):
    #0  0x00007f1b4d31dc4d in nanosleep () from /lib64/libc.so.6
    #1  0x00007f1b4d31dac0 in sleep () from /lib64/libc.so.6
    #2  0x0000000000400721 in main ()
    

    strace打印程序运行情况

    strace输出的不是堆栈信息,而是类似于程序的运行步骤,具体信息如下:

    [albert@localhost#15:57:56#/home/albert/test/threadtest]$strace -p 21473
    Process 21473 attached
    write(1, "main sum = 2580918016\n", 22) = 22
    rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
    rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    nanosleep({1, 0}, 0x7fff56a49c50)       = 0
    write(1, "main sum = 1337608704\n", 22) = 22
    rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
    rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    nanosleep({1, 0}, 0x7fff56a49c50)       = 0
    write(1, "main sum = 94299392\n", 20)   = 20
    rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
    rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    nanosleep({1, 0}, 0x7fff56a49c50)       = 0
    ^CProcess 21473 detached
    

    总结

    • 在解决实际问题的过程中,上述几种方法可以结合使用,选取合适的使用方法,比如面对程序突然崩溃,那么gdb proc core就是调试的首选方法。
    • 如果只是想简单的查看堆栈信息,可以使用pstack pid这种方式,免去了生成巨大core文件的麻烦。
    • 如果还想查看运行逻辑中的变量信息,那么gdb使我们可以帮助我们动态调试程序,查看一些程序运行时的状态。
    展开全文
  • Linux查看线程的堆栈信息

    千次阅读 2016-01-18 22:49:39
    1、使用top命令,查找pid 2、显示线程,查找线程tid  ps -mp #pid# -o THREAD,tid,time | sort -rn 3、将线程id转化为16进制0xtid ...4、显示线程的堆栈信息  jstack #pid#|grep #0xtid# -A 30

    1、使用top命令,查找pid

    2、显示线程,查找线程tid

         ps -mp #pid# -o THREAD,tid,time | sort -rn

    3、将线程id转化为16进制0xtid

         printf "%x\n" #tid#

    4、显示线程的堆栈信息

        jstack #pid#|grep #0xtid# -A 30

    展开全文
  • 1、jps:查看本机java进程信息。 2、jstack:打印线程信息,制作线程dump文件。 3、jmap:打印内存映射,制作堆dump文件 4、jstat:性能监控工具 5、jhat:内存分析工具 6、jconsole:简易可视化控制台 7、...
  • linux出现cpu被java程序消耗过高时,我们使用jdk自带jstack来分析: 1.使用top命令,找到cpu消耗很高进程pid (jps或或ps -ef|grep java也可以查看java进程) 语法:top 2、查看高负载进程下高负载线程...
  • linux下编写程序时,有时会出现Segment fault错误,此时内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb可以查看core文件,可以指示出导致程序出错代码所在文件和行数...
  • linux下查找java进程占用CPU过高原因 1. 查找进程 top查看进程占用资源情况 明显看出java两个进程22714,...3.查找java的堆栈信息 将线程id转换成十六进制 #printf "%x\n" 15664 #3d30 ps:16进制转10进制...
  • 最近系统总是每隔一周左右就莫名其妙down掉。...这个命令对于查看Jvm的堆栈信息很有用。能够查看eden,survivor,old,perm等heapcapacity,utility信息 对于查看系统是不是有能存泄漏以及参数设置是否合理有不错意义
  •  在linux下 用ps命令查看java进程进程号,然后用kill -3 pid方法,使得java进程向自己标准输出(或已经被重定向),输出当前进程的堆栈、内存、线程、锁使用情况。  如果有死锁话,会明确打印出dead lock...
  • 记得3年前,我为了查看100M日志文件里面错误堆栈信息,百度了许久都毫无结果 没想到今天再次百度时,一下子看到了grep -A 命令,激动不已。 原来只需要用, grep -A 100 'KeyWord' file grep -B 100 '...
  • Linux 如何查看一个进程的堆栈

    千次阅读 2019-07-07 12:51:27
    1.用pstack 第一种:pstack 进程ID ...不过,使用gdb方法,还可以查看某些信息,例如局部变量,指针等。 不过,如果只看调用栈话,pstack还是很方便。 https://www.ibm.com/developerw...
  • 查看寄存器值,很简单,可以使用如下命令:   info registers 查看寄存器情况。(除了浮点寄存器)   info all-registers 查看所有寄存器情况。(包括浮点寄存器)   info registers ...
  • Java线程堆栈信息查看

    万次阅读 2015-12-15 09:55:58
    Linux 下Java程序线程堆栈查看方法: 第一步:在终端运行Java程序 ...第四步:通常情况下运行项目可能会比较大,那么这个时候打印的堆栈信息可能会有几千到几万行,为了方便查看,我们往往需要将输出内容进行
  • 写应用程序时时常出现程序异常崩溃退出,这里总结Linux反汇编根据程序崩溃堆栈信息定位问题方法。 程序崩溃堆栈信息 使用命令dmesg查看: ifotond: unhandled page fault (11) at 0x00000000, code 0x017 pgd ...
  • 这个命令对于查看Jvm的堆栈信息很有用。能够查看eden,survivor,old,perm等heapcapacity,utility信息 对于查看系统是不是有能存泄漏以及参数设置是否合理有不错意义 2. jstack 这个是用来查看jvm当前...
  • 一、linux下查找java进程占用cpu过高原因 ...3.查找java的堆栈信息 将线程id转换成十六进制 printf "%x\n" 15664 4.使用jstack查询线程堆栈信息 jstack <pid> | grep -a 20 线程id(十六进制) 定位到...
  • JVM定位占用cpu过高堆栈信息Linux

    千次阅读 2017-01-09 15:22:14
    第一步:使用top命令查看进程占用CPU情况,获取PID top 从图中可以看出cpu占用高达46.5%进程ID是16243 第二步:使用top -H -p命令获取进程中线程信息(其中H:显示进程中线程情况,p表示进程pid) top -H...
  • 1、用命令top查看java进程内存和cpu占用情况。 2、jmap -histo pid查询指定java进程所有对象使用内存情况。 3、ps -mp pid -o THREAD,tid,time查询...5、jstack pid |grep tid -A 30 查看该线程堆栈信息 ...
  • 最近系统总是每隔一周左右就莫名其妙down掉。于是研究了一下jdk下面几个常用的查看 ...jvm运行时状态参数,很快找出了问题所在。... 这个命令对于查看Jvm的堆栈信息很有用。能够查看eden,survivor,old,per

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 176
精华内容 70
关键字:

查看堆栈信息的命令linux

linux 订阅