dbghelp.c 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * File dbghelp.c - generic routines (process) for dbghelp DLL
 *
 * Copyright (C) 2004, Eric Pouech
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26
 */

#include "config.h"

#include "dbghelp_private.h"
#include "winerror.h"
#include "psapi.h"
#include "wine/debug.h"
27
#include "wdbgexts.h"
Eric Pouech's avatar
Eric Pouech committed
28
#include "winnls.h"
29 30 31 32 33 34

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);

/* TODO
 *  - support for symbols' types is still partly missing
 *      + C++ support
35
 *      + we should store the underlying type for an enum in the symt_enum struct
36 37 38 39
 *      + for enums, we store the names & values (associated to the enum type), 
 *        but those values are not directly usable from a debugger (that's why, I
 *        assume, that we have also to define constants for enum values, as 
 *        Codeview does BTW.
40 41
 *      + SymEnumTypes should only return *user* defined types (UDT, typedefs...) not
 *        all the types stored/used in the modules (like char*)
42 43 44 45
 *  - SymGetLine{Next|Prev} don't work as expected (they don't seem to work across
 *    functions, and even across function blocks...). Basically, for *Next* to work
 *    it requires an address after the prolog of the func (the base address of the 
 *    func doesn't work)
46
 *  - most options (dbghelp_options) are not used (loading lines...)
47 48 49 50
 *  - in symbol lookup by name, we don't use RE everywhere we should. Moreover, when
 *    we're supposed to use RE, it doesn't make use of our hash tables. Therefore,
 *    we could use hash if name isn't a RE, and fall back to a full search when we
 *    get a full RE
51
 *  - msc:
52 53
 *      + we should add parameters' types to the function's signature
 *        while processing a function's parameters
54
 *      + add support for function-less labels (as MSC seems to define them)
55 56
 *      + C++ management
 *  - stabs: 
57 58 59 60 61 62 63
 *      + when, in a same module, the same definition is used in several compilation
 *        units, we get several definitions of the same object (especially 
 *        struct/union). we should find a way not to duplicate them
 *      + in some cases (dlls/user/dialog16.c DIALOG_GetControl16), the same static
 *        global variable is defined several times (at different scopes). We are
 *        getting several of those while looking for a unique symbol. Part of the 
 *        issue is that we don't give a scope to a static variable inside a function
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 *      + C++ management
 */

unsigned   dbghelp_options = SYMOPT_UNDNAME;

static struct process* process_first /* = NULL */;

/******************************************************************
 *		process_find_by_handle
 *
 */
struct process*    process_find_by_handle(HANDLE hProcess)
{
    struct process* p;

    for (p = process_first; p && p->handle != hProcess; p = p->next);
    if (!p) SetLastError(ERROR_INVALID_HANDLE);
    return p;
}

84 85 86 87 88 89
/******************************************************************
 *             validate_addr64 (internal)
 *
 */
BOOL validate_addr64(DWORD64 addr)
{
90
    if (sizeof(void*) == sizeof(int) && (addr >> 32))
91 92 93 94 95 96 97 98
    {
        FIXME("Unsupported address %s\n", wine_dbgstr_longlong(addr));
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
    return TRUE;
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/******************************************************************
 *		fetch_buffer
 *
 * Ensures process' internal buffer is large enough.
 */
void* fetch_buffer(struct process* pcs, unsigned size)
{
    if (size > pcs->buffer_size)
    {
        if (pcs->buffer)
            pcs->buffer = HeapReAlloc(GetProcessHeap(), 0, pcs->buffer, size);
        else
            pcs->buffer = HeapAlloc(GetProcessHeap(), 0, size);
        pcs->buffer_size = (pcs->buffer) ? size : 0;
    }
    return pcs->buffer;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
const char* wine_dbgstr_addr(const ADDRESS64* addr)
{
    if (!addr) return "(null)";
    switch (addr->Mode)
    {
    case AddrModeFlat:
        return wine_dbg_sprintf("flat<%s>", wine_dbgstr_longlong(addr->Offset));
    case AddrMode1616:
        return wine_dbg_sprintf("1616<%04x:%04x>", addr->Segment, (DWORD)addr->Offset);
    case AddrMode1632:
        return wine_dbg_sprintf("1632<%04x:%08x>", addr->Segment, (DWORD)addr->Offset);
    case AddrModeReal:
        return wine_dbg_sprintf("real<%04x:%04x>", addr->Segment, (DWORD)addr->Offset);
    default:
        return "unknown";
    }
}

135
extern struct cpu       cpu_i386, cpu_x86_64, cpu_ppc, cpu_arm, cpu_arm64;
136

137
static struct cpu*      dbghelp_cpus[] = {&cpu_i386, &cpu_x86_64, &cpu_ppc, &cpu_arm, &cpu_arm64, NULL};
138 139 140
struct cpu*             dbghelp_current_cpu =
#if defined(__i386__)
    &cpu_i386
141 142
#elif defined(__x86_64__)
    &cpu_x86_64
143 144
#elif defined(__powerpc__)
    &cpu_ppc
145 146
#elif defined(__arm__)
    &cpu_arm
147 148
#elif defined(__aarch64__)
    &cpu_arm64
149
#else
André Hentschel's avatar
André Hentschel committed
150
#error define support for your CPU
151 152 153 154 155 156 157 158 159 160 161 162 163 164
#endif
    ;

struct cpu* cpu_find(DWORD machine)
{
    struct cpu** cpu;

    for (cpu = dbghelp_cpus ; *cpu; cpu++)
    {
        if (cpu[0]->machine == machine) return cpu[0];
    }
    return NULL;
}

165
/******************************************************************
Eric Pouech's avatar
Eric Pouech committed
166
 *		SymSetSearchPathW (DBGHELP.@)
167 168
 *
 */
Eric Pouech's avatar
Eric Pouech committed
169
BOOL WINAPI SymSetSearchPathW(HANDLE hProcess, PCWSTR searchPath)
170 171 172 173 174 175 176
{
    struct process* pcs = process_find_by_handle(hProcess);

    if (!pcs) return FALSE;
    if (!searchPath) return FALSE;

    HeapFree(GetProcessHeap(), 0, pcs->search_path);
Eric Pouech's avatar
Eric Pouech committed
177 178 179
    pcs->search_path = lstrcpyW(HeapAlloc(GetProcessHeap(), 0, 
                                          (lstrlenW(searchPath) + 1) * sizeof(WCHAR)),
                                searchPath);
180 181 182
    return TRUE;
}

Eric Pouech's avatar
Eric Pouech committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/******************************************************************
 *		SymSetSearchPath (DBGHELP.@)
 *
 */
BOOL WINAPI SymSetSearchPath(HANDLE hProcess, PCSTR searchPath)
{
    BOOL        ret = FALSE;
    unsigned    len;
    WCHAR*      sp;

    len = MultiByteToWideChar(CP_ACP, 0, searchPath, -1, NULL, 0);
    if ((sp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
    {
        MultiByteToWideChar(CP_ACP, 0, searchPath, -1, sp, len);

        ret = SymSetSearchPathW(hProcess, sp);
        HeapFree(GetProcessHeap(), 0, sp);
    }
    return ret;
}

204
/***********************************************************************
Eric Pouech's avatar
Eric Pouech committed
205
 *		SymGetSearchPathW (DBGHELP.@)
206
 */
207
BOOL WINAPI SymGetSearchPathW(HANDLE hProcess, PWSTR szSearchPath,
Eric Pouech's avatar
Eric Pouech committed
208
                              DWORD SearchPathLength)
209 210 211 212
{
    struct process* pcs = process_find_by_handle(hProcess);
    if (!pcs) return FALSE;

Eric Pouech's avatar
Eric Pouech committed
213
    lstrcpynW(szSearchPath, pcs->search_path, SearchPathLength);
214 215 216
    return TRUE;
}

Eric Pouech's avatar
Eric Pouech committed
217 218 219
/***********************************************************************
 *		SymGetSearchPath (DBGHELP.@)
 */
220
BOOL WINAPI SymGetSearchPath(HANDLE hProcess, PSTR szSearchPath,
Eric Pouech's avatar
Eric Pouech committed
221 222
                             DWORD SearchPathLength)
{
223
    WCHAR*      buffer = HeapAlloc(GetProcessHeap(), 0, SearchPathLength * sizeof(WCHAR));
Eric Pouech's avatar
Eric Pouech committed
224 225 226 227
    BOOL        ret = FALSE;

    if (buffer)
    {
228
        ret = SymGetSearchPathW(hProcess, buffer, SearchPathLength);
Eric Pouech's avatar
Eric Pouech committed
229 230 231 232 233 234 235 236
        if (ret)
            WideCharToMultiByte(CP_ACP, 0, buffer, SearchPathLength,
                                szSearchPath, SearchPathLength, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, buffer);
    }
    return ret;
}

237 238 239 240 241 242
/******************************************************************
 *		invade_process
 *
 * SymInitialize helper: loads in dbghelp all known (and loaded modules)
 * this assumes that hProcess is a handle on a valid process
 */
243
static BOOL WINAPI process_invade_cb(PCWSTR name, ULONG64 base, ULONG size, PVOID user)
244
{
245
    WCHAR       tmp[MAX_PATH];
246
    HANDLE      hProcess = user;
247

248 249
    if (!GetModuleFileNameExW(hProcess, (HMODULE)(DWORD_PTR)base, tmp, ARRAY_SIZE(tmp)))
        lstrcpynW(tmp, name, ARRAY_SIZE(tmp));
250

251
    SymLoadModuleExW(hProcess, 0, tmp, name, base, size, NULL, 0);
252
    return TRUE;
253 254
}

Eric Pouech's avatar
Eric Pouech committed
255 256 257 258 259 260 261
/******************************************************************
 *		check_live_target
 *
 */
static BOOL check_live_target(struct process* pcs)
{
    if (!GetProcessId(pcs->handle)) return FALSE;
262
    if (GetEnvironmentVariableA("DBGHELP_NOLIVE", NULL, 0)) return FALSE;
263 264
    if (!elf_read_wine_loader_dbg_info(pcs))
        macho_read_wine_loader_dbg_info(pcs);
265
    return TRUE;
Eric Pouech's avatar
Eric Pouech committed
266 267
}

268
/******************************************************************
Eric Pouech's avatar
Eric Pouech committed
269
 *		SymInitializeW (DBGHELP.@)
270 271 272 273
 *
 * The initialisation of a dbghelp's context.
 * Note that hProcess doesn't need to be a valid process handle (except
 * when fInvadeProcess is TRUE).
274
 * Since we also allow loading ELF (pure) libraries and Wine ELF libraries
275 276 277 278 279 280 281 282
 * containing PE (and NE) module(s), here's how we handle it:
 * - we load every module (ELF, NE, PE) passed in SymLoadModule
 * - in fInvadeProcess (in SymInitialize) is TRUE, we set up what is called ELF
 *   synchronization: hProcess should be a valid process handle, and we hook
 *   ourselves on hProcess's loaded ELF-modules, and keep this list in sync with
 *   our internal ELF modules representation (loading / unloading). This way,
 *   we'll pair every loaded builtin PE module with its ELF counterpart (and
 *   access its debug information).
Eric Pouech's avatar
Eric Pouech committed
283 284 285 286 287 288 289 290
 * - if fInvadeProcess (in SymInitialize) is FALSE, we check anyway if the 
 *   hProcess refers to a running process. We use some heuristics here, so YMMV.
 *   If we detect a live target, then we get the same handling as if
 *   fInvadeProcess is TRUE (except that the modules are not loaded). Otherwise,
 *   we won't be able to make the peering between a builtin PE module and its ELF
 *   counterpart. Hence we won't be able to provide the requested debug
 *   information. We'll however be able to load native PE modules (and their
 *   debug information) without any trouble.
291 292 293
 * Note also that this scheme can be intertwined with the deferred loading 
 * mechanism (ie only load the debug information when we actually need it).
 */
Eric Pouech's avatar
Eric Pouech committed
294
BOOL WINAPI SymInitializeW(HANDLE hProcess, PCWSTR UserSearchPath, BOOL fInvadeProcess)
295 296
{
    struct process*     pcs;
297
    BOOL wow64, child_wow64;
298

Eric Pouech's avatar
Eric Pouech committed
299
    TRACE("(%p %s %u)\n", hProcess, debugstr_w(UserSearchPath), fInvadeProcess);
300

301 302
    if (process_find_by_handle(hProcess))
    {
303 304 305 306 307 308 309
        WARN("the symbols for this process have already been initialized!\n");

        /* MSDN says to only call this function once unless SymCleanup() has been called since the last call.
           It also says to call SymRefreshModuleList() instead if you just want the module list refreshed.
           Native still returns TRUE even if the process has already been initialized. */
        return TRUE;
    }
310

311 312
    IsWow64Process(GetCurrentProcess(), &wow64);

313
    if (GetProcessId(hProcess) && !IsWow64Process(hProcess, &child_wow64))
314
        return FALSE;
315 316 317 318 319

    pcs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pcs));
    if (!pcs) return FALSE;

    pcs->handle = hProcess;
320 321
    pcs->is_64bit = (sizeof(void *) == 8 || wow64) && !child_wow64;

322 323
    if (UserSearchPath)
    {
Eric Pouech's avatar
Eric Pouech committed
324 325 326
        pcs->search_path = lstrcpyW(HeapAlloc(GetProcessHeap(), 0,      
                                              (lstrlenW(UserSearchPath) + 1) * sizeof(WCHAR)),
                                    UserSearchPath);
327 328 329 330 331
    }
    else
    {
        unsigned        size;
        unsigned        len;
Eric Pouech's avatar
Eric Pouech committed
332 333
        static const WCHAR      sym_path[] = {'_','N','T','_','S','Y','M','B','O','L','_','P','A','T','H',0};
        static const WCHAR      alt_sym_path[] = {'_','N','T','_','A','L','T','E','R','N','A','T','E','_','S','Y','M','B','O','L','_','P','A','T','H',0};
334

Eric Pouech's avatar
Eric Pouech committed
335 336 337 338
        pcs->search_path = HeapAlloc(GetProcessHeap(), 0, (len = MAX_PATH) * sizeof(WCHAR));
        while ((size = GetCurrentDirectoryW(len, pcs->search_path)) >= len)
            pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (len *= 2) * sizeof(WCHAR));
        pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1) * sizeof(WCHAR));
339

Eric Pouech's avatar
Eric Pouech committed
340
        len = GetEnvironmentVariableW(sym_path, NULL, 0);
341 342
        if (len)
        {
Eric Pouech's avatar
Eric Pouech committed
343
            pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1 + len + 1) * sizeof(WCHAR));
344
            pcs->search_path[size] = ';';
Eric Pouech's avatar
Eric Pouech committed
345
            GetEnvironmentVariableW(sym_path, pcs->search_path + size + 1, len);
346 347
            size += 1 + len;
        }
Eric Pouech's avatar
Eric Pouech committed
348
        len = GetEnvironmentVariableW(alt_sym_path, NULL, 0);
349 350
        if (len)
        {
Eric Pouech's avatar
Eric Pouech committed
351
            pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, (size + 1 + len + 1) * sizeof(WCHAR));
352
            pcs->search_path[size] = ';';
Eric Pouech's avatar
Eric Pouech committed
353
            GetEnvironmentVariableW(alt_sym_path, pcs->search_path + size + 1, len);
354 355 356 357 358 359 360
        }
    }

    pcs->lmodules = NULL;
    pcs->dbg_hdr_addr = 0;
    pcs->next = process_first;
    process_first = pcs;
Eric Pouech's avatar
Eric Pouech committed
361 362
    
    if (check_live_target(pcs))
363
    {
Eric Pouech's avatar
Eric Pouech committed
364
        if (fInvadeProcess)
365
            EnumerateLoadedModulesW64(hProcess, process_invade_cb, hProcess);
366
        elf_synchronize_module_list(pcs);
367
        macho_synchronize_module_list(pcs);
368
    }
Eric Pouech's avatar
Eric Pouech committed
369 370 371 372 373 374
    else if (fInvadeProcess)
    {
        SymCleanup(hProcess);
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
375

376 377 378
    return TRUE;
}

Eric Pouech's avatar
Eric Pouech committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/******************************************************************
 *		SymInitialize (DBGHELP.@)
 *
 *
 */
BOOL WINAPI SymInitialize(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess)
{
    WCHAR*              sp = NULL;
    BOOL                ret;

    if (UserSearchPath)
    {
        unsigned len;

        len = MultiByteToWideChar(CP_ACP, 0, UserSearchPath, -1, NULL, 0);
        sp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, UserSearchPath, -1, sp, len);
    }

    ret = SymInitializeW(hProcess, sp, fInvadeProcess);
    HeapFree(GetProcessHeap(), 0, sp);
    return ret;
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/******************************************************************
 *		SymCleanup (DBGHELP.@)
 *
 */
BOOL WINAPI SymCleanup(HANDLE hProcess)
{
    struct process**    ppcs;
    struct process*     next;

    for (ppcs = &process_first; *ppcs; ppcs = &(*ppcs)->next)
    {
        if ((*ppcs)->handle == hProcess)
        {
            while ((*ppcs)->lmodules) module_remove(*ppcs, (*ppcs)->lmodules);

            HeapFree(GetProcessHeap(), 0, (*ppcs)->search_path);
            next = (*ppcs)->next;
            HeapFree(GetProcessHeap(), 0, *ppcs);
            *ppcs = next;
            return TRUE;
        }
    }
425 426

    ERR("this process has not had SymInitialize() called for it!\n");
427 428 429 430 431 432 433 434 435
    return FALSE;
}

/******************************************************************
 *		SymSetOptions (DBGHELP.@)
 *
 */
DWORD WINAPI SymSetOptions(DWORD opts)
{
436 437 438 439 440 441
    struct process* pcs;

    for (pcs = process_first; pcs; pcs = pcs->next)
    {
        pcs_callback(pcs, CBA_SET_OPTIONS, &opts);
    }
442 443 444 445 446 447 448 449 450 451 452 453
    return dbghelp_options = opts;
}

/******************************************************************
 *		SymGetOptions (DBGHELP.@)
 *
 */
DWORD WINAPI SymGetOptions(void)
{
    return dbghelp_options;
}

454 455 456 457 458 459 460 461 462 463 464
/******************************************************************
 *		SymSetParentWindow (DBGHELP.@)
 *
 */
BOOL WINAPI SymSetParentWindow(HWND hwnd)
{
    /* Save hwnd so it can be used as parent window */
    FIXME("(%p): stub\n", hwnd);
    return TRUE;
}

465 466 467 468 469 470 471 472 473 474
/******************************************************************
 *		SymSetContext (DBGHELP.@)
 *
 */
BOOL WINAPI SymSetContext(HANDLE hProcess, PIMAGEHLP_STACK_FRAME StackFrame,
                          PIMAGEHLP_CONTEXT Context)
{
    struct process* pcs = process_find_by_handle(hProcess);
    if (!pcs) return FALSE;

475 476 477 478 479 480 481 482
    if (pcs->ctx_frame.ReturnOffset == StackFrame->ReturnOffset &&
        pcs->ctx_frame.FrameOffset  == StackFrame->FrameOffset  &&
        pcs->ctx_frame.StackOffset  == StackFrame->StackOffset)
    {
        TRACE("Setting same frame {rtn=%s frm=%s stk=%s}\n",
              wine_dbgstr_longlong(pcs->ctx_frame.ReturnOffset),
              wine_dbgstr_longlong(pcs->ctx_frame.FrameOffset),
              wine_dbgstr_longlong(pcs->ctx_frame.StackOffset));
483
        pcs->ctx_frame.InstructionOffset = StackFrame->InstructionOffset;
484 485 486 487
        SetLastError(ERROR_ACCESS_DENIED); /* latest MSDN says ERROR_SUCCESS */
        return FALSE;
    }

488 489 490 491 492
    pcs->ctx_frame = *StackFrame;
    /* MSDN states that Context is not (no longer?) used */
    return TRUE;
}

493 494 495 496 497 498 499
/******************************************************************
 *		reg_cb64to32 (internal)
 *
 * Registered callback for converting information from 64 bit to 32 bit
 */
static BOOL CALLBACK reg_cb64to32(HANDLE hProcess, ULONG action, ULONG64 data, ULONG64 user)
{
500
    struct process*                     pcs = process_find_by_handle(hProcess);
501 502 503 504
    void*                               data32;
    IMAGEHLP_DEFERRED_SYMBOL_LOAD64*    idsl64;
    IMAGEHLP_DEFERRED_SYMBOL_LOAD       idsl;

505
    if (!pcs) return FALSE;
506 507 508 509 510 511
    switch (action)
    {
    case CBA_DEBUG_INFO:
    case CBA_DEFERRED_SYMBOL_LOAD_CANCEL:
    case CBA_SET_OPTIONS:
    case CBA_SYMBOLS_UNLOADED:
512
        data32 = (void*)(DWORD_PTR)data;
513 514 515 516 517
        break;
    case CBA_DEFERRED_SYMBOL_LOAD_COMPLETE:
    case CBA_DEFERRED_SYMBOL_LOAD_FAILURE:
    case CBA_DEFERRED_SYMBOL_LOAD_PARTIAL:
    case CBA_DEFERRED_SYMBOL_LOAD_START:
518
        idsl64 = (IMAGEHLP_DEFERRED_SYMBOL_LOAD64*)(DWORD_PTR)data;
519 520 521 522 523 524 525 526 527 528 529 530 531 532
        if (!validate_addr64(idsl64->BaseOfImage))
            return FALSE;
        idsl.SizeOfStruct = sizeof(idsl);
        idsl.BaseOfImage = (DWORD)idsl64->BaseOfImage;
        idsl.CheckSum = idsl64->CheckSum;
        idsl.TimeDateStamp = idsl64->TimeDateStamp;
        memcpy(idsl.FileName, idsl64->FileName, sizeof(idsl.FileName));
        idsl.Reparse = idsl64->Reparse;
        data32 = &idsl;
        break;
    case CBA_DUPLICATE_SYMBOL:
    case CBA_EVENT:
    case CBA_READ_MEMORY:
    default:
533
        FIXME("No mapping for action %u\n", action);
534 535
        return FALSE;
    }
536
    return pcs->reg_cb32(hProcess, action, data32, (PVOID)(DWORD_PTR)user);
537 538 539 540 541 542 543
}

/******************************************************************
 *		pcs_callback (internal)
 */
BOOL pcs_callback(const struct process* pcs, ULONG action, void* data)
{
544 545
    IMAGEHLP_DEFERRED_SYMBOL_LOAD64 idsl;

546
    TRACE("%p %u %p\n", pcs, action, data);
547

548
    if (!pcs->reg_cb) return FALSE;
549
    if (!pcs->reg_is_unicode)
550
    {
551
        IMAGEHLP_DEFERRED_SYMBOL_LOADW64*   idslW;
552 553 554 555 556 557 558 559 560 561 562 563

        switch (action)
        {
        case CBA_DEBUG_INFO:
        case CBA_DEFERRED_SYMBOL_LOAD_CANCEL:
        case CBA_SET_OPTIONS:
        case CBA_SYMBOLS_UNLOADED:
            break;
        case CBA_DEFERRED_SYMBOL_LOAD_COMPLETE:
        case CBA_DEFERRED_SYMBOL_LOAD_FAILURE:
        case CBA_DEFERRED_SYMBOL_LOAD_PARTIAL:
        case CBA_DEFERRED_SYMBOL_LOAD_START:
564
            idslW = data;
565 566 567 568 569 570 571 572
            idsl.SizeOfStruct = sizeof(idsl);
            idsl.BaseOfImage = idslW->BaseOfImage;
            idsl.CheckSum = idslW->CheckSum;
            idsl.TimeDateStamp = idslW->TimeDateStamp;
            WideCharToMultiByte(CP_ACP, 0, idslW->FileName, -1,
                                idsl.FileName, sizeof(idsl.FileName), NULL, NULL);
            idsl.Reparse = idslW->Reparse;
            data = &idsl;
573 574 575 576 577
            break;
        case CBA_DUPLICATE_SYMBOL:
        case CBA_EVENT:
        case CBA_READ_MEMORY:
        default:
578
            FIXME("No mapping for action %u\n", action);
579 580 581
            return FALSE;
        }
    }
582 583 584
    return pcs->reg_cb(pcs->handle, action, (ULONG64)(DWORD_PTR)data, pcs->reg_user);
}

585 586 587 588 589
/******************************************************************
 *		sym_register_cb
 *
 * Helper for registering a callback.
 */
590
static BOOL sym_register_cb(HANDLE hProcess,
591
                            PSYMBOL_REGISTERED_CALLBACK64 cb,
592
                            PSYMBOL_REGISTERED_CALLBACK cb32,
593 594 595 596 597 598
                            DWORD64 user, BOOL unicode)
{
    struct process* pcs = process_find_by_handle(hProcess);

    if (!pcs) return FALSE;
    pcs->reg_cb = cb;
599
    pcs->reg_cb32 = cb32;
600 601 602 603 604 605
    pcs->reg_is_unicode = unicode;
    pcs->reg_user = user;

    return TRUE;
}

606 607 608 609 610 611 612
/***********************************************************************
 *		SymRegisterCallback (DBGHELP.@)
 */
BOOL WINAPI SymRegisterCallback(HANDLE hProcess, 
                                PSYMBOL_REGISTERED_CALLBACK CallbackFunction,
                                PVOID UserContext)
{
613 614
    TRACE("(%p, %p, %p)\n", 
          hProcess, CallbackFunction, UserContext);
615
    return sym_register_cb(hProcess, reg_cb64to32, CallbackFunction, (DWORD_PTR)UserContext, FALSE);
616 617
}

618 619 620 621 622 623 624
/***********************************************************************
 *		SymRegisterCallback64 (DBGHELP.@)
 */
BOOL WINAPI SymRegisterCallback64(HANDLE hProcess, 
                                  PSYMBOL_REGISTERED_CALLBACK64 CallbackFunction,
                                  ULONG64 UserContext)
{
625
    TRACE("(%p, %p, %s)\n", 
626
          hProcess, CallbackFunction, wine_dbgstr_longlong(UserContext));
627
    return sym_register_cb(hProcess, CallbackFunction, NULL, UserContext, FALSE);
628
}
629

630 631 632 633 634 635 636 637 638
/***********************************************************************
 *		SymRegisterCallbackW64 (DBGHELP.@)
 */
BOOL WINAPI SymRegisterCallbackW64(HANDLE hProcess, 
                                   PSYMBOL_REGISTERED_CALLBACK64 CallbackFunction,
                                   ULONG64 UserContext)
{
    TRACE("(%p, %p, %s)\n", 
          hProcess, CallbackFunction, wine_dbgstr_longlong(UserContext));
639
    return sym_register_cb(hProcess, CallbackFunction, NULL, UserContext, TRUE);
640 641
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
/* This is imagehlp version not dbghelp !! */
static API_VERSION api_version = { 4, 0, 2, 0 };

/***********************************************************************
 *           ImagehlpApiVersion (DBGHELP.@)
 */
LPAPI_VERSION WINAPI ImagehlpApiVersion(VOID)
{
    return &api_version;
}

/***********************************************************************
 *           ImagehlpApiVersionEx (DBGHELP.@)
 */
LPAPI_VERSION WINAPI ImagehlpApiVersionEx(LPAPI_VERSION AppVersion)
{
    if (!AppVersion) return NULL;

    AppVersion->MajorVersion = api_version.MajorVersion;
    AppVersion->MinorVersion = api_version.MinorVersion;
    AppVersion->Revision = api_version.Revision;
    AppVersion->Reserved = api_version.Reserved;

    return AppVersion;
}
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

/******************************************************************
 *		ExtensionApiVersion (DBGHELP.@)
 */
LPEXT_API_VERSION WINAPI ExtensionApiVersion(void)
{
    static EXT_API_VERSION      eav = {5, 5, 5, 0};
    return &eav;
}

/******************************************************************
 *		WinDbgExtensionDllInit (DBGHELP.@)
 */
void WINAPI WinDbgExtensionDllInit(PWINDBG_EXTENSION_APIS lpExtensionApis,
                                   unsigned short major, unsigned short minor)
{
}