如何給有死鎖的進程發送信號,讓他產生core文件

當然就是沒有死鎖也可以發信號讓他產生core文件

 

 

1 首先來了解信號

 

Signal Description Signal number on Linux x86[1] 
SIGABRT Process aborted 6 
SIGALRM Signal raised by alarm 14 
SIGBUS Bus error: "access to undefined portion of memory object" 7 
SIGCHLD Child process terminated, stopped (or continued*) 17 
SIGCONT Continue if stopped 18 
SIGFPE Floating point exception: "erroneous arithmetic operation" 8 
SIGHUP Hangup 1 
SIGILL Illegal instruction 4 
SIGINT Interrupt 2 
SIGKILL Kill (terminate immediately) 9 
SIGPIPE Write to pipe with no one reading 13 
SIGQUIT Quit and dump core 3 
SIGSEGV Segmentation violation 11 
SIGSTOP Stop executing temporarily 19 
SIGTERM Termination (request to terminate) 15 
SIGTSTP Terminal stop signal 20 
SIGTTIN Background process attempting to read from tty ("in") 21 
SIGTTOU Background process attempting to write to tty ("out") 22 
SIGUSR1 User-defined 1 10 
SIGUSR2 User-defined 2 12 
SIGPOLL Pollable event 29 
SIGPROF Profiling timer expired 27 
SIGSYS Bad syscall 31 
SIGTRAP Trace/breakpoint trap 5 
SIGURG Urgent data available on socket 23 
SIGVTALRM Signal raised by timer counting virtual time: "virtual timer expired" 26 
SIGXCPU CPU time limit exceeded 24 
SIGXFSZ File size limit exceeded 25 

 

 

能讓進程產生段錯誤,生成core文件的信號

SIGABRT Process aborted 6 

2 使用方法:

比如a.out進程id號爲1234

執行a.out之後,新開一個會話端。

kill   -信號編號 進程id

執行kill -6 1234

core馬上產生

 

 

3. 例子

先使用 ulimit -c

查看shell是否限制了core的生成大小

 

執行 ulimit -c unlimited

不限制core大小

 

編譯程序(文章最後將獻上代碼),記得加上-g

 gcc  New0001.c -g  -lpthread  -o  a.out

 

執行./a.out

 

新開會話

root@ubuntu:~# ps -aux| grep a.out

root     16606 45.5  0.1  43320   696 pts/4    Sl+  00:14   0:04 ./a.out

root     16613  0.0  0.3   4540  1928 pts/6    S+   00:14   0:00 grep --color=auto a.out

root@ubuntu:~#

root@ubuntu:~#

root@ubuntu:~#

root@ubuntu:~# kill -6 16606

root@ubuntu:~#

 


./a.out進程終止

 

root@ubuntu:/share# ./a.out

Aborted (core dumped)

root@ubuntu:/share#

 

 

使用gdb查看core文件

 

root@ubuntu:/share#

root@ubuntu:/share# gdb a.out core

GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10

Copyright (C) 2015 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 "i686-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"...

Reading symbols from a.out...done.

 

warning: exec file is newer than core file.

[New LWP 16551]

[New LWP 16552]

[New LWP 16553]

[New LWP 16554]

[New LWP 16555]

[Thread debugging using libthread_db enabled]

Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".

Core was generated by `./a.out'.

Program terminated with signal SIGABRT, Aborted.

#0  0xb775dbe8 in __kernel_vsyscall ()

[Current thread is 1 (Thread 0xb7569700 (LWP 16551))]

(gdb) info threads

  Id   Target Id         Frame

  5    Thread 0xb5d65b40 (LWP 16555) 0xb775dbe8 in __kernel_vsyscall ()

  4    Thread 0xb6566b40 (LWP 16554) 0xb775dbe8 in __kernel_vsyscall ()

  3    Thread 0xb6d67b40 (LWP 16553) 0xb775dbe8 in __kernel_vsyscall ()

  2    Thread 0xb7568b40 (LWP 16552) 0xb775dbe8 in __kernel_vsyscall ()

* 1    Thread 0xb7569700 (LWP 16551) 0xb775dbe8 in __kernel_vsyscall ()

(gdb)

 

 4.代碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t g_smutex ; 

void * func(void *arg)
{
	int i=0;

	//lock

	pthread_mutex_lock( &g_smutex);
	
	for(i = 0 ;i < 0x7fffffff; i++)
	{

	}

	//forget unlock
	
	return NULL;
}

int main()
{
	pthread_t  thread_id_01;
	pthread_t  thread_id_02;
	pthread_t  thread_id_03;
	pthread_t  thread_id_04;
	pthread_t  thread_id_05;
	
	pthread_mutex_init( &g_smutex, NULL );

	pthread_create(&thread_id_01, NULL, func, NULL);
	pthread_create(&thread_id_02, NULL, func, NULL);
	pthread_create(&thread_id_03, NULL, func, NULL);
	pthread_create(&thread_id_04, NULL, func, NULL);
	pthread_create(&thread_id_05, NULL, func, NULL);

	while(1)
	{
		sleep(0xfff);
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章