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