测试代码:
import time
from threading import Thread, current_thread
def func(msg):
while True:
print(f"{hex(current_thread().ident)}: {msg}")
time.sleep(1)
t1 = Thread(target=func, args=(1, ))
t2 = Thread(target=func, args=(2, ))
t1.start()
t2.start()
登录linux系统, python3.8 test.py启动进程, 会一直控制台打印
root>python3.8 test.py
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
1、使用 ps -ef 查到进程的id.
root>ps -ef | grep python
root 1272 1 0 Mar01 ? 00:57:12 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
root 181711 181682 0 16:07 pts/0 00:00:00 python3.8 test.py
root 181723 181659 0 16:07 pts/1 00:00:00 grep --color=auto python
查到test.py的进程id是181711
2、使用gdb -p pid命令 attach到进程里 3、使用info threads查看当前的线程
root>gdb -p 181711
GNU gdb (GDB) Red Hat Enterprise Linux 9.2-7.0.2.an8
Copyright (C) 2020 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 181711
[New LWP 181712]
[New LWP 181713]
warning: Loadable section ".note.gnu.property" outside of ELF segments
warning: Loadable section ".note.gnu.property" outside of ELF segments
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
warning: Loadable section ".note.gnu.property" outside of ELF segments
0x00007fddd0a10d96 in do_futex_wait.constprop () from /lib64/libpthread.so.0
(gdb) info threads
Id Target Id Frame
* 1 Thread 0x7fddd2005100 (LWP 181711) "python3.8" 0x00007fddd0a10d96 in do_futex_wait.constprop () from /lib64/libpthread.so.0
2 Thread 0x7fddcf5ff700 (LWP 181712) "python3.8" 0x00007fddcfb2721f in select () from /lib64/libc.so.6
3 Thread 0x7fddcedfe700 (LWP 181713) "python3.8" 0x00007fddcfb2721f in select () from /lib64/libc.so.6
(gdb)
4、找到想要kill的线程id
例如:0x7fddcf5ff700,对应的线程Id是2
使用 t 2 切换到执行线程
5、执行 call (void)pthread_exit((void*)1) 命令kill线程, 然后按ctrl + c, 按 q 退出gdb
(gdb) t 2
[Switching to thread 2 (Thread 0x7fddcf5ff700 (LWP 181712))]
#0 0x00007fddcfb2721f in select () from /lib64/libc.so.6
(gdb) call (void)pthread_exit((void*)1)
warning: Loadable section ".note.gnu.property" outside of ELF segments
[Thread 0x7fddcf5ff700 (LWP 181712) exited]
Thread 1 "python3.8" received signal SIGINT, Interrupt.
[Switching to Thread 0x7fddd2005100 (LWP 181711)]
0x00007fddd0a10d96 in do_futex_wait.constprop () from /lib64/libpthread.so.0
The program received a signal in another thread while
making a function call from GDB.
Evaluation of the expression containing the function
(pthread_exit) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) q
A debugging session is active.
Inferior 1 [process 181711] will be detached.
Quit anyway? (y or n) y
Detaching from program: /usr/bin/python3.8, process 181711
[Inferior 1 (process 181711) detached]
root>
6、kill后,可以观察到线程1的打印不再打印
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcf5ff700: 1
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2
0x7fddcedfe700: 2