Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 1 | /* -*- tab-width: 8; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* Wine internal debugger |
| 4 | * Interface to Windows debugger API |
| 5 | * Eric Pouech (c) 2000 |
| 6 | */ |
| 7 | |
| 8 | #include <stdlib.h> |
Alexandre Julliard | 58d5a0a | 2000-07-29 21:58:17 +0000 | [diff] [blame] | 9 | #include <stdarg.h> |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 11 | #include <string.h> |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 12 | #include "debugger.h" |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 13 | |
Alexandre Julliard | 09d7822 | 2001-05-05 00:44:48 +0000 | [diff] [blame] | 14 | #include "ntddk.h" |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 15 | #include "thread.h" |
Alexandre Julliard | f818d42 | 2000-05-03 17:48:21 +0000 | [diff] [blame] | 16 | #include "wincon.h" |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 17 | #include "wingdi.h" |
| 18 | #include "winuser.h" |
| 19 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 20 | #include "winreg.h" |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 21 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 22 | DBG_PROCESS* DEBUG_CurrProcess = NULL; |
| 23 | DBG_THREAD* DEBUG_CurrThread = NULL; |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 24 | DWORD DEBUG_CurrTid; |
| 25 | DWORD DEBUG_CurrPid; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 26 | CONTEXT DEBUG_context; |
Eric Pouech | 52c7534 | 2001-05-03 18:32:47 +0000 | [diff] [blame] | 27 | BOOL DEBUG_interactiveP = FALSE; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 28 | int curr_frame = 0; |
| 29 | static char* DEBUG_LastCmdLine = NULL; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 30 | |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 31 | static DBG_PROCESS* DEBUG_ProcessList = NULL; |
Alexandre Julliard | f5a8b96 | 2001-10-22 19:00:34 +0000 | [diff] [blame] | 32 | static int automatic_mode; |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 33 | DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST]; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 34 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 35 | void DEBUG_Output(int chn, const char* buffer, int len) |
| 36 | { |
| 37 | if (DBG_IVAR(ConChannelMask) & chn) |
| 38 | WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); |
| 39 | if (DBG_IVAR(StdChannelMask) & chn) |
| 40 | fwrite(buffer, len, 1, stderr); |
| 41 | } |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 42 | |
| 43 | int DEBUG_Printf(int chn, const char* format, ...) |
| 44 | { |
Eric Pouech | 52c7534 | 2001-05-03 18:32:47 +0000 | [diff] [blame] | 45 | static char buf[4*1024]; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 46 | va_list valist; |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 47 | int len; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 48 | |
| 49 | va_start(valist, format); |
Alexandre Julliard | 58d5a0a | 2000-07-29 21:58:17 +0000 | [diff] [blame] | 50 | len = vsnprintf(buf, sizeof(buf), format, valist); |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 51 | va_end(valist); |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 52 | |
| 53 | if (len <= -1) { |
| 54 | len = sizeof(buf) - 1; |
| 55 | buf[len] = 0; |
| 56 | buf[len - 1] = buf[len - 2] = buf[len - 3] = '.'; |
| 57 | } |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 58 | DEBUG_Output(chn, buf, len); |
| 59 | return len; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 60 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 61 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 62 | static BOOL DEBUG_IntVarsRW(int read) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 63 | { |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 64 | HKEY hkey; |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 65 | DWORD type = REG_DWORD; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 66 | DWORD val; |
| 67 | DWORD count = sizeof(val); |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 68 | int i; |
| 69 | DBG_INTVAR* div = DEBUG_IntVars; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 70 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 71 | if (read) { |
| 72 | /* initializes internal vars table */ |
| 73 | #define INTERNAL_VAR(_var,_val,_ref,_typ) \ |
| 74 | div->val = _val; div->name = #_var; div->pval = _ref; \ |
Eric Pouech | 02ecb68 | 2001-12-21 20:29:58 +0000 | [diff] [blame^] | 75 | div->type = DEBUG_GetBasicType(_typ); div++; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 76 | #include "intvar.h" |
| 77 | #undef INTERNAL_VAR |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 80 | if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) { |
Eric Pouech | 26342c9 | 2000-05-01 14:22:02 +0000 | [diff] [blame] | 81 | /* since the IVars are not yet setup, DEBUG_Printf doesn't work, |
| 82 | * so don't use it */ |
| 83 | fprintf(stderr, "Cannot create WineDbg key in registry\n"); |
| 84 | return FALSE; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 85 | } |
Eric Pouech | 26342c9 | 2000-05-01 14:22:02 +0000 | [diff] [blame] | 86 | |
| 87 | for (i = 0; i < DBG_IV_LAST; i++) { |
| 88 | if (read) { |
| 89 | if (!DEBUG_IntVars[i].pval) { |
| 90 | if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0, |
| 91 | &type, (LPSTR)&val, &count)) |
| 92 | DEBUG_IntVars[i].val = val; |
| 93 | DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val; |
| 94 | } else { |
| 95 | *DEBUG_IntVars[i].pval = 0; |
| 96 | } |
| 97 | } else { |
| 98 | /* FIXME: type should be infered from basic type -if any- of intvar */ |
| 99 | if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val) |
| 100 | RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0, |
| 101 | type, (LPCVOID)DEBUG_IntVars[i].pval, count); |
| 102 | } |
| 103 | } |
| 104 | RegCloseKey(hkey); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 105 | return TRUE; |
| 106 | } |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 107 | |
| 108 | DBG_INTVAR* DEBUG_GetIntVar(const char* name) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < DBG_IV_LAST; i++) { |
| 113 | if (!strcmp(DEBUG_IntVars[i].name, name)) |
| 114 | return &DEBUG_IntVars[i]; |
| 115 | } |
| 116 | return NULL; |
| 117 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 118 | |
| 119 | static WINE_EXCEPTION_FILTER(wine_dbg) |
| 120 | { |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 121 | DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode()); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 122 | DEBUG_ExternalDebugger(); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 123 | return EXCEPTION_EXECUTE_HANDLER; |
| 124 | } |
| 125 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 126 | DBG_PROCESS* DEBUG_GetProcess(DWORD pid) |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 127 | { |
| 128 | DBG_PROCESS* p; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 129 | |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 130 | for (p = DEBUG_ProcessList; p; p = p->next) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 131 | if (p->pid == pid) break; |
| 132 | return p; |
| 133 | } |
| 134 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 135 | static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 136 | { |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 137 | DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS)); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 138 | if (!p) |
| 139 | return NULL; |
| 140 | p->handle = h; |
| 141 | p->pid = pid; |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 142 | p->imageName = imageName ? DBG_strdup(imageName) : NULL; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 143 | p->threads = NULL; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 144 | p->num_threads = 0; |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 145 | p->continue_on_first_exception = FALSE; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 146 | p->modules = NULL; |
Eric Pouech | e939206 | 2000-11-15 22:16:26 +0000 | [diff] [blame] | 147 | p->num_modules = 0; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 148 | p->next_index = 0; |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 149 | p->dbg_hdr_addr = 0; |
Eric Pouech | eaafb73 | 2001-05-21 18:33:15 +0000 | [diff] [blame] | 150 | p->delayed_bp = NULL; |
| 151 | p->num_delayed_bp = 0; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 152 | |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 153 | p->next = DEBUG_ProcessList; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 154 | p->prev = NULL; |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 155 | if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p; |
| 156 | DEBUG_ProcessList = p; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 157 | return p; |
| 158 | } |
| 159 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 160 | static void DEBUG_DelThread(DBG_THREAD* p); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 161 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 162 | static void DEBUG_DelProcess(DBG_PROCESS* p) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 163 | { |
Eric Pouech | eaafb73 | 2001-05-21 18:33:15 +0000 | [diff] [blame] | 164 | int i; |
| 165 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 166 | if (p->threads != NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 167 | DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 168 | while (p->threads) DEBUG_DelThread(p->threads); |
| 169 | } |
Eric Pouech | eaafb73 | 2001-05-21 18:33:15 +0000 | [diff] [blame] | 170 | for (i = 0; i < p->num_delayed_bp; i++) { |
| 171 | DBG_free(p->delayed_bp[i].name); |
| 172 | } |
| 173 | DBG_free(p->delayed_bp); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 174 | if (p->prev) p->prev->next = p->next; |
| 175 | if (p->next) p->next->prev = p->prev; |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 176 | if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 177 | if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL; |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 178 | DBG_free((char*)p->imageName); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 179 | DBG_free(p); |
| 180 | } |
| 181 | |
| 182 | static void DEBUG_InitCurrProcess(void) |
| 183 | { |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Julliard | 0966a6d | 2000-03-10 22:25:07 +0000 | [diff] [blame] | 186 | static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr) |
| 187 | { |
| 188 | DWORD sz; |
| 189 | *(WCHAR*)buffer = 0; |
| 190 | return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz)); |
| 191 | } |
| 192 | |
| 193 | static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 194 | { |
| 195 | LPVOID ad; |
| 196 | DWORD sz; |
| 197 | |
| 198 | if ( addr |
| 199 | && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz) |
| 200 | && sz == sizeof(ad) |
| 201 | && ad |
| 202 | && ReadProcessMemory(hp, ad, buffer, size, &sz)) |
| 203 | return TRUE; |
| 204 | *(WCHAR*)buffer = 0; |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 208 | DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 209 | { |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 210 | DBG_THREAD* t; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 211 | |
| 212 | for (t = p->threads; t; t = t->next) |
| 213 | if (t->tid == tid) break; |
| 214 | return t; |
| 215 | } |
| 216 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 217 | static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid, |
| 218 | HANDLE h, LPVOID start, LPVOID teb) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 219 | { |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 220 | DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD)); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 221 | if (!t) |
| 222 | return NULL; |
| 223 | |
| 224 | t->handle = h; |
| 225 | t->tid = tid; |
| 226 | t->start = start; |
| 227 | t->teb = teb; |
| 228 | t->process = p; |
| 229 | t->wait_for_first_exception = 0; |
| 230 | t->dbg_exec_mode = EXEC_CONT; |
| 231 | t->dbg_exec_count = 0; |
Eric Pouech | 2b713ba | 2001-04-13 22:26:23 +0000 | [diff] [blame] | 232 | sprintf(t->name, "%08lx", tid); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 233 | |
| 234 | p->num_threads++; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 235 | t->next = p->threads; |
| 236 | t->prev = NULL; |
| 237 | if (p->threads) p->threads->prev = t; |
| 238 | p->threads = t; |
| 239 | |
| 240 | return t; |
| 241 | } |
| 242 | |
| 243 | static void DEBUG_InitCurrThread(void) |
| 244 | { |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 245 | if (DEBUG_CurrThread->start) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 246 | if (DEBUG_CurrThread->process->num_threads == 1 || |
| 247 | DBG_IVAR(BreakAllThreadsStartup)) { |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 248 | DBG_VALUE value; |
| 249 | |
| 250 | DEBUG_SetBreakpoints(FALSE); |
| 251 | value.type = NULL; |
| 252 | value.cookie = DV_TARGET; |
| 253 | value.addr.seg = 0; |
| 254 | value.addr.off = (DWORD)DEBUG_CurrThread->start; |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 255 | DEBUG_AddBreakpoint(&value, NULL); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 256 | DEBUG_SetBreakpoints(TRUE); |
| 257 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 258 | } else { |
| 259 | DEBUG_CurrThread->wait_for_first_exception = 1; |
| 260 | } |
| 261 | } |
| 262 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 263 | static void DEBUG_DelThread(DBG_THREAD* t) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 264 | { |
| 265 | if (t->prev) t->prev->next = t->next; |
| 266 | if (t->next) t->next->prev = t->prev; |
| 267 | if (t == t->process->threads) t->process->threads = t->next; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 268 | t->process->num_threads--; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 269 | if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 270 | DBG_free(t); |
| 271 | } |
| 272 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 273 | BOOL DEBUG_Attach(DWORD pid, BOOL cofe) |
| 274 | { |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 275 | if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 276 | |
| 277 | if (!DebugActiveProcess(pid)) { |
Alexandre Julliard | 1ce1bef | 2001-07-11 17:33:08 +0000 | [diff] [blame] | 278 | DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError()); |
| 279 | DEBUG_DelProcess(DEBUG_CurrProcess); |
| 280 | DEBUG_CurrProcess = NULL; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 281 | return FALSE; |
| 282 | } |
| 283 | DEBUG_CurrProcess->continue_on_first_exception = cofe; |
| 284 | return TRUE; |
| 285 | } |
| 286 | |
| 287 | static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code) |
| 288 | { |
| 289 | DBG_ADDR addr; |
| 290 | int newmode; |
| 291 | |
| 292 | DEBUG_GetCurrentAddress(&addr); |
| 293 | DEBUG_SuspendExecution(); |
| 294 | |
| 295 | if (!is_debug) { |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 296 | if (!addr.seg) |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 297 | DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 298 | else |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 299 | switch(DEBUG_GetSelectorType(addr.seg)) |
| 300 | { |
| 301 | case MODE_32: |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 302 | DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off); |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 303 | break; |
| 304 | case MODE_16: |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 305 | DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off); |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 306 | break; |
| 307 | case MODE_VM86: |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 308 | DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off); |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 309 | break; |
| 310 | case MODE_INVALID: |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 311 | DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg); |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 314 | DEBUG_Printf(DBG_CHN_MESG, ".\n"); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 315 | } |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 316 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 317 | DEBUG_LoadEntryPoints("Loading new modules symbols:\n"); |
| 318 | |
| 319 | if (!force && is_debug && |
James Abbatiello | ebef9a9 | 2000-09-01 01:22:05 +0000 | [diff] [blame] | 320 | DEBUG_ShouldContinue(&addr, |
| 321 | code, |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 322 | DEBUG_CurrThread->dbg_exec_mode, |
| 323 | &DEBUG_CurrThread->dbg_exec_count)) |
| 324 | return FALSE; |
| 325 | |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 326 | if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 327 | if (newmode != DEBUG_CurrThread->dbg_mode) |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 328 | { |
| 329 | static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" }; |
| 330 | DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] ); |
| 331 | DEBUG_CurrThread->dbg_mode = newmode; |
| 332 | } |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 333 | |
| 334 | DEBUG_DoDisplay(); |
| 335 | |
| 336 | if (is_debug || force) { |
| 337 | /* |
| 338 | * Do a quiet backtrace so that we have an idea of what the situation |
| 339 | * is WRT the source files. |
| 340 | */ |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 341 | DEBUG_BackTrace(DEBUG_CurrTid, FALSE); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 342 | } else { |
| 343 | /* This is a real crash, dump some info */ |
| 344 | DEBUG_InfoRegisters(); |
| 345 | DEBUG_InfoStack(); |
| 346 | #ifdef __i386__ |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 347 | if (DEBUG_CurrThread->dbg_mode == MODE_16) { |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 348 | DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1); |
| 349 | if (DEBUG_context.SegEs != DEBUG_context.SegDs) |
| 350 | DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1); |
| 351 | } |
| 352 | DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1); |
| 353 | #endif |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 354 | DEBUG_BackTrace(DEBUG_CurrTid, TRUE); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (!is_debug || |
| 358 | (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) || |
| 359 | (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) { |
Eric Pouech | 71189b5 | 2000-07-25 12:51:56 +0000 | [diff] [blame] | 360 | |
| 361 | struct list_id list; |
| 362 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 363 | /* Show where we crashed */ |
| 364 | curr_frame = 0; |
Eric Pouech | 71189b5 | 2000-07-25 12:51:56 +0000 | [diff] [blame] | 365 | DEBUG_DisassembleInstruction(&addr); |
| 366 | |
| 367 | /* resets list internal arguments so we can look at source code when needed */ |
| 368 | DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list); |
| 369 | if (list.sourcefile) DEBUG_List(&list, NULL, 0); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 370 | } |
| 371 | return TRUE; |
| 372 | } |
| 373 | |
| 374 | static DWORD DEBUG_ExceptionEpilog(void) |
| 375 | { |
| 376 | DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode, |
| 377 | DEBUG_CurrThread->dbg_exec_count); |
| 378 | /* |
| 379 | * This will have gotten absorbed into the breakpoint info |
| 380 | * if it was used. Otherwise it would have been ignored. |
| 381 | * In any case, we don't mess with it any more. |
| 382 | */ |
| 383 | if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) |
| 384 | DEBUG_CurrThread->dbg_exec_count = 0; |
| 385 | |
| 386 | return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE; |
| 387 | } |
| 388 | |
| 389 | static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 390 | { |
James Hatheway | f552359 | 2001-04-09 18:31:47 +0000 | [diff] [blame] | 391 | BOOL is_debug = FALSE; |
| 392 | BOOL ret = TRUE; |
| 393 | THREADNAME_INFO *pThreadName; |
| 394 | DBG_THREAD *pThread; |
| 395 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 396 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 397 | *cont = DBG_CONTINUE; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 398 | |
| 399 | switch (rec->ExceptionCode) |
| 400 | { |
| 401 | case EXCEPTION_BREAKPOINT: |
| 402 | case EXCEPTION_SINGLE_STEP: |
| 403 | is_debug = TRUE; |
| 404 | break; |
James Hatheway | f552359 | 2001-04-09 18:31:47 +0000 | [diff] [blame] | 405 | case EXCEPTION_NAME_THREAD: |
| 406 | pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation); |
| 407 | if (pThreadName->dwThreadID == -1) |
| 408 | pThread = DEBUG_CurrThread; |
| 409 | else |
| 410 | pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID); |
| 411 | |
| 412 | if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName, |
| 413 | pThread->name, 9, NULL)) |
| 414 | DEBUG_Printf (DBG_CHN_MESG, |
| 415 | "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n", |
| 416 | pThread->tid, pThread->name); |
| 417 | return TRUE; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Alexandre Julliard | 840434a | 2001-10-04 16:17:16 +0000 | [diff] [blame] | 420 | if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance)) |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 421 | { |
| 422 | /* pass exception to program except for debug exceptions */ |
| 423 | *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED; |
| 424 | return TRUE; |
| 425 | } |
| 426 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 427 | if (!is_debug) |
| 428 | { |
| 429 | /* print some infos */ |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 430 | DEBUG_Printf(DBG_CHN_MESG, "%s: ", |
| 431 | first_chance ? "First chance exception" : "Unhandled exception"); |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 432 | switch (rec->ExceptionCode) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 433 | { |
| 434 | case EXCEPTION_INT_DIVIDE_BY_ZERO: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 435 | DEBUG_Printf(DBG_CHN_MESG, "divide by zero"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 436 | break; |
| 437 | case EXCEPTION_INT_OVERFLOW: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 438 | DEBUG_Printf(DBG_CHN_MESG, "overflow"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 439 | break; |
| 440 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 441 | DEBUG_Printf(DBG_CHN_MESG, "array bounds "); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 442 | break; |
| 443 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 444 | DEBUG_Printf(DBG_CHN_MESG, "illegal instruction"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 445 | break; |
| 446 | case EXCEPTION_STACK_OVERFLOW: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 447 | DEBUG_Printf(DBG_CHN_MESG, "stack overflow"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 448 | break; |
| 449 | case EXCEPTION_PRIV_INSTRUCTION: |
Andreas Mohr | e15badb | 2001-10-21 15:18:15 +0000 | [diff] [blame] | 450 | DEBUG_Printf(DBG_CHN_MESG, "privileged instruction"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 451 | break; |
| 452 | case EXCEPTION_ACCESS_VIOLATION: |
| 453 | if (rec->NumberParameters == 2) |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 454 | DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx", |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 455 | rec->ExceptionInformation[0] ? "write" : "read", |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 456 | rec->ExceptionInformation[1]); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 457 | else |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 458 | DEBUG_Printf(DBG_CHN_MESG, "page fault"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 459 | break; |
| 460 | case EXCEPTION_DATATYPE_MISALIGNMENT: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 461 | DEBUG_Printf(DBG_CHN_MESG, "Alignment"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 462 | break; |
Eric Pouech | d0a0493 | 2001-11-23 23:10:08 +0000 | [diff] [blame] | 463 | case DBG_CONTROL_C: |
| 464 | DEBUG_Printf(DBG_CHN_MESG, "^C"); |
| 465 | break; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 466 | case CONTROL_C_EXIT: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 467 | DEBUG_Printf(DBG_CHN_MESG, "^C"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 468 | break; |
| 469 | case EXCEPTION_CRITICAL_SECTION_WAIT: |
Eric Pouech | c2b5cb3 | 2001-08-07 19:32:14 +0000 | [diff] [blame] | 470 | { |
| 471 | DBG_ADDR addr; |
| 472 | |
| 473 | addr.seg = 0; |
| 474 | addr.off = rec->ExceptionInformation[0]; |
| 475 | |
| 476 | DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section "); |
| 477 | DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE); |
| 478 | } |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 479 | if (!DBG_IVAR(BreakOnCritSectTimeOut)) |
Andreas Mohr | 20cd935 | 2000-09-12 23:40:40 +0000 | [diff] [blame] | 480 | { |
| 481 | DEBUG_Printf(DBG_CHN_MESG, "\n"); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 482 | return TRUE; |
Andreas Mohr | 20cd935 | 2000-09-12 23:40:40 +0000 | [diff] [blame] | 483 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 484 | break; |
Alexandre Julliard | 634824b | 2000-10-26 22:03:34 +0000 | [diff] [blame] | 485 | case EXCEPTION_WINE_STUB: |
| 486 | { |
| 487 | char dll[32], name[64]; |
| 488 | DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle, |
| 489 | (char *)rec->ExceptionInformation[0] ); |
| 490 | DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle, |
| 491 | (char *)rec->ExceptionInformation[1] ); |
| 492 | DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name ); |
| 493 | } |
| 494 | break; |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 495 | case EXCEPTION_VM86_INTx: |
| 496 | DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode", |
| 497 | rec->ExceptionInformation[0]); |
| 498 | break; |
| 499 | case EXCEPTION_VM86_STI: |
| 500 | DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode"); |
| 501 | break; |
| 502 | case EXCEPTION_VM86_PICRETURN: |
| 503 | DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode"); |
| 504 | break; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 505 | default: |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 506 | DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 511 | #if 0 |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 512 | DEBUG_Printf(DBG_CHN_TRACE, |
| 513 | "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n", |
Ulrich Weigand | 1cbf27a | 2000-06-04 01:33:21 +0000 | [diff] [blame] | 514 | #ifdef __i386__ |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 515 | DEBUG_context.Eip, DEBUG_context.EFlags, |
Ulrich Weigand | 1cbf27a | 2000-06-04 01:33:21 +0000 | [diff] [blame] | 516 | #else |
| 517 | 0L, 0L, |
| 518 | #endif |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 519 | DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count); |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 520 | #endif |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 521 | |
Alexandre Julliard | f5a8b96 | 2001-10-22 19:00:34 +0000 | [diff] [blame] | 522 | if (automatic_mode) |
| 523 | { |
| 524 | DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode); |
| 525 | return FALSE; /* terminate execution */ |
| 526 | } |
| 527 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 528 | if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) { |
Eric Pouech | 52c7534 | 2001-05-03 18:32:47 +0000 | [diff] [blame] | 529 | DEBUG_interactiveP = TRUE; |
Eric Pouech | 5b471ba | 2000-06-25 12:49:13 +0000 | [diff] [blame] | 530 | while ((ret = DEBUG_Parser())) { |
| 531 | if (DEBUG_ValidateRegisters()) { |
| 532 | if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance) |
| 533 | break; |
| 534 | DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n"); |
| 535 | } |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 536 | } |
Eric Pouech | 52c7534 | 2001-05-03 18:32:47 +0000 | [diff] [blame] | 537 | DEBUG_interactiveP = FALSE; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 538 | } |
| 539 | *cont = DEBUG_ExceptionEpilog(); |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 540 | |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 541 | #if 0 |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 542 | DEBUG_Printf(DBG_CHN_TRACE, |
| 543 | "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n", |
Ulrich Weigand | 1cbf27a | 2000-06-04 01:33:21 +0000 | [diff] [blame] | 544 | #ifdef __i386__ |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 545 | DEBUG_context.Eip, DEBUG_context.EFlags, |
Ulrich Weigand | 1cbf27a | 2000-06-04 01:33:21 +0000 | [diff] [blame] | 546 | #else |
| 547 | 0L, 0L, |
| 548 | #endif |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 549 | DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count); |
Eric Pouech | 38f2be4 | 2001-08-15 17:40:31 +0000 | [diff] [blame] | 550 | #endif |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 555 | static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 556 | { |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 557 | char buffer[256]; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 558 | BOOL ret; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 559 | |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 560 | DEBUG_CurrPid = de->dwProcessId; |
| 561 | DEBUG_CurrTid = de->dwThreadId; |
| 562 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 563 | __TRY { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 564 | ret = TRUE; |
| 565 | *cont = 0L; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 566 | |
| 567 | if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) |
| 568 | DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 569 | else |
| 570 | DEBUG_CurrThread = NULL; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 571 | |
| 572 | switch (de->dwDebugEventCode) { |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 573 | case EXCEPTION_DEBUG_EVENT: |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 574 | if (!DEBUG_CurrThread) { |
| 575 | DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n", |
| 576 | de->dwProcessId, de->dwThreadId); |
| 577 | break; |
| 578 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 579 | |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 580 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n", |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 581 | de->dwProcessId, de->dwThreadId, |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 582 | de->u.Exception.ExceptionRecord.ExceptionCode); |
| 583 | |
| 584 | if (DEBUG_CurrProcess->continue_on_first_exception) { |
| 585 | DEBUG_CurrProcess->continue_on_first_exception = FALSE; |
| 586 | if (!DBG_IVAR(BreakOnAttach)) { |
| 587 | *cont = DBG_CONTINUE; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
Ulrich Weigand | 1cbf27a | 2000-06-04 01:33:21 +0000 | [diff] [blame] | 592 | DEBUG_context.ContextFlags = CONTEXT_CONTROL |
| 593 | | CONTEXT_INTEGER |
| 594 | #ifdef CONTEXT_SEGMENTS |
| 595 | | CONTEXT_SEGMENTS |
| 596 | #endif |
| 597 | #ifdef CONTEXT_DEBUG_REGISTERS |
| 598 | | CONTEXT_DEBUG_REGISTERS |
| 599 | #endif |
| 600 | ; |
| 601 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 602 | if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 603 | DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 604 | break; |
| 605 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 606 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 607 | ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord, |
| 608 | de->u.Exception.dwFirstChance, |
| 609 | DEBUG_CurrThread->wait_for_first_exception, |
| 610 | cont); |
| 611 | if (DEBUG_CurrThread) { |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 612 | DEBUG_CurrThread->wait_for_first_exception = 0; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 613 | SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 614 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 615 | break; |
| 616 | |
| 617 | case CREATE_THREAD_DEBUG_EVENT: |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 618 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId, |
| 619 | (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 620 | |
| 621 | if (DEBUG_CurrProcess == NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 622 | DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 625 | if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 626 | DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 627 | break; |
| 628 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 629 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 630 | DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess, |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 631 | de->dwThreadId, |
| 632 | de->u.CreateThread.hThread, |
| 633 | de->u.CreateThread.lpStartAddress, |
| 634 | de->u.CreateThread.lpThreadLocalBase); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 635 | if (!DEBUG_CurrThread) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 636 | DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | DEBUG_InitCurrThread(); |
| 640 | break; |
| 641 | |
| 642 | case CREATE_PROCESS_DEBUG_EVENT: |
Alexandre Julliard | 0966a6d | 2000-03-10 22:25:07 +0000 | [diff] [blame] | 643 | DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer), |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 644 | de->u.CreateProcessInfo.hProcess, |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 645 | de->u.CreateProcessInfo.lpImageName); |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 646 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 647 | /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */ |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 648 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n", |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 649 | de->dwProcessId, de->dwThreadId, |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 650 | buffer, de->u.CreateProcessInfo.lpImageName, |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 651 | (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress, |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 652 | de->u.CreateProcessInfo.dwDebugInfoFileOffset, |
| 653 | de->u.CreateProcessInfo.nDebugInfoSize); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 654 | |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 655 | if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) { |
| 656 | if (DEBUG_CurrProcess->handle) { |
| 657 | DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n"); |
| 658 | break; |
| 659 | } |
| 660 | DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess; |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 661 | if (DEBUG_CurrProcess->imageName == NULL) |
| 662 | DEBUG_CurrProcess->imageName = DBG_strdup(buffer[0] ? buffer : "<Debugged Process>"); |
| 663 | |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 664 | } else { |
| 665 | DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId, |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 666 | de->u.CreateProcessInfo.hProcess, |
| 667 | buffer[0] ? buffer : "<Debugged Process>"); |
Eric Pouech | b971745 | 2000-05-05 18:14:34 +0000 | [diff] [blame] | 668 | if (DEBUG_CurrProcess == NULL) { |
| 669 | DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n"); |
| 670 | break; |
| 671 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 674 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n", |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 675 | de->dwProcessId, de->dwThreadId, |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 676 | (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 677 | |
| 678 | DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess, |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 679 | de->dwThreadId, |
| 680 | de->u.CreateProcessInfo.hThread, |
| 681 | de->u.CreateProcessInfo.lpStartAddress, |
| 682 | de->u.CreateProcessInfo.lpThreadLocalBase); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 683 | if (!DEBUG_CurrThread) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 684 | DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | |
| 688 | DEBUG_InitCurrProcess(); |
| 689 | DEBUG_InitCurrThread(); |
Alexandre Julliard | 291fa66 | 2000-06-08 05:02:19 +0000 | [diff] [blame] | 690 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 691 | DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile, |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 692 | (DWORD)de->u.CreateProcessInfo.lpBaseOfImage); |
Alexandre Julliard | 291fa66 | 2000-06-08 05:02:19 +0000 | [diff] [blame] | 693 | |
| 694 | if (buffer[0]) /* we got a process name */ |
| 695 | { |
| 696 | DWORD type; |
| 697 | if (!GetBinaryTypeA( buffer, &type )) |
| 698 | { |
| 699 | /* not a Windows binary, assume it's a Unix executable then */ |
Alexandre Julliard | 633293f | 2000-12-06 03:20:47 +0000 | [diff] [blame] | 700 | char unixname[MAX_PATH]; |
Alexandre Julliard | 291fa66 | 2000-06-08 05:02:19 +0000 | [diff] [blame] | 701 | /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */ |
Alexandre Julliard | 633293f | 2000-12-06 03:20:47 +0000 | [diff] [blame] | 702 | if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) )) |
Alexandre Julliard | 291fa66 | 2000-06-08 05:02:19 +0000 | [diff] [blame] | 703 | { |
Alexandre Julliard | 633293f | 2000-12-06 03:20:47 +0000 | [diff] [blame] | 704 | DEBUG_ReadExecutableDbgInfo( unixname ); |
Alexandre Julliard | 291fa66 | 2000-06-08 05:02:19 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | /* if it is a Windows binary, or an invalid or missing file name, |
| 710 | * we use wine itself as the main executable */ |
| 711 | DEBUG_ReadExecutableDbgInfo( "wine" ); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 712 | break; |
| 713 | |
| 714 | case EXIT_THREAD_DEBUG_EVENT: |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 715 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n", |
| 716 | de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 717 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 718 | if (DEBUG_CurrThread == NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 719 | DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 720 | break; |
| 721 | } |
| 722 | /* FIXME: remove break point set on thread startup */ |
| 723 | DEBUG_DelThread(DEBUG_CurrThread); |
| 724 | break; |
| 725 | |
| 726 | case EXIT_PROCESS_DEBUG_EVENT: |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 727 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n", |
| 728 | de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode); |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 729 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 730 | if (DEBUG_CurrProcess == NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 731 | DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 732 | break; |
| 733 | } |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 734 | /* just in case */ |
| 735 | DEBUG_SetBreakpoints(FALSE); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 736 | /* kill last thread */ |
| 737 | DEBUG_DelThread(DEBUG_CurrProcess->threads); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 738 | DEBUG_DelProcess(DEBUG_CurrProcess); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 739 | |
| 740 | DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 741 | break; |
| 742 | |
| 743 | case LOAD_DLL_DEBUG_EVENT: |
| 744 | if (DEBUG_CurrThread == NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 745 | DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
Alexandre Julliard | 0966a6d | 2000-03-10 22:25:07 +0000 | [diff] [blame] | 748 | DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer), |
| 749 | DEBUG_CurrThread->process->handle, |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 750 | de->u.LoadDll.lpImageName); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 751 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 752 | /* FIXME unicode: de->u.LoadDll.fUnicode */ |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 753 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n", |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 754 | de->dwProcessId, de->dwThreadId, |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 755 | buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll, |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 756 | de->u.LoadDll.dwDebugInfoFileOffset, |
| 757 | de->u.LoadDll.nDebugInfoSize); |
Alexandre Julliard | 09d7822 | 2001-05-05 00:44:48 +0000 | [diff] [blame] | 758 | _strupr(buffer); |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 759 | DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll); |
Eric Pouech | eaafb73 | 2001-05-21 18:33:15 +0000 | [diff] [blame] | 760 | DEBUG_CheckDelayedBP(); |
Eric Pouech | f516be6 | 2000-07-10 13:05:44 +0000 | [diff] [blame] | 761 | if (DBG_IVAR(BreakOnDllLoad)) { |
| 762 | DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n", |
| 763 | buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll); |
| 764 | ret = DEBUG_Parser(); |
| 765 | } |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 766 | break; |
| 767 | |
| 768 | case UNLOAD_DLL_DEBUG_EVENT: |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 769 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId, |
| 770 | (unsigned long)de->u.UnloadDll.lpBaseOfDll); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 771 | break; |
| 772 | |
| 773 | case OUTPUT_DEBUG_STRING_EVENT: |
| 774 | if (DEBUG_CurrThread == NULL) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 775 | DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n"); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 778 | |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 779 | DEBUG_ProcessGetString(buffer, sizeof(buffer), |
| 780 | DEBUG_CurrThread->process->handle, |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 781 | de->u.DebugString.lpDebugStringData); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 782 | |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 783 | /* fixme unicode de->u.DebugString.fUnicode ? */ |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 784 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n", |
| 785 | de->dwProcessId, de->dwThreadId, buffer); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 786 | break; |
| 787 | |
| 788 | case RIP_EVENT: |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 789 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n", |
| 790 | de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError, |
| 791 | de->u.RipInfo.dwType); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 792 | break; |
| 793 | |
| 794 | default: |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 795 | DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n", |
| 796 | de->dwProcessId, de->dwThreadId, de->dwDebugEventCode); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 797 | } |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 798 | |
| 799 | } __EXCEPT(wine_dbg) { |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 800 | *cont = 0; |
| 801 | ret = TRUE; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 802 | } |
| 803 | __ENDTRY; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 804 | return ret; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 807 | static DWORD DEBUG_MainLoop(void) |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 808 | { |
| 809 | DEBUG_EVENT de; |
| 810 | DWORD cont; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 811 | BOOL ret; |
Eric Pouech | ac11a68 | 2000-03-26 13:37:39 +0000 | [diff] [blame] | 812 | |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 813 | DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid); |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 814 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 815 | for (ret = TRUE; ret; ) { |
| 816 | /* wait until we get at least one loaded process */ |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 817 | while (!DEBUG_ProcessList && (ret = DEBUG_Parser())); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 818 | if (!ret) break; |
| 819 | |
Eric Pouech | 7c43b22 | 2000-06-24 12:52:13 +0000 | [diff] [blame] | 820 | while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) { |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 821 | ret = DEBUG_HandleDebugEvent(&de, &cont); |
| 822 | ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont); |
| 823 | } |
| 824 | }; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 825 | |
Alexandre Julliard | 954a413 | 2000-09-24 03:15:50 +0000 | [diff] [blame] | 826 | DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid); |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 827 | |
| 828 | return 0; |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Alexandre Julliard | f5a8b96 | 2001-10-22 19:00:34 +0000 | [diff] [blame] | 831 | static DWORD DEBUG_AutoMode(void) |
| 832 | { |
| 833 | DEBUG_EVENT de; |
| 834 | DWORD cont; |
| 835 | BOOL ret = TRUE; |
| 836 | |
| 837 | DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid); |
| 838 | |
| 839 | while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) |
| 840 | { |
| 841 | ret = DEBUG_HandleDebugEvent(&de, &cont); |
| 842 | ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont); |
| 843 | } |
| 844 | /* print some extra information */ |
| 845 | DEBUG_Printf(DBG_CHN_MESG, "Modules:\n"); |
| 846 | DEBUG_WalkModules(); |
| 847 | DEBUG_Printf(DBG_CHN_MESG, "Threads:\n"); |
| 848 | DEBUG_WalkThreads(); |
| 849 | |
| 850 | DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid); |
| 851 | return 0; |
| 852 | } |
| 853 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 854 | static BOOL DEBUG_Start(LPSTR cmdLine) |
| 855 | { |
| 856 | PROCESS_INFORMATION info; |
| 857 | STARTUPINFOA startup; |
| 858 | |
| 859 | memset(&startup, 0, sizeof(startup)); |
| 860 | startup.cb = sizeof(startup); |
| 861 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 862 | startup.wShowWindow = SW_SHOWNORMAL; |
| 863 | |
| 864 | if (!CreateProcess(NULL, cmdLine, NULL, NULL, |
Eric Pouech | d0a0493 | 2001-11-23 23:10:08 +0000 | [diff] [blame] | 865 | FALSE, DEBUG_PROCESS|DETACHED_PROCESS, NULL, NULL, &startup, &info)) { |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 866 | DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine); |
| 867 | return FALSE; |
| 868 | } |
| 869 | DEBUG_CurrPid = info.dwProcessId; |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 870 | if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 871 | |
| 872 | return TRUE; |
| 873 | } |
| 874 | |
| 875 | void DEBUG_Run(const char* args) |
| 876 | { |
| 877 | DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess); |
| 878 | const char* pgm = (wmod) ? wmod->module_name : "none"; |
| 879 | |
| 880 | if (args) { |
| 881 | DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args); |
| 882 | } else { |
| 883 | if (!DEBUG_LastCmdLine) { |
| 884 | DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n"); |
| 885 | return; |
| 886 | } |
| 887 | DEBUG_Start(DEBUG_LastCmdLine); |
| 888 | } |
| 889 | } |
| 890 | |
Eric Pouech | d0a0493 | 2001-11-23 23:10:08 +0000 | [diff] [blame] | 891 | static void DEBUG_InitConsole(void) |
| 892 | { |
| 893 | COORD c; |
| 894 | SMALL_RECT sr; |
| 895 | DWORD mode; |
| 896 | |
| 897 | /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin, |
| 898 | * stdout and stderr streams |
| 899 | */ |
| 900 | if (DBG_IVAR(UseXTerm)) |
| 901 | { |
| 902 | FreeConsole(); |
| 903 | AllocConsole(); |
| 904 | } |
| 905 | /* this would be nicer for output */ |
| 906 | c.X = 132; |
| 907 | c.Y = 500; |
| 908 | SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c); |
| 909 | |
| 910 | /* sets the console's window width accordingly */ |
| 911 | sr.Left = 0; |
| 912 | sr.Top = 0; |
| 913 | sr.Right = c.X - 1; |
| 914 | sr.Bottom = 50; |
| 915 | SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &sr); |
| 916 | |
| 917 | /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */ |
| 918 | if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode)) |
| 919 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode | WINE_ENABLE_LINE_INPUT_EMACS); |
| 920 | } |
| 921 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 922 | int DEBUG_main(int argc, char** argv) |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 923 | { |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 924 | DWORD retv = 0; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 925 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 926 | /* Initialize the type handling stuff. */ |
| 927 | DEBUG_InitTypes(); |
| 928 | DEBUG_InitCVDataTypes(); |
Eric Pouech | 527eea9 | 2000-03-08 16:44:54 +0000 | [diff] [blame] | 929 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 930 | /* Initialize internal vars (types must have been initialized before) */ |
Eric Pouech | 26342c9 | 2000-05-01 14:22:02 +0000 | [diff] [blame] | 931 | if (!DEBUG_IntVarsRW(TRUE)) return -1; |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 932 | |
Alexandre Julliard | f5a8b96 | 2001-10-22 19:00:34 +0000 | [diff] [blame] | 933 | if (argc > 1 && !strcmp( argv[1], "--auto" )) |
| 934 | { |
| 935 | argc--; |
| 936 | argv++; |
| 937 | automatic_mode = 1; |
| 938 | /* force some internal variables */ |
| 939 | DBG_IVAR(UseXTerm) = 0; |
| 940 | DBG_IVAR(BreakOnDllLoad) = 0; |
| 941 | DBG_IVAR(ConChannelMask) = 0; |
| 942 | DBG_IVAR(StdChannelMask) = DBG_CHN_MESG; |
| 943 | } |
| 944 | |
Eric Pouech | d0a0493 | 2001-11-23 23:10:08 +0000 | [diff] [blame] | 945 | DEBUG_InitConsole(); |
| 946 | |
Andreas Mohr | a59324d | 2000-11-25 01:40:10 +0000 | [diff] [blame] | 947 | DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... "); |
Eric Pouech | 52c7534 | 2001-05-03 18:32:47 +0000 | [diff] [blame] | 948 | |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 949 | if (argc == 3) { |
| 950 | HANDLE hEvent; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 951 | DWORD pid; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 952 | |
François Gouget | d5042c4 | 2000-12-29 05:38:00 +0000 | [diff] [blame] | 953 | if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) { |
Eric Pouech | 2b713ba | 2001-04-13 22:26:23 +0000 | [diff] [blame] | 954 | if (!DEBUG_Attach(pid, TRUE)) { |
Eric Pouech | 2b713ba | 2001-04-13 22:26:23 +0000 | [diff] [blame] | 955 | /* don't care about result */ |
| 956 | SetEvent(hEvent); |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 957 | goto leave; |
| 958 | } |
Eric Pouech | 2b713ba | 2001-04-13 22:26:23 +0000 | [diff] [blame] | 959 | if (!SetEvent(hEvent)) { |
| 960 | DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent); |
| 961 | goto leave; |
| 962 | } |
| 963 | CloseHandle(hEvent); |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 964 | DEBUG_CurrPid = pid; |
Eric Pouech | f182235 | 2000-05-11 21:43:43 +0000 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 968 | if (DEBUG_CurrPid == 0 && argc > 1) { |
| 969 | int i, len; |
| 970 | LPSTR cmdLine; |
| 971 | |
| 972 | if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave; |
| 973 | cmdLine[0] = '\0'; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 974 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 975 | for (i = 1; i < argc; i++) { |
| 976 | len += strlen(argv[i]) + 1; |
| 977 | if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave; |
| 978 | strcat(cmdLine, argv[i]); |
| 979 | cmdLine[len - 2] = ' '; |
| 980 | cmdLine[len - 1] = '\0'; |
| 981 | } |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 982 | |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 983 | if (!DEBUG_Start(cmdLine)) { |
| 984 | DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine); |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 985 | goto leave; |
Eric Pouech | e5efa0c | 2000-04-13 19:31:58 +0000 | [diff] [blame] | 986 | } |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 987 | DBG_free(DEBUG_LastCmdLine); |
| 988 | DEBUG_LastCmdLine = cmdLine; |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Alexandre Julliard | f5a8b96 | 2001-10-22 19:00:34 +0000 | [diff] [blame] | 991 | if (automatic_mode) |
| 992 | { |
| 993 | retv = DEBUG_AutoMode(); |
| 994 | /* don't save modified variables in auto mode */ |
| 995 | } |
| 996 | else |
| 997 | { |
| 998 | retv = DEBUG_MainLoop(); |
| 999 | /* saves modified variables */ |
| 1000 | DEBUG_IntVarsRW(FALSE); |
| 1001 | } |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 1002 | |
Eric Pouech | 800773f | 2001-08-06 17:51:52 +0000 | [diff] [blame] | 1003 | leave: |
Eric Pouech | 04c16b8 | 2000-04-30 12:21:15 +0000 | [diff] [blame] | 1004 | return retv; |
Eric Pouech | 911436b | 2000-06-18 19:30:24 +0000 | [diff] [blame] | 1005 | |
| 1006 | oom_leave: |
| 1007 | DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n"); |
| 1008 | goto leave; |
Eric Pouech | cbb7a17 | 2000-03-09 18:45:43 +0000 | [diff] [blame] | 1009 | } |