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
[root@gateway Python-3.6.8]# cat ~/.gdbinit /root/test/Python-3.6.8 [root@gateway Python-3.6.8]# gdb 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". (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.8]# pwd /root/test/Python-3.6.8 [root@gateway Python-3.6.8]# cd .. [root@gateway test]# ll -as total 22492 4 drwxr-xr-x 3 root root 4096 Oct 30 20:28 . 4 dr-xr-x---. 7 root root 4096 Oct 30 20:36 .. 4 -rw-r--r-- 1 root root 176 Oct 30 20:28 gdb_time_random_once.py 4 -rw-r--r-- 1 root root 165 Oct 30 20:16 gdb_time_random.py 4 drwxr-xr-x 18 501 test 4096 Oct 30 20:02 Python-3.6.8 22472 -rw-r--r-- 1 root root 23010188 Oct 30 17:10 Python-3.6.8.tgz [root@gateway test]# gdb -ex r --args python3 gdb_time_random_once.py 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". 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] 1 1572439245.440082 1 0.8718098185593804 Program exited normally. 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) python 20371 (gdb) q [root@gateway test]# gdb attach 20371 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 20371 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 0x00007fafa3e837b0 in __write_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 0x00007fafa3e837b0 in __write_nocancel () from /lib64/libpthread.so.0 #1 0x000000000043124d in _Py_write_impl (fd=1, buf=0x1c93090, count=18, gil_held=1) at Python/fileutils.c:1331 #2 0x000000000058d0b6 in _io_FileIO_write_impl (self=0x7fafa41d7288, arg=<value optimized out>) at ./Modules/_io/fileio.c:857 #3 _io_FileIO_write (self=0x7fafa41d7288, arg=<value optimized out>) at ./Modules/_io/clinic/fileio.c.h:251 #4 0x00000000004986d1 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeffa68, args=0x7ffef93ebee0, nargs=1, kwargs=0x0) at Objects/methodobject.c:209 #5 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeffa68, args=0x7ffef93ebee0, nargs=1, kwargs=0x0) at Objects/abstract.c:2313 #6 0x00000000004493ea in PyObject_CallMethodObjArgs (callable=0x7faf9aeffa68, name=<value optimized out>) at Objects/abstract.c:2759 #7 0x0000000000590930 in _bufferedwriter_raw_write (self=0x7fafa429ab48, start=<value optimized out>, len=18) at ./Modules/_io/bufferedio.c:1853 #8 0x0000000000590cac in _bufferedwriter_flush_unlocked (self=0x7fafa429ab48) at ./Modules/_io/bufferedio.c:1898 #9 0x0000000000592c09 in buffered_flush_and_rewind_unlocked (self=0x7fafa429ab48, args=<value optimized out>) at ./Modules/_io/bufferedio.c:827 #10 buffered_flush (self=0x7fafa429ab48, args=<value optimized out>) at ./Modules/_io/bufferedio.c:854 #11 0x00000000004986a3 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeff9d8, args=0x7ffef93ec1b0, nargs=0, kwargs=0x0) at Objects/methodobject.c:192 #12 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeff9d8, args=0x7ffef93ec1b0, nargs=0, kwargs=0x0) at Objects/abstract.c:2313 #13 0x00000000004493ea in PyObject_CallMethodObjArgs (callable=0x7faf9aeff9d8, name=<value optimized out>) at Objects/abstract.c:2759 #14 0x0000000000596fa5 in _io_TextIOWrapper_write_impl (self=0x7fafa4247630, arg=<value optimized out>) at ./Modules/_io/textio.c:1367 #15 _io_TextIOWrapper_write (self=0x7fafa4247630, arg=<value optimized out>) at ./Modules/_io/clinic/textio.c.h:216 #16 0x00000000004986d1 in _PyCFunction_FastCallDict (func_obj=0x7faf9aeffa20, args=0x7ffef93ec3e8, nargs=1, kwargs=0x0) at Objects/methodobject.c:209 #17 0x0000000000446825 in _PyObject_FastCallDict (func=0x7faf9aeffa20, args=0x7ffef93ec3e8, nargs=1, kwargs=0x0) at Objects/abstract.c:2313 #18 0x000000000046dd9f in PyFile_WriteObject (v=0x7fafa41faa40, f=<value optimized out>, flags=<value optimized out>) at Objects/fileobject.c:150 #19 0x000000000046df10 in PyFile_WriteString (s=<value optimized out>, f=0x7fafa4247630) at Objects/fileobject.c:174 #20 0x00000000005198fb in builtin_print (self=<value optimized out>, args=0x7fafa4241d30, kwds=<value optimized out>) at Python/bltinmodule.c:1814 #21 0x00000000004985ca in _PyCFunction_FastCallDict (func_obj=0x7fafa4297f30, args=0x1ca3318, nargs=<value optimized out>, kwargs=0x0) at Objects/methodobject.c:231 #22 0x000000000051edc9 in call_function (pp_stack=0x7ffef93ec6a8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:4851 #23 0x0000000000520936 in _PyEval_EvalFrameDefault (f=<value optimized out>, throwflag=<value optimized out>) at Python/ceval.c:3335 #24 0x000000000051c67d in PyEval_EvalFrameEx (co=<value optimized out>, args=<value optimized out>, nargs=0, globals=<value optimized out>) at Python/ceval.c:754 #25 _PyFunction_FastCall (co=<value optimized out>, args=<value optimized out>, nargs=0, globals=<value optimized out>) at Python/ceval.c:4933 #26 0x000000000051eec5 in fast_function (pp_stack=0x7ffef93ec8c8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:4968 #27 call_function (pp_stack=0x7ffef93ec8c8, oparg=<value optimized out>, kwnames=0x0) at Python/ceval.c:4872 #28 0x0000000000520936 in _PyEval_EvalFrameDefault (f=<value optimized out>, throwflag=<value optimized out>) at Python/ceval.c:3335 #29 0x000000000051d236 in PyEval_EvalFrameEx (_co=0x7fafa4191660, 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 #30 _PyEval_EvalCodeWithName (_co=0x7fafa4191660, 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 ---Type <return> to continue, or q <return> to quit--- #31 0x000000000051e750 in PyEval_EvalCodeEx (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:4187 #32 PyEval_EvalCode (co=<value optimized out>, globals=<value optimized out>, locals=<value optimized out>) at Python/ceval.c:731 #33 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:1025 #34 0x0000000000424f55 in PyRun_FileExFlags (fp=0x1c6cc50, filename_str=<value optimized out>, start=<value optimized out>, globals=0x7fafa4217120, locals=0x7fafa4217120, closeit=1, flags=0x7ffef93ecc00) at Python/pythonrun.c:978 #35 0x0000000000425147 in PyRun_SimpleFileExFlags (fp=0x1c6cc50, filename=<value optimized out>, closeit=1, flags=0x7ffef93ecc00) at Python/pythonrun.c:419 #36 0x00000000004362a3 in run_file (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:340 #37 Py_Main (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:810 #38 0x000000000041ce0d in main (argc=2, argv=<value optimized out>) at ./Programs/python.c:69
[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.): 172407500000 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.6 [.] _PyObject_Alloc ▒ _PyObject_Alloc ▒ + 0.32% 0.31% gdb_time_random python3.6 [.] _PyEval_EvalFrameDefault ▒ + 0.31% 0.13% gdb_time_random libpthread-2.12.so [.] llseek ▒ + 0.30% 0.29% gdb_time_random python3.6 [.] PyObject_GenericGetAttr ▒ + 0.28% 0.28% gdb_time_random [kernel.kallsyms] [k] system_call_after_swapgs ▒ + 0.22% 0.21% gdb_time_random python3.6 [.] _PyType_Lookup ▒ + 0.17% 0.06% gdb_time_random libc-2.12.so [.] __fxstat64 ▒ + 0.16% 0.15% gdb_time_random python3.6 [.] 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.6 [.] convertsimple ▒ + 0.12% 0.12% gdb_time_random [kernel.kallsyms] [k] __ext4_get_inode_loc ▒ + 0.12% 0.12% gdb_time_random python3.6 [.] _PyCFunction_FastCallDict ▒ + 0.12% 0.11% gdb_time_random libc-2.12.so [.] __strlen_sse42 ▒ + 0.12% 0.11% gdb_time_random python3.6 [.] _PyObject_Free ▒ + 0.11% 0.11% gdb_time_random python3.6 [.] vgetargskeywordsfast_impl ▒ + 0.10% 0.10% gdb_time_random [kernel.kallsyms] [k] blk_queue_bio ▒ + 0.09% 0.09% gdb_time_random python3.6 [.] 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.6 [.] call_function ▒ + 0.08% 0.08% gdb_time_random python3.6 [.] PyDict_GetItem ▒ + 0.08% 0.08% gdb_time_random [kernel.kallsyms] [k] memset ▒ + 0.08% 0.08% gdb_time_random python3.6 [.] PyObject_GetAttr ▒ + 0.08% 0.08% gdb_time_random libc-2.12.so [.] memcpy ▒ + 0.08% 0.08% gdb_time_random python3.6 [.] _PyObject_FastCallDict ▒ + 0.08% 0.07% gdb_time_random python3.6 [.] _PyObject_GetAttrId ▒ + 0.08% 0.07% gdb_time_random python3.6 [.] _PyUnicode_FromId ▒ + 0.07% 0.07% gdb_time_random python3.6 [.] vgetargs1 ▒ + 0.07% 0.07% gdb_time_random python3.6 [.] objargs_mkstack ▒ + 0.07% 0.07% gdb_time_random python3.6 [.] meth_dealloc ▒ - 0.07% 0.07% gdb_time_random python3.6 [.] _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)