symbol.c 62.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * File symbol.c - management of symbols (lexical tree)
 *
 * Copyright (C) 1993, Eric Youngdale.
 *               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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
24

25
#include "config.h"
26

27 28 29 30 31 32
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <assert.h>
33 34 35
#ifdef HAVE_REGEX_H
# include <regex.h>
#endif
36

37
#include "wine/debug.h"
38
#include "dbghelp_private.h"
39
#include "winnls.h"
40 41

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42
WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
43

44
static inline int cmp_addr(ULONG64 a1, ULONG64 a2)
45 46 47 48 49 50
{
    if (a1 > a2) return 1;
    if (a1 < a2) return -1;
    return 0;
}

51
static inline int cmp_sorttab_addr(const struct module* module, int idx, ULONG64 addr)
52
{
53
    ULONG64     ref;
54 55 56 57 58 59 60

    symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &ref);
    return cmp_addr(ref, addr);
}

int symt_cmp_addr(const void* p1, const void* p2)
{
61 62
    const struct symt*  sym1 = *(const struct symt* const *)p1;
    const struct symt*  sym2 = *(const struct symt* const *)p2;
63
    ULONG64     a1, a2;
64 65 66 67 68 69

    symt_get_info(sym1, TI_GET_ADDRESS, &a1);
    symt_get_info(sym2, TI_GET_ADDRESS, &a2);
    return cmp_addr(a1, a2);
}

70 71
#ifdef HAVE_REGEX_H

72 73 74 75 76 77 78 79 80
/* transforms a dbghelp's regular expression into a POSIX one
 * Here are the valid dbghelp reg ex characters:
 *      *       0 or more characters
 *      ?       a single character
 *      []      list
 *      #       0 or more of preceding char
 *      +       1 or more of preceding char
 *      escapes \ on #, ?, [, ], *, +. don't work on -
 */
81
static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
82
{
83
    char *mask, *p;
84
    BOOL        in_escape = FALSE;
85
    unsigned    flags = REG_NOSUB;
86

87 88 89 90
    if (numchar == -1) numchar = strlen( str );

    p = mask = HeapAlloc( GetProcessHeap(), 0, 2 * numchar + 3 );
    *p++ = '^';
91 92

    while (*str && numchar--)
93 94 95 96
    {
        /* FIXME: this shouldn't be valid on '-' */
        if (in_escape)
        {
97 98
            *p++ = '\\';
            *p++ = *str;
99 100 101 102 103
            in_escape = FALSE;
        }
        else switch (*str)
        {
        case '\\': in_escape = TRUE; break;
104 105 106
        case '*':  *p++ = '.'; *p++ = '*'; break;
        case '?':  *p++ = '.'; break;
        case '#':  *p++ = '*'; break;
107
        /* escape some valid characters in dbghelp reg exp:s */
108
        case '$':  *p++ = '\\'; *p++ = '$'; break;
109
        /* +, [, ], - are the same in dbghelp & POSIX, use them as any other char */
110
        default:   *p++ = *str; break;
111 112 113 114 115
        }
        str++;
    }
    if (in_escape)
    {
116 117
        *p++ = '\\';
        *p++ = '\\';
118
    }
119 120
    *p++ = '$';
    *p = 0;
121 122
    if (_case) flags |= REG_ICASE;
    if (regcomp(re, mask, flags)) FIXME("Couldn't compile %s\n", mask);
123 124 125
    HeapFree(GetProcessHeap(), 0, mask);
}

126 127 128 129 130
static BOOL compile_file_regex(regex_t* re, const char* srcfile)
{
    char *mask, *p;
    BOOL ret;

131 132
    if (!srcfile || !*srcfile) return regcomp(re, ".*", REG_NOSUB);

133 134
    p = mask = HeapAlloc(GetProcessHeap(), 0, 5 * strlen(srcfile) + 4);
    *p++ = '^';
135
    while (*srcfile)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    {
        switch (*srcfile)
        {
        case '\\':
        case '/':
            *p++ = '[';
            *p++ = '\\';
            *p++ = '\\';
            *p++ = '/';
            *p++ = ']';
            break;
        case '.':
            *p++ = '\\';
            *p++ = '.';
            break;
        default:
            *p++ = *srcfile;
            break;
        }
        srcfile++;
    }
    *p++ = '$';
    *p = 0;
    ret = !regcomp(re, mask, REG_NOSUB);
    HeapFree(GetProcessHeap(), 0, mask);
    if (!ret)
    {
        FIXME("Couldn't compile %s\n", mask);
        SetLastError(ERROR_INVALID_PARAMETER);
    }
    return ret;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static int match_regexp( const regex_t *re, const char *str )
{
    return !regexec( re, str, 0, NULL, 0 );
}

#else /* HAVE_REGEX_H */

/* if we don't have regexp support, fall back to a simple string comparison */

typedef struct
{
    char *str;
    BOOL  icase;
} regex_t;

static void compile_regex(const char* str, int numchar, regex_t* re, BOOL _case)
{
    if (numchar == -1) numchar = strlen( str );

    re->str = HeapAlloc( GetProcessHeap(), 0, numchar + 1 );
    memcpy( re->str, str, numchar );
    re->str[numchar] = 0;
    re->icase = _case;
}

194 195
static BOOL compile_file_regex(regex_t* re, const char* srcfile)
{
196 197
    if (!srcfile || !*srcfile) re->str = NULL;
    else compile_regex( srcfile, -1, re, FALSE );
198 199 200
    return TRUE;
}

201 202
static int match_regexp( const regex_t *re, const char *str )
{
203
    if (!re->str) return 1;
204 205 206 207 208 209 210 211 212 213 214
    if (re->icase) return !lstrcmpiA( re->str, str );
    return !strcmp( re->str, str );
}

static void regfree( regex_t *re )
{
    HeapFree( GetProcessHeap(), 0, re->str );
}

#endif /* HAVE_REGEX_H */

215 216
struct symt_compiland* symt_new_compiland(struct module* module, 
                                          unsigned long address, unsigned src_idx)
217 218 219
{
    struct symt_compiland*    sym;

220
    TRACE_(dbghelp_symt)("Adding compiland symbol %s:%s\n",
221
                         debugstr_w(module->module.ModuleName), source_get(module, src_idx));
222 223 224
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagCompiland;
225
        sym->address  = address;
226
        sym->source   = src_idx;
227 228 229 230 231 232 233 234 235 236 237 238 239 240
        vector_init(&sym->vchildren, sizeof(struct symt*), 32);
    }
    return sym;
}

struct symt_public* symt_new_public(struct module* module, 
                                    struct symt_compiland* compiland,
                                    const char* name,
                                    unsigned long address, unsigned size,
                                    BOOL in_code, BOOL is_func)
{
    struct symt_public* sym;
    struct symt**       p;

241
    TRACE_(dbghelp_symt)("Adding public symbol %s:%s @%lx\n",
242
                         debugstr_w(module->module.ModuleName), name, address);
243
    if ((dbghelp_options & SYMOPT_AUTO_PUBLICS) &&
244
        symt_find_nearest(module, address) != NULL)
245
        return NULL;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag      = SymTagPublicSymbol;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->container     = compiland ? &compiland->symt : NULL;
        sym->address       = address;
        sym->size          = size;
        sym->in_code       = in_code;
        sym->is_function   = is_func;
        if (compiland)
        {
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

struct symt_data* symt_new_global_variable(struct module* module, 
                                           struct symt_compiland* compiland, 
                                           const char* name, unsigned is_static,
                                           unsigned long addr, unsigned long size,
                                           struct symt* type)
{
    struct symt_data*   sym;
    struct symt**       p;
274
    DWORD64             tsz;
275

276
    TRACE_(dbghelp_symt)("Adding global symbol %s:%s @%lx %p\n",
277
                         debugstr_w(module->module.ModuleName), name, addr, type);
278 279 280 281 282 283 284 285 286
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag      = SymTagData;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->kind          = is_static ? DataIsFileStatic : DataIsGlobal;
        sym->container     = compiland ? &compiland->symt : NULL;
        sym->type          = type;
287
        sym->u.var.offset  = addr;
288 289 290
        if (type && size && symt_get_info(type, TI_GET_LENGTH, &tsz))
        {
            if (tsz != size)
291
                FIXME("Size mismatch for %s.%s between type (%s) and src (%lu)\n",
292
                      debugstr_w(module->module.ModuleName), name,
293
                      wine_dbgstr_longlong(tsz), size);
294
        }
295 296 297 298 299 300 301 302 303 304 305 306 307
        if (compiland)
        {
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

struct symt_function* symt_new_function(struct module* module, 
                                        struct symt_compiland* compiland, 
                                        const char* name,
                                        unsigned long addr, unsigned long size,
308
                                        struct symt* sig_type)
309 310 311 312
{
    struct symt_function*       sym;
    struct symt**               p;

313
    TRACE_(dbghelp_symt)("Adding global function %s:%s @%lx-%lx\n",
314
                         debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
315 316

    assert(!sig_type || sig_type->tag == SymTagFunctionType);
317 318 319 320 321 322 323
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag  = SymTagFunction;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->container = &compiland->symt;
324
        sym->address   = addr;
325
        sym->type      = sig_type;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        sym->size      = size;
        vector_init(&sym->vlines,  sizeof(struct line_info), 64);
        vector_init(&sym->vchildren, sizeof(struct symt*), 8);
        if (compiland)
        {
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

void symt_add_func_line(struct module* module, struct symt_function* func,
                        unsigned source_idx, int line_num, unsigned long offset)
{
    struct line_info*   dli;
    BOOL                last_matches = FALSE;
343
    int                 i;
344 345 346

    if (func == NULL || !(dbghelp_options & SYMOPT_LOAD_LINES)) return;

347 348 349
    TRACE_(dbghelp_symt)("(%p)%s:%lx %s:%u\n", 
                         func, func->hash_elt.name, offset, 
                         source_get(module, source_idx), line_num);
350 351 352

    assert(func->symt.tag == SymTagFunction);

353
    for (i=vector_length(&func->vlines)-1; i>=0; i--)
354
    {
355
        dli = vector_at(&func->vlines, i);
356
        if (dli->is_source_file)
357 358 359 360 361 362 363 364 365 366
        {
            last_matches = (source_idx == dli->u.source_file);
            break;
        }
    }

    if (!last_matches)
    {
        /* we shouldn't have line changes on first line of function */
        dli = vector_add(&func->vlines, &module->pool);
367 368 369 370
        dli->is_source_file = 1;
        dli->is_first       = dli->is_last = 0;
        dli->line_number    = 0;
        dli->u.source_file  = source_idx;
371 372
    }
    dli = vector_add(&func->vlines, &module->pool);
373 374 375
    dli->is_source_file = 0;
    dli->is_first       = dli->is_last = 0;
    dli->line_number    = line_num;
376
    dli->u.pc_offset    = func->address + offset;
377 378
}

379
/******************************************************************
380
 *             symt_add_func_local
381 382
 *
 * Adds a new local/parameter to a given function:
383
 * In any cases, dt tells whether it's a local variable or a parameter
384 385
 * If regno it's not 0:
 *      - then variable is stored in a register
386
 *      - otherwise, value is referenced by register + offset
387
 * Otherwise, the variable is stored on the stack:
388
 *      - offset is then the offset from the frame register
389
 */
390 391
struct symt_data* symt_add_func_local(struct module* module, 
                                      struct symt_function* func, 
392
                                      enum DataKind dt,
393
                                      const struct location* loc,
394 395 396 397 398 399
                                      struct symt_block* block, 
                                      struct symt* type, const char* name)
{
    struct symt_data*   locsym;
    struct symt**       p;

400
    TRACE_(dbghelp_symt)("Adding local symbol (%s:%s): %s %p\n",
401
                         debugstr_w(module->module.ModuleName), func->hash_elt.name,
402
                         name, type);
403 404 405 406 407

    assert(func);
    assert(func->symt.tag == SymTagFunction);
    assert(dt == DataIsParam || dt == DataIsLocal);

408 409 410 411
    locsym = pool_alloc(&module->pool, sizeof(*locsym));
    locsym->symt.tag      = SymTagData;
    locsym->hash_elt.name = pool_strdup(&module->pool, name);
    locsym->hash_elt.next = NULL;
412
    locsym->kind          = dt;
413 414
    locsym->container     = &block->symt;
    locsym->type          = type;
415
    locsym->u.var         = *loc;
416 417 418 419 420 421 422 423
    if (block)
        p = vector_add(&block->vchildren, &module->pool);
    else
        p = vector_add(&func->vchildren, &module->pool);
    *p = &locsym->symt;
    return locsym;
}

424

425 426 427
struct symt_block* symt_open_func_block(struct module* module, 
                                        struct symt_function* func,
                                        struct symt_block* parent_block, 
428
                                        unsigned pc, unsigned len)
429 430 431 432 433 434 435 436 437
{
    struct symt_block*  block;
    struct symt**       p;

    assert(func);
    assert(func->symt.tag == SymTagFunction);

    assert(!parent_block || parent_block->symt.tag == SymTagBlock);
    block = pool_alloc(&module->pool, sizeof(*block));
438
    block->symt.tag = SymTagBlock;
439
    block->address  = func->address + pc;
440
    block->size     = len;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    block->container = parent_block ? &parent_block->symt : &func->symt;
    vector_init(&block->vchildren, sizeof(struct symt*), 4);
    if (parent_block)
        p = vector_add(&parent_block->vchildren, &module->pool);
    else
        p = vector_add(&func->vchildren, &module->pool);
    *p = &block->symt;

    return block;
}

struct symt_block* symt_close_func_block(struct module* module, 
                                         struct symt_function* func,
                                         struct symt_block* block, unsigned pc)
{
456
    assert(func);
457 458
    assert(func->symt.tag == SymTagFunction);

459
    if (pc) block->size = func->address + pc - block->address;
460 461 462 463
    return (block->container->tag == SymTagBlock) ? 
        GET_ENTRY(block->container, struct symt_block, symt) : NULL;
}

464 465 466 467 468
struct symt_hierarchy_point* symt_add_function_point(struct module* module,
                                                     struct symt_function* func,
                                                     enum SymTagEnum point,
                                                     const struct location* loc,
                                                     const char* name)
469
{
470
    struct symt_hierarchy_point*sym;
471 472 473 474 475
    struct symt**               p;

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = point;
476
        sym->parent   = &func->symt;
477
        sym->loc      = *loc;
478
        sym->hash_elt.name = name ? pool_strdup(&module->pool, name) : NULL;
479 480 481 482 483 484
        p = vector_add(&func->vchildren, &module->pool);
        *p = &sym->symt;
    }
    return sym;
}

485 486 487 488 489
BOOL symt_normalize_function(struct module* module, struct symt_function* func)
{
    unsigned            len;
    struct line_info*   dli;

490
    assert(func);
491 492 493 494 495 496 497 498 499 500 501
    /* We aren't adding any more locals or line numbers to this function.
     * Free any spare memory that we might have allocated.
     */
    assert(func->symt.tag == SymTagFunction);

/* EPP     vector_pool_normalize(&func->vlines,    &module->pool); */
/* EPP     vector_pool_normalize(&func->vchildren, &module->pool); */

    len = vector_length(&func->vlines);
    if (len--)
    {
502 503
        dli = vector_at(&func->vlines,   0);  dli->is_first = 1;
        dli = vector_at(&func->vlines, len);  dli->is_last  = 1;
504 505 506 507
    }
    return TRUE;
}

508 509 510 511 512 513 514
struct symt_thunk* symt_new_thunk(struct module* module, 
                                  struct symt_compiland* compiland, 
                                  const char* name, THUNK_ORDINAL ord,
                                  unsigned long addr, unsigned long size)
{
    struct symt_thunk*  sym;

515
    TRACE_(dbghelp_symt)("Adding global thunk %s:%s @%lx-%lx\n",
516
                         debugstr_w(module->module.ModuleName), name, addr, addr + size - 1);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag  = SymTagThunk;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->container = &compiland->symt;
        sym->address   = addr;
        sym->size      = size;
        sym->ordinal   = ord;
        if (compiland)
        {
            struct symt**       p;
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

538 539 540 541 542 543 544 545
struct symt_data* symt_new_constant(struct module* module,
                                    struct symt_compiland* compiland,
                                    const char* name, struct symt* type,
                                    const VARIANT* v)
{
    struct symt_data*  sym;

    TRACE_(dbghelp_symt)("Adding constant value %s:%s\n",
546
                         debugstr_w(module->module.ModuleName), name);
547 548 549 550 551 552 553 554 555 556 557 558 559 560

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag      = SymTagData;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->kind          = DataIsConstant;
        sym->container     = compiland ? &compiland->symt : NULL;
        sym->type          = type;
        sym->u.value       = *v;
        if (compiland)
        {
            struct symt**       p;
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

struct symt_hierarchy_point* symt_new_label(struct module* module,
                                            struct symt_compiland* compiland,
                                            const char* name, unsigned long address)
{
    struct symt_hierarchy_point*        sym;

    TRACE_(dbghelp_symt)("Adding global label value %s:%s\n",
                         debugstr_w(module->module.ModuleName), name);

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag      = SymTagLabel;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_symbols, &sym->hash_elt);
        module->sortlist_valid = FALSE;
        sym->loc.kind      = loc_absolute;
        sym->loc.offset    = address;
        sym->parent        = compiland ? &compiland->symt : NULL;
        if (compiland)
        {
            struct symt**       p;
589 590 591 592 593 594 595
            p = vector_add(&compiland->vchildren, &module->pool);
            *p = &sym->symt;
        }
    }
    return sym;
}

596
/* expect sym_info->MaxNameLen to be set before being called */
597
static void symt_fill_sym_info(const struct module_pair* pair,
598
                               const struct symt_function* func,
599 600 601
                               const struct symt* sym, SYMBOL_INFO* sym_info)
{
    const char* name;
602
    DWORD64 size;
603

604 605 606
    if (!symt_get_info(sym, TI_GET_TYPE, &sym_info->TypeIndex))
        sym_info->TypeIndex = 0;
    sym_info->info = (DWORD)sym;
Eric Pouech's avatar
Eric Pouech committed
607
    sym_info->Reserved[0] = sym_info->Reserved[1] = 0;
608
    if (!symt_get_info(sym, TI_GET_LENGTH, &size) &&
Eric Pouech's avatar
Eric Pouech committed
609 610
        (!sym_info->TypeIndex ||
         !symt_get_info((struct symt*)sym_info->TypeIndex, TI_GET_LENGTH, &size)))
611
        size = 0;
612
    sym_info->Size = (DWORD)size;
Eric Pouech's avatar
Eric Pouech committed
613
    sym_info->ModBase = pair->requested->module.BaseOfImage;
614
    sym_info->Flags = 0;
Eric Pouech's avatar
Eric Pouech committed
615 616
    sym_info->Value = 0;

617 618 619 620
    switch (sym->tag)
    {
    case SymTagData:
        {
621
            const struct symt_data*  data = (const struct symt_data*)sym;
622
            switch (data->kind)
623
            {
624
            case DataIsParam:
625 626
                sym_info->Flags |= SYMFLAG_PARAMETER;
                /* fall through */
627
            case DataIsLocal:
628
                {
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
                    struct location loc = data->u.var;

                    if (loc.kind >= loc_user)
                        pair->effective->loc_compute(pair->pcs, pair->effective, func, &loc);

                    switch (loc.kind)
                    {
                    case loc_error:
                        /* for now we report error cases as a negative register number */
                        sym_info->Flags |= SYMFLAG_LOCAL;
                        /* fall through */
                    case loc_register:
                        sym_info->Flags |= SYMFLAG_REGISTER;
                        sym_info->Register = loc.reg;
                        sym_info->Address = 0;
                        break;
                    case loc_regrel:
                        sym_info->Flags |= SYMFLAG_LOCAL | SYMFLAG_REGREL;
                        /* FIXME: it's i386 dependent !!! */
                        sym_info->Register = loc.reg ? loc.reg : CV_REG_EBP;
                        sym_info->Address = loc.offset;
                        break;
                    default:
                        FIXME("Shouldn't happen (kind=%d), debug reader backend is broken\n", loc.kind);
                        assert(0);
                    }
655
                }
656
                break;
657 658
            case DataIsGlobal:
            case DataIsFileStatic:
659 660 661
                symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
                sym_info->Register = 0;
                break;
662
            case DataIsConstant:
663
                sym_info->Flags |= SYMFLAG_VALUEPRESENT;
664 665 666 667 668 669 670 671
                switch (data->u.value.n1.n2.vt)
                {
                case VT_I4:  sym_info->Value = (ULONG)data->u.value.n1.n2.n3.lVal; break;
                case VT_I2:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.iVal; break;
                case VT_I1:  sym_info->Value = (ULONG)(long)data->u.value.n1.n2.n3.cVal; break;
                case VT_UI4: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.ulVal; break;
                case VT_UI2: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.uiVal; break;
                case VT_UI1: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.bVal; break;
672 673
                case VT_I1 | VT_BYREF: sym_info->Value = (ULONG)data->u.value.n1.n2.n3.byref; break;
                default:
674
                    FIXME("Unsupported variant type (%u)\n", data->u.value.n1.n2.vt);
675 676
                    sym_info->Value = 0;
                    break;
677
                }
678 679
                break;
            default:
680
                FIXME("Unhandled kind (%u) in sym data\n", data->kind);
681 682 683 684 685 686 687 688 689 690 691
            }
        }
        break;
    case SymTagPublicSymbol:
        sym_info->Flags |= SYMFLAG_EXPORT;
        symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
        break;
    case SymTagFunction:
        sym_info->Flags |= SYMFLAG_FUNCTION;
        symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
        break;
692 693 694 695
    case SymTagThunk:
        sym_info->Flags |= SYMFLAG_THUNK;
        symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
        break;
696 697 698 699 700 701 702 703 704 705
    default:
        symt_get_info(sym, TI_GET_ADDRESS, &sym_info->Address);
        sym_info->Register = 0;
        break;
    }
    sym_info->Scope = 0; /* FIXME */
    sym_info->Tag = sym->tag;
    name = symt_get_name(sym);
    if (sym_info->MaxNameLen)
    {
706
        if (sym->tag != SymTagPublicSymbol || !(dbghelp_options & SYMOPT_UNDNAME) ||
707 708
            (sym_info->NameLen = UnDecorateSymbolName(name, sym_info->Name,
                                                      sym_info->MaxNameLen, UNDNAME_NAME_ONLY) == 0))
709 710
        {
            sym_info->NameLen = min(strlen(name), sym_info->MaxNameLen - 1);
711
            memcpy(sym_info->Name, name, sym_info->NameLen);
712 713
            sym_info->Name[sym_info->NameLen] = '\0';
        }
714
    }
715
    TRACE_(dbghelp_symt)("%p => %s %u %s\n",
716 717
                         sym, sym_info->Name, sym_info->Size,
                         wine_dbgstr_longlong(sym_info->Address));
718 719
}

720 721 722 723 724
struct sym_enum
{
    PSYM_ENUMERATESYMBOLS_CALLBACK      cb;
    PVOID                               user;
    SYMBOL_INFO*                        sym_info;
725 726 727
    DWORD                               index;
    DWORD                               tag;
    DWORD64                             addr;
728 729
    char                                buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
};
730

731
static BOOL send_symbol(const struct sym_enum* se, const struct module_pair* pair,
732
                        const struct symt_function* func, const struct symt* sym)
733
{
734
    symt_fill_sym_info(pair, func, sym, se->sym_info);
735 736 737 738 739 740
    if (se->index && se->sym_info->info != se->index) return FALSE;
    if (se->tag && se->sym_info->Tag != se->tag) return FALSE;
    if (se->addr && !(se->addr >= se->sym_info->Address && se->addr < se->sym_info->Address + se->sym_info->Size)) return FALSE;
    return !se->cb(se->sym_info, se->sym_info->Size, se->user);
}

741
static BOOL symt_enum_module(struct module_pair* pair, const regex_t* regex,
742
                             const struct sym_enum* se)
743 744 745 746 747
{
    void*                       ptr;
    struct symt_ht*             sym = NULL;
    struct hash_table_iter      hti;

Eric Pouech's avatar
Eric Pouech committed
748
    hash_table_iter_init(&pair->effective->ht_symbols, &hti, NULL);
749 750 751
    while ((ptr = hash_table_iter_up(&hti)))
    {
        sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
752
        if (sym->hash_elt.name && match_regexp(regex, sym->hash_elt.name))
753
        {
754 755
            se->sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
            se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
756
            if (send_symbol(se, pair, NULL, &sym->symt)) return TRUE;
757 758
        }
    }   
759
    return FALSE;
760 761 762 763 764 765 766 767 768 769 770 771
}

/***********************************************************************
 *              resort_symbols
 *
 * Rebuild sorted list of symbols for a module.
 */
static BOOL resort_symbols(struct module* module)
{
    void*                       ptr;
    struct symt_ht*             sym;
    struct hash_table_iter      hti;
772
    ULONG64                     addr;
773

774 775
    if (!(module->module.NumSyms = module->ht_symbols.num_elts))
        return FALSE;
776 777 778 779
    
    if (module->addr_sorttab)
        module->addr_sorttab = HeapReAlloc(GetProcessHeap(), 0,
                                           module->addr_sorttab, 
780
                                           module->module.NumSyms * sizeof(struct symt_ht*));
781 782
    else
        module->addr_sorttab = HeapAlloc(GetProcessHeap(), 0,
783
                                         module->module.NumSyms * sizeof(struct symt_ht*));
784 785
    if (!module->addr_sorttab) return FALSE;

786
    module->num_sorttab = 0;
787 788 789 790 791
    hash_table_iter_init(&module->ht_symbols, &hti, NULL);
    while ((ptr = hash_table_iter_up(&hti)))
    {
        sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
        assert(sym);
792 793 794 795 796 797 798
        /* Don't store in sorttab symbol without address, they are of
         * no use here (e.g. constant values)
         * As the number of those symbols is very couple (a couple per module)
         * we don't bother for the unused spots at the end of addr_sorttab
         */
        if (symt_get_info(&sym->symt, TI_GET_ADDRESS, &addr))
            module->addr_sorttab[module->num_sorttab++] = sym;
799
    }
800
    qsort(module->addr_sorttab, module->num_sorttab, sizeof(struct symt_ht*), symt_cmp_addr);
801 802 803
    return module->sortlist_valid = TRUE;
}

804 805 806 807 808 809 810 811 812 813 814 815
static void symt_get_length(struct symt* symt, ULONG64* size)
{
    DWORD       type_index;

    if (symt_get_info(symt, TI_GET_LENGTH, size) && *size)
        return;

    if (symt_get_info(symt, TI_GET_TYPE, &type_index) &&
        symt_get_info((struct symt*)type_index, TI_GET_LENGTH, size)) return;
    *size = 0x1000; /* arbitrary value */
}

816
/* assume addr is in module */
817
struct symt_ht* symt_find_nearest(struct module* module, DWORD addr)
818 819
{
    int         mid, high, low;
820
    ULONG64     ref_addr, ref_size;
821

822 823
    if (!module->sortlist_valid || !module->addr_sorttab)
    {
824
        if (!resort_symbols(module)) return NULL;
825
    }
826 827 828 829 830

    /*
     * Binary search to find closest symbol.
     */
    low = 0;
831
    high = module->num_sorttab;
832

833
    symt_get_info(&module->addr_sorttab[0]->symt, TI_GET_ADDRESS, &ref_addr);
834
    if (addr < ref_addr) return NULL;
835 836
    if (high)
    {
837
        symt_get_info(&module->addr_sorttab[high - 1]->symt, TI_GET_ADDRESS, &ref_addr);
838
        symt_get_length(&module->addr_sorttab[high - 1]->symt, &ref_size);
839
        if (addr >= ref_addr + ref_size) return NULL;
840
    }
841 842 843 844 845 846 847 848 849
    
    while (high > low + 1)
    {
        mid = (high + low) / 2;
        if (cmp_sorttab_addr(module, mid, addr) < 0)
            low = mid;
        else
            high = mid;
    }
850
    if (low != high && high != module->num_sorttab &&
851 852 853 854 855 856 857 858
        cmp_sorttab_addr(module, high, addr) <= 0)
        low = high;

    /* If found symbol is a public symbol, check if there are any other entries that
     * might also have the same address, but would get better information
     */
    if (module->addr_sorttab[low]->symt.tag == SymTagPublicSymbol)
    {   
859
        symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
860 861
        if (low > 0 &&
            module->addr_sorttab[low - 1]->symt.tag != SymTagPublicSymbol &&
862
            !cmp_sorttab_addr(module, low - 1, ref_addr))
863
            low--;
864
        else if (low < module->num_sorttab - 1 &&
865
                 module->addr_sorttab[low + 1]->symt.tag != SymTagPublicSymbol &&
866
                 !cmp_sorttab_addr(module, low + 1, ref_addr))
867 868
            low++;
    }
869 870
    /* finally check that we fit into the found symbol */
    symt_get_info(&module->addr_sorttab[low]->symt, TI_GET_ADDRESS, &ref_addr);
871
    if (addr < ref_addr) return NULL;
872
    symt_get_length(&module->addr_sorttab[low]->symt, &ref_size);
873
    if (addr >= ref_addr + ref_size) return NULL;
874

875
    return module->addr_sorttab[low];
876 877
}

878
static BOOL symt_enum_locals_helper(struct module_pair* pair,
879
                                    regex_t* preg, const struct sym_enum* se,
880
                                    struct symt_function* func, const struct vector* v)
881 882
{
    struct symt*        lsym = NULL;
883
    DWORD               pc = pair->pcs->ctx_frame.InstructionOffset;
884
    unsigned int        i;
885

886
    for (i=0; i<vector_length(v); i++)
887
    {
888
        lsym = *(struct symt**)vector_at(v, i);
889 890 891 892 893 894 895
        switch (lsym->tag)
        {
        case SymTagBlock:
            {
                struct symt_block*  block = (struct symt_block*)lsym;
                if (pc < block->address || block->address + block->size <= pc)
                    continue;
896
                if (!symt_enum_locals_helper(pair, preg, se, func, &block->vchildren))
897 898 899 900
                    return FALSE;
            }
            break;
        case SymTagData:
901
            if (match_regexp(preg, symt_get_name(lsym)))
902
            {
903
                if (send_symbol(se, pair, func, lsym)) return FALSE;
904 905
            }
            break;
906 907 908
        case SymTagLabel:
        case SymTagFuncDebugStart:
        case SymTagFuncDebugEnd:
909
        case SymTagCustom:
910
            break;
911 912 913 914 915 916 917 918
        default:
            FIXME("Unknown type: %u (%x)\n", lsym->tag, lsym->tag);
            assert(0);
        }
    }
    return TRUE;
}

919 920
static BOOL symt_enum_locals(struct process* pcs, const char* mask, 
                             const struct sym_enum* se)
921
{
Eric Pouech's avatar
Eric Pouech committed
922
    struct module_pair  pair;
923 924 925
    struct symt_ht*     sym;
    DWORD               pc = pcs->ctx_frame.InstructionOffset;

926 927
    se->sym_info->SizeOfStruct = sizeof(*se->sym_info);
    se->sym_info->MaxNameLen = sizeof(se->buffer) - sizeof(SYMBOL_INFO);
928

929 930 931
    pair.pcs = pcs;
    pair.requested = module_find_by_addr(pair.pcs, pc, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
932
    if ((sym = symt_find_nearest(pair.effective, pc)) == NULL) return FALSE;
933 934 935 936 937 938

    if (sym->symt.tag == SymTagFunction)
    {
        BOOL            ret;
        regex_t         preg;

939 940
        compile_regex(mask ? mask : "*", -1, &preg,
                      dbghelp_options & SYMOPT_CASE_INSENSITIVE);
941
        ret = symt_enum_locals_helper(&pair, &preg, se, (struct symt_function*)sym,
942 943 944 945 946
                                      &((struct symt_function*)sym)->vchildren);
        regfree(&preg);
        return ret;
        
    }
947
    return send_symbol(se, &pair, NULL, &sym->symt);
948 949
}

950 951 952
/******************************************************************
 *		copy_symbolW
 *
953
 * Helper for transforming an ANSI symbol info into a UNICODE one.
954 955
 * Assume that MaxNameLen is the same for both version (A & W).
 */
956
void copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si)
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
{
    siw->SizeOfStruct = si->SizeOfStruct;
    siw->TypeIndex = si->TypeIndex; 
    siw->Reserved[0] = si->Reserved[0];
    siw->Reserved[1] = si->Reserved[1];
    siw->Index = si->info; /* FIXME: see dbghelp.h */
    siw->Size = si->Size;
    siw->ModBase = si->ModBase;
    siw->Flags = si->Flags;
    siw->Value = si->Value;
    siw->Address = si->Address;
    siw->Register = si->Register;
    siw->Scope = si->Scope;
    siw->Tag = si->Tag;
    siw->NameLen = si->NameLen;
    siw->MaxNameLen = si->MaxNameLen;
    MultiByteToWideChar(CP_ACP, 0, si->Name, -1, siw->Name, siw->MaxNameLen);
}
975

976
/******************************************************************
977
 *		sym_enum
978
 *
979
 * Core routine for most of the enumeration of symbols
980
 */
981 982
static BOOL sym_enum(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
                     const struct sym_enum* se)
983
{
Eric Pouech's avatar
Eric Pouech committed
984
    struct module_pair  pair;
985 986
    const char*         bang;
    regex_t             mod_regex, sym_regex;
987

988
    pair.pcs = process_find_by_handle(hProcess);
989 990
    if (BaseOfDll == 0)
    {
991 992
        /* do local variables ? */
        if (!Mask || !(bang = strchr(Mask, '!')))
993
            return symt_enum_locals(pair.pcs, Mask, se);
994 995 996

        if (bang == Mask) return FALSE;

997
        compile_regex(Mask, bang - Mask, &mod_regex, TRUE);
998 999
        compile_regex(bang + 1, -1, &sym_regex, 
                      dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1000
        
1001
        for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1002
        {
1003
            if (pair.requested->type == DMT_PE && module_get_debug(&pair))
1004
            {
1005
                if (match_regexp(&mod_regex, pair.requested->module_name) &&
1006
                    symt_enum_module(&pair, &sym_regex, se))
1007 1008 1009 1010 1011
                    break;
            }
        }
        /* not found in PE modules, retry on the ELF ones
         */
1012
        if (!pair.requested && (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES))
1013
        {
1014
            for (pair.requested = pair.pcs->lmodules; pair.requested; pair.requested = pair.requested->next)
1015
            {
1016
                if ((pair.requested->type == DMT_ELF || pair.requested->type == DMT_MACHO) &&
1017 1018
                    !module_get_containee(pair.pcs, pair.requested) &&
                    module_get_debug(&pair))
1019
                {
1020
                    if (match_regexp(&mod_regex, pair.requested->module_name) &&
1021
                        symt_enum_module(&pair, &sym_regex, se))
1022 1023
                    break;
                }
1024 1025
            }
        }
1026 1027 1028
        regfree(&mod_regex);
        regfree(&sym_regex);
        return TRUE;
1029
    }
1030 1031
    pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
    if (!module_get_debug(&pair))
1032 1033 1034 1035
        return FALSE;

    /* we always ignore module name from Mask when BaseOfDll is defined */
    if (Mask && (bang = strchr(Mask, '!')))
1036
    {
1037 1038
        if (bang == Mask) return FALSE;
        Mask = bang + 1;
1039
    }
1040

1041
    compile_regex(Mask ? Mask : "*", -1, &sym_regex, 
Eric Pouech's avatar
Eric Pouech committed
1042
                  dbghelp_options & SYMOPT_CASE_INSENSITIVE);
1043
    symt_enum_module(&pair, &sym_regex, se);
1044 1045
    regfree(&sym_regex);

1046 1047 1048
    return TRUE;
}

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
/******************************************************************
 *		SymEnumSymbols (DBGHELP.@)
 *
 * cases BaseOfDll = 0
 *      !foo fails always (despite what MSDN states)
 *      RE1!RE2 looks up all modules matching RE1, and in all these modules, lookup RE2
 *      no ! in Mask, lookup in local Context
 * cases BaseOfDll != 0
 *      !foo fails always (despite what MSDN states)
 *      RE1!RE2 gets RE2 from BaseOfDll (whatever RE1 is)
 */
BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask,
                           PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
                           PVOID UserContext)
{
    struct sym_enum     se;

    TRACE("(%p %s %s %p %p)\n", 
          hProcess, wine_dbgstr_longlong(BaseOfDll), debugstr_a(Mask),
          EnumSymbolsCallback, UserContext);

    se.cb = EnumSymbolsCallback;
    se.user = UserContext;
1072 1073 1074
    se.index = 0;
    se.tag = 0;
    se.addr = 0;
1075 1076 1077 1078 1079
    se.sym_info = (PSYMBOL_INFO)se.buffer;

    return sym_enum(hProcess, BaseOfDll, Mask, &se);
}

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
struct sym_enumW
{
    PSYM_ENUMERATESYMBOLS_CALLBACKW     cb;
    void*                               ctx;
    PSYMBOL_INFOW                       sym_info;
    char                                buffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME];

};
    
static BOOL CALLBACK sym_enumW(PSYMBOL_INFO si, ULONG size, PVOID ctx)
{
    struct sym_enumW*   sew = ctx;

    copy_symbolW(sew->sym_info, si);

    return (sew->cb)(sew->sym_info, size, sew->ctx);
}

/******************************************************************
 *		SymEnumSymbolsW (DBGHELP.@)
 *
 */
BOOL WINAPI SymEnumSymbolsW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR Mask,
                            PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
                            PVOID UserContext)
{
    struct sym_enumW    sew;
    BOOL                ret = FALSE;
    char*               maskA = NULL;

    sew.ctx = UserContext;
    sew.cb = EnumSymbolsCallback;
    sew.sym_info = (PSYMBOL_INFOW)sew.buffer;

    if (Mask)
    {
        unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
        maskA = HeapAlloc(GetProcessHeap(), 0, len);
        if (!maskA) return FALSE;
        WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
    }
    ret = SymEnumSymbols(hProcess, BaseOfDll, maskA, sym_enumW, &sew);
    HeapFree(GetProcessHeap(), 0, maskA);

    return ret;
}

1127 1128 1129 1130 1131 1132 1133 1134
struct sym_enumerate
{
    void*                       ctx;
    PSYM_ENUMSYMBOLS_CALLBACK   cb;
};

static BOOL CALLBACK sym_enumerate_cb(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
{
1135
    struct sym_enumerate*       se = ctx;
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
}

/***********************************************************************
 *		SymEnumerateSymbols (DBGHELP.@)
 */
BOOL WINAPI SymEnumerateSymbols(HANDLE hProcess, DWORD BaseOfDll,
                                PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback, 
                                PVOID UserContext)
{
    struct sym_enumerate        se;

    se.ctx = UserContext;
    se.cb  = EnumSymbolsCallback;
    
    return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb, &se);
}

1154 1155 1156 1157 1158 1159 1160 1161
struct sym_enumerate64
{
    void*                       ctx;
    PSYM_ENUMSYMBOLS_CALLBACK64 cb;
};

static BOOL CALLBACK sym_enumerate_cb64(PSYMBOL_INFO syminfo, ULONG size, void* ctx)
{
1162
    struct sym_enumerate64*     se = ctx;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    return (se->cb)(syminfo->Name, syminfo->Address, syminfo->Size, se->ctx);
}

/***********************************************************************
 *              SymEnumerateSymbols64 (DBGHELP.@)
 */
BOOL WINAPI SymEnumerateSymbols64(HANDLE hProcess, DWORD64 BaseOfDll,
                                  PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,
                                  PVOID UserContext)
{
    struct sym_enumerate64      se;

    se.ctx = UserContext;
    se.cb  = EnumSymbolsCallback;

    return SymEnumSymbols(hProcess, BaseOfDll, NULL, sym_enumerate_cb64, &se);
}

1181 1182 1183 1184
/******************************************************************
 *		SymFromAddr (DBGHELP.@)
 *
 */
1185 1186
BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, 
                        DWORD64* Displacement, PSYMBOL_INFO Symbol)
1187
{
Eric Pouech's avatar
Eric Pouech committed
1188
    struct module_pair  pair;
1189 1190
    struct symt_ht*     sym;

1191 1192 1193 1194
    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, Address, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
1195
    if ((sym = symt_find_nearest(pair.effective, Address)) == NULL) return FALSE;
1196

1197
    symt_fill_sym_info(&pair, NULL, &sym->symt, Symbol);
1198
    *Displacement = Address - Symbol->Address;
1199 1200 1201
    return TRUE;
}

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
/******************************************************************
 *		SymFromAddrW (DBGHELP.@)
 *
 */
BOOL WINAPI SymFromAddrW(HANDLE hProcess, DWORD64 Address, 
                         DWORD64* Displacement, PSYMBOL_INFOW Symbol)
{
    PSYMBOL_INFO        si;
    unsigned            len;
    BOOL                ret;

    len = sizeof(*si) + Symbol->MaxNameLen * sizeof(WCHAR);
    si = HeapAlloc(GetProcessHeap(), 0, len);
    if (!si) return FALSE;

    si->SizeOfStruct = sizeof(*si);
    si->MaxNameLen = Symbol->MaxNameLen;
    if ((ret = SymFromAddr(hProcess, Address, Displacement, si)))
    {
        copy_symbolW(Symbol, si);
    }
    HeapFree(GetProcessHeap(), 0, si);
    return ret;
}

1227 1228 1229 1230 1231 1232 1233
/******************************************************************
 *		SymGetSymFromAddr (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetSymFromAddr(HANDLE hProcess, DWORD Address,
                              PDWORD Displacement, PIMAGEHLP_SYMBOL Symbol)
{
Eric Pouech's avatar
Eric Pouech committed
1234
    char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1235 1236
    SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
    size_t      len;
1237
    DWORD64     Displacement64;
1238 1239 1240

    if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
    si->SizeOfStruct = sizeof(*si);
Eric Pouech's avatar
Eric Pouech committed
1241
    si->MaxNameLen = MAX_SYM_NAME;
1242
    if (!SymFromAddr(hProcess, Address, &Displacement64, si))
1243 1244
        return FALSE;

1245 1246
    if (Displacement)
        *Displacement = Displacement64;
1247 1248 1249 1250
    Symbol->Address = si->Address;
    Symbol->Size    = si->Size;
    Symbol->Flags   = si->Flags;
    len = min(Symbol->MaxNameLength, si->MaxNameLen);
1251
    lstrcpynA(Symbol->Name, si->Name, len);
1252 1253 1254
    return TRUE;
}

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
/******************************************************************
 *		SymGetSymFromAddr64 (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetSymFromAddr64(HANDLE hProcess, DWORD64 Address,
                                PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol)
{
    char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
    SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
    size_t      len;
    DWORD64     Displacement64;

    if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
    si->SizeOfStruct = sizeof(*si);
    si->MaxNameLen = MAX_SYM_NAME;
    if (!SymFromAddr(hProcess, Address, &Displacement64, si))
        return FALSE;

    if (Displacement)
        *Displacement = Displacement64;
    Symbol->Address = si->Address;
    Symbol->Size    = si->Size;
    Symbol->Flags   = si->Flags;
    len = min(Symbol->MaxNameLength, si->MaxNameLen);
    lstrcpynA(Symbol->Name, si->Name, len);
    return TRUE;
}

Eric Pouech's avatar
Eric Pouech committed
1283 1284 1285 1286 1287 1288 1289 1290
static BOOL find_name(struct process* pcs, struct module* module, const char* name,
                      SYMBOL_INFO* symbol)
{
    struct hash_table_iter      hti;
    void*                       ptr;
    struct symt_ht*             sym = NULL;
    struct module_pair          pair;

1291
    pair.pcs = pcs;
Eric Pouech's avatar
Eric Pouech committed
1292
    if (!(pair.requested = module)) return FALSE;
1293
    if (!module_get_debug(&pair)) return FALSE;
Eric Pouech's avatar
Eric Pouech committed
1294 1295 1296 1297 1298 1299 1300 1301

    hash_table_iter_init(&pair.effective->ht_symbols, &hti, name);
    while ((ptr = hash_table_iter_up(&hti)))
    {
        sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);

        if (!strcmp(sym->hash_elt.name, name))
        {
1302
            symt_fill_sym_info(&pair, NULL, &sym->symt, symbol);
Eric Pouech's avatar
Eric Pouech committed
1303 1304 1305 1306 1307 1308
            return TRUE;
        }
    }
    return FALSE;

}
1309 1310 1311 1312
/******************************************************************
 *		SymFromName (DBGHELP.@)
 *
 */
1313
BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
1314 1315 1316
{
    struct process*             pcs = process_find_by_handle(hProcess);
    struct module*              module;
1317
    const char*                 name;
1318 1319 1320 1321

    TRACE("(%p, %s, %p)\n", hProcess, Name, Symbol);
    if (!pcs) return FALSE;
    if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
1322 1323 1324 1325 1326 1327 1328
    name = strchr(Name, '!');
    if (name)
    {
        char    tmp[128];
        assert(name - Name < sizeof(tmp));
        memcpy(tmp, Name, name - Name);
        tmp[name - Name] = '\0';
1329
        module = module_find_by_nameA(pcs, tmp);
1330
        return find_name(pcs, module, name + 1, Symbol);
1331
    }
Eric Pouech's avatar
Eric Pouech committed
1332
    for (module = pcs->lmodules; module; module = module->next)
1333
    {
Eric Pouech's avatar
Eric Pouech committed
1334 1335 1336 1337 1338
        if (module->type == DMT_PE && find_name(pcs, module, Name, Symbol))
            return TRUE;
    }
    /* not found in PE modules, retry on the ELF ones
     */
1339
    if (dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES)
Eric Pouech's avatar
Eric Pouech committed
1340 1341
    {
        for (module = pcs->lmodules; module; module = module->next)
1342
        {
1343 1344
            if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
                !module_get_containee(pcs, module) &&
Eric Pouech's avatar
Eric Pouech committed
1345
                find_name(pcs, module, Name, Symbol))
1346
                return TRUE;
1347 1348 1349 1350 1351 1352 1353 1354
        }
    }
    return FALSE;
}

/***********************************************************************
 *		SymGetSymFromName (DBGHELP.@)
 */
1355
BOOL WINAPI SymGetSymFromName(HANDLE hProcess, PCSTR Name, PIMAGEHLP_SYMBOL Symbol)
1356
{
Eric Pouech's avatar
Eric Pouech committed
1357
    char        buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1358 1359 1360 1361 1362
    SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
    size_t      len;

    if (Symbol->SizeOfStruct < sizeof(*Symbol)) return FALSE;
    si->SizeOfStruct = sizeof(*si);
Eric Pouech's avatar
Eric Pouech committed
1363
    si->MaxNameLen = MAX_SYM_NAME;
1364 1365 1366 1367 1368 1369
    if (!SymFromName(hProcess, Name, si)) return FALSE;

    Symbol->Address = si->Address;
    Symbol->Size    = si->Size;
    Symbol->Flags   = si->Flags;
    len = min(Symbol->MaxNameLength, si->MaxNameLen);
1370
    lstrcpynA(Symbol->Name, si->Name, len);
1371 1372 1373 1374
    return TRUE;
}

/******************************************************************
1375
 *		sym_fill_func_line_info
1376 1377 1378
 *
 * fills information about a file
 */
1379
BOOL symt_fill_func_line_info(const struct module* module, const struct symt_function* func,
1380
                              DWORD addr, IMAGEHLP_LINE* line)
1381 1382 1383
{
    struct line_info*   dli = NULL;
    BOOL                found = FALSE;
1384
    int                 i;
1385 1386 1387

    assert(func->symt.tag == SymTagFunction);

1388
    for (i=vector_length(&func->vlines)-1; i>=0; i--)
1389
    {
1390
        dli = vector_at(&func->vlines, i);
1391
        if (!dli->is_source_file)
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
        {
            if (found || dli->u.pc_offset > addr) continue;
            line->LineNumber = dli->line_number;
            line->Address    = dli->u.pc_offset;
            line->Key        = dli;
            found = TRUE;
            continue;
        }
        if (found)
        {
            line->FileName = (char*)source_get(module, dli->u.source_file);
            return TRUE;
        }
    }
    return FALSE;
}

/***********************************************************************
 *		SymGetSymNext (DBGHELP.@)
 */
BOOL WINAPI SymGetSymNext(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
{
    /* algo:
     * get module from Symbol.Address
     * get index in module.addr_sorttab of Symbol.Address
     * increment index
     * if out of module bounds, move to next module in process address space
     */
    FIXME("(%p, %p): stub\n", hProcess, Symbol);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

/***********************************************************************
 *		SymGetSymPrev (DBGHELP.@)
 */

BOOL WINAPI SymGetSymPrev(HANDLE hProcess, PIMAGEHLP_SYMBOL Symbol)
{
    FIXME("(%p, %p): stub\n", hProcess, Symbol);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

/******************************************************************
 *		SymGetLineFromAddr (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLineFromAddr(HANDLE hProcess, DWORD dwAddr, 
                               PDWORD pdwDisplacement, PIMAGEHLP_LINE Line)
{
Eric Pouech's avatar
Eric Pouech committed
1443
    struct module_pair  pair;
1444
    struct symt_ht*     symt;
1445

1446
    TRACE("%p %08x %p %p\n", hProcess, dwAddr, pdwDisplacement, Line);
1447 1448 1449

    if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;

1450 1451 1452 1453
    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, dwAddr, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
1454
    if ((symt = symt_find_nearest(pair.effective, dwAddr)) == NULL) return FALSE;
1455

1456 1457
    if (symt->symt.tag != SymTagFunction) return FALSE;
    if (!symt_fill_func_line_info(pair.effective, (struct symt_function*)symt,
1458
                                  dwAddr, Line)) return FALSE;
1459
    *pdwDisplacement = dwAddr - Line->Address;
1460 1461 1462
    return TRUE;
}

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
/******************************************************************
 *		copy_line_64_from_32 (internal)
 *
 */
static void copy_line_64_from_32(IMAGEHLP_LINE64* l64, const IMAGEHLP_LINE* l32)

{
    l64->Key = l32->Key;
    l64->LineNumber = l32->LineNumber;
    l64->FileName = l32->FileName;
    l64->Address = l32->Address;
}

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
/******************************************************************
 *		copy_line_W64_from_32 (internal)
 *
 */
static void copy_line_W64_from_32(struct process* pcs, IMAGEHLP_LINEW64* l64, const IMAGEHLP_LINE* l32)
{
    unsigned len;

    l64->Key = l32->Key;
    l64->LineNumber = l32->LineNumber;
    len = MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, NULL, 0);
    if ((l64->FileName = fetch_buffer(pcs, len * sizeof(WCHAR))))
        MultiByteToWideChar(CP_ACP, 0, l32->FileName, -1, l64->FileName, len);
    l64->Address = l32->Address;
}

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
/******************************************************************
 *		copy_line_32_from_64 (internal)
 *
 */
static void copy_line_32_from_64(IMAGEHLP_LINE* l32, const IMAGEHLP_LINE64* l64)

{
    l32->Key = l64->Key;
    l32->LineNumber = l64->LineNumber;
    l32->FileName = l64->FileName;
    l32->Address = l64->Address;
}

/******************************************************************
 *		SymGetLineFromAddr64 (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr, 
                                 PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line)
{
    IMAGEHLP_LINE       line32;

    if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
    if (!validate_addr64(dwAddr)) return FALSE;
    line32.SizeOfStruct = sizeof(line32);
    if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
        return FALSE;
    copy_line_64_from_32(Line, &line32);
    return TRUE;
}

1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
/******************************************************************
 *		SymGetLineFromAddrW64 (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLineFromAddrW64(HANDLE hProcess, DWORD64 dwAddr, 
                                  PDWORD pdwDisplacement, PIMAGEHLP_LINEW64 Line)
{
    struct process*     pcs = process_find_by_handle(hProcess);
    IMAGEHLP_LINE       line32;

    if (!pcs) return FALSE;
    if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
    if (!validate_addr64(dwAddr)) return FALSE;
    line32.SizeOfStruct = sizeof(line32);
    if (!SymGetLineFromAddr(hProcess, (DWORD)dwAddr, pdwDisplacement, &line32))
        return FALSE;
    copy_line_W64_from_32(pcs, Line, &line32);
    return TRUE;
}

1543 1544 1545 1546 1547 1548
/******************************************************************
 *		SymGetLinePrev (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLinePrev(HANDLE hProcess, PIMAGEHLP_LINE Line)
{
Eric Pouech's avatar
Eric Pouech committed
1549
    struct module_pair  pair;
1550 1551 1552 1553 1554 1555 1556
    struct line_info*   li;
    BOOL                in_search = FALSE;

    TRACE("(%p %p)\n", hProcess, Line);

    if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;

1557 1558 1559 1560
    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
1561 1562

    if (Line->Key == 0) return FALSE;
1563
    li = Line->Key;
1564 1565 1566 1567 1568
    /* things are a bit complicated because when we encounter a DLIT_SOURCEFILE
     * element we have to go back until we find the prev one to get the real
     * source file name for the DLIT_OFFSET element just before 
     * the first DLIT_SOURCEFILE
     */
1569
    while (!li->is_first)
1570 1571
    {
        li--;
1572
        if (!li->is_source_file)
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
        {
            Line->LineNumber = li->line_number;
            Line->Address    = li->u.pc_offset;
            Line->Key        = li;
            if (!in_search) return TRUE;
        }
        else
        {
            if (in_search)
            {
Eric Pouech's avatar
Eric Pouech committed
1583
                Line->FileName = (char*)source_get(pair.effective, li->u.source_file);
1584 1585 1586 1587 1588 1589 1590 1591 1592
                return TRUE;
            }
            in_search = TRUE;
        }
    }
    SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
    return FALSE;
}

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
/******************************************************************
 *		SymGetLinePrev64 (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLinePrev64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
{
    IMAGEHLP_LINE       line32;

    line32.SizeOfStruct = sizeof(line32);
    copy_line_32_from_64(&line32, Line);
    if (!SymGetLinePrev(hProcess, &line32)) return FALSE;
    copy_line_64_from_32(Line, &line32);
    return TRUE;
}
    
1608
BOOL symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line)
1609 1610 1611 1612
{
    struct line_info*   li;

    if (line->Key == 0) return FALSE;
1613
    li = line->Key;
1614
    while (!li->is_last)
1615 1616
    {
        li++;
1617
        if (!li->is_source_file)
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
        {
            line->LineNumber = li->line_number;
            line->Address    = li->u.pc_offset;
            line->Key        = li;
            return TRUE;
        }
        line->FileName = (char*)source_get(module, li->u.source_file);
    }
    return FALSE;
}

1629 1630 1631 1632 1633 1634
/******************************************************************
 *		SymGetLineNext (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLineNext(HANDLE hProcess, PIMAGEHLP_LINE Line)
{
Eric Pouech's avatar
Eric Pouech committed
1635
    struct module_pair  pair;
1636 1637 1638 1639

    TRACE("(%p %p)\n", hProcess, Line);

    if (Line->SizeOfStruct < sizeof(*Line)) return FALSE;
1640 1641 1642 1643
    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, Line->Address, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
1644

Eric Pouech's avatar
Eric Pouech committed
1645
    if (symt_get_func_line_next(pair.effective, Line)) return TRUE;
1646 1647 1648 1649
    SetLastError(ERROR_NO_MORE_ITEMS); /* FIXME */
    return FALSE;
}

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
/******************************************************************
 *		SymGetLineNext64 (DBGHELP.@)
 *
 */
BOOL WINAPI SymGetLineNext64(HANDLE hProcess, PIMAGEHLP_LINE64 Line)
{
    IMAGEHLP_LINE       line32;

    line32.SizeOfStruct = sizeof(line32);
    copy_line_32_from_64(&line32, Line);
    if (!SymGetLineNext(hProcess, &line32)) return FALSE;
    copy_line_64_from_32(Line, &line32);
    return TRUE;
}
    
1665 1666 1667 1668 1669
/***********************************************************************
 *		SymFunctionTableAccess (DBGHELP.@)
 */
PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
{
1670
    WARN("(%p, 0x%08x): stub\n", hProcess, AddrBase);
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    return NULL;
}

/***********************************************************************
 *		SymFunctionTableAccess64 (DBGHELP.@)
 */
PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
{
    WARN("(%p, %s): stub\n", hProcess, wine_dbgstr_longlong(AddrBase));
    return NULL;
1681 1682 1683 1684 1685
}

/***********************************************************************
 *		SymUnDName (DBGHELP.@)
 */
1686
BOOL WINAPI SymUnDName(PIMAGEHLP_SYMBOL sym, PSTR UnDecName, DWORD UnDecNameLength)
1687
{
1688 1689
    TRACE("(%p %s %u)\n", sym, UnDecName, UnDecNameLength);
    return UnDecorateSymbolName(sym->Name, UnDecName, UnDecNameLength,
1690
                                UNDNAME_COMPLETE) != 0;
1691 1692
}

1693 1694 1695
static void* und_alloc(size_t len) { return HeapAlloc(GetProcessHeap(), 0, len); }
static void  und_free (void* ptr)  { HeapFree(GetProcessHeap(), 0, ptr); }

1696 1697 1698
/***********************************************************************
 *		UnDecorateSymbolName (DBGHELP.@)
 */
1699
DWORD WINAPI UnDecorateSymbolName(PCSTR DecoratedName, PSTR UnDecoratedName,
1700 1701
                                  DWORD UndecoratedLength, DWORD Flags)
{
1702 1703
    /* undocumented from msvcrt */
    static char* (*p_undname)(char*, const char*, int, void* (*)(size_t), void (*)(void*), unsigned short);
1704
    static const WCHAR szMsvcrt[] = {'m','s','v','c','r','t','.','d','l','l',0};
1705

1706
    TRACE("(%s, %p, %d, 0x%08x)\n",
1707 1708
          debugstr_a(DecoratedName), UnDecoratedName, UndecoratedLength, Flags);

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
    if (!p_undname)
    {
        if (!hMsvcrt) hMsvcrt = LoadLibraryW(szMsvcrt);
        if (hMsvcrt) p_undname = (void*)GetProcAddress(hMsvcrt, "__unDName");
        if (!p_undname) return 0;
    }

    if (!UnDecoratedName) return 0;
    if (!p_undname(UnDecoratedName, DecoratedName, UndecoratedLength, 
                   und_alloc, und_free, Flags))
        return 0;
    return strlen(UnDecoratedName);
1721
}
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734

/******************************************************************
 *		SymMatchString (DBGHELP.@)
 *
 */
BOOL WINAPI SymMatchString(PCSTR string, PCSTR re, BOOL _case)
{
    regex_t     preg;
    BOOL        ret;

    TRACE("%s %s %c\n", string, re, _case ? 'Y' : 'N');

    compile_regex(re, -1, &preg, _case);
1735
    ret = match_regexp(&preg, string);
1736 1737 1738 1739
    regfree(&preg);
    return ret;
}

1740 1741 1742 1743 1744 1745 1746 1747
/******************************************************************
 *		SymSearch (DBGHELP.@)
 */
BOOL WINAPI SymSearch(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
                      DWORD SymTag, PCSTR Mask, DWORD64 Address,
                      PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
                      PVOID UserContext, DWORD Options)
{
1748 1749
    struct sym_enum     se;

1750 1751
    TRACE("(%p %s %u %u %s %s %p %p %x)\n",
          hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, Mask,
1752 1753 1754 1755 1756
          wine_dbgstr_longlong(Address), EnumSymbolsCallback,
          UserContext, Options);

    if (Options != SYMSEARCH_GLOBALSONLY)
    {
1757
        FIXME("Unsupported searching with options (%x)\n", Options);
1758 1759 1760
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
1761 1762 1763 1764 1765 1766 1767 1768 1769

    se.cb = EnumSymbolsCallback;
    se.user = UserContext;
    se.index = Index;
    se.tag = SymTag;
    se.addr = Address;
    se.sym_info = (PSYMBOL_INFO)se.buffer;

    return sym_enum(hProcess, BaseOfDll, Mask, &se);
1770
}
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783

/******************************************************************
 *		SymSearchW (DBGHELP.@)
 */
BOOL WINAPI SymSearchW(HANDLE hProcess, ULONG64 BaseOfDll, DWORD Index,
                       DWORD SymTag, PCWSTR Mask, DWORD64 Address,
                       PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,
                       PVOID UserContext, DWORD Options)
{
    struct sym_enumW    sew;
    BOOL                ret = FALSE;
    char*               maskA = NULL;

1784 1785
    TRACE("(%p %s %u %u %s %s %p %p %x)\n",
          hProcess, wine_dbgstr_longlong(BaseOfDll), Index, SymTag, debugstr_w(Mask),
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
          wine_dbgstr_longlong(Address), EnumSymbolsCallback,
          UserContext, Options);

    sew.ctx = UserContext;
    sew.cb = EnumSymbolsCallback;
    sew.sym_info = (PSYMBOL_INFOW)sew.buffer;

    if (Mask)
    {
        unsigned len = WideCharToMultiByte(CP_ACP, 0, Mask, -1, NULL, 0, NULL, NULL);
        maskA = HeapAlloc(GetProcessHeap(), 0, len);
        if (!maskA) return FALSE;
        WideCharToMultiByte(CP_ACP, 0, Mask, -1, maskA, len, NULL, NULL);
    }
    ret = SymSearch(hProcess, BaseOfDll, Index, SymTag, maskA, Address,
                    sym_enumW, &sew, Options);
    HeapFree(GetProcessHeap(), 0, maskA);

    return ret;
}
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839

/******************************************************************
 *		SymAddSymbol (DBGHELP.@)
 *
 */
BOOL WINAPI SymAddSymbol(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR name,
                         DWORD64 addr, DWORD size, DWORD flags)
{
    WCHAR       nameW[MAX_SYM_NAME];

    MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW) / sizeof(WCHAR));
    return SymAddSymbolW(hProcess, BaseOfDll, nameW, addr, size, flags);
}

/******************************************************************
 *		SymAddSymbolW (DBGHELP.@)
 *
 */
BOOL WINAPI SymAddSymbolW(HANDLE hProcess, ULONG64 BaseOfDll, PCWSTR name,
                          DWORD64 addr, DWORD size, DWORD flags)
{
    struct module_pair  pair;

    TRACE("(%p %s %s %u)\n", hProcess, wine_dbgstr_w(name), wine_dbgstr_longlong(addr), size);

    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
/******************************************************************
 *		SymSetScopeFromAddr (DBGHELP.@)
 */
BOOL WINAPI SymSetScopeFromAddr(HANDLE hProcess, ULONG64 addr)
{
    struct process*     pcs;

    FIXME("(%p %s): stub\n", hProcess, wine_dbgstr_longlong(addr));

    if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
    return TRUE;
}
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912

/******************************************************************
 *		SymEnumLines (DBGHELP.@)
 *
 */
BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
                         PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
{
    struct module_pair          pair;
    struct hash_table_iter      hti;
    struct symt_ht*             sym;
    regex_t                     re;
    struct line_info*           dli;
    void*                       ptr;
    SRCCODEINFO                 sci;
    const char*                 file;

    if (!cb) return FALSE;
    if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;

    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
    if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
    pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
    if (!compile_file_regex(&re, srcfile)) return FALSE;

    sci.SizeOfStruct = sizeof(sci);
    sci.ModBase      = base;

    hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
    while ((ptr = hash_table_iter_up(&hti)))
    {
        unsigned int    i;

        sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
        if (sym->symt.tag != SymTagFunction) continue;

        sci.FileName[0] = '\0';
        for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
        {
            dli = vector_at(&((struct symt_function*)sym)->vlines, i);
            if (dli->is_source_file)
            {
                file = source_get(pair.effective, dli->u.source_file);
                if (!match_regexp(&re, file)) file = "";
                strcpy(sci.FileName, file);
            }
            else if (sci.FileName[0])
            {
                sci.Key = dli;
                sci.Obj[0] = '\0'; /* FIXME */
                sci.LineNumber = dli->line_number;
                sci.Address = dli->u.pc_offset;
                if (!cb(&sci, user)) break;
            }
        }
    }
    regfree(&re);
    return TRUE;
}