Windows- "my_safe_kill <pid> dump" will now also dump child processes

This is necessary to analyze hangs on buildbot, because they also happen
when mysqltest executes external programs.
This commit is contained in:
Vladislav Vaintroub 2017-10-25 10:10:18 +00:00
parent a6a4c25bf7
commit a02551b0ba

View File

@ -28,41 +28,39 @@
#include <psapi.h> #include <psapi.h>
#include <dbghelp.h> #include <dbghelp.h>
#include <tlhelp32.h> #include <tlhelp32.h>
#include <vector>
static DWORD find_child(DWORD pid)
static std::vector<DWORD> find_children(DWORD pid)
{ {
HANDLE h= NULL; HANDLE h= NULL;
PROCESSENTRY32 pe={ 0 }; PROCESSENTRY32 pe={ 0 };
DWORD child_pid = 0; std::vector<DWORD> children;
pe.dwSize = sizeof(PROCESSENTRY32); pe.dwSize = sizeof(PROCESSENTRY32);
h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(h == INVALID_HANDLE_VALUE) if(h == INVALID_HANDLE_VALUE)
return 0; return children;
for (BOOL ret = Process32First(h, &pe); ret; ret = Process32Next(h, &pe)) for (BOOL ret = Process32First(h, &pe); ret; ret = Process32Next(h, &pe))
{ {
if (pe.th32ParentProcessID == pid) if (pe.th32ParentProcessID == pid)
{ children.push_back(pe.th32ProcessID);
child_pid = pe.th32ProcessID;
break;
}
} }
CloseHandle(h); CloseHandle(h);
return (child_pid); return children;
} }
static int create_dump(DWORD pid) void dump_single_process(DWORD pid)
{ {
HANDLE file = 0;
HANDLE process= 0;
DWORD size= MAX_PATH;
char path[MAX_PATH]; char path[MAX_PATH];
char working_dir[MAX_PATH]; char working_dir[MAX_PATH];
int ret= -1; char tmpname[MAX_PATH];
HANDLE process= INVALID_HANDLE_VALUE;
HANDLE file= INVALID_HANDLE_VALUE;
char *p;
for(;;) process= OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
{
process= OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, (DWORD)pid);
if (!process) if (!process)
{ {
fprintf(stderr, "safe_kill : cannot open process pid=%u to create dump, last error %u\n", fprintf(stderr, "safe_kill : cannot open process pid=%u to create dump, last error %u\n",
@ -70,46 +68,47 @@ static int create_dump(DWORD pid)
goto exit; goto exit;
} }
DWORD size= MAX_PATH;
if (QueryFullProcessImageName(process, 0, path, &size) == 0) if (QueryFullProcessImageName(process, 0, path, &size) == 0)
{ {
fprintf(stderr, "safe_kill : cannot read process path for pid %u, last error %u\n", fprintf(stderr, "safe_kill : cannot read process path for pid %u, last error %u\n",
pid, GetLastError()); pid, GetLastError());
goto exit; goto exit;
} }
const char *filename= strrchr(path, '\\');
char *filename= strrchr(path, '\\');
if (filename) if (filename)
{ {
filename++; filename++;
// We are not interested in my_safe_process.exe, // We are not interested in dump of some proceses (my_safe_process.exe,cmd.exe)
// since it is only used to start up other programs. // since they are only used to start up other programs.
// We're interested however in my_safe_processes' child. // We're interested however in their children;
if (strcmp(filename, "my_safe_process.exe") == 0) const char *exclude_programs[] = {"my_safe_process.exe","cmd.exe", 0};
{ for(size_t i=0; exclude_programs[i]; i++)
pid= find_child(pid); if (_stricmp(filename, exclude_programs[i]) == 0)
if (!pid)
{
fprintf(stderr,"safe_kill : can't find child process for safe_process.exe\n");
goto exit; goto exit;
} }
CloseHandle(process);
}
else else
break; filename= path;
}
}
if ((p= strrchr(path, '.')) == 0) // Add .dmp extension
p= path + strlen(path); char *p;
if ((p= strrchr(filename, '.')) == 0)
p= filename + strlen(filename);
strncpy(p, ".dmp", path + MAX_PATH - p); strncpy(p, ".dmp", path + MAX_PATH - p);
/* Create dump in current directory.*/ // Íf file with this name exist, generate unique name with .dmp extension
const char *filename= strrchr(path, '\\'); if (GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES)
if (filename == 0) {
filename= path; if (!GetTempFileName(".", filename, 0, tmpname))
else {
filename++; fprintf(stderr, "GetTempFileName failed, last error %u", GetLastError());
goto exit;
}
strncat(tmpname, ".dmp", sizeof(tmpname));
filename= tmpname;
}
if (!GetCurrentDirectory(MAX_PATH, working_dir)) if (!GetCurrentDirectory(MAX_PATH, working_dir))
{ {
@ -134,7 +133,6 @@ static int create_dump(DWORD pid)
goto exit; goto exit;
} }
ret= 0;
fprintf(stderr, "Minidump written to %s, directory %s\n", filename, working_dir); fprintf(stderr, "Minidump written to %s, directory %s\n", filename, working_dir);
exit: exit:
@ -143,7 +141,19 @@ exit:
if (file != 0 && file != INVALID_HANDLE_VALUE) if (file != 0 && file != INVALID_HANDLE_VALUE)
CloseHandle(file); CloseHandle(file);
return ret; }
static int create_dump(DWORD pid, int recursion_depth= 5)
{
if (recursion_depth < 0)
return 0;
dump_single_process(pid);
std::vector<DWORD> children= find_children(pid);
for(size_t i=0; i < children.size(); i++)
create_dump(children[i], recursion_depth -1);
return 0;
} }