| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * File module.c - module handling for the wine debugger |
| 3 | * |
| 4 | * Copyright (C) 1993, Eric Youngdale. |
| 5 | * 2000-2004, Eric Pouech |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "dbghelp_private.h" |
| 29 | #include "psapi.h" |
| 30 | #include "winreg.h" |
| 31 | #include "winternl.h" |
| 32 | #include "wine/debug.h" |
| 33 | |
| 34 | WINE_DEFAULT_DEBUG_CHANNEL(dbghelp); |
| 35 | |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 36 | static void module_fill_module(const char* in, char* out, unsigned size) |
| 37 | { |
| 38 | const char* ptr; |
| 39 | unsigned len; |
| 40 | |
| 41 | for (ptr = in + strlen(in) - 1; |
| 42 | *ptr != '/' && *ptr != '\\' && ptr >= in; |
| 43 | ptr--); |
| 44 | if (ptr < in || *ptr == '/' || *ptr == '\\') ptr++; |
| 45 | strncpy(out, ptr, size); |
| 46 | out[size - 1] = '\0'; |
| 47 | len = strlen(out); |
| 48 | if (len > 4 && |
| 49 | (!strcasecmp(&out[len - 4], ".dll") || !strcasecmp(&out[len - 4], ".exe"))) |
| 50 | out[len - 4] = '\0'; |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 51 | else |
| 52 | { |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 53 | if (len > 7 && |
| 54 | (!strcasecmp(&out[len - 7], ".dll.so") || !strcasecmp(&out[len - 7], ".exe.so"))) |
| 55 | strcpy(&out[len - 7], "<elf>"); |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 56 | else if (len > 7 && |
| 57 | out[len - 7] == '.' && !strcasecmp(&out[len - 3], ".so")) |
| 58 | { |
| 59 | if (len + 3 < size) strcpy(&out[len - 3], "<elf>"); |
| 60 | else WARN("Buffer too short: %s\n", out); |
| 61 | } |
| 62 | } |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 63 | while ((*out = tolower(*out))) out++; |
| 64 | } |
| 65 | |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 66 | /*********************************************************************** |
| 67 | * Creates and links a new module to a process |
| 68 | */ |
| 69 | struct module* module_new(struct process* pcs, const char* name, |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 70 | enum module_type type, |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 71 | unsigned long mod_addr, unsigned long size, |
| 72 | unsigned long stamp, unsigned long checksum) |
| 73 | { |
| 74 | struct module* module; |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 75 | |
| 76 | if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module)))) |
| 77 | return NULL; |
| 78 | |
| 79 | memset(module, 0, sizeof(*module)); |
| 80 | |
| 81 | module->next = pcs->lmodules; |
| 82 | pcs->lmodules = module; |
| 83 | |
| 84 | TRACE("=> %s %08lx-%08lx %s\n", |
| 85 | type == DMT_ELF ? "ELF" : (type == DMT_PE ? "PE" : "---"), |
| 86 | mod_addr, mod_addr + size, name); |
| 87 | |
| 88 | pool_init(&module->pool, 65536); |
| 89 | |
| 90 | module->module.SizeOfStruct = sizeof(module->module); |
| 91 | module->module.BaseOfImage = mod_addr; |
| 92 | module->module.ImageSize = size; |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 93 | module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName)); |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 94 | module->module.ImageName[0] = '\0'; |
| 95 | strncpy(module->module.LoadedImageName, name, |
| 96 | sizeof(module->module.LoadedImageName)); |
| 97 | module->module.LoadedImageName[sizeof(module->module.LoadedImageName) - 1] = '\0'; |
| 98 | module->module.SymType = SymNone; |
| 99 | module->module.NumSyms = 0; |
| 100 | module->module.TimeDateStamp = stamp; |
| 101 | module->module.CheckSum = checksum; |
| 102 | |
| 103 | module->type = type; |
| 104 | module->sortlist_valid = FALSE; |
| 105 | module->addr_sorttab = NULL; |
| 106 | /* FIXME: this seems a bit too high (on a per module basis) |
| 107 | * need some statistics about this |
| 108 | */ |
| 109 | hash_table_init(&module->pool, &module->ht_symbols, 4096); |
| 110 | hash_table_init(&module->pool, &module->ht_types, 4096); |
| 111 | |
| 112 | module->sources_used = 0; |
| 113 | module->sources_alloc = 0; |
| 114 | module->sources = 0; |
| 115 | |
| 116 | return module; |
| 117 | } |
| 118 | |
| 119 | /*********************************************************************** |
| 120 | * module_find_by_name |
| 121 | * |
| 122 | */ |
| 123 | struct module* module_find_by_name(const struct process* pcs, |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 124 | const char* name, enum module_type type) |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 125 | { |
| 126 | struct module* module; |
| 127 | |
| 128 | if (type == DMT_UNKNOWN) |
| 129 | { |
| 130 | if ((module = module_find_by_name(pcs, name, DMT_PE)) || |
| 131 | (module = module_find_by_name(pcs, name, DMT_ELF))) |
| 132 | return module; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | for (module = pcs->lmodules; module; module = module->next) |
| 137 | { |
| 138 | if (type == module->type && !strcasecmp(name, module->module.LoadedImageName)) |
| 139 | return module; |
| 140 | } |
| 141 | for (module = pcs->lmodules; module; module = module->next) |
| 142 | { |
| 143 | if (type == module->type && !strcasecmp(name, module->module.ModuleName)) |
| 144 | return module; |
| 145 | } |
| 146 | } |
| 147 | SetLastError(ERROR_INVALID_NAME); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /*********************************************************************** |
| 152 | * module_has_container |
| 153 | * |
| 154 | */ |
| 155 | static struct module* module_get_container(const struct process* pcs, |
| 156 | const struct module* inner) |
| 157 | { |
| 158 | struct module* module; |
| 159 | |
| 160 | for (module = pcs->lmodules; module; module = module->next) |
| 161 | { |
| 162 | if (module != inner && |
| 163 | module->module.BaseOfImage <= inner->module.BaseOfImage && |
| 164 | module->module.BaseOfImage + module->module.ImageSize >= |
| 165 | inner->module.BaseOfImage + inner->module.ImageSize) |
| 166 | return module; |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | /****************************************************************** |
| 172 | * module_get_debug |
| 173 | * |
| 174 | * get the debug information from a module: |
| 175 | * - if the module's type is deferred, then force loading of debug info (and return |
| 176 | * the module itself) |
| 177 | * - if the module has no debug info and has an ELF container, then return the ELF |
| 178 | * container (and also force the ELF container's debug info loading if deferred) |
| 179 | * - otherwise return the module itself if it has some debug info |
| 180 | */ |
| 181 | struct module* module_get_debug(const struct process* pcs, struct module* module) |
| 182 | { |
| 183 | if (!module) return NULL; |
| 184 | switch (module->module.SymType) |
| 185 | { |
| 186 | case -1: break; |
| 187 | case SymNone: |
| 188 | module = module_get_container(pcs, module); |
| 189 | if (!module || module->module.SymType != SymDeferred) break; |
| 190 | /* fall through */ |
| 191 | case SymDeferred: |
| 192 | switch (module->type) |
| 193 | { |
| 194 | case DMT_ELF: |
| 195 | elf_load_debug_info(module); |
| 196 | break; |
| 197 | case DMT_PE: |
| 198 | pe_load_debug_info(pcs, module); |
| 199 | break; |
| 200 | default: break; |
| 201 | } |
| 202 | break; |
| 203 | default: break; |
| 204 | } |
| 205 | return (module && module->module.SymType > SymNone) ? module : NULL; |
| 206 | } |
| 207 | |
| 208 | /*********************************************************************** |
| 209 | * module_find_by_addr |
| 210 | * |
| 211 | * either the addr where module is loaded, or any address inside the |
| 212 | * module |
| 213 | */ |
| 214 | struct module* module_find_by_addr(const struct process* pcs, unsigned long addr, |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 215 | enum module_type type) |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 216 | { |
| 217 | struct module* module; |
| 218 | |
| 219 | if (type == DMT_UNKNOWN) |
| 220 | { |
| 221 | if ((module = module_find_by_addr(pcs, addr, DMT_PE)) || |
| 222 | (module = module_find_by_addr(pcs, addr, DMT_ELF))) |
| 223 | return module; |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | for (module = pcs->lmodules; module; module = module->next) |
| 228 | { |
| 229 | if (type == module->type && addr >= module->module.BaseOfImage && |
| 230 | addr < module->module.BaseOfImage + module->module.ImageSize) |
| 231 | return module; |
| 232 | } |
| 233 | } |
| 234 | SetLastError(ERROR_INVALID_ADDRESS); |
| 235 | return module; |
| 236 | } |
| 237 | |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 238 | static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName, |
| 239 | const char* ModuleName) |
| 240 | { |
| 241 | char buffer[MAX_PATH]; |
| 242 | size_t len; |
| 243 | struct module* module; |
| 244 | |
| 245 | if (!ModuleName) |
| 246 | { |
| 247 | module_fill_module(ImageName, buffer, sizeof(buffer)); |
| 248 | ModuleName = buffer; |
| 249 | } |
| 250 | len = strlen(ModuleName); |
| 251 | for (module = pcs->lmodules; module; module = module->next) |
| 252 | { |
| 253 | if (!strncasecmp(module->module.ModuleName, ModuleName, len) && |
| 254 | module->type == DMT_ELF && |
| 255 | !strcmp(module->module.ModuleName + len, "<elf>")) |
| 256 | return TRUE; |
| 257 | } |
| 258 | return FALSE; |
| 259 | } |
| 260 | |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 261 | /*********************************************************************** |
| 262 | * SymLoadModule (DBGHELP.@) |
| 263 | */ |
| 264 | DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, char* ImageName, |
| 265 | char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll) |
| 266 | { |
| 267 | struct process* pcs; |
| 268 | struct module* module = NULL; |
| 269 | |
| 270 | TRACE("(%p %p %s %s %08lx %08lx)\n", |
| 271 | hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName), |
| 272 | BaseOfDll, SizeOfDll); |
| 273 | |
| 274 | pcs = process_find_by_handle(hProcess); |
| 275 | if (!pcs) return FALSE; |
| 276 | |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 277 | /* force transparent ELF loading / unloading */ |
| 278 | elf_synchronize_module_list(pcs); |
| 279 | |
| 280 | /* this is a Wine extension to the API just to redo the synchronisation */ |
| 281 | if (!ImageName && !hFile) return 0; |
| 282 | |
| 283 | if (module_is_elf_container_loaded(pcs, ImageName, ModuleName)) |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 284 | { |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 285 | /* force the loading of DLL as builtin */ |
| 286 | if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll))) |
| 287 | goto done; |
| 288 | WARN("Couldn't locate %s\n", ImageName); |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 291 | TRACE("Assuming %s as native DLL\n", ImageName); |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 292 | if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll))) |
| 293 | { |
| 294 | unsigned len = strlen(ImageName); |
| 295 | |
| 296 | if (!strcmp(ImageName + len - 3, ".so") && |
| 297 | (module = elf_load_module(pcs, ImageName))) goto done; |
| Eric Pouech | eef83b3 | 2004-05-24 19:08:19 +0000 | [diff] [blame] | 298 | FIXME("should no longer happen\n"); |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 299 | if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll))) |
| 300 | goto done; |
| 301 | WARN("Couldn't locate %s\n", ImageName); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | done: |
| 306 | /* by default pe_load_module fills module.ModuleName from a derivation |
| 307 | * of ImageName. Overwrite it, if we have better information |
| 308 | */ |
| 309 | if (ModuleName) |
| 310 | { |
| 311 | strncpy(module->module.ModuleName, ModuleName, |
| 312 | sizeof(module->module.ModuleName)); |
| 313 | module->module.ModuleName[sizeof(module->module.ModuleName) - 1] = '\0'; |
| 314 | } |
| 315 | strncpy(module->module.ImageName, ImageName, sizeof(module->module.ImageName)); |
| 316 | module->module.ImageName[sizeof(module->module.ImageName) - 1] = '\0'; |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 317 | |
| 318 | return module->module.BaseOfImage; |
| 319 | } |
| 320 | |
| 321 | /****************************************************************** |
| 322 | * module_remove |
| 323 | * |
| 324 | */ |
| 325 | BOOL module_remove(struct process* pcs, struct module* module) |
| 326 | { |
| 327 | struct module** p; |
| 328 | |
| 329 | TRACE("%s (%p)\n", module->module.ModuleName, module); |
| 330 | hash_table_destroy(&module->ht_symbols); |
| 331 | hash_table_destroy(&module->ht_types); |
| 332 | HeapFree(GetProcessHeap(), 0, (char*)module->sources); |
| 333 | HeapFree(GetProcessHeap(), 0, module->addr_sorttab); |
| 334 | pool_destroy(&module->pool); |
| 335 | |
| 336 | for (p = &pcs->lmodules; *p; p = &(*p)->next) |
| 337 | { |
| 338 | if (*p == module) |
| 339 | { |
| 340 | *p = module->next; |
| 341 | HeapFree(GetProcessHeap(), 0, module); |
| 342 | return TRUE; |
| 343 | } |
| 344 | } |
| 345 | FIXME("This shouldn't happen\n"); |
| 346 | return FALSE; |
| 347 | } |
| 348 | |
| 349 | /****************************************************************** |
| 350 | * SymUnloadModule (DBGHELP.@) |
| 351 | * |
| 352 | */ |
| 353 | BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll) |
| 354 | { |
| 355 | struct process* pcs; |
| 356 | struct module* module; |
| 357 | |
| 358 | pcs = process_find_by_handle(hProcess); |
| 359 | if (!pcs) return FALSE; |
| 360 | module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN); |
| 361 | if (!module) return FALSE; |
| 362 | return module_remove(pcs, module); |
| 363 | } |
| 364 | |
| 365 | /****************************************************************** |
| 366 | * SymEnumerateModules (DBGHELP.@) |
| 367 | * |
| 368 | */ |
| 369 | BOOL WINAPI SymEnumerateModules(HANDLE hProcess, |
| 370 | PSYM_ENUMMODULES_CALLBACK EnumModulesCallback, |
| 371 | PVOID UserContext) |
| 372 | { |
| 373 | struct process* pcs = process_find_by_handle(hProcess); |
| 374 | struct module* module; |
| 375 | |
| 376 | if (!pcs) return FALSE; |
| 377 | |
| 378 | for (module = pcs->lmodules; module; module = module->next) |
| 379 | { |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 380 | if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type != DMT_PE) |
| 381 | continue; |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 382 | if (!EnumModulesCallback(module->module.ModuleName, |
| 383 | module->module.BaseOfImage, UserContext)) |
| 384 | break; |
| 385 | } |
| 386 | return TRUE; |
| 387 | } |
| 388 | |
| 389 | /****************************************************************** |
| 390 | * EnumerateLoadedModules (DBGHELP.@) |
| 391 | * |
| 392 | */ |
| 393 | BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess, |
| 394 | PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback, |
| 395 | PVOID UserContext) |
| 396 | { |
| 397 | HMODULE* hMods; |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 398 | char base[256], mod[256]; |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 399 | DWORD i, sz; |
| 400 | MODULEINFO mi; |
| 401 | |
| 402 | hMods = HeapAlloc(GetProcessHeap(), 0, sz); |
| 403 | if (!hMods) return FALSE; |
| 404 | |
| 405 | if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz)) |
| 406 | { |
| 407 | /* hProcess should also be a valid process handle !! */ |
| 408 | FIXME("If this happens, bump the number in mod\n"); |
| 409 | HeapFree(GetProcessHeap(), 0, hMods); |
| 410 | return FALSE; |
| 411 | } |
| 412 | sz /= sizeof(HMODULE); |
| 413 | for (i = 0; i < sz; i++) |
| 414 | { |
| 415 | if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) || |
| Eric Pouech | 48a8659 | 2004-05-18 21:29:09 +0000 | [diff] [blame] | 416 | !GetModuleBaseNameA(hProcess, hMods[i], base, sizeof(base))) |
| 417 | continue; |
| 418 | module_fill_module(base, mod, sizeof(mod)); |
| 419 | |
| Eric Pouech | 800864a | 2004-04-05 22:21:27 +0000 | [diff] [blame] | 420 | EnumLoadedModulesCallback(mod, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage, |
| 421 | UserContext); |
| 422 | } |
| 423 | HeapFree(GetProcessHeap(), 0, hMods); |
| 424 | |
| 425 | return sz != 0 && i == sz; |
| 426 | } |
| 427 | |
| 428 | /****************************************************************** |
| 429 | * SymGetModuleInfo (DBGHELP.@) |
| 430 | * |
| 431 | */ |
| 432 | BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr, |
| 433 | PIMAGEHLP_MODULE ModuleInfo) |
| 434 | { |
| 435 | struct process* pcs = process_find_by_handle(hProcess); |
| 436 | struct module* module; |
| 437 | |
| 438 | if (!pcs) return FALSE; |
| 439 | if (ModuleInfo->SizeOfStruct < sizeof(*ModuleInfo)) return FALSE; |
| 440 | module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN); |
| 441 | if (!module) return FALSE; |
| 442 | |
| 443 | *ModuleInfo = module->module; |
| 444 | if (module->module.SymType <= SymNone) |
| 445 | { |
| 446 | module = module_get_container(pcs, module); |
| 447 | if (module && module->module.SymType > SymNone) |
| 448 | ModuleInfo->SymType = module->module.SymType; |
| 449 | } |
| 450 | |
| 451 | return TRUE; |
| 452 | } |
| 453 | |
| 454 | /*********************************************************************** |
| 455 | * SymGetModuleBase (IMAGEHLP.@) |
| 456 | */ |
| 457 | DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr) |
| 458 | { |
| 459 | struct process* pcs = process_find_by_handle(hProcess); |
| 460 | struct module* module; |
| 461 | |
| 462 | if (!pcs) return 0; |
| 463 | module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN); |
| 464 | if (!module) return 0; |
| 465 | return module->module.BaseOfImage; |
| 466 | } |