Linux View process Stack information __linux

Source: Internet
Author: User

Today, when debugging a program on Linux, I found that sometimes the program will hang live, so I would like to find out if the program has those threads, what are doing. Looking for a Linux command, I can really meet my needs. Let's look at a small example.

To prepare a program for simplicity, use python here to write, creating two threads to perform each task.

Import Threading
Import Time


def test1 (): While
    (True):
        time.sleep (1)
        print ' test1 '

def Test2 (): While
    (True):
        time.sleep (1)
        print ' test2 '


t1 = threading. Thread (Target=test1, args= ())
t2 = Threading. Thread (Target=test2, args= ())
T1.start ()
T2.start ()

time.sleep (12345)

And then run this program

$ python test.py

First Use "PSTREE-APL" to view the process structure

$ PSTREE-APL 26855
python,26855 test.py
  |-{python},26858
  |-{python},26859

Then use "PS-LF" to view the thread information

$ ps-lf 26855
UID        PID  PPID   LWP  C nlwp stime TTY      STAT time   CMD
jhadmin  26855 25902 26855  0    3 15:15 pts/5    sl+    0:00 python test.py
jhadmin 26855  25902 26858  0    3 15:15 pts/5    sl+    0:00 python test.py
jhadmin 26855 25902  26859  0    3 15:15    PTS/5 sl+< c24/>0:00 python test.py

Finally, you can use "Pstack" to view the details of the thread, as follows:

$ pstack 26855 thread 3 (thread 0x7f8a344f2700 (LWP 26858)): #0 0x00007f8a3b5387a3 in Select () from/lib64/libc.so.6 #1 0x00007f8a344f5070 in Time_sleep () from/usr/lib64/python2.7/lib-dynload/timemodule.so #2 0x00007f8a3c215af0 in PyEval _evalframeex () from/lib64/libpython2.7.so.1.0 #3 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.s o.1.0 #4 0x00007f8a3c1a188d in Function_call () from/lib64/libpython2.7.so.1.0 #5 0x00007f8a3c17c8e3 in PyObject_Call ( ) from/lib64/libpython2.7.so.1.0 #6 0x00007f8a3c2104fd in Pyeval_evalframeex () from/lib64/libpython2.7.so.1.0 #7 0x00 007F8A3C2154BD in Pyeval_evalframeex () from/lib64/libpython2.7.so.1.0 #8 0x00007f8a3c2154bd in PyEval_EvalFrameEx () fr om/lib64/libpython2.7.so.1.0 #9 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 #10 0x00007f 8a3c1a1798 in Function_call () from/lib64/libpython2.7.so.1.0 #11 0x00007f8a3c17c8e3 in Pyobject_call () FROM/LIB64/LIBP ython2.7.so.1.0 #12 0x00007F8a3c18b8d5 in Instancemethod_call () from/lib64/libpython2.7.so.1.0 #13 0x00007f8a3c17c8e3 in PyObject_Call () from/lib 64/libpython2.7.so.1.0 #14 0x00007f8a3c20e6f7 in Pyeval_callobjectwithkeywords () from/lib64/libpython2.7.so.1.0 #15 0x00007f8a3c2465c2 in T_bootstrap () from/lib64/libpython2.7.so.1.0 #16 0x00007f8a3bf1ce25 in Start_thread () from/lib64   /libpthread.so.0 #17 0x00007f8a3b54134d in Clone () from/lib64/libc.so.6 thread 2 (thread 0x7f8a33cf1700 (LWP 26859)): #0 0X00007F8A3B5387A3 in Select () from/lib64/libc.so.6 #1 0x00007f8a344f5070 in Time_sleep () from/usr/lib64/python2.7/ Lib-dynload/timemodule.so #2 0x00007f8a3c215af0 in Pyeval_evalframeex () from/lib64/libpython2.7.so.1.0 #3 0x00007f8a3 C217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 #4 0x00007f8a3c1a188d in Function_call () from/lib64/lib python2.7.so.1.0 #5 0x00007f8a 3c17c8e3 in Pyobject_call ()from/lib64/libpython2.7.so.1.0                                                                                      
#6 0x00007f8a3c2104fd in Pyeval_evalframeex () from/lib64/libpython2.7.so.1.0 #7 0x00007f8a3c2154bd in Pyeval_evalframeex () from/lib64/libpython2.7.so.1 .0 #8 0X00007F8A3C2154BD in Pyeval_                                                                                      
Evalframeex () from/lib64/libpython2.7.so.1.0 #9 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 #10 0x00007f8a3c1a1798 in Functi  On_call () from/lib64/libpython2.7.so.1.0 #11 0x00007f8a3c17c8e3 in Pyobject_call () from/lib64/libpython2.7.so.1.0 #12 0x00007f8a3c18b8d5 in Instancemethod_call () from/lib64/libpython2.7.so.1.0 #13 0x00007f8a3c17c8e3 in PyObject_Call () F Rom/lib64/libpython2.7.so.1.0 #14 0x00007f8a3c20e6f7 in Pyeval_callobjectwithkeywords () from/lib64/libpython2.7.so.1.0 #15 0x00007f8a3c2465c2 In T_bootstrap () from/lib64/libpython2.7.so.1.0 #16 0x00007f8a3bf1ce25 in Start_thread () from/lib64/libpthread.so.0 #1 7 0x00007f8a3b54134d in Clone () from/lib64/libc.so.6 thread 1 (thread 0x7f8a3c6f3740 (LWP 26855)): #0 0x00007f8a3bf22a0 B in DO_FUTEX_WAIT.CONSTPROP.1 () from/lib64/libpthread.so.0 #1 0x00007f8a3bf22a9f in __new_sem_wait_slow.constprop.0 ( ) from/lib64/libpthread.so.0 #2 0x00007f8a3bf22b3b in sem_wait@ @GLIBC_2.2.5 () from/lib64/libpthread.so.0 #3 0x00007f8 a3c242535 in Pythread_acquire_lock () from/lib64/libpython2.7.so.1.0 #4 0x00007f8a3c2461c2 in Lock_PyThread_acquire_   Lock () from/lib64/libpython2.7.so.1.0 #5 0x00007f8a3c215af0 in Pyeval_evalframeex () from/lib64/libpython2.7.so.1.0 #6 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 #7 0x00007f8a3c21533c in PyEval_EvalFrameEx () from/lib64/libpython2.7.so.1.0 #8 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 #9 0x00007f8a3c21533c in PyEval_EvalFr Ameex () from/lib64/libpython2.7.so.1.0 #10 0x00007f8a3c217e3d in Pyeval_evalcodeex () from/lib64/libpython2.7.so.1.0 # One 0x00007f8a3c1a1798 in Function_call () from/lib64/libpython2.7.so.1.0 #12 0x00007f8a3c17c8e3 into PyObject_Call () from /lib64/libpython2.7.so.1.0 #13 0x00007f8a3c18b8d5 in Instancemethod_call () from/lib64/libpython2.7.so.1.0 #14 0x00007f8a3c17c8e3 in Pyobject_call () from/lib64/libpython2.7.so.1.0 #15 0x00007f8a3c17c9c5 in Call_function_tail () fr om/lib64/libpython2.7.so.1.0 #16 0X00007F8A3C17CCFB in Pyobject_callmethod () from/lib64/libpython2.7.so.1.0 #17 0x00007f8a3c232f29 in Py_finalize () from/lib64/libpython2.7.so.1.0 #18 0x00007f8a3c244325 in Py_Main () FROM/LIB64/LIBP ython2.7.so.1.0 #19 0x00007f8a3b46ac05 in __libc_start_main () from/lib64/libc.so.6 #20 0x000000000040071e in _start ()

Here is a word, if you want to see the Java program stack information, you can use the "kill-3" to view, such as:

$ nohub java Test > Test.out &

$ kill-3 <pid>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.