Skip to content

Commit bcd1a08

Browse files
committed
Fix mtr to create a process dump on Window for hanging or looping processes
1 parent 9ee840c commit bcd1a08

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

mysql-test/lib/My/SafeProcess.pm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,14 @@ sub start_kill {
336336

337337
sub dump_core {
338338
my ($self)= @_;
339-
return if IS_WINDOWS;
340339
my $pid= $self->{SAFE_PID};
341340
die "Can't get core from not started process" unless defined $pid;
341+
342+
if (IS_WINDOWS) {
343+
system("$safe_kill $pid dump");
344+
return 1;
345+
}
346+
342347
_verbose("Sending ABRT to $self");
343348
kill ("ABRT", $pid);
344349
return 1;

mysql-test/lib/My/SafeProcess/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SET(INSTALL_ARGS
2525
IF (WIN32)
2626
MYSQL_ADD_EXECUTABLE(my_safe_process safe_process_win.cc ${INSTALL_ARGS})
2727
MYSQL_ADD_EXECUTABLE(my_safe_kill safe_kill_win.cc ${INSTALL_ARGS})
28+
TARGET_LINK_LIBRARIES(my_safe_kill dbghelp psapi)
2829
ELSE()
2930
MYSQL_ADD_EXECUTABLE(my_safe_process safe_process.cc ${INSTALL_ARGS})
3031
ENDIF()

mysql-test/lib/My/SafeProcess/safe_kill_win.cc

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,80 @@
2525
#include <stdio.h>
2626
#include <signal.h>
2727
#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+
}
28102

29103
int main(int argc, const char** argv )
30104
{
@@ -37,12 +111,16 @@ int main(int argc, const char** argv )
37111
signal(SIGBREAK, SIG_IGN);
38112
signal(SIGTERM, SIG_IGN);
39113

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");
42116
exit(2);
43117
}
44118
pid= atoi(argv[1]);
45119

120+
if (argc == 3)
121+
{
122+
return create_dump(pid);
123+
}
46124
_snprintf(safe_process_name, sizeof(safe_process_name),
47125
"safe_process[%d]", pid);
48126

0 commit comments

Comments
 (0)