gdiplus: Remove unused variable.
[wine.git] / programs / winedbg / winedbg.c
blob92fa77429f60cf5c5ac5666f418d4897784458a6
1 /* Wine internal debugger
2 * Interface to Windows debugger API
3 * Copyright 2000-2004 Eric Pouech
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.
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.
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
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "debugger.h"
25 #include "winternl.h"
26 #include "wine/debug.h"
28 /* TODO list:
30 * - minidump
31 * + ensure that all commands work as expected in minidump reload function
32 * (and re-enable parser usage)
33 * - CPU adherence
34 * + we always assume the stack grows as on i386 (i.e. downwards)
35 * - UI
36 * + re-enable the limited output (depth of structure printing and number of
37 * lines)
38 * + make the output as close as possible to what gdb does
39 * - symbol management:
40 * + symbol table loading is broken
41 * + in symbol_get_lvalue, we don't do any scoping (as C does) between local and
42 * global vars (we may need this to force some display for example). A solution
43 * would be always to return arrays with: local vars, global vars, thunks
44 * - type management:
45 * + some bits of internal types are missing (like type casts and the address
46 * operator)
47 * + all computations should be made on 64bit
48 * o bitfield spreading on more bytes than dbg_lgint_t isn't supported
49 * (can happen on 128bit integers, of an ELF build...)
50 * - execution:
51 * + set a better fix for gdb (proxy mode) than the step-mode hack
52 * + implement function call in debuggee
53 * + trampoline management is broken when getting 16 <=> 32 thunk destination
54 * address
55 * + thunking of delayed imports doesn't work as expected (ie, when stepping,
56 * it currently stops at first insn with line number during the library
57 * loading). We should identify this (__wine_delay_import) and set a
58 * breakpoint instead of single stepping the library loading.
59 * + it's wrong to copy thread->step_over_bp into process->bp[0] (when
60 * we have a multi-thread debuggee). complete fix must include storing all
61 * thread's step-over bp in process-wide bp array, and not to handle bp
62 * when we have the wrong thread running into that bp
63 * + code in CREATE_PROCESS debug event doesn't work on Windows, as we cannot
64 * get the name of the main module this way. We should rewrite all this code
65 * and store in struct dbg_process as early as possible (before process
66 * creation or attachment), the name of the main module
67 * - global:
68 * + define a better way to enable the wine extensions (either DBG SDK function
69 * in dbghelp, or TLS variable, or environment variable or ...)
70 * + audit all files to ensure that we check all potential return values from
71 * every function call to catch the errors
72 * + BTW check also whether the exception mechanism is the best way to return
73 * errors (or find a proper fix for MinGW port)
76 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
78 struct dbg_process* dbg_curr_process = NULL;
79 struct dbg_thread* dbg_curr_thread = NULL;
80 DWORD dbg_curr_tid = 0;
81 DWORD dbg_curr_pid = 0;
82 dbg_ctx_t dbg_context;
83 BOOL dbg_interactiveP = FALSE;
84 HANDLE dbg_houtput = 0;
86 static struct list dbg_process_list = LIST_INIT(dbg_process_list);
88 struct dbg_internal_var dbg_internal_vars[DBG_IV_LAST];
90 static void dbg_outputA(const char* buffer, int len)
92 static char line_buff[4096];
93 static unsigned int line_pos;
95 DWORD w, i;
97 while (len > 0)
99 unsigned int count = min( len, sizeof(line_buff) - line_pos );
100 memcpy( line_buff + line_pos, buffer, count );
101 buffer += count;
102 len -= count;
103 line_pos += count;
104 for (i = line_pos; i > 0; i--) if (line_buff[i-1] == '\n') break;
105 if (!i) /* no newline found */
107 if (len > 0) i = line_pos; /* buffer is full, flush anyway */
108 else break;
110 WriteFile(dbg_houtput, line_buff, i, &w, NULL);
111 memmove( line_buff, line_buff + i, line_pos - i );
112 line_pos -= i;
116 int WINAPIV dbg_printf(const char* format, ...)
118 static char buf[4*1024];
119 va_list valist;
120 int len;
122 va_start(valist, format);
123 len = vsnprintf(buf, sizeof(buf), format, valist);
124 va_end(valist);
126 if (len <= -1 || len >= sizeof(buf))
128 len = sizeof(buf) - 1;
129 buf[len] = 0;
130 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
132 dbg_outputA(buf, len);
133 return len;
136 static unsigned dbg_load_internal_vars(void)
138 HKEY hkey;
139 DWORD type = REG_DWORD;
140 DWORD val;
141 DWORD count = sizeof(val);
142 int i;
143 struct dbg_internal_var* div = dbg_internal_vars;
145 /* initializes internal vars table */
146 #define INTERNAL_VAR(_var,_val,_ref,_tid) \
147 div->val = _val; div->name = #_var; div->pval = _ref; \
148 div->typeid = _tid; div++;
149 #include "intvar.h"
150 #undef INTERNAL_VAR
152 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
153 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
155 WINE_ERR("Cannot create WineDbg key in registry\n");
156 return FALSE;
159 for (i = 0; i < DBG_IV_LAST; i++)
161 if (!dbg_internal_vars[i].pval)
163 if (!RegQueryValueExA(hkey, dbg_internal_vars[i].name, 0,
164 &type, (LPBYTE)&val, &count))
165 dbg_internal_vars[i].val = val;
166 dbg_internal_vars[i].pval = &dbg_internal_vars[i].val;
169 RegCloseKey(hkey);
171 return TRUE;
174 static unsigned dbg_save_internal_vars(void)
176 HKEY hkey;
177 int i;
179 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
180 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
182 WINE_ERR("Cannot create WineDbg key in registry\n");
183 return FALSE;
186 for (i = 0; i < DBG_IV_LAST; i++)
188 /* FIXME: type should be inferred from basic type -if any- of intvar */
189 if (dbg_internal_vars[i].pval == &dbg_internal_vars[i].val)
191 DWORD val = dbg_internal_vars[i].val;
192 RegSetValueExA(hkey, dbg_internal_vars[i].name, 0, REG_DWORD, (BYTE *)&val, sizeof(val));
195 RegCloseKey(hkey);
196 return TRUE;
199 const struct dbg_internal_var* dbg_get_internal_var(const char* name)
201 const struct dbg_internal_var* div;
203 for (div = &dbg_internal_vars[DBG_IV_LAST - 1]; div >= dbg_internal_vars; div--)
205 if (!strcmp(div->name, name)) return div;
207 for (div = dbg_curr_process->be_cpu->context_vars; div->name; div++)
209 if (!strcasecmp(div->name, name))
211 struct dbg_internal_var* ret = (void*)lexeme_alloc_size(sizeof(*ret));
212 /* relocate register's field against current context */
213 *ret = *div;
214 ret->pval = (char*)&dbg_context + (DWORD_PTR)div->pval;
215 return ret;
219 return NULL;
222 unsigned dbg_num_processes(void)
224 return list_count(&dbg_process_list);
227 struct dbg_process* dbg_get_process(DWORD pid)
229 struct dbg_process* p;
231 LIST_FOR_EACH_ENTRY(p, &dbg_process_list, struct dbg_process, entry)
232 if (p->pid == pid) return p;
233 return NULL;
236 struct dbg_process* dbg_get_process_h(HANDLE h)
238 struct dbg_process* p;
240 LIST_FOR_EACH_ENTRY(p, &dbg_process_list, struct dbg_process, entry)
241 if (p->handle == h) return p;
242 return NULL;
245 #ifdef __i386__
246 extern struct backend_cpu be_i386;
247 #elif defined(__x86_64__)
248 extern struct backend_cpu be_i386;
249 extern struct backend_cpu be_x86_64;
250 #elif defined(__arm__) && !defined(__ARMEB__)
251 extern struct backend_cpu be_arm;
252 #elif defined(__aarch64__) && !defined(__AARCH64EB__)
253 extern struct backend_cpu be_arm64;
254 #else
255 # error CPU unknown
256 #endif
258 struct dbg_process* dbg_add_process(const struct be_process_io* pio, DWORD pid, HANDLE h)
260 struct dbg_process* p;
261 BOOL wow64;
263 if ((p = dbg_get_process(pid)))
264 return p;
266 if (!h)
267 h = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
269 if (!IsWow64Process(h, &wow64)) wow64 = FALSE;
271 if (!(p = malloc(sizeof(struct dbg_process)))) return NULL;
272 p->handle = h;
273 p->pid = pid;
274 p->process_io = pio;
275 p->pio_data = NULL;
276 p->imageName = NULL;
277 list_init(&p->threads);
278 list_init(&p->modules);
279 p->event_on_first_exception = NULL;
280 p->active_debuggee = FALSE;
281 p->is_wow64 = wow64;
282 p->next_bp = 1; /* breakpoint 0 is reserved for step-over */
283 memset(p->bp, 0, sizeof(p->bp));
284 p->delayed_bp = NULL;
285 p->num_delayed_bp = 0;
286 p->source_ofiles = NULL;
287 p->search_path = NULL;
288 p->source_current_file[0] = '\0';
289 p->source_start_line = -1;
290 p->source_end_line = -1;
291 p->data_model = NULL;
292 p->synthetized_types = NULL;
293 p->num_synthetized_types = 0;
295 list_add_head(&dbg_process_list, &p->entry);
297 #ifdef __i386__
298 p->be_cpu = &be_i386;
299 #elif defined(__x86_64__)
300 p->be_cpu = wow64 ? &be_i386 : &be_x86_64;
301 #elif defined(__arm__) && !defined(__ARMEB__)
302 p->be_cpu = &be_arm;
303 #elif defined(__aarch64__) && !defined(__AARCH64EB__)
304 p->be_cpu = &be_arm64;
305 #else
306 # error CPU unknown
307 #endif
308 return p;
311 void dbg_set_process_name(struct dbg_process* p, const WCHAR* imageName)
313 assert(p->imageName == NULL);
314 if (imageName) p->imageName = wcsdup(imageName);
317 void dbg_del_process(struct dbg_process* p)
319 struct dbg_thread* t;
320 struct dbg_thread* t2;
321 struct dbg_module* mod;
322 struct dbg_module* mod2;
323 int i;
325 LIST_FOR_EACH_ENTRY_SAFE(t, t2, &p->threads, struct dbg_thread, entry)
326 dbg_del_thread(t);
328 LIST_FOR_EACH_ENTRY_SAFE(mod, mod2, &p->modules, struct dbg_module, entry)
329 dbg_del_module(mod);
331 for (i = 0; i < p->num_delayed_bp; i++)
332 if (p->delayed_bp[i].is_symbol)
333 free(p->delayed_bp[i].u.symbol.name);
335 free(p->delayed_bp);
336 source_nuke_path(p);
337 source_free_files(p);
338 list_remove(&p->entry);
339 if (p == dbg_curr_process) dbg_curr_process = NULL;
340 if (p->event_on_first_exception) CloseHandle(p->event_on_first_exception);
341 free((char*)p->imageName);
342 free(p->synthetized_types);
343 free(p);
346 /******************************************************************
347 * dbg_init
349 * Initializes the dbghelp library, and also sets the application directory
350 * as a place holder for symbol searches.
352 BOOL dbg_init(HANDLE hProc, const WCHAR* in, BOOL invade)
354 BOOL ret;
356 ret = SymInitialize(hProc, NULL, invade);
357 if (ret && in)
359 const WCHAR* last;
361 for (last = in + lstrlenW(in) - 1; last >= in; last--)
363 if (*last == '/' || *last == '\\')
365 WCHAR* tmp;
366 tmp = malloc((1024 + 1 + (last - in) + 1) * sizeof(WCHAR));
367 if (tmp && SymGetSearchPathW(hProc, tmp, 1024))
369 WCHAR* x = tmp + lstrlenW(tmp);
371 *x++ = ';';
372 memcpy(x, in, (last - in) * sizeof(WCHAR));
373 x[last - in] = '\0';
374 ret = SymSetSearchPathW(hProc, tmp);
376 else ret = FALSE;
377 free(tmp);
378 break;
382 return ret;
385 BOOL dbg_load_module(HANDLE hProc, HANDLE hFile, const WCHAR* name, DWORD_PTR base, DWORD size)
387 struct dbg_process* pcs = dbg_get_process_h(hProc);
388 struct dbg_module* mod;
389 IMAGEHLP_MODULEW64 info;
390 HANDLE hMap;
391 void* image;
393 if (!pcs) return FALSE;
394 mod = malloc(sizeof(struct dbg_module));
395 if (!mod) return FALSE;
396 if (!SymLoadModuleExW(hProc, hFile, name, NULL, base, size, NULL, 0))
398 free(mod);
399 return FALSE;
401 mod->base = base;
402 list_add_head(&pcs->modules, &mod->entry);
404 mod->tls_index_offset = 0;
405 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)))
407 if ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)))
409 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(image);
410 const void* tlsdir;
411 ULONG sz;
413 tlsdir = RtlImageDirectoryEntryToData(image, TRUE, IMAGE_DIRECTORY_ENTRY_TLS, &sz);
414 switch (nth->OptionalHeader.Magic)
416 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
417 if (tlsdir && sz >= sizeof(IMAGE_TLS_DIRECTORY32))
418 mod->tls_index_offset = (const char*)tlsdir - (const char*)image +
419 offsetof(IMAGE_TLS_DIRECTORY32, AddressOfIndex);
420 break;
421 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
422 if (tlsdir && sz >= sizeof(IMAGE_TLS_DIRECTORY64))
423 mod->tls_index_offset = (const char*)tlsdir - (const char*)image +
424 offsetof(IMAGE_TLS_DIRECTORY64, AddressOfIndex);
425 break;
427 UnmapViewOfFile(image);
429 CloseHandle(hMap);
431 info.SizeOfStruct = sizeof(info);
432 if (SymGetModuleInfoW64(hProc, base, &info))
433 if (info.PdbUnmatched || info.DbgUnmatched)
434 dbg_printf("Loaded unmatched debug information for %s\n", wine_dbgstr_w(name));
436 return TRUE;
439 void dbg_del_module(struct dbg_module* mod)
441 list_remove(&mod->entry);
442 free(mod);
445 struct dbg_module* dbg_get_module(struct dbg_process* pcs, DWORD_PTR base)
447 struct dbg_module* mod;
449 if (!pcs)
450 return NULL;
451 LIST_FOR_EACH_ENTRY(mod, &pcs->modules, struct dbg_module, entry)
452 if (mod->base == base)
453 return mod;
454 return NULL;
457 BOOL dbg_unload_module(struct dbg_process* pcs, DWORD_PTR base)
459 struct dbg_module* mod = dbg_get_module(pcs, base);
461 types_unload_module(pcs, base);
462 SymUnloadModule64(pcs->handle, base);
463 dbg_del_module(mod);
465 return !!mod;
468 struct dbg_thread* dbg_get_thread(struct dbg_process* p, DWORD tid)
470 struct dbg_thread* t;
472 if (!p) return NULL;
473 LIST_FOR_EACH_ENTRY(t, &p->threads, struct dbg_thread, entry)
474 if (t->tid == tid) return t;
475 return NULL;
478 struct dbg_thread* dbg_add_thread(struct dbg_process* p, DWORD tid,
479 HANDLE h, void* teb)
481 struct dbg_thread* t = malloc(sizeof(struct dbg_thread));
483 if (!t)
484 return NULL;
486 t->handle = h;
487 t->tid = tid;
488 t->teb = teb;
489 t->process = p;
490 t->exec_mode = dbg_exec_cont;
491 t->exec_count = 0;
492 t->step_over_bp.enabled = FALSE;
493 t->step_over_bp.refcount = 0;
494 t->stopped_xpoint = -1;
495 t->name[0] = '\0';
496 t->in_exception = FALSE;
497 t->frames = NULL;
498 t->num_frames = 0;
499 t->curr_frame = -1;
500 t->addr_mode = AddrModeFlat;
501 t->suspended = FALSE;
503 list_add_head(&p->threads, &t->entry);
505 return t;
508 void dbg_del_thread(struct dbg_thread* t)
510 free(t->frames);
511 list_remove(&t->entry);
512 if (t == dbg_curr_thread) dbg_curr_thread = NULL;
513 free(t);
516 void dbg_set_option(const char* option, const char* val)
518 if (!strcasecmp(option, "module_load_mismatched"))
520 DWORD opt = SymGetOptions();
521 if (!val)
522 dbg_printf("Option: module_load_mismatched %s\n", opt & SYMOPT_LOAD_ANYTHING ? "true" : "false");
523 else if (!strcasecmp(val, "true")) opt |= SYMOPT_LOAD_ANYTHING;
524 else if (!strcasecmp(val, "false")) opt &= ~SYMOPT_LOAD_ANYTHING;
525 else
527 dbg_printf("Syntax: module_load_mismatched [true|false]\n");
528 return;
530 SymSetOptions(opt);
532 else if (!strcasecmp(option, "symbol_picker"))
534 if (!val)
535 dbg_printf("Option: symbol_picker %s\n",
536 symbol_current_picker == symbol_picker_interactive ? "interactive" : "scoped");
537 else if (!strcasecmp(val, "interactive"))
538 symbol_current_picker = symbol_picker_interactive;
539 else if (!strcasecmp(val, "scoped"))
540 symbol_current_picker = symbol_picker_scoped;
541 else
543 dbg_printf("Syntax: symbol_picker [interactive|scoped]\n");
544 return;
547 else if (!strcasecmp(option, "data_model"))
549 if (!dbg_curr_process)
551 dbg_printf("Not attached to a process\n");
552 return;
554 if (!val)
556 const char* model = "";
557 if (dbg_curr_process->data_model == NULL) model = "auto";
558 else if (dbg_curr_process->data_model == ilp32_data_model) model = "ilp32";
559 else if (dbg_curr_process->data_model == llp64_data_model) model = "llp64";
560 else if (dbg_curr_process->data_model == lp64_data_model) model = "lp64";
561 dbg_printf("Option: data_model %s\n", model);
563 else if (!strcasecmp(val, "auto")) dbg_curr_process->data_model = NULL;
564 else if (!strcasecmp(val, "ilp32")) dbg_curr_process->data_model = ilp32_data_model;
565 else if (!strcasecmp(val, "llp64")) dbg_curr_process->data_model = llp64_data_model;
566 else if (!strcasecmp(val, "lp64")) dbg_curr_process->data_model = lp64_data_model;
567 else
568 dbg_printf("Unknown data model %s\n", val);
570 else dbg_printf("Unknown option '%s'\n", option);
573 BOOL dbg_interrupt_debuggee(void)
575 struct dbg_process* p;
576 if (list_empty(&dbg_process_list)) return FALSE;
577 /* FIXME: since we likely have a single process, signal the first process
578 * in list
580 p = LIST_ENTRY(list_head(&dbg_process_list), struct dbg_process, entry);
581 if (list_next(&dbg_process_list, &p->entry)) dbg_printf("Ctrl-C: only stopping the first process\n");
582 else dbg_printf("Ctrl-C: stopping debuggee\n");
583 if (p->event_on_first_exception)
585 SetEvent(p->event_on_first_exception);
586 CloseHandle(p->event_on_first_exception);
587 p->event_on_first_exception = NULL;
589 return DebugBreakProcess(p->handle);
592 static BOOL WINAPI ctrl_c_handler(DWORD dwCtrlType)
594 if (dwCtrlType == CTRL_C_EVENT)
596 return dbg_interrupt_debuggee();
598 return FALSE;
601 void dbg_init_console(void)
603 /* set the output handle */
604 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
606 /* set our control-C handler */
607 SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
609 /* set our own title */
610 SetConsoleTitleA("Wine Debugger");
613 static int dbg_winedbg_usage(BOOL advanced)
615 if (advanced)
617 dbg_printf("Usage:\n"
618 " winedbg <cmdline> launch process <cmdline> (as if you were starting\n"
619 " it with wine) and run WineDbg on it\n"
620 " winedbg <num> attach to running process of wpid <num> and run\n"
621 " WineDbg on it\n"
622 " winedbg --gdb <cmdline> launch process <cmdline> (as if you were starting\n"
623 " wine) and run gdb (proxied) on it\n"
624 " winedbg --gdb <num> attach to running process of wpid <num> and run\n"
625 " gdb (proxied) on it\n"
626 " winedbg <file.mdmp> reload the minidump <file.mdmp> into memory and run\n"
627 " WineDbg on it\n"
628 " winedbg --help prints advanced options\n");
630 else
631 dbg_printf("Usage:\n\twinedbg [ [ --gdb ] [ <prog-name> [ <prog-args> ] | <num> | <file.mdmp> | --help ]\n");
632 return 0;
635 void dbg_start_interactive(const char* filename, HANDLE hFile)
637 struct dbg_process* p;
638 struct dbg_process* p2;
640 if (dbg_curr_process)
642 dbg_printf("WineDbg starting on pid %04lx\n", dbg_curr_pid);
643 if (dbg_curr_process->active_debuggee) dbg_active_wait_for_first_exception();
646 dbg_interactiveP = TRUE;
647 parser_handle(filename, hFile);
649 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &dbg_process_list, struct dbg_process, entry)
650 p->process_io->close_process(p, FALSE);
652 dbg_save_internal_vars();
655 static LONG CALLBACK top_filter( EXCEPTION_POINTERS *ptr )
657 dbg_printf( "winedbg: Internal crash at %p\n", ptr->ExceptionRecord->ExceptionAddress );
658 return EXCEPTION_EXECUTE_HANDLER;
661 static void restart_if_wow64(void)
663 BOOL is_wow64;
665 if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
667 STARTUPINFOW si;
668 PROCESS_INFORMATION pi;
669 WCHAR filename[MAX_PATH];
670 void *redir;
671 DWORD exit_code;
673 memset( &si, 0, sizeof(si) );
674 si.cb = sizeof(si);
675 GetSystemDirectoryW( filename, MAX_PATH );
676 lstrcatW( filename, L"\\winedbg.exe" );
678 Wow64DisableWow64FsRedirection( &redir );
679 if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
681 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
682 SetConsoleCtrlHandler( NULL, TRUE ); /* Ignore ^C */
683 WaitForSingleObject( pi.hProcess, INFINITE );
684 GetExitCodeProcess( pi.hProcess, &exit_code );
685 ExitProcess( exit_code );
687 else WINE_ERR( "failed to restart 64-bit %s, err %ld\n", wine_dbgstr_w(filename), GetLastError() );
688 Wow64RevertWow64FsRedirection( redir );
692 int main(int argc, char** argv)
694 int retv = 0;
695 HANDLE hFile = INVALID_HANDLE_VALUE;
696 enum dbg_start ds;
697 const char* filename = NULL;
699 /* Initialize the output */
700 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
702 SetUnhandledExceptionFilter( top_filter );
704 /* Initialize internal vars */
705 if (!dbg_load_internal_vars()) return -1;
707 /* as we don't care about exec name */
708 argc--; argv++;
710 if (argc && !strcmp(argv[0], "--help"))
711 return dbg_winedbg_usage(TRUE);
713 if (argc && !strcmp(argv[0], "--gdb"))
715 restart_if_wow64();
716 retv = gdb_main(argc, argv);
717 if (retv == -1) dbg_winedbg_usage(FALSE);
718 return retv;
720 dbg_init_console();
722 SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
723 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS |
724 SYMOPT_INCLUDE_32BIT_MODULES);
726 SymSetExtendedOption(SYMOPT_EX_WINE_EXTENSION_API, TRUE);
727 SymSetExtendedOption(SYMOPT_EX_WINE_SOURCE_ACTUAL_PATH, TRUE);
729 if (argc && !strcmp(argv[0], "--auto"))
731 switch (dbg_active_auto(argc, argv))
733 case start_ok: return 0;
734 case start_error_parse: return dbg_winedbg_usage(FALSE);
735 case start_error_init: return -1;
738 if (argc && !strcmp(argv[0], "--minidump"))
740 switch (dbg_active_minidump(argc, argv))
742 case start_ok: return 0;
743 case start_error_parse: return dbg_winedbg_usage(FALSE);
744 case start_error_init: return -1;
747 /* parse options */
748 while (argc > 0 && argv[0][0] == '-')
750 if (!strcmp(argv[0], "--command") && argc > 1)
752 argc--; argv++;
753 hFile = parser_generate_command_file(argv[0], NULL);
754 if (hFile == INVALID_HANDLE_VALUE)
756 dbg_printf("Couldn't open temp file (%lu)\n", GetLastError());
757 return 1;
759 argc--; argv++;
760 continue;
762 if (!strcmp(argv[0], "--file") && argc > 1)
764 argc--; argv++;
765 filename = argv[0];
766 hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0,
767 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
768 if (hFile == INVALID_HANDLE_VALUE)
770 dbg_printf("Couldn't open file %s (%lu)\n", argv[0], GetLastError());
771 return 1;
773 argc--; argv++;
774 continue;
776 if (!strcmp(argv[0], "--"))
778 argc--; argv++;
779 break;
781 return dbg_winedbg_usage(FALSE);
783 if (!argc) ds = start_ok;
784 else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse &&
785 (ds = minidump_reload(argc, argv)) == start_error_parse)
786 ds = dbg_active_launch(argc, argv);
787 switch (ds)
789 case start_ok: break;
790 case start_error_parse: return dbg_winedbg_usage(FALSE);
791 case start_error_init: return -1;
794 restart_if_wow64();
796 dbg_start_interactive(filename, hFile);
798 return 0;