LinuxÏÂʹÓÃGDBµ÷ÊÔ¶àÏ̳߳ÌÐòµÄ³£¼ûÉèÖÃÒªÁì
linuxÏÂʹÓÃgdbµ÷ÊÔ¶àÏ̳߳ÌÐòµÄ³£¼ûÉèÖÃÒªÁì
СÐò:
ÔÚ¶àÏ̱߳à³ÌÖУ¬µ÷ÊÔÊÇÒ»Ïî±Ø²»¿ÉÉÙµÄÊÂÇé¡£GDBÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢µÄµ÷ÊÔÆ÷£¬¿ÉÒÔ×ÊÖúÎÒÃǶ¨Î»Ï¢Õù¾ö¶àÏ̳߳ÌÐòÖзºÆðµÄ¹ýʧ¡£±¾ÎĽ«ÏÈÈÝÔÚlinuxÏÂʹÓÃgdbµ÷ÊÔ¶àÏ̳߳ÌÐòµÄ³£¼ûÉèÖÃÒªÁ죬²¢Å䱸´úÂëʾÀý£¬Ï£ÍûÄÜ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÔËÓÃGDB¡£
Ò»¡¢×°ÖÃGDB
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚLinuxϵͳÖÐ×°ÖÃGDB¡£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ¼´¿ÉÍê³É×°Öãº
$ sudo apt-get install gdb
µÇ¼ºó¸´ÖÆ
¶þ¡¢±àÒë¶àÏ̳߳ÌÐò
ÔÚ¾ÙÐжàÏ̳߳ÌÐòµÄµ÷ÊÔ֮ǰ£¬ÎÒÃÇÊ×ÏÈÐèÒª±àдºÍ±àÒëÒ»¸ö¼òÆӵĶàÏ̳߳ÌÐò¡£ÒÔÏÂÊÇÒ»¸öʾÀý³ÌÐòµÄ´úÂ룺
#include <stdio.h> #include <pthread.h> #define NUM_THREADS 5 void* thread_func(void* thread_id) { long tid = (long)thread_id; printf("Hello World! It's me, thread #%ld! ", tid); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int rc; long t; for (t = 0; t < NUM_THREADS; t++) { printf("In main: creating thread %ld ", t); rc = pthread_create(&threads[t], NULL, thread_func, (void*)t); if (rc) { printf("ERROR; return code from pthread_create() is %d ", rc); return -1; } } pthread_exit(NULL); }
µÇ¼ºó¸´ÖÆ
ÎÒÃǽ«ÉÏÊö´úÂëÉúÑÄÖÁÒ»¸öÃûΪmultithread.cµÄÎļþÖУ¬²¢Ê¹ÓÃÒÔÏÂÏÂÁî¾ÙÐбàÒ룺
$ gcc -g -pthread -o multithread multithread.c
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬-gÑ¡ÏîÓÃÓÚÔÚ¿ÉÖ´ÐÐÎļþÖмÓÈëµ÷ÊÔÐÅÏ¢£¬-pthreadÑ¡ÏîÔòÓÃÓÚÒýÈë¶àÏ߳̿⡣
Èý¡¢Æô¶¯GDBµ÷ÊÔ
Íê³É±àÒëÖ®ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃGDBÆô¶¯µ÷ÊÔ¡£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
$ gdb multithread
µÇ¼ºó¸´ÖÆ
ËÄ¡¢ÉèÖÃGDBµ÷ÊÔÑ¡Ïî
ÔÚGDBÖУ¬ÓÐһЩµ÷ÊÔÑ¡Ïî¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØµ÷ÊÔ¶àÏ̳߳ÌÐò¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÊäÈëÒÔÏÂÏÂÁî¾ÙÐÐÉèÖãº
ÉèÖÃÏÔʾÏ̺߳Å
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖÃÏÔʾÏ̺߳ţº
(gdb) set print thread-events off
µÇ¼ºó¸´ÖÆ
ÉèÖÃÏÔʾ¿ÍÕ»
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖÃÏÔʾ¿ÍÕ»£º
(gdb) set backtrace limit 10
µÇ¼ºó¸´ÖÆ
ÉèÖÃÏÔʾÏß³ÌÐÅÏ¢
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖÃÏÔʾÏß³ÌÐÅÏ¢£º
(gdb) show scheduling
µÇ¼ºó¸´ÖÆ
ÉèÖÃÏÔʾÏß³ÌÖ´ÐеĴúÂëλÖÃ
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖÃÏÔʾÏß³ÌÖ´ÐеĴúÂëλÖãº
(gdb) set scheduler-locking on
µÇ¼ºó¸´ÖÆ
Îå¡¢ÉèÖöϵãºÍ¼àÊÓµã
ÔÚµ÷ÊÔÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔÉèÖöϵãºÍ¼àÊÓµãÀ´¿ØÖƳÌÐòµÄÖ´ÐÐÁ÷³Ì¡£ÒÔÏÂÊÇһЩ³£ÓõÄÏÂÁîʾÀý£º
ÉèÖöϵã
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖöϵ㣺
(gdb) break function_name
µÇ¼ºó¸´ÖÆ
ɾ³ý¶Ïµã
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔɾ³ý¶Ïµã£º
(gdb) delete breakpoints
µÇ¼ºó¸´ÖÆ
ÉèÖüàÊÓµã
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÉèÖüàÊӵ㣺
(gdb) watch variable_name
µÇ¼ºó¸´ÖÆ
ɾ³ý¼àÊÓµã
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔɾ³ý¼àÊӵ㣺
(gdb) delete watchpoints
µÇ¼ºó¸´ÖÆ
Áù¡¢µ÷ÊÔ¶àÏ̳߳ÌÐò
ÔÚGDBÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´µ÷ÊÔ¶àÏ̳߳ÌÐò£º
Æô¶¯³ÌÐò
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÆô¶¯³ÌÐò£º
(gdb) run
µÇ¼ºó¸´ÖÆ
ÔÝÍ£³ÌÐò
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÔÝÍ£ÕýÔÚÖ´ÐеijÌÐò£º
(gdb) Ctrl+C
µÇ¼ºó¸´ÖÆ
ÁгöËùÓÐÏß³Ì
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÁгöËùÓÐỊ̈߳º
(gdb) info threads
µÇ¼ºó¸´ÖÆ
Çл»µ½Ö¸¶¨Ïß³Ì
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔÇл»µ½Ö¸¶¨Ị̈߳º
(gdb) thread thread_id
µÇ¼ºó¸´ÖÆ
¼ÌÐøÖ´ÐгÌÐò
ÔÚGDBÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒÔ¼ÌÐøÖ´ÐгÌÐò£º
(gdb) continue
µÇ¼ºó¸´ÖÆ
Æß¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚlinuxÏÂʹÓÃgdbµ÷ÊÔ¶àÏ̳߳ÌÐòµÄ³£¼ûÉèÖÃÒªÁ죬²¢Å䱸ÁË´úÂëʾÀý¡£Í¨¹ýºÏÀíµÄÉèÖõ÷ÊÔÑ¡ÏîºÍʹÓÃÏìÓ¦µÄÏÂÁÎÒÃÇ¿ÉÒԺܺõؿØÖƺͶ¨Î»¶àÏ̳߳ÌÐòÖеÄÎÊÌ⣬Ìá¸ßµ÷ÊÔЧÂʺÍ׼ȷÐÔ¡£Ï£Íû±¾ÎÄÄܹ»¶Ô¶ÁÕßÔÚ¶àÏ̵߳÷ÊÔ·½ÃæÌṩ×ÊÖú£¬²¢Òý·¢¸ü¶àµÄѧϰºÍʵ¼ù¡£
ÒÔÉϾÍÊÇLinuxÏÂʹÓÃGDBµ÷ÊÔ¶àÏ̳߳ÌÐòµÄ³£¼ûÉèÖÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡