https://devguide.python.org/gdb/

https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html#Python

https://python-book.readthedocs.io/zh_CN/latest/practices/debug-python-with-gdb.html

Makefile

# Rules
# Default target
all:        build_all
build_all:  check-clean-src $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks \
        Programs/_testembed python-config

# Copy up the gdb python hooks into a position where they can be automatically
# loaded by gdb during Lib/test/test_gdb.py
#
# Distributors are likely to want to install this somewhere else e.g. relative
# to the stripped DWARF data for the shared library.
gdbhooks: $(BUILDPYTHON)-gdb.py
 
[root@gateway Python-3.6.]# cat ~/.gdbinit
/root/test/Python-3.6.
[root@gateway Python-3.6.]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-.el6)
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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/>.
/root/.gdbinit:: Error in sourced command file:
Undefined command: "". Try "help".
(gdb) p globals
No symbol table is loaded. Use the "file" command.
(gdb) py-list
Undefined command: "py-list". Try "help".
(gdb) q
[root@gateway Python-3.6.]# pwd
/root/test/Python-3.6.
[root@gateway Python-3.6.]# cd ..
[root@gateway test]# ll -as
total
drwxr-xr-x root root Oct : .
dr-xr-x---. root root Oct : ..
-rw-r--r-- root root Oct : gdb_time_random_once.py
-rw-r--r-- root root Oct : gdb_time_random.py
drwxr-xr-x test Oct : Python-3.6.
-rw-r--r-- root root Oct : Python-3.6..tgz
[root@gateway test]# gdb -ex r --args python3 gdb_time_random_once.py
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-.el6)
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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/>...
/root/.gdbinit:: Error in sourced command file:
Undefined command: "". Try "help".
Reading symbols from /usr/local/bin/python3...done.
Starting program: /usr/local/bin/python3 gdb_time_random_once.py
[Thread debugging using libthread_db enabled] 1572439245.440082 0.8718098185593804 Program exited normally.
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.212.el6_10..x86_64 keyutils-libs-1.4-.el6.x86_64 krb5-libs-1.10.-.el6.x86_64 libcom_err-1.41.-.el6.x86_64 libselinux-2.0.-.el6.x86_64 openssl-1.0.1e-.el6_10.x86_64 zlib-1.2.-.el6.x86_64
(gdb) python
(gdb) q
[root@gateway test]# gdb attach
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-.el6)
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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/>...
/root/.gdbinit:: Error in sourced command file:
Undefined command: "". Try "help".
attach: No such file or directory.
Attaching to process
Reading symbols from /usr/local/bin/python3....done.
Reading symbols from /lib64/libpthread.so....(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Loaded symbols for /lib64/libpthread.so.
Reading symbols from /lib64/libdl.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.
Reading symbols from /lib64/libutil.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libutil.so.
Reading symbols from /lib64/librt.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.
Reading symbols from /lib64/libm.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.
Reading symbols from /lib64/libc.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.
Reading symbols from /lib64/ld-linux-x86-.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-.so.
Reading symbols from /usr/local/lib/python3./lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3./lib-dynload/math.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/math.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3./lib-dynload/_hashlib.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_hashlib.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/lib64/libssl.so....(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libssl.so.
Reading symbols from /usr/lib64/libcrypto.so....(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libcrypto.so.
Reading symbols from /lib64/libgssapi_krb5.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libgssapi_krb5.so.
Reading symbols from /lib64/libkrb5.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libkrb5.so.
Reading symbols from /lib64/libcom_err.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libcom_err.so.
Reading symbols from /lib64/libk5crypto.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libk5crypto.so.
Reading symbols from /lib64/libz.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libz.so.
Reading symbols from /lib64/libkrb5support.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libkrb5support.so.
Reading symbols from /lib64/libkeyutils.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libkeyutils.so.
Reading symbols from /lib64/libresolv.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libresolv.so.
Reading symbols from /lib64/libselinux.so....(no debugging symbols found)...done.
Loaded symbols for /lib64/libselinux.so.
Reading symbols from /usr/local/lib/python3./lib-dynload/_blake2.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_blake2.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3./lib-dynload/_sha3.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_sha3.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3./lib-dynload/_bisect.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_bisect.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3./lib-dynload/_random.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3./lib-dynload/_random.cpython-36m-x86_64-linux-gnu.so
0x00007fafa3e837b0 in __write_nocancel () from /lib64/libpthread.so.
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.212.el6_10..x86_64 keyutils-libs-1.4-.el6.x86_64 krb5-libs-1.10.-.el6.x86_64 libcom_err-1.41.-.el6.x86_64 libselinux-2.0.-.el6.x86_64 openssl-1.0.1e-.el6_10.x86_64 zlib-1.2.-.el6.x86_64
(gdb) bt
# 0x00007fafa3e837b0 in __write_nocancel () from /lib64/libpthread.so.
# 0x000000000043124d in _Py_write_impl (fd=, buf=0x1c93090, count=, gil_held=) at Python/fileutils.c:
# 0x000000000058d0b6 in _io_FileIO_write_impl (self=0x7fafa41d7288, arg=<value optimized out>) at ./Modules/_io/fileio.c:
# _io_FileIO_write (self=0x7fafa41d7288, arg=<value optimized out>) at ./Modules/_io/clinic/fileio.c.h:
# 0x00000000004986d1 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeffa68, args=0x7ffef93ebee0, nargs=, kwargs=0x0) at Objects/methodobject.c:
# 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeffa68, args=0x7ffef93ebee0, nargs=, kwargs=0x0) at Objects/abstract.c:
# 0x00000000004493ea in PyObject_CallMethodObjArgs (callable=0x7faf9aeffa68, name=<value optimized out>) at Objects/abstract.c:
# 0x0000000000590930 in _bufferedwriter_raw_write (self=0x7fafa429ab48, start=<value optimized out>, len=) at ./Modules/_io/bufferedio.c:
# 0x0000000000590cac in _bufferedwriter_flush_unlocked (self=0x7fafa429ab48) at ./Modules/_io/bufferedio.c:
# 0x0000000000592c09 in buffered_flush_and_rewind_unlocked (self=0x7fafa429ab48, args=<value optimized out>) at ./Modules/_io/bufferedio.c:
# buffered_flush (self=0x7fafa429ab48, args=<value optimized out>) at ./Modules/_io/bufferedio.c:
# 0x00000000004986a3 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeff9d8, args=0x7ffef93ec1b0, nargs=, kwargs=0x0) at Objects/methodobject.c:
# 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeff9d8, args=0x7ffef93ec1b0, nargs=, kwargs=0x0) at Objects/abstract.c:
# 0x00000000004493ea in PyObject_CallMethodObjArgs (callable=0x7faf9aeff9d8, name=<value optimized out>) at Objects/abstract.c:
# 0x0000000000596fa5 in _io_TextIOWrapper_write_impl (self=0x7fafa4247630, arg=<value optimized out>) at ./Modules/_io/textio.c:
# _io_TextIOWrapper_write (self=0x7fafa4247630, arg=<value optimized out>) at ./Modules/_io/clinic/textio.c.h:
# 0x00000000004986d1 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeffa20, args=0x7ffef93ec3e8, nargs=, kwargs=0x0) at Objects/methodobject.c:
# 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeffa20, args=0x7ffef93ec3e8, nargs=, kwargs=0x0) at Objects/abstract.c:
# 0x000000000046dd9f in PyFile_WriteObject (v=0x7fafa41faa40, f=<value optimized out>, flags=<value optimized out>) at Objects/fileobject.c:
# 0x000000000046df10 in PyFile_WriteString (s=<value optimized out>, f=0x7fafa4247630) at Objects/fileobject.c:
# 0x00000000005198fb in builtin_print (self=<value optimized out>, args=0x7fafa4241d30, kwds=<value optimized out>) at Python/bltinmodule.c:
# 0x00000000004985ca in _PyCFunction_FastCallDict (func_obj=0x7fafa4297f30, args=0x1ca3318, nargs=<value optimized out>, kwargs=0x0) at Objects/methodobject.c:
# 0x000000000051edc9 in call_function (pp_stack=0x7ffef93ec6a8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:
# 0x0000000000520936 in _PyEval_EvalFrameDefault (f=<value optimized out>, throwflag=<value optimized out>) at Python/ceval.c:
# 0x000000000051c67d in PyEval_EvalFrameEx (co=<value optimized out>, args=<value optimized out>, nargs=, globals=<value optimized out>) at Python/ceval.c:
# _PyFunction_FastCall (co=<value optimized out>, args=<value optimized out>, nargs=, globals=<value optimized out>) at Python/ceval.c:
# 0x000000000051eec5 in fast_function (pp_stack=0x7ffef93ec8c8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:
# call_function (pp_stack=0x7ffef93ec8c8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:
# 0x0000000000520936 in _PyEval_EvalFrameDefault (f=<value optimized out>, throwflag=<value optimized out>) at Python/ceval.c:
# 0x000000000051d236 in PyEval_EvalFrameEx (_co=0x7fafa4191660, globals=<value optimized out>, locals=<value optimized out>, args=<value optimized out>, argcount=,
kwnames=0x0, kwargs=0x0, kwcount=, kwstep=, defs=0x0, defcount=, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:
# _PyEval_EvalCodeWithName (_co=0x7fafa4191660, globals=<value optimized out>, locals=<value optimized out>, args=<value optimized out>, argcount=, kwnames=0x0, kwargs=0x0,
kwcount=, kwstep=, defs=0x0, defcount=, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:
---Type <return> to continue, or q <return> to quit---
# 0x000000000051e750 in PyEval_EvalCodeEx (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:
# PyEval_EvalCode (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:
# 0x0000000000423541 in run_mod (mod=<value optimized out>, filename=<value optimized out>, globals=0x7fafa4217120, locals=0x7fafa4217120, flags=<value optimized out>,
arena=<value optimized out>) at Python/pythonrun.c:
# 0x0000000000424f55 in PyRun_FileExFlags (fp=0x1c6cc50, filename_str=<value optimized out>, start=<value optimized out>, globals=0x7fafa4217120, locals=0x7fafa4217120,
closeit=, flags=0x7ffef93ecc00) at Python/pythonrun.c:
# 0x0000000000425147 in PyRun_SimpleFileExFlags (fp=0x1c6cc50, filename=<value optimized out>, closeit=, flags=0x7ffef93ecc00) at Python/pythonrun.c:
# 0x00000000004362a3 in run_file (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:
# Py_Main (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:
# 0x000000000041ce0d in main (argc=, argv=<value optimized out>) at ./Programs/python.c:
 
[root@gateway ~]# ps -aux | grep pyth
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 1643 0.0 0.5 177236 5516 ? S Sep19 0:26 /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf
root 20645 0.5 0.7 159204 7816 pts/2 S+ 22:37 0:08 /usr/local/bin/python3 ./gdb_time_random
root 20691 15.8 0.7 159204 7828 pts/0 R+ 22:51 1:47 /usr/local/bin/python3 ./gdb_time_random_wf
root 20736 0.0 0.0 103320 872 pts/1 R+ 23:03 0:00 grep pyth
[root@gateway ~]# gdb attach 20691
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.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/>...
/root/.gdbinit:1: Error in sourced command file:
Undefined command: "". Try "help".
attach: No such file or directory.
Attaching to process 20691
Reading symbols from /usr/local/bin/python3.6...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libutil.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libutil.so.1
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.1
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3.6/lib-dynload/math.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/math.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_hashlib.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_hashlib.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/lib64/libssl.so.10...(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libssl.so.10
Reading symbols from /usr/lib64/libcrypto.so.10...(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/libcrypto.so.10
Reading symbols from /lib64/libgssapi_krb5.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgssapi_krb5.so.2
Reading symbols from /lib64/libkrb5.so.3...(no debugging symbols found)...done.
Loaded symbols for /lib64/libkrb5.so.3
Reading symbols from /lib64/libcom_err.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libcom_err.so.2
Reading symbols from /lib64/libk5crypto.so.3...(no debugging symbols found)...done.
Loaded symbols for /lib64/libk5crypto.so.3
Reading symbols from /lib64/libz.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libz.so.1
Reading symbols from /lib64/libkrb5support.so.0...(no debugging symbols found)...done.
Loaded symbols for /lib64/libkrb5support.so.0
Reading symbols from /lib64/libkeyutils.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libkeyutils.so.1
Reading symbols from /lib64/libresolv.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libresolv.so.2
Reading symbols from /lib64/libselinux.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libselinux.so.1
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_blake2.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_blake2.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_sha3.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_sha3.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_bisect.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_bisect.cpython-36m-x86_64-linux-gnu.so
Reading symbols from /usr/local/lib/python3.6/lib-dynload/_random.cpython-36m-x86_64-linux-gnu.so...done.
Loaded symbols for /usr/local/lib/python3.6/lib-dynload/_random.cpython-36m-x86_64-linux-gnu.so
0x00007facf45ef050 in __open_nocancel () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.212.el6_10.3.x86_64 keyutils-libs-1.4-5.el6.x86_64 krb5-libs-1.10.3-65.el6.x86_64 libcom_err-1.41.12-24.el6.x86_64 libselinux-2.0.94-7.el6.x86_64 openssl-1.0.1e-58.el6_10.x86_64 zlib-1.2.3-29.el6.x86_64
(gdb) bt
#0 0x00007facf45ef050 in __open_nocancel () from /lib64/libpthread.so.0
#1 0x000000000058d90a in _io_FileIO___init___impl (self=0x7faceb66aaf8, args=<value optimized out>, kwargs=<value optimized out>) at ./Modules/_io/fileio.c:388
#2 _io_FileIO___init__ (self=0x7faceb66aaf8, args=<value optimized out>, kwargs=<value optimized out>) at ./Modules/_io/clinic/fileio.c.h:63
#3 0x00000000004ad06f in type_call (type=<value optimized out>, args=0x7facf48b4958, kwds=0x0) at Objects/typeobject.c:915
#4 0x00000000004469b2 in PyObject_Call (callable=0x88c060, args=0x7facf48b4958) at Objects/abstract.c:2261
#5 call_function_tail (callable=0x88c060, args=0x7facf48b4958) at Objects/abstract.c:2512
#6 0x0000000000446c46 in _PyObject_CallFunction_SizeT (callable=0x88c060, format=<value optimized out>) at Objects/abstract.c:2565
#7 0x0000000000589b4b in _io_open_impl (module=<value optimized out>, args=<value optimized out>, nargs=<value optimized out>, kwnames=<value optimized out>) at ./Modules/_io/_iomodule.c:375
#8 _io_open (module=<value optimized out>, args=<value optimized out>, nargs=<value optimized out>, kwnames=<value optimized out>) at ./Modules/_io/clinic/_iomodule.c.h:156
#9 0x0000000000498633 in _PyCFunction_FastCallDict (func_obj=0x7facf49ca120, args=0xed4148, nargs=2, kwargs=0x7faceb66aca8) at Objects/methodobject.c:250
#10 0x000000000049881f in _PyCFunction_FastCallKeywords (func=0x7facf49ca120, stack=0xed4148, nargs=<value optimized out>, kwnames=<value optimized out>) at Objects/methodobject.c:294
#11 0x000000000051edc9 in call_function (pp_stack=0x7ffe3d22caa8, oparg=<value optimized out>, kwnames=0x7facf4931400) at Python/ceval.c:4851
#12 0x0000000000520d67 in _PyEval_EvalFrameDefault (f=<value optimized out>, throwflag=<value optimized out>) at Python/ceval.c:3351
#13 0x000000000051d236 in PyEval_EvalFrameEx (_co=0x7facf48fc660, globals=<value optimized out>, locals=<value optimized out>, args=<value optimized out>, argcount=0, kwnames=0x0, kwargs=0x0, kwcount=0,
kwstep=2, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:754
#14 _PyEval_EvalCodeWithName (_co=0x7facf48fc660, globals=<value optimized out>, locals=<value optimized out>, args=<value optimized out>, argcount=0, kwnames=0x0, kwargs=0x0, kwcount=0, kwstep=2, defs=0x0,
defcount=0, kwdefs=0x0, closure=0x0, name=0x0, qualname=0x0) at Python/ceval.c:4166
#15 0x000000000051e750 in PyEval_EvalCodeEx (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:4187
#16 PyEval_EvalCode (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:731
#17 0x0000000000423541 in run_mod (mod=<value optimized out>, filename=<value optimized out>, globals=0x7facf4982120, locals=0x7facf4982120, flags=<value optimized out>, arena=<value optimized out>)
at Python/pythonrun.c:1025
#18 0x0000000000424f55 in PyRun_FileExFlags (fp=0xeb6c90, filename_str=<value optimized out>, start=<value optimized out>, globals=0x7facf4982120, locals=0x7facf4982120, closeit=1, flags=0x7ffe3d22cde0)
at Python/pythonrun.c:978
#19 0x0000000000425147 in PyRun_SimpleFileExFlags (fp=0xeb6c90, filename=<value optimized out>, closeit=1, flags=0x7ffe3d22cde0) at Python/pythonrun.c:419
#20 0x00000000004362a3 in run_file (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:340
#21 Py_Main (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:810
#22 0x000000000041ce0d in main (argc=2, argv=<value optimized out>) at ./Programs/python.c:69
(gdb) backtrace 1
#0 0x00007facf45ef050 in __open_nocancel () from /lib64/libpthread.so.0
(More stack frames follow...)
(gdb) backtrace -1
#22 0x000000000041ce0d in main (argc=2, argv=<value optimized out>) at ./Programs/python.c:69
(gdb) frame 1
#1 0x000000000058d90a in _io_FileIO___init___impl (self=0x7faceb66aaf8, args=<value optimized out>, kwargs=<value optimized out>) at ./Modules/_io/fileio.c:388
388 self->fd = open(name, flags, 0666);
(gdb) frame -1
#0 0x0000000000000000 in ?? ()
(gdb)

  

栈顶 backtrace 1

栈底 backtrace -1

[root@gateway test]# cat gdb_time_random_wf
#!/usr/local/bin/python3 import time, random def ftime():
print(1)
print(time.time()) def frandom():
print(1)
print(random.random()) while True:
ftime()
frandom()
with open('f', 'w', encoding = 'utf-8') as fw:
fw.write('{}'.format(frandom()))
[root@gateway test]# ps -aux | grep pyth
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 1643 0.0 0.5 177236 5516 ? S Sep19 0:26 /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf
root 20691 6.4 0.7 159204 7828 pts/0 T+ 22:51 1:48 /usr/local/bin/python3 ./gdb_time_random_wf
root 20780 0.0 0.0 103320 872 pts/2 R+ 23:19 0:00 grep pyth
[root@gateway test]#

  

结合perf 查看 不符合的时间消耗

perf record -a -g -e cycles -e cs

perf report --pid 20691

Samples: 689K of event 'cpu-clock', Event count (approx.):
Children Self Command Shared Object Symbol ◆
+ 4.04% 0.06% gdb_time_random libpthread-2.12.so [.] __open_nocancel ▒
+ 1.98% 0.19% gdb_time_random libpthread-2.12.so [.] __write_nocancel ▒
+ 1.63% 0.06% gdb_time_random libpthread-2.12.so [.] __libc_close ▒
+ 1.23% 1.23% gdb_time_random [kernel.kallsyms] [k] generic_unplug_device ▒
+ 0.59% 0.59% gdb_time_random [kernel.kallsyms] [k] _spin_unlock_irqrestore ▒
- 0.32% 0.30% gdb_time_random python3. [.] _PyObject_Alloc ▒
_PyObject_Alloc ▒
+ 0.32% 0.31% gdb_time_random python3. [.] _PyEval_EvalFrameDefault ▒
+ 0.31% 0.13% gdb_time_random libpthread-2.12.so [.] llseek ▒
+ 0.30% 0.29% gdb_time_random python3. [.] PyObject_GenericGetAttr ▒
+ 0.28% 0.28% gdb_time_random [kernel.kallsyms] [k] system_call_after_swapgs ▒
+ 0.22% 0.21% gdb_time_random python3. [.] _PyType_Lookup ▒
+ 0.17% 0.06% gdb_time_random libc-2.12.so [.] __fxstat64 ▒
+ 0.16% 0.15% gdb_time_random python3. [.] lookdict_unicode_nodummy ▒
+ 0.15% 0.15% gdb_time_random [kernel.kallsyms] [k] ext4_mark_iloc_dirty ▒
+ 0.15% 0.15% gdb_time_random [kernel.kallsyms] [k] kmem_cache_alloc ▒
+ 0.15% 0.15% gdb_time_random [kernel.kallsyms] [k] finish_task_switch ▒
+ 0.15% 0.06% gdb_time_random libc-2.12.so [.] __tcgetattr ▒
+ 0.13% 0.12% gdb_time_random python3. [.] convertsimple ▒
+ 0.12% 0.12% gdb_time_random [kernel.kallsyms] [k] __ext4_get_inode_loc ▒
+ 0.12% 0.12% gdb_time_random python3. [.] _PyCFunction_FastCallDict ▒
+ 0.12% 0.11% gdb_time_random libc-2.12.so [.] __strlen_sse42 ▒
+ 0.12% 0.11% gdb_time_random python3. [.] _PyObject_Free ▒
+ 0.11% 0.11% gdb_time_random python3. [.] vgetargskeywordsfast_impl ▒
+ 0.10% 0.10% gdb_time_random [kernel.kallsyms] [k] blk_queue_bio ▒
+ 0.09% 0.09% gdb_time_random python3. [.] PyUnicode_FromFormatV ▒
+ 0.09% 0.09% gdb_time_random [kernel.kallsyms] [k] jbd2_journal_add_journal_head ▒
+ 0.09% 0.08% gdb_time_random python3. [.] call_function ▒
+ 0.08% 0.08% gdb_time_random python3. [.] PyDict_GetItem ▒
+ 0.08% 0.08% gdb_time_random [kernel.kallsyms] [k] memset ▒
+ 0.08% 0.08% gdb_time_random python3. [.] PyObject_GetAttr ▒
+ 0.08% 0.08% gdb_time_random libc-2.12.so [.] memcpy ▒
+ 0.08% 0.08% gdb_time_random python3. [.] _PyObject_FastCallDict ▒
+ 0.08% 0.07% gdb_time_random python3. [.] _PyObject_GetAttrId ▒
+ 0.08% 0.07% gdb_time_random python3. [.] _PyUnicode_FromId ▒
+ 0.07% 0.07% gdb_time_random python3. [.] vgetargs1 ▒
+ 0.07% 0.07% gdb_time_random python3. [.] objargs_mkstack ▒
+ 0.07% 0.07% gdb_time_random python3. [.] meth_dealloc ▒
- 0.07% 0.07% gdb_time_random python3. [.] _io_FileIO___init__ ▒
_io_FileIO___init__

Debugging with GDB: Top https://sourceware.org/gdb/current/onlinedocs/gdb/

使用 GDB 调试 Linux 软件 https://www.ibm.com/developerworks/cn/linux/sdk/gdb/index.html

8 Examining the Stack

When your program has stopped, the first thing you need to know is where it stopped and how it got there.

Each time your program performs a function call, information about the call is generated. That information includes the location of the call in your program, the arguments of the call, and the local variables of the function being called. The information is saved in a block of data called a stack frame. The stack frames are allocated in a region of memory called the call stack.

When your program stops, the GDB commands for examining the stack allow you to see all of this information.

One of the stack frames is selected by GDB and many GDB commands refer implicitly to the selected frame. In particular, whenever you ask GDB for the value of a variable in your program, the value is found in the selected frame. There are special GDB commands to select whichever frame you are interested in. See Selecting a Frame.

When your program stops, GDB automatically selects the currently executing frame and describes it briefly, similar to the frame command (see Information about a Frame).

• Frames:    Stack frames
• Backtrace:    Backtraces
• Selection:    Selecting a frame
• Frame Info:    Information on a frame
• Frame Apply:    Applying a command to several frames
• Frame Filter Management:    Managing frame filters

Debugging with GDB: Frame Info https://sourceware.org/gdb/current/onlinedocs/gdb/Frame-Info.html#Frame-Info

(gdb) info frame 1
Stack frame at 0x7ffe3d22c5f0:
rip = 0x58d90a in _io_FileIO___init___impl (./Modules/_io/fileio.c:388); saved rip 0x4ad06f
inlined into frame 2, caller of frame at 0x7ffe3d22c4d0
source language c.
Arglist at unknown address.
Locals at unknown address, Previous frame's sp is 0x7ffe3d22c4d0
Saved registers:
rip at 0x7ffe3d22c4c8
(gdb) info frame -1
Stack frame at 0xffffffffffffffff:
rip = 0x0; saved rip 0x58d90a
called by frame at 0x7ffe3d22c5f0
Arglist at 0x7ffe3d22c4c0, args:
Locals at 0x7ffe3d22c4c0, Previous frame's sp is 0x7ffe3d22c4d0
Saved registers:
rip at 0x7ffe3d22c4c8
(gdb)

  

 
 
 
 

8 Examining the Stack

When your program has stopped, the first thing you need to know is where it stopped and how it got there.

Each time your program performs a function call, information about the call is generated. That information includes the location of the call in your program, the arguments of the call, and the local variables of the function being called. The information is saved in a block of data called a stack frame. The stack frames are allocated in a region of memory called the call stack.

When your program stops, the GDB commands for examining the stack allow you to see all of this information.

One of the stack frames is selected by GDB and many GDB commands refer implicitly to the selected frame. In particular, whenever you ask GDB for the value of a variable in your program, the value is found in the selected frame. There are special GDB commands to select whichever frame you are interested in. See Selecting a Frame.

When your program stops, GDB automatically selects the currently executing frame and describes it briefly, similar to the frame command (see Information about a Frame).

• Frames:    Stack frames
• Backtrace:    Backtraces
• Selection:    Selecting a frame
• Frame Info:    Information on a frame
• Frame Apply:    Applying a command to several frames
• Frame Filter Management:    Managing frame filters

查看寄存器信息

(gdb) info registers
rax 0x4 4
rbx 0x7faceb66aaf8 140380660476664
rcx 0xffffffffffffffff -1
rdx 0x1b6 438
rsi 0x80241 524865
rdi 0x7facf488f910 140380813719824
rbp 0x7facf49ac030 0x7facf49ac030
rsp 0x7ffe3d22c4c8 0x7ffe3d22c4c8
r8 0x38 56
r9 0x0 0
r10 0x1 1
r11 0x246 582
r12 0xe7fc60 15203424
r13 0x7facf488f910 140380813719824
r14 0x80241 524865
r15 0x7facf4a136a8 140380815308456
rip 0x7facf45ef050 0x7facf45ef050 <__open_nocancel+7>
eflags 0x246 [ PF ZF IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb)

  

gdbhooks 栈信息的更多相关文章

  1. 在C/C++程序里打印调用栈信息

    我们知道,GDB的backtrace命令可以查看堆栈信息.但很多时候,GDB根本用不上.比如说,在线上环境中可能没有GDB,即使有,也不太可能让我们直接在上面调试.如果能让程序自己输出调用栈,那是最好 ...

  2. [改善Java代码]使用Throwable获得栈信息

    AOP(Aspect Oriented Programming面向切面编程)可以很轻松的控制一个方法调用哪些类,也能够控制哪些方法允许被调用,一般来说切面编程(比如AspectJ)只能控制到方法级别, ...

  3. Java程序栈信息文件中的秘密(五)

    最近发现在使用jstack工具在导出Java应用的线程栈时有一个小小的窍门,比如Linux环境上有一个用户为appuser,假如以这个用户启动了一个Java进程B,如果想要导出进程B的线程栈,则必须切 ...

  4. Linux驱动调试-根据oops的栈信息,确定函数调用过程

    上章链接入口: http://www.cnblogs.com/lifexy/p/8006748.html 在上章里,我们分析了oops的PC值在哪个函数出错的,那如何通过栈信息来查看出错函数的整个调用 ...

  5. Python 捕捉traceback异常栈信息

    捕捉traceback异常栈信息   by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...

  6. 【原】cpu消耗高,查看对应的线程栈信息

    在压测过程中,有时候cpu会飙升,造成这种现象的原因很多, 可能是gc造成的,也可能是某个方法造成的, 如果从找对应的方法入手,下面简单罗列下步骤: 1.top,获取pid 下面cpu消耗90%左右 ...

  7. 37.Linux驱动调试-根据oops的栈信息,确定函数调用过程

    上章链接入口: http://www.cnblogs.com/lifexy/p/8006748.html 在上章里,我们分析了oops的PC值在哪个函数出错的 本章便通过栈信息来分析函数调用过程 1. ...

  8. java Exception 出错的栈信息打印到日志中 打印堆栈信息

    我们在开发程序的过程当中,日志是必不可少的工具,这有助于我们分析问题的原因,和出错的详细信息,而java的异常机制又会方便且迅速的帮我们找到出错行的位置. try { .... } catch (Ex ...

  9. scala如何在任意方法中打印当前线程栈信息(StackTrace)

    1.以wordcount为例 package org.apache.spark.examples import org.apache.spark.{SparkConf, SparkContext} / ...

随机推荐

  1. Linux学习django-CentOS部署自己本地的django项目

    前言 自己本地写好的django项目,如何部署到linux服务器上,让其他的小伙伴也能访问呢?本篇以centos系统为例,把本地写好的django项目部署到linux服务器上环境准备: 环境准备:1. ...

  2. c# 字符串修改

  3. 如何使用Arduino和SIM900A GPRS / GSM模块将数据发送到Web服务器

    今天我们在这里介绍一个非常有趣的项目,我们将使用Arduino开发板和GPRS将数据发送到SparkFun服务器.这是一个基于IoT的项目,我们将使用GSM模块SIM900A将一些数据发送到互联网上的 ...

  4. 51nod 2485 小b重排字符串

    小b有一个字符串S,现在她希望重排列S,使得S中相邻字符不同. 请你判断小b是否可能成功. 样例解释:将"aab"重排为"aba"即可. 收起   输入 输入一 ...

  5. 30.第一个Linq 数据库查询

    使用Linq to Entity查询数据库 首先在项目中添加ADO.NET实体数据模型,如下 新建连接 勾选生成的表 点击完成即可看到两个建立的实体数据对象模型 模型生成好之后就可以直接使用了 Cus ...

  6. 简单理解和使用 C# 委托与事件

    委托和事件 委托:委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程 ...

  7. 解决jdbc向数据库存入数据出现乱码的情况

    解决办法 1.修改项目的编码,建议统一使用utf-8来实现,这样整个项目就是utf-8. 2.jdbc:mysql://locathost:3306/数据库名称?useUnicode=true& ...

  8. *JavaScript标准参考教程 - 阮一峰

    参考 页向对象编程的模式(继承&模块化)

  9. NET Framework 的泛型

    NET Framework 的泛型 泛型是具有占位符(类型参数)的类.结构.接口和方法,这些占位符是类.结构.接口和方法所存储或使用的一个或多个类型的占位符.泛型集合类可以将类型参数用作它所存储的对象 ...

  10. js判断日期格式(YYYYMM)

    function datepanduan(obj){ var date = document.getElementById(obj.id).value; var reg = /^\b[1-3]\d{3 ...