25
25
#include < stdio.h>
26
26
#include < signal.h>
27
27
#include < stdlib.h>
28
+ #include < psapi.h>
29
+ #include < DbgHelp.h>
30
+
31
+ static int create_dump (DWORD pid)
32
+ {
33
+ char path[MAX_PATH];
34
+ char working_dir[MAX_PATH];
35
+ int ret= -1 ;
36
+ HANDLE process= INVALID_HANDLE_VALUE;
37
+ HANDLE file= INVALID_HANDLE_VALUE;
38
+ char *p;
39
+
40
+ process = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , (DWORD)pid);
41
+ if (!process)
42
+ {
43
+ fprintf (stderr," safe_kill : cannot open process pid=%u to create dump, last error %u\n " ,
44
+ pid, GetLastError ());
45
+ goto exit;
46
+ }
47
+
48
+ DWORD size = MAX_PATH;
49
+ if (QueryFullProcessImageName (process, 0 , path, &size) == 0 )
50
+ {
51
+ fprintf (stderr," safe_kill : cannot read process path for pid %u, last error %u\n " ,
52
+ pid, GetLastError ());
53
+ goto exit;
54
+ }
55
+
56
+ if ((p = strrchr (path, ' .' )) == 0 )
57
+ p= path + strlen (path);
58
+
59
+ strncpy (p, " .dmp" , path + MAX_PATH - p);
60
+
61
+ /* Create dump in current directory.*/
62
+ const char *filename= strrchr (path, ' \\ ' );
63
+ if (filename == 0 )
64
+ filename = path;
65
+ else
66
+ filename++;
67
+
68
+ if (!GetCurrentDirectory (MAX_PATH, working_dir))
69
+ {
70
+ fprintf (stderr, " GetCurrentDirectory failed, last error %u" ,GetLastError ());
71
+ goto exit;
72
+ }
73
+
74
+ file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE,
75
+ 0 , 0 , CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
76
+
77
+ if (file == INVALID_HANDLE_VALUE)
78
+ {
79
+ fprintf (stderr," safe_kill : CreateFile() failed for file %s, working dir %s, last error = %u\n " ,
80
+ filename, working_dir, GetLastError ());
81
+ goto exit;
82
+ }
83
+
84
+ if (!MiniDumpWriteDump (process, pid, file, MiniDumpNormal, 0 ,0 ,0 ))
85
+ {
86
+ fprintf (stderr, " Failed to write minidump to %s, working dir %s, last error %u\n " ,
87
+ filename, working_dir, GetLastError ());
88
+ goto exit;
89
+ }
90
+
91
+ ret = 0 ;
92
+ fprintf (stderr, " Minidump written to %s, directory %s\n " , filename, working_dir);
93
+
94
+ exit:
95
+ if (process!= 0 && process != INVALID_HANDLE_VALUE)
96
+ CloseHandle (process);
97
+
98
+ if (file != 0 && file != INVALID_HANDLE_VALUE)
99
+ CloseHandle (file);
100
+ return ret;
101
+ }
28
102
29
103
int main (int argc, const char ** argv )
30
104
{
@@ -37,12 +111,16 @@ int main(int argc, const char** argv )
37
111
signal (SIGBREAK, SIG_IGN);
38
112
signal (SIGTERM, SIG_IGN);
39
113
40
- if (argc != 2 ) {
41
- fprintf (stderr, " safe_kill <pid>\n " );
114
+ if (( argc != 2 && argc != 3 ) || (argc == 3 && strcmp (argv[ 2 ], " dump " )) ) {
115
+ fprintf (stderr, " safe_kill <pid> [dump] \n " );
42
116
exit (2 );
43
117
}
44
118
pid= atoi (argv[1 ]);
45
119
120
+ if (argc == 3 )
121
+ {
122
+ return create_dump (pid);
123
+ }
46
124
_snprintf (safe_process_name, sizeof (safe_process_name),
47
125
" safe_process[%d]" , pid);
48
126
0 commit comments