2 #define _WIN32_WINNT 0x0501
5 #if _WIN32_WINNT < 0x0501
6 #error Must target Windows NT 5.0.1 or later for DebugBreakProcess
15 /* Compile with this line:
17 gcc -o debugbreak -mno-cygwin -mthreads debugbreak.c
21 static char errbuffer[256];
23 static const char *geterrstr(DWORD errcode)
27 chars = FormatMessage(
28 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
29 NULL, errcode, 0, errbuffer, sizeof(errbuffer)-1, 0);
30 errbuffer[sizeof(errbuffer)-1] = 0;
32 while (errbuffer[chars-1] == '\r' || errbuffer[chars-1] == '\n') {
33 errbuffer[--chars] = 0;
36 if (chars && errbuffer[chars-1] == '.') errbuffer[--chars] = 0;
37 if (chars >= 2 && errbuffer[0] == '%' && errbuffer[1] >= '0'
38 && errbuffer[1] <= '9')
41 while (chars > skip && errbuffer[skip] == ' ') ++skip;
42 if (chars >= skip+2 && errbuffer[skip] == 'i'
43 && errbuffer[skip+1] == 's')
46 while (chars > skip && errbuffer[skip] == ' ') ++skip;
49 if (chars > skip && errbuffer[skip] >= 'A' && errbuffer[skip] <= 'Z') {
50 errbuffer[skip] += 'a' - 'A';
52 return errbuffer+skip;
55 int main(int argc, char *argv[])
62 printf("Usage: debugbreak process_id_number\n");
65 proc_id = (unsigned) strtol(argv[1], NULL, 0);
67 printf("Invalid process id %u\n", proc_id);
70 proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)proc_id);
72 DWORD lastError = GetLastError();
73 printf("Failed to open process %u\n", proc_id);
74 printf("Error code is %lu (%s)\n", (unsigned long)lastError,
75 geterrstr(lastError));
78 break_result = DebugBreakProcess(proc);
80 DWORD lastError = GetLastError();
81 printf("Failed to debug break process %u\n", proc_id);
82 printf("Error code is %lu (%s)\n", (unsigned long)lastError,
83 geterrstr(lastError));
87 printf("DebugBreak sent successfully to process id %u\n", proc_id);
92 /* END debugbreak.c */