winedbg: Return 0 if --help is specified.
[wine] / programs / winedbg / winedbg.c
1 /* Wine internal debugger
2  * Interface to Windows debugger API
3  * Copyright 2000-2004 Eric Pouech
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "debugger.h"
27
28 #include "winternl.h"
29 #include "wine/exception.h"
30 #include "wine/library.h"
31
32 #include "wine/debug.h"
33
34 /* TODO list:
35  *
36  * - minidump
37  *      + ensure that all commands work as expected in minidump reload function
38  *        (and reenable parser usager)
39  * - CPU adherence
40  *      + we always assume the stack grows as on i386 (ie downwards)
41  * - UI
42  *      + enable back the limited output (depth of structure printing and number of 
43  *        lines)
44  *      + make the output as close as possible to what gdb does
45  * - symbol management:
46  *      + symbol table loading is broken
47  *      + in symbol_get_lvalue, we don't do any scoping (as C does) between local and
48  *        global vars (we may need this to force some display for example). A solution
49  *        would be always to return arrays with: local vars, global vars, thunks
50  * - type management:
51  *      + some bits of internal types are missing (like type casts and the address
52  *        operator)
53  *      + the type for an enum's value is always inferred as int (winedbg & dbghelp)
54  *      + most of the code implies that sizeof(void*) = sizeof(int)
55  *      + all computations should be made on long long
56  *              o expr computations are in int:s
57  *              o bitfield size is on a 4-bytes
58  *      + array_index and deref should be the same function (or should share the same
59  *        core)
60  * - execution:
61  *      + set a better fix for gdb (proxy mode) than the step-mode hack
62  *      + implement function call in debuggee
63  *      + trampoline management is broken when getting 16 <=> 32 thunk destination
64  *        address
65  *      + thunking of delayed imports doesn't work as expected (ie, when stepping,
66  *        it currently stops at first insn with line number during the library 
67  *        loading). We should identify this (__wine_delay_import) and set a
68  *        breakpoint instead of single stepping the library loading.
69  *      + it's wrong to copy thread->step_over_bp into process->bp[0] (when 
70  *        we have a multi-thread debuggee). complete fix must include storing all
71  *        thread's step-over bp in process-wide bp array, and not to handle bp
72  *        when we have the wrong thread running into that bp
73  *      + code in CREATE_PROCESS debug event doesn't work on Windows, as we cannot
74  *        get the name of the main module this way. We should rewrite all this code
75  *        and store in struct dbg_process as early as possible (before process
76  *        creation or attachment), the name of the main module
77  * - global:
78  *      + define a better way to enable the wine extensions (either DBG SDK function
79  *        in dbghelp, or TLS variable, or environment variable or ...)
80  *      + audit all files to ensure that we check all potential return values from
81  *        every function call to catch the errors
82  *      + BTW check also whether the exception mechanism is the best way to return
83  *        errors (or find a proper fix for MinGW port)
84  *      + use Wine standard list mechanism for all list handling
85  */
86
87 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
88
89 struct dbg_process*     dbg_curr_process = NULL;
90 struct dbg_thread*      dbg_curr_thread = NULL;
91 DWORD                   dbg_curr_tid;
92 DWORD                   dbg_curr_pid;
93 CONTEXT                 dbg_context;
94 BOOL                    dbg_interactiveP = FALSE;
95
96 static struct dbg_process*      dbg_process_list = NULL;
97
98 struct dbg_internal_var         dbg_internal_vars[DBG_IV_LAST];
99 const struct dbg_internal_var*  dbg_context_vars;
100 static HANDLE                   dbg_houtput;
101
102 static void dbg_outputA(const char* buffer, int len)
103 {
104     static char line_buff[4096];
105     static unsigned int line_pos;
106
107     DWORD w, i;
108
109     while (len > 0)
110     {
111         unsigned int count = min( len, sizeof(line_buff) - line_pos );
112         memcpy( line_buff + line_pos, buffer, count );
113         buffer += count;
114         len -= count;
115         line_pos += count;
116         for (i = line_pos; i > 0; i--) if (line_buff[i-1] == '\n') break;
117         if (!i)  /* no newline found */
118         {
119             if (len > 0) i = line_pos;  /* buffer is full, flush anyway */
120             else break;
121         }
122         WriteFile(dbg_houtput, line_buff, i, &w, NULL);
123         memmove( line_buff, line_buff + i, line_pos - i );
124         line_pos -= i;
125     }
126 }
127
128 const char* dbg_W2A(const WCHAR* buffer, unsigned len)
129 {
130     static unsigned ansilen;
131     static char* ansi;
132     unsigned newlen;
133
134     newlen = WideCharToMultiByte(CP_ACP, 0, buffer, len, NULL, 0, NULL, NULL);
135     if (newlen > ansilen)
136     {
137         static char* newansi;
138         if (ansi)
139             newansi = HeapReAlloc(GetProcessHeap(), 0, ansi, newlen);
140         else
141             newansi = HeapAlloc(GetProcessHeap(), 0, newlen);
142         if (!newansi) return NULL;
143         ansilen = newlen;
144         ansi = newansi;
145     }
146     WideCharToMultiByte(CP_ACP, 0, buffer, len, ansi, newlen, NULL, NULL);
147     return ansi;
148 }
149
150 void    dbg_outputW(const WCHAR* buffer, int len)
151 {
152     const char* ansi = dbg_W2A(buffer, len);
153     if (ansi) dbg_outputA(ansi, strlen(ansi));
154     /* FIXME: should CP_ACP be GetConsoleCP()? */
155 }
156
157 int     dbg_printf(const char* format, ...)
158 {
159     static    char      buf[4*1024];
160     va_list     valist;
161     int         len;
162
163     va_start(valist, format);
164     len = vsnprintf(buf, sizeof(buf), format, valist);
165     va_end(valist);
166
167     if (len <= -1 || len >= sizeof(buf)) 
168     {
169         len = sizeof(buf) - 1;
170         buf[len] = 0;
171         buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
172     }
173     dbg_outputA(buf, len);
174     return len;
175 }
176
177 static  unsigned dbg_load_internal_vars(void)
178 {
179     HKEY                        hkey;
180     DWORD                       type = REG_DWORD;
181     DWORD                       val;
182     DWORD                       count = sizeof(val);
183     int                         i;
184     struct dbg_internal_var*    div = dbg_internal_vars;
185
186 /* initializes internal vars table */
187 #define  INTERNAL_VAR(_var,_val,_ref,_tid)                      \
188         div->val = _val; div->name = #_var; div->pval = _ref;   \
189         div->typeid = _tid; div++;
190 #include "intvar.h"
191 #undef   INTERNAL_VAR
192
193     /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
194     if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) 
195     {
196         WINE_ERR("Cannot create WineDbg key in registry\n");
197         return FALSE;
198     }
199
200     for (i = 0; i < DBG_IV_LAST; i++) 
201     {
202         if (!dbg_internal_vars[i].pval) 
203         {
204             if (!RegQueryValueEx(hkey, dbg_internal_vars[i].name, 0,
205                                  &type, (LPBYTE)&val, &count))
206                 dbg_internal_vars[i].val = val;
207             dbg_internal_vars[i].pval = &dbg_internal_vars[i].val;
208         }
209     }
210     RegCloseKey(hkey);
211     /* set up the debug variables for the CPU context */
212     dbg_context_vars = be_cpu->init_registers(&dbg_context);
213     return TRUE;
214 }
215
216 static  unsigned dbg_save_internal_vars(void)
217 {
218     HKEY                        hkey;
219     int                         i;
220
221     /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
222     if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) 
223     {
224         WINE_ERR("Cannot create WineDbg key in registry\n");
225         return FALSE;
226     }
227
228     for (i = 0; i < DBG_IV_LAST; i++) 
229     {
230         /* FIXME: type should be inferred from basic type -if any- of intvar */
231         if (dbg_internal_vars[i].pval == &dbg_internal_vars[i].val)
232             RegSetValueEx(hkey, dbg_internal_vars[i].name, 0,
233                           REG_DWORD, (const void*)dbg_internal_vars[i].pval, 
234                           sizeof(*dbg_internal_vars[i].pval));
235     }
236     RegCloseKey(hkey);
237     return TRUE;
238 }
239
240 const struct dbg_internal_var* dbg_get_internal_var(const char* name)
241 {
242     const struct dbg_internal_var*      div;
243
244     for (div = &dbg_internal_vars[DBG_IV_LAST - 1]; div >= dbg_internal_vars; div--)
245     {
246         if (!strcmp(div->name, name)) return div;
247     }
248     for (div = dbg_context_vars; div->name; div++)
249     {
250         if (!strcasecmp(div->name, name)) return div;
251     }
252
253     return NULL;
254 }
255
256 unsigned         dbg_num_processes(void)
257 {
258     struct dbg_process* p;
259     unsigned            num = 0;
260
261     for (p = dbg_process_list; p; p = p->next)
262         num++;
263     return num;
264 }
265
266 struct dbg_process*     dbg_get_process(DWORD pid)
267 {
268     struct dbg_process* p;
269
270     for (p = dbg_process_list; p; p = p->next)
271         if (p->pid == pid) break;
272     return p;
273 }
274
275 struct dbg_process*     dbg_get_process_h(HANDLE h)
276 {
277     struct dbg_process* p;
278
279     for (p = dbg_process_list; p; p = p->next)
280         if (p->handle == h) break;
281     return p;
282 }
283
284 struct dbg_process*     dbg_add_process(const struct be_process_io* pio, DWORD pid, HANDLE h)
285 {
286     struct dbg_process* p;
287
288     if ((p = dbg_get_process(pid)))
289     {
290         if (p->handle != 0)
291         {
292             WINE_ERR("Process (%04x) is already defined\n", pid);
293         }
294         else
295         {
296             p->handle = h;
297             p->process_io = pio;
298             p->imageName = NULL;
299         }
300         return p;
301     }
302
303     if (!(p = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_process)))) return NULL;
304     p->handle = h;
305     p->pid = pid;
306     p->process_io = pio;
307     p->pio_data = NULL;
308     p->imageName = NULL;
309     p->threads = NULL;
310     p->continue_on_first_exception = FALSE;
311     p->active_debuggee = FALSE;
312     p->next_bp = 1;  /* breakpoint 0 is reserved for step-over */
313     memset(p->bp, 0, sizeof(p->bp));
314     p->delayed_bp = NULL;
315     p->num_delayed_bp = 0;
316     p->source_ofiles = NULL;
317     p->search_path = NULL;
318     p->source_current_file[0] = '\0';
319     p->source_start_line = -1;
320     p->source_end_line = -1;
321
322     p->next = dbg_process_list;
323     p->prev = NULL;
324     if (dbg_process_list) dbg_process_list->prev = p;
325     dbg_process_list = p;
326     return p;
327 }
328
329 void dbg_set_process_name(struct dbg_process* p, const WCHAR* imageName)
330 {
331     assert(p->imageName == NULL);
332     if (imageName)
333     {
334         WCHAR* tmp = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(imageName) + 1) * sizeof(WCHAR));
335         if (tmp) p->imageName = lstrcpyW(tmp, imageName);
336     }
337 }
338
339 void dbg_del_process(struct dbg_process* p)
340 {
341     int i;
342
343     while (p->threads) dbg_del_thread(p->threads);
344
345     for (i = 0; i < p->num_delayed_bp; i++)
346         if (p->delayed_bp[i].is_symbol)
347             HeapFree(GetProcessHeap(), 0, p->delayed_bp[i].u.symbol.name);
348
349     HeapFree(GetProcessHeap(), 0, p->delayed_bp);
350     source_nuke_path(p);
351     source_free_files(p);
352     if (p->prev) p->prev->next = p->next;
353     if (p->next) p->next->prev = p->prev;
354     if (p == dbg_process_list) dbg_process_list = p->next;
355     if (p == dbg_curr_process) dbg_curr_process = NULL;
356     HeapFree(GetProcessHeap(), 0, (char*)p->imageName);
357     HeapFree(GetProcessHeap(), 0, p);
358 }
359
360 /******************************************************************
361  *              dbg_init
362  *
363  * Initializes the dbghelp library, and also sets the application directory
364  * as a place holder for symbol searches.
365  */
366 BOOL dbg_init(HANDLE hProc, const WCHAR* in, BOOL invade)
367 {
368     BOOL        ret;
369
370     ret = SymInitialize(hProc, NULL, invade);
371     if (ret && in)
372     {
373         const WCHAR*    last;
374
375         for (last = in + lstrlenW(in) - 1; last >= in; last--)
376         {
377             if (*last == '/' || *last == '\\')
378             {
379                 WCHAR*  tmp;
380                 tmp = HeapAlloc(GetProcessHeap(), 0, (1024 + 1 + (last - in) + 1) * sizeof(WCHAR));
381                 if (tmp && SymGetSearchPathW(hProc, tmp, 1024))
382                 {
383                     WCHAR*      x = tmp + lstrlenW(tmp);
384
385                     *x++ = ';';
386                     memcpy(x, in, (last - in) * sizeof(WCHAR));
387                     x[last - in] = '\0';
388                     ret = SymSetSearchPathW(hProc, tmp);
389                 }
390                 else ret = FALSE;
391                 HeapFree(GetProcessHeap(), 0, tmp);
392                 break;
393             }
394         }
395     }
396     return ret;
397 }
398
399 struct mod_loader_info
400 {
401     HANDLE              handle;
402     IMAGEHLP_MODULE*    imh_mod;
403 };
404
405 static BOOL CALLBACK mod_loader_cb(PCSTR mod_name, ULONG base, PVOID ctx)
406 {
407     struct mod_loader_info*     mli = ctx;
408
409     if (!strcmp(mod_name, "<wine-loader>"))
410     {
411         if (SymGetModuleInfo(mli->handle, base, mli->imh_mod))
412             return FALSE; /* stop enum */
413     }
414     return TRUE;
415 }
416
417 BOOL dbg_get_debuggee_info(HANDLE hProcess, IMAGEHLP_MODULE* imh_mod)
418 {
419     struct mod_loader_info  mli;
420     DWORD                   opt;
421
422     /* this will resynchronize builtin dbghelp's internal ELF module list */
423     SymLoadModule(hProcess, 0, 0, 0, 0, 0);
424     mli.handle  = hProcess;
425     mli.imh_mod = imh_mod;
426     imh_mod->SizeOfStruct = sizeof(*imh_mod);
427     imh_mod->BaseOfImage = 0;
428     /* this is a wine specific options to return also ELF modules in the
429      * enumeration
430      */
431     SymSetOptions((opt = SymGetOptions()) | 0x40000000);
432     SymEnumerateModules(hProcess, mod_loader_cb, (void*)&mli);
433     SymSetOptions(opt);
434
435     return imh_mod->BaseOfImage != 0;
436 }
437
438 BOOL dbg_load_module(HANDLE hProc, HANDLE hFile, const WCHAR* name, DWORD base, DWORD size)
439 {
440     BOOL ret = SymLoadModuleExW(hProc, NULL, name, NULL, base, size, NULL, 0);
441     if (ret)
442     {
443         IMAGEHLP_MODULEW64      ihm;
444         ihm.SizeOfStruct = sizeof(ihm);
445         if (SymGetModuleInfoW64(hProc, base, &ihm) && (ihm.PdbUnmatched || ihm.DbgUnmatched))
446             dbg_printf("Loaded unmatched debug information for %s\n", wine_dbgstr_w(name));
447     }
448     return ret;
449 }
450
451 struct dbg_thread* dbg_get_thread(struct dbg_process* p, DWORD tid)
452 {
453     struct dbg_thread*  t;
454
455     if (!p) return NULL;
456     for (t = p->threads; t; t = t->next)
457         if (t->tid == tid) break;
458     return t;
459 }
460
461 struct dbg_thread* dbg_add_thread(struct dbg_process* p, DWORD tid,
462                                   HANDLE h, void* teb)
463 {
464     struct dbg_thread*  t = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_thread));
465
466     if (!t)
467         return NULL;
468
469     t->handle = h;
470     t->tid = tid;
471     t->teb = teb;
472     t->process = p;
473     t->exec_mode = dbg_exec_cont;
474     t->exec_count = 0;
475     t->step_over_bp.enabled = FALSE;
476     t->step_over_bp.refcount = 0;
477     t->stopped_xpoint = -1;
478     t->in_exception = FALSE;
479     t->frames = NULL;
480     t->num_frames = 0;
481     t->curr_frame = -1;
482     t->addr_mode = AddrModeFlat;
483
484     snprintf(t->name, sizeof(t->name), "%04x", tid);
485
486     t->next = p->threads;
487     t->prev = NULL;
488     if (p->threads) p->threads->prev = t;
489     p->threads = t;
490
491     return t;
492 }
493
494 void dbg_del_thread(struct dbg_thread* t)
495 {
496     HeapFree(GetProcessHeap(), 0, t->frames);
497     if (t->prev) t->prev->next = t->next;
498     if (t->next) t->next->prev = t->prev;
499     if (t == t->process->threads) t->process->threads = t->next;
500     if (t == dbg_curr_thread) dbg_curr_thread = NULL;
501     HeapFree(GetProcessHeap(), 0, t);
502 }
503
504 void dbg_set_option(const char* option, const char* val)
505 {
506     if (!strcasecmp(option, "module_load_mismatched"))
507     {
508         DWORD   opt = SymGetOptions();
509         if (!val)
510             dbg_printf("Option: module_load_mismatched %s\n", opt & SYMOPT_LOAD_ANYTHING ? "true" : "false");
511         else if (!strcasecmp(val, "true"))      opt |= SYMOPT_LOAD_ANYTHING;
512         else if (!strcasecmp(val, "false"))     opt &= ~SYMOPT_LOAD_ANYTHING;
513         else
514         {
515             dbg_printf("Syntax: module_load_mismatched [true|false]\n");
516             return;
517         }
518         SymSetOptions(opt);
519     }
520     else if (!strcasecmp(option, "symbol_picker"))
521     {
522         if (!val)
523             dbg_printf("Option: symbol_picker %s\n",
524                        symbol_current_picker == symbol_picker_interactive ? "interactive" : "scoped");
525         else if (!strcasecmp(val, "interactive"))
526             symbol_current_picker = symbol_picker_interactive;
527         else if (!strcasecmp(val, "scoped"))
528             symbol_current_picker = symbol_picker_scoped;
529         else
530         {
531             dbg_printf("Syntax: symbol_picker [interactive|scoped]\n");
532             return;
533         }
534     }
535     else dbg_printf("Unknown option '%s'\n", option);
536 }
537
538 BOOL dbg_interrupt_debuggee(void)
539 {
540     if (!dbg_process_list) return FALSE;
541     /* FIXME: since we likely have a single process, signal the first process
542      * in list
543      */
544     if (dbg_process_list->next) dbg_printf("Ctrl-C: only stopping the first process\n");
545     else dbg_printf("Ctrl-C: stopping debuggee\n");
546     dbg_process_list->continue_on_first_exception = FALSE;
547     return DebugBreakProcess(dbg_process_list->handle);
548 }
549
550 static BOOL WINAPI ctrl_c_handler(DWORD dwCtrlType)
551 {
552     if (dwCtrlType == CTRL_C_EVENT)
553     {
554         return dbg_interrupt_debuggee();
555     }
556     return FALSE;
557 }
558
559 void dbg_init_console(void)
560 {
561     /* set the output handle */
562     dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
563
564     /* set our control-C handler */
565     SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
566
567     /* set our own title */
568     SetConsoleTitle("Wine Debugger");
569 }
570
571 static int dbg_winedbg_usage(BOOL advanced)
572 {
573     if (advanced)
574     {
575     dbg_printf("Usage:\n"
576                "   winedbg cmdline         launch process 'cmdline' (as if you were starting\n"
577                "                           it with wine) and run WineDbg on it\n"
578                "   winedbg <num>           attach to running process of pid <num> and run\n"
579                "                           WineDbg on it\n"
580                "   winedbg --gdb cmdline   launch process 'cmdline' (as if you were starting\n"
581                "                           wine) and run gdb (proxied) on it\n"
582                "   winedbg --gdb <num>     attach to running process of pid <num> and run\n"
583                "                           gdb (proxied) on it\n"
584                "   winedbg file.mdmp       reload the minidump file.mdmp into memory and run\n"
585                "                           WineDbg on it\n"
586                "   winedbg --help          prints advanced options\n");
587     }
588     else
589         dbg_printf("Usage:\n\twinedbg [ [ --gdb ] [ prog-name [ prog-args ] | <num> | file.mdmp | --help ]\n");
590     return 0;
591 }
592
593 void dbg_start_interactive(HANDLE hFile)
594 {
595     if (dbg_curr_process)
596     {
597         dbg_printf("WineDbg starting on pid %04x\n", dbg_curr_pid);
598         if (dbg_curr_process->active_debuggee) dbg_active_wait_for_first_exception();
599     }
600
601     dbg_interactiveP = TRUE;
602     parser_handle(hFile);
603
604     while (dbg_process_list)
605         dbg_process_list->process_io->close_process(dbg_process_list, FALSE);
606
607     dbg_save_internal_vars();
608 }
609
610 struct backend_cpu* be_cpu;
611 #ifdef __i386__
612 extern struct backend_cpu be_i386;
613 #elif __powerpc__
614 extern struct backend_cpu be_ppc;
615 #elif __ALPHA__
616 extern struct backend_cpu be_alpha;
617 #elif __x86_64__
618 extern struct backend_cpu be_x86_64;
619 #else
620 # error CPU unknown
621 #endif
622
623 int main(int argc, char** argv)
624 {
625     int                 retv = 0;
626     HANDLE              hFile = INVALID_HANDLE_VALUE;
627     enum dbg_start      ds;
628
629 #ifdef __i386__
630     be_cpu = &be_i386;
631 #elif __powerpc__
632     be_cpu = &be_ppc;
633 #elif __ALPHA__
634     be_cpu = &be_alpha;
635 #elif __x86_64__
636     be_cpu = &be_x86_64;
637 #else
638 # error CPU unknown
639 #endif
640     /* Initialize the output */
641     dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
642
643     /* Initialize internal vars */
644     if (!dbg_load_internal_vars()) return -1;
645
646     /* as we don't care about exec name */
647     argc--; argv++;
648
649     if (argc && !strcmp(argv[0], "--help"))
650         return dbg_winedbg_usage(TRUE);
651
652     if (argc && !strcmp(argv[0], "--gdb"))
653     {
654         retv = gdb_main(argc, argv);
655         if (retv == -1) dbg_winedbg_usage(FALSE);
656         return retv;
657     }
658     dbg_init_console();
659
660     SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
661                   SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
662
663     if (argc && (!strcmp(argv[0], "--auto") || !strcmp(argv[0], "--minidump")))
664     {
665         /* force some internal variables */
666         DBG_IVAR(BreakOnDllLoad) = 0;
667         dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
668         switch (dbg_active_auto(argc, argv))
669         {
670         case start_ok:          return 0;
671         case start_error_parse: return dbg_winedbg_usage(FALSE);
672         case start_error_init:  return -1;
673         }
674     }
675     /* parse options */
676     while (argc > 0 && argv[0][0] == '-')
677     {
678         if (!strcmp(argv[0], "--command"))
679         {
680             argc--; argv++;
681             hFile = parser_generate_command_file(argv[0], NULL);
682             if (hFile == INVALID_HANDLE_VALUE)
683             {
684                 dbg_printf("Couldn't open temp file (%u)\n", GetLastError());
685                 return 1;
686             }
687             argc--; argv++;
688             continue;
689         }
690         if (!strcmp(argv[0], "--file"))
691         {
692             argc--; argv++;
693             hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0, 
694                                 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
695             if (hFile == INVALID_HANDLE_VALUE)
696             {
697                 dbg_printf("Couldn't open file %s (%u)\n", argv[0], GetLastError());
698                 return 1;
699             }
700             argc--; argv++;
701             continue;
702         }
703         if (!strcmp(argv[0], "--"))
704         {
705             argc--; argv++;
706             break;
707         }
708         return dbg_winedbg_usage(FALSE);
709     }
710     if (!argc) ds = start_ok;
711     else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse &&
712              (ds = minidump_reload(argc, argv)) == start_error_parse)
713         ds = dbg_active_launch(argc, argv);
714     switch (ds)
715     {
716     case start_ok:              break;
717     case start_error_parse:     return dbg_winedbg_usage(FALSE);
718     case start_error_init:      return -1;
719     }
720
721     dbg_start_interactive(hFile);
722
723     return 0;
724 }