Revived the GEN_C_SRCS variable to support wrc lex/yacc sources.
[wine.git] / debugger / winedbg.c
blob6ebda1fe84d8cb8ff024a3e257d7ab0bfabb1d6b
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
5 * Eric Pouech (c) 2000
6 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include "debugger.h"
13 #include "thread.h"
14 #include "process.h"
15 #include "wincon.h"
16 #include "wingdi.h"
17 #include "winuser.h"
19 #include "winreg.h"
21 #ifdef DBG_need_heap
22 HANDLE dbg_heap = 0;
23 #endif
25 DBG_PROCESS* DEBUG_CurrProcess = NULL;
26 DBG_THREAD* DEBUG_CurrThread = NULL;
27 DWORD DEBUG_CurrTid;
28 DWORD DEBUG_CurrPid;
29 CONTEXT DEBUG_context;
31 static DBG_PROCESS* proc = NULL;
32 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
34 void DEBUG_Output(int chn, const char* buffer, int len)
36 if (DBG_IVAR(ConChannelMask) & chn)
37 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
38 if (DBG_IVAR(StdChannelMask) & chn)
39 fwrite(buffer, len, 1, stderr);
42 int DEBUG_Printf(int chn, const char* format, ...)
44 char buf[1024];
45 va_list valist;
46 int len;
48 va_start(valist, format);
49 len = wvsnprintf(buf, sizeof(buf), format, valist);
50 va_end(valist);
52 if (len <= -1) {
53 len = sizeof(buf) - 1;
54 buf[len] = 0;
55 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
57 DEBUG_Output(chn, buf, len);
58 return len;
61 static BOOL DEBUG_IntVarsRW(int read)
63 HKEY hkey;
64 DWORD type = REG_DWORD;
65 DWORD val;
66 DWORD count = sizeof(val);
67 int i;
68 DBG_INTVAR* div = DEBUG_IntVars;
70 if (read) {
71 /* initializes internal vars table */
72 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
73 div->val = _val; div->name = #_var; div->pval = _ref; \
74 div->type = _typ; div++;
75 #include "intvar.h"
76 #undef INTERNAL_VAR
79 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
80 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
81 * so don't use it */
82 fprintf(stderr, "Cannot create WineDbg key in registry\n");
83 return FALSE;
86 for (i = 0; i < DBG_IV_LAST; i++) {
87 if (read) {
88 if (!DEBUG_IntVars[i].pval) {
89 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
90 &type, (LPSTR)&val, &count))
91 DEBUG_IntVars[i].val = val;
92 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
93 } else {
94 *DEBUG_IntVars[i].pval = 0;
96 } else {
97 /* FIXME: type should be infered from basic type -if any- of intvar */
98 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
99 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
100 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
103 RegCloseKey(hkey);
104 return TRUE;
107 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
109 int i;
111 for (i = 0; i < DBG_IV_LAST; i++) {
112 if (!strcmp(DEBUG_IntVars[i].name, name))
113 return &DEBUG_IntVars[i];
115 return NULL;
118 static WINE_EXCEPTION_FILTER(wine_dbg)
120 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
121 DEBUG_ExternalDebugger();
122 return EXCEPTION_EXECUTE_HANDLER;
125 static DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
127 DBG_PROCESS* p;
129 for (p = proc; p; p = p->next)
130 if (p->pid == pid) break;
131 return p;
134 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h)
136 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
137 if (!p)
138 return NULL;
139 p->handle = h;
140 p->pid = pid;
141 p->threads = NULL;
142 p->num_threads = 0;
143 p->continue_on_first_exception = FALSE;
144 p->modules = NULL;
145 p->next_index = 0;
146 p->dbg_hdr_addr = 0;
148 p->next = proc;
149 p->prev = NULL;
150 if (proc) proc->prev = p;
151 proc = p;
152 return p;
155 static void DEBUG_DelThread(DBG_THREAD* p);
157 static void DEBUG_DelProcess(DBG_PROCESS* p)
159 if (p->threads != NULL) {
160 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
161 while (p->threads) DEBUG_DelThread(p->threads);
163 if (p->prev) p->prev->next = p->next;
164 if (p->next) p->next->prev = p->prev;
165 if (p == proc) proc = p->next;
166 DBG_free(p);
169 static void DEBUG_InitCurrProcess(void)
173 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
175 DWORD sz;
176 *(WCHAR*)buffer = 0;
177 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
180 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
182 LPVOID ad;
183 DWORD sz;
185 if ( addr
186 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
187 && sz == sizeof(ad)
188 && ad
189 && ReadProcessMemory(hp, ad, buffer, size, &sz))
190 return TRUE;
191 *(WCHAR*)buffer = 0;
192 return FALSE;
195 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
197 DBG_THREAD* t;
199 for (t = p->threads; t; t = t->next)
200 if (t->tid == tid) break;
201 return t;
204 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
205 HANDLE h, LPVOID start, LPVOID teb)
207 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
208 if (!t)
209 return NULL;
211 t->handle = h;
212 t->tid = tid;
213 t->start = start;
214 t->teb = teb;
215 t->process = p;
216 t->wait_for_first_exception = 0;
217 t->dbg_exec_mode = EXEC_CONT;
218 t->dbg_exec_count = 0;
220 p->num_threads++;
221 t->next = p->threads;
222 t->prev = NULL;
223 if (p->threads) p->threads->prev = t;
224 p->threads = t;
226 return t;
229 static void DEBUG_InitCurrThread(void)
231 if (DEBUG_CurrThread->start) {
232 if (DEBUG_CurrThread->process->num_threads == 1 ||
233 DBG_IVAR(BreakAllThreadsStartup)) {
234 DBG_VALUE value;
236 DEBUG_SetBreakpoints(FALSE);
237 value.type = NULL;
238 value.cookie = DV_TARGET;
239 value.addr.seg = 0;
240 value.addr.off = (DWORD)DEBUG_CurrThread->start;
241 DEBUG_AddBreakpoint(&value, NULL);
242 DEBUG_SetBreakpoints(TRUE);
244 } else {
245 DEBUG_CurrThread->wait_for_first_exception = 1;
249 static void DEBUG_DelThread(DBG_THREAD* t)
251 if (t->prev) t->prev->next = t->next;
252 if (t->next) t->next->prev = t->prev;
253 if (t == t->process->threads) t->process->threads = t->next;
254 t->process->num_threads--;
255 DBG_free(t);
258 static BOOL DEBUG_HandleException( EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force )
260 BOOL is_debug = FALSE;
261 BOOL ret;
263 /* FIXME: need for a configuration var ? */
264 /* pass to app first ??? */
265 /* if (first_chance && !force) return 0; */
267 switch (rec->ExceptionCode)
269 case EXCEPTION_BREAKPOINT:
270 case EXCEPTION_SINGLE_STEP:
271 is_debug = TRUE;
272 break;
275 if (!is_debug)
277 /* print some infos */
278 DEBUG_Printf( DBG_CHN_MESG, "%s: ",
279 first_chance ? "First chance exception" : "Unhandled exception" );
280 switch (rec->ExceptionCode)
282 case EXCEPTION_INT_DIVIDE_BY_ZERO:
283 DEBUG_Printf( DBG_CHN_MESG, "divide by zero" );
284 break;
285 case EXCEPTION_INT_OVERFLOW:
286 DEBUG_Printf( DBG_CHN_MESG, "overflow" );
287 break;
288 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
289 DEBUG_Printf( DBG_CHN_MESG, "array bounds " );
290 break;
291 case EXCEPTION_ILLEGAL_INSTRUCTION:
292 DEBUG_Printf( DBG_CHN_MESG, "illegal instruction" );
293 break;
294 case EXCEPTION_STACK_OVERFLOW:
295 DEBUG_Printf( DBG_CHN_MESG, "stack overflow" );
296 break;
297 case EXCEPTION_PRIV_INSTRUCTION:
298 DEBUG_Printf( DBG_CHN_MESG, "priviledged instruction" );
299 break;
300 case EXCEPTION_ACCESS_VIOLATION:
301 if (rec->NumberParameters == 2)
302 DEBUG_Printf( DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
303 rec->ExceptionInformation[0] ? "write" : "read",
304 rec->ExceptionInformation[1] );
305 else
306 DEBUG_Printf( DBG_CHN_MESG, "page fault" );
307 break;
308 case EXCEPTION_DATATYPE_MISALIGNMENT:
309 DEBUG_Printf( DBG_CHN_MESG, "Alignment" );
310 break;
311 case CONTROL_C_EXIT:
312 DEBUG_Printf( DBG_CHN_MESG, "^C" );
313 break;
314 case EXCEPTION_CRITICAL_SECTION_WAIT:
315 DEBUG_Printf( DBG_CHN_MESG, "critical section %08lx wait failed",
316 rec->ExceptionInformation[0] );
317 if (!DBG_IVAR(BreakOnCritSectTimeOut))
318 return DBG_CONTINUE;
319 break;
320 default:
321 DEBUG_Printf( DBG_CHN_MESG, "%08lx", rec->ExceptionCode );
322 break;
324 DEBUG_Printf(DBG_CHN_MESG, "\n");
327 DEBUG_Printf(DBG_CHN_TRACE,
328 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
329 DEBUG_context.Eip, DEBUG_context.EFlags,
330 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
332 ret = DEBUG_Main( is_debug, force, rec->ExceptionCode );
334 DEBUG_Printf(DBG_CHN_TRACE,
335 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
336 DEBUG_context.Eip, DEBUG_context.EFlags,
337 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
339 return ret;
342 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
344 char buffer[256];
345 BOOL ret;
347 DEBUG_CurrPid = de->dwProcessId;
348 DEBUG_CurrTid = de->dwThreadId;
350 __TRY {
351 ret = TRUE;
352 *cont = 0L;
354 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
355 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
356 else
357 DEBUG_CurrThread = NULL;
359 switch (de->dwDebugEventCode) {
360 case EXCEPTION_DEBUG_EVENT:
361 if (!DEBUG_CurrThread) {
362 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
363 de->dwProcessId, de->dwThreadId);
364 break;
367 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
368 de->dwProcessId, de->dwThreadId,
369 de->u.Exception.ExceptionRecord.ExceptionCode);
371 if (DEBUG_CurrProcess->continue_on_first_exception) {
372 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
373 if (!DBG_IVAR(BreakOnAttach)) {
374 *cont = DBG_CONTINUE;
375 break;
379 DEBUG_context.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_SEGMENTS|CONTEXT_DEBUG_REGISTERS;
380 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
381 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
382 break;
385 *cont = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
386 de->u.Exception.dwFirstChance,
387 DEBUG_CurrThread->wait_for_first_exception);
388 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_KILL) {
389 ret = FALSE;
390 } else {
391 DEBUG_CurrThread->wait_for_first_exception = 0;
392 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
394 break;
396 case CREATE_THREAD_DEBUG_EVENT:
397 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
398 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
400 if (DEBUG_CurrProcess == NULL) {
401 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
402 break;
404 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
405 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
406 break;
409 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
410 de->dwThreadId,
411 de->u.CreateThread.hThread,
412 de->u.CreateThread.lpStartAddress,
413 de->u.CreateThread.lpThreadLocalBase);
414 if (!DEBUG_CurrThread) {
415 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
416 break;
418 DEBUG_InitCurrThread();
419 break;
421 case CREATE_PROCESS_DEBUG_EVENT:
422 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
423 de->u.CreateProcessInfo.hProcess,
424 de->u.CreateProcessInfo.lpImageName);
426 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
427 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
428 de->dwProcessId, de->dwThreadId,
429 buffer,
430 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
431 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
432 de->u.CreateProcessInfo.nDebugInfoSize);
434 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
435 if (DEBUG_CurrProcess->handle) {
436 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
437 break;
439 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
440 } else {
441 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
442 de->u.CreateProcessInfo.hProcess);
443 if (DEBUG_CurrProcess == NULL) {
444 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
445 break;
449 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
450 de->dwProcessId, de->dwThreadId,
451 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
453 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
454 de->dwThreadId,
455 de->u.CreateProcessInfo.hThread,
456 de->u.CreateProcessInfo.lpStartAddress,
457 de->u.CreateProcessInfo.lpThreadLocalBase);
458 if (!DEBUG_CurrThread) {
459 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
460 break;
463 DEBUG_InitCurrProcess();
464 DEBUG_InitCurrThread();
465 /* so far, process name is not set */
466 DEBUG_LoadModule32("<Debugged process>", de->u.CreateProcessInfo.hFile,
467 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
468 break;
470 case EXIT_THREAD_DEBUG_EVENT:
471 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
472 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
474 if (DEBUG_CurrThread == NULL) {
475 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
476 break;
478 /* FIXME: remove break point set on thread startup */
479 DEBUG_DelThread(DEBUG_CurrThread);
480 break;
482 case EXIT_PROCESS_DEBUG_EVENT:
483 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
484 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
486 if (DEBUG_CurrProcess == NULL) {
487 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
488 break;
490 /* just in case */
491 DEBUG_SetBreakpoints(FALSE);
492 /* kill last thread */
493 DEBUG_DelThread(DEBUG_CurrProcess->threads);
494 DEBUG_DelProcess(DEBUG_CurrProcess);
495 ret = FALSE;
496 break;
498 case LOAD_DLL_DEBUG_EVENT:
499 if (DEBUG_CurrThread == NULL) {
500 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
501 break;
503 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
504 DEBUG_CurrThread->process->handle,
505 de->u.LoadDll.lpImageName);
507 /* FIXME unicode: de->u.LoadDll.fUnicode */
508 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
509 de->dwProcessId, de->dwThreadId,
510 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
511 de->u.LoadDll.dwDebugInfoFileOffset,
512 de->u.LoadDll.nDebugInfoSize);
513 CharUpper(buffer);
514 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
515 break;
517 case UNLOAD_DLL_DEBUG_EVENT:
518 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
519 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
520 break;
522 case OUTPUT_DEBUG_STRING_EVENT:
523 if (DEBUG_CurrThread == NULL) {
524 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
525 break;
528 DEBUG_ProcessGetString(buffer, sizeof(buffer),
529 DEBUG_CurrThread->process->handle,
530 de->u.DebugString.lpDebugStringData);
532 /* fixme unicode de->u.DebugString.fUnicode ? */
533 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
534 de->dwProcessId, de->dwThreadId, buffer);
535 break;
537 case RIP_EVENT:
538 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
539 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
540 de->u.RipInfo.dwType);
541 break;
543 default:
544 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
545 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
548 } __EXCEPT(wine_dbg) {
549 *cont = 0;
550 ret = TRUE;
552 __ENDTRY;
554 return ret;
557 static DWORD DEBUG_MainLoop(DWORD pid)
559 DEBUG_EVENT de;
560 DWORD cont;
561 BOOL ret = TRUE;
563 DEBUG_Printf(DBG_CHN_MESG, " on pid %ld\n", pid);
565 while (ret && WaitForDebugEvent(&de, INFINITE)) {
566 ret = DEBUG_HandleDebugEvent(&de, &cont);
567 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
570 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %ld\n", pid);
572 return 0;
575 int DEBUG_main(int argc, char** argv)
577 DWORD pid = 0, retv = 0;
579 #ifdef DBG_need_heap
580 /* Initialize the debugger heap. */
581 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
582 #endif
584 /* Initialize the type handling stuff. */
585 DEBUG_InitTypes();
586 DEBUG_InitCVDataTypes();
588 /* Initialize internal vars (types must be initialized before) */
589 if (!DEBUG_IntVarsRW(TRUE)) return -1;
591 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
592 * stdout and stderr streams
594 if (DBG_IVAR(UseXTerm)) {
595 COORD pos;
597 /* This is a hack: it forces creation of an xterm, not done by default */
598 pos.X = 0; pos.Y = 1;
599 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
602 DEBUG_Printf(DBG_CHN_MESG, "Starting WineDbg... ");
603 if (argc == 3) {
604 HANDLE hEvent;
606 if ((pid = atoi(argv[1])) != 0 && (hEvent = atoi(argv[2])) != 0) {
607 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) goto leave;
608 DEBUG_CurrProcess->continue_on_first_exception = TRUE;
610 if (!DebugActiveProcess(pid)) {
611 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
612 pid, GetLastError());
613 SetEvent(hEvent);
614 goto leave;
616 SetEvent(hEvent);
617 } else {
618 pid = 0;
621 if (argc == 1) {
622 LPSTR org, ptr;
624 DEBUG_Printf(DBG_CHN_MESG, "\n");
625 DEBUG_WalkProcess();
626 pid = strtol(org = readline("Enter pid to debug: "), &ptr, 0);
627 if (pid && ptr && ptr != org && *ptr == '\0') {
628 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) goto leave;
630 if (!DebugActiveProcess(pid)) {
631 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
632 pid, GetLastError());
633 goto leave;
635 } else {
636 pid = 0;
640 if (pid == 0) {
641 PROCESS_INFORMATION info;
642 STARTUPINFOA startup;
644 memset(&startup, 0, sizeof(startup));
645 startup.cb = sizeof(startup);
646 startup.dwFlags = STARTF_USESHOWWINDOW;
647 startup.wShowWindow = SW_SHOWNORMAL;
649 if (!CreateProcess(NULL, argv[1], NULL, NULL,
650 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
651 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", argv[1]);
652 goto leave;
654 pid = info.dwProcessId;
657 if (pid) retv = DEBUG_MainLoop(pid);
658 leave:
659 /* saves modified variables */
660 DEBUG_IntVarsRW(FALSE);
662 return retv;