type.c 28.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * File types.c - management of types (hierarchical tree)
 *
 * Copyright (C) 1997, 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 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * Note: This really doesn't do much at the moment, but it forms the framework
 * upon which full support for datatype handling will eventually be built.
 */
#include "config.h"
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winnls.h"
#include "wine/debug.h"
#include "dbghelp_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37
WINE_DECLARE_DEBUG_CHANNEL(dbghelp_symt);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

static const char* symt_get_tag_str(DWORD tag)
{
    switch (tag)
    {
    case SymTagNull:                    return "SymTagNull";
    case SymTagExe:                     return "SymTagExe";
    case SymTagCompiland:               return "SymTagCompiland";
    case SymTagCompilandDetails:        return "SymTagCompilandDetails";
    case SymTagCompilandEnv:            return "SymTagCompilandEnv";
    case SymTagFunction:                return "SymTagFunction";
    case SymTagBlock:                   return "SymTagBlock";
    case SymTagData:                    return "SymTagData";
    case SymTagAnnotation:              return "SymTagAnnotation";
    case SymTagLabel:                   return "SymTagLabel";
    case SymTagPublicSymbol:            return "SymTagPublicSymbol";
    case SymTagUDT:                     return "SymTagUDT";
    case SymTagEnum:                    return "SymTagEnum";
    case SymTagFunctionType:            return "SymTagFunctionType";
    case SymTagPointerType:             return "SymTagPointerType";
    case SymTagArrayType:               return "SymTagArrayType";
    case SymTagBaseType:                return "SymTagBaseType";
    case SymTagTypedef:                 return "SymTagTypedef,";
    case SymTagBaseClass:               return "SymTagBaseClass";
    case SymTagFriend:                  return "SymTagFriend";
    case SymTagFunctionArgType:         return "SymTagFunctionArgType,";
    case SymTagFuncDebugStart:          return "SymTagFuncDebugStart,";
    case SymTagFuncDebugEnd:            return "SymTagFuncDebugEnd";
    case SymTagUsingNamespace:          return "SymTagUsingNamespace,";
    case SymTagVTableShape:             return "SymTagVTableShape";
    case SymTagVTable:                  return "SymTagVTable";
    case SymTagCustom:                  return "SymTagCustom";
    case SymTagThunk:                   return "SymTagThunk";
    case SymTagCustomType:              return "SymTagCustomType";
    case SymTagManagedType:             return "SymTagManagedType";
    case SymTagDimension:               return "SymTagDimension";
    default:                            return "---";
    }
}

const char* symt_get_name(const struct symt* sym)
{
    switch (sym->tag)
    {
    /* lexical tree */
83 84 85 86 87 88
    case SymTagData:            return ((const struct symt_data*)sym)->hash_elt.name;
    case SymTagFunction:        return ((const struct symt_function*)sym)->hash_elt.name;
    case SymTagPublicSymbol:    return ((const struct symt_public*)sym)->hash_elt.name;
    case SymTagBaseType:        return ((const struct symt_basic*)sym)->hash_elt.name;
    case SymTagLabel:           return ((const struct symt_function_point*)sym)->name;
    case SymTagThunk:           return ((const struct symt_thunk*)sym)->hash_elt.name;
89
    /* hierarchy tree */
90 91 92
    case SymTagEnum:            return ((const struct symt_enum*)sym)->name;
    case SymTagTypedef:         return ((const struct symt_typedef*)sym)->hash_elt.name;
    case SymTagUDT:             return ((const struct symt_udt*)sym)->hash_elt.name;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    default:
        FIXME("Unsupported sym-tag %s\n", symt_get_tag_str(sym->tag));
        /* fall through */
    case SymTagArrayType:
    case SymTagPointerType:
    case SymTagFunctionType:
        return NULL;
    }
}

static struct symt* symt_find_type_by_name(struct module* module, 
                                           enum SymTagEnum sym_tag, 
                                           const char* typename)
{
    void*                       ptr;
    struct symt_ht*             type;
    struct hash_table_iter      hti;

    assert(typename);
    assert(module);

    hash_table_iter_init(&module->ht_types, &hti, typename);
    while ((ptr = hash_table_iter_up(&hti)))
    {
        type = GET_ENTRY(ptr, struct symt_ht, hash_elt);

        if ((sym_tag == SymTagNull || type->symt.tag == sym_tag) &&
            type->hash_elt.name && !strcmp(type->hash_elt.name, typename))
            return &type->symt;
    }
    SetLastError(ERROR_INVALID_NAME); /* FIXME ?? */
    return NULL;
}

127 128 129 130 131 132 133 134
static void symt_add_type(struct module* module, struct symt* symt)
{
    struct symt**       p;
    p = vector_add(&module->vtypes, &module->pool);
    assert(p);
    *p = symt;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
struct symt_basic* symt_new_basic(struct module* module, enum BasicType bt, 
                                  const char* typename, unsigned size)
{
    struct symt_basic*          sym;

    if (typename)
    {
        sym = (struct symt_basic*)symt_find_type_by_name(module, SymTagBaseType,
                                                         typename);
        if (sym && sym->bt == bt && sym->size == size)
            return sym;
    }
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagBaseType;
        if (typename)
        {
            sym->hash_elt.name = pool_strdup(&module->pool, typename);
            hash_table_add(&module->ht_types, &sym->hash_elt);
        } else sym->hash_elt.name = NULL;
        sym->bt = bt;
        sym->size = size;
157
        symt_add_type(module, &sym->symt);
158 159 160 161 162 163 164 165 166
    }
    return sym;
}

struct symt_udt* symt_new_udt(struct module* module, const char* typename, 
                              unsigned size, enum UdtKind kind)
{
    struct symt_udt*            sym;

167
    TRACE_(dbghelp_symt)("Adding udt %s:%s\n", module->module.ModuleName, typename);
168 169 170 171 172 173 174 175 176 177 178
    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagUDT;
        sym->kind     = kind;
        sym->size     = size;
        if (typename)
        {
            sym->hash_elt.name = pool_strdup(&module->pool, typename);
            hash_table_add(&module->ht_types, &sym->hash_elt);
        } else sym->hash_elt.name = NULL;
        vector_init(&sym->vchildren, sizeof(struct symt*), 8);
179
        symt_add_type(module, &sym->symt);
180 181 182 183 184 185 186 187 188 189
    }
    return sym;
}

BOOL symt_set_udt_size(struct module* module, struct symt_udt* udt, unsigned size)
{
    assert(udt->symt.tag == SymTagUDT);
    if (vector_length(&udt->vchildren) != 0)
    {
        if (udt->size != size)
190 191
            FIXME_(dbghelp_symt)("Changing size for %s from %u to %u\n", 
                                 udt->hash_elt.name, udt->size, size);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        return TRUE;
    }
    udt->size = size;
    return TRUE;
}

/******************************************************************
 *		symt_add_udt_element
 *
 * add an element to a udt (struct, class, union)
 * the size & offset parameters are expressed in bits (not bytes) so that
 * we can mix in the single call bytes aligned elements (regular fields) and
 * the others (bit fields)
 */
BOOL symt_add_udt_element(struct module* module, struct symt_udt* udt_type,
                          const char* name, struct symt* elt_type,
                          unsigned offset, unsigned size)
{
    struct symt_data*   m;
    struct symt**       p;

    assert(udt_type->symt.tag == SymTagUDT);

215
    TRACE_(dbghelp_symt)("Adding %s to UDT %s\n", name, udt_type->hash_elt.name);
216
    if (name)
217
    {
218 219 220 221 222 223 224 225 226
        p = NULL;
        while ((p = vector_iter_up(&udt_type->vchildren, p)))
        {
            m = (struct symt_data*)*p;
            assert(m);
            assert(m->symt.tag == SymTagData);
            if (strcmp(m->hash_elt.name, name) == 0)
                return TRUE;
        }
227 228 229 230 231
    }

    if ((m = pool_alloc(&module->pool, sizeof(*m))) == NULL) return FALSE;
    memset(m, 0, sizeof(*m));
    m->symt.tag      = SymTagData;
232
    m->hash_elt.name = name ? pool_strdup(&module->pool, name) : "";
233 234
    m->hash_elt.next = NULL;

235 236 237 238 239
    m->kind            = DataIsMember;
    m->container       = &udt_type->symt;
    m->type            = elt_type;
    m->u.member.offset = offset;
    m->u.member.length = ((offset & 7) || (size & 7)) ? size : 0;
240 241 242 243 244 245 246 247 248 249 250 251 252
    p = vector_add(&udt_type->vchildren, &module->pool);
    *p = &m->symt;

    return TRUE;
}

struct symt_enum* symt_new_enum(struct module* module, const char* typename)
{
    struct symt_enum*   sym;

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag            = SymTagEnum;
253
        sym->name = (typename) ? pool_strdup(&module->pool, typename) : NULL;
254 255 256 257 258 259
        vector_init(&sym->vchildren, sizeof(struct symt*), 8);
    }
    return sym;
}

BOOL symt_add_enum_element(struct module* module, struct symt_enum* enum_type,
260
                           const char* name, int value)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
    struct symt_data*   e;
    struct symt**       p;

    assert(enum_type->symt.tag == SymTagEnum);
    e = pool_alloc(&module->pool, sizeof(*e));
    if (e == NULL) return FALSE;

    e->symt.tag = SymTagData;
    e->hash_elt.name = pool_strdup(&module->pool, name);
    e->hash_elt.next = NULL;
    e->kind = DataIsConstant;
    e->container = &enum_type->symt;
    /* CV defines the underlying type for the enumeration */
    e->type = &symt_new_basic(module, btInt, "int", 4)->symt;
276 277
    e->u.value.n1.n2.vt = VT_I4;
    e->u.value.n1.n2.n3.lVal = value;
278 279 280 281 282 283 284 285 286

    p = vector_add(&enum_type->vchildren, &module->pool);
    if (!p) return FALSE; /* FIXME we leak e */
    *p = &e->symt;

    return TRUE;
}

struct symt_array* symt_new_array(struct module* module, int min, int max, 
287
                                  struct symt* base, struct symt* index)
288 289 290 291 292
{
    struct symt_array*  sym;

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
293 294 295 296 297
        sym->symt.tag   = SymTagArrayType;
        sym->start      = min;
        sym->end        = max;
        sym->base_type  = base;
        sym->index_type = index;
298
        symt_add_type(module, &sym->symt);
299 300 301 302 303
    }
    return sym;
}

struct symt_function_signature* symt_new_function_signature(struct module* module, 
304 305
                                                            struct symt* ret_type,
                                                            enum CV_call_e call_conv)
306
{
307
    struct symt_function_signature*     sym;
308 309 310 311 312

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagFunctionType;
        sym->rettype  = ret_type;
313
        vector_init(&sym->vchildren, sizeof(struct symt*), 4);
314
        sym->call_conv = call_conv;
315
        symt_add_type(module, &sym->symt);
316 317 318 319
    }
    return sym;
}

320 321 322 323
BOOL symt_add_function_signature_parameter(struct module* module,
                                           struct symt_function_signature* sig_type,
                                           struct symt* param)
{
324 325
    struct symt**                       p;
    struct symt_function_arg_type*      arg;
326 327

    assert(sig_type->symt.tag == SymTagFunctionType);
328 329 330 331 332
    arg = pool_alloc(&module->pool, sizeof(*arg));
    if (!arg) return FALSE;
    arg->symt.tag = SymTagFunctionArgType;
    arg->arg_type = param;
    arg->container = &sig_type->symt;
333
    p = vector_add(&sig_type->vchildren, &module->pool);
334 335
    if (!p) return FALSE; /* FIXME we leak arg */
    *p = &arg->symt;
336 337 338 339

    return TRUE;
}

340 341 342 343 344 345 346 347
struct symt_pointer* symt_new_pointer(struct module* module, struct symt* ref_type)
{
    struct symt_pointer*        sym;

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagPointerType;
        sym->pointsto = ref_type;
348
        symt_add_type(module, &sym->symt);
349 350 351 352
    }
    return sym;
}

353 354 355 356 357 358 359 360 361 362 363
struct symt_typedef* symt_new_typedef(struct module* module, struct symt* ref, 
                                      const char* name)
{
    struct symt_typedef* sym;

    if ((sym = pool_alloc(&module->pool, sizeof(*sym))))
    {
        sym->symt.tag = SymTagTypedef;
        sym->type     = ref;
        sym->hash_elt.name = pool_strdup(&module->pool, name);
        hash_table_add(&module->ht_types, &sym->hash_elt);
364
        symt_add_type(module, &sym->symt);
365 366 367 368
    }
    return sym;
}

369 370 371 372
/******************************************************************
 *		SymEnumTypes (DBGHELP.@)
 *
 */
373
BOOL WINAPI SymEnumTypes(HANDLE hProcess, ULONG64 BaseOfDll,
374
                         PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,
375
                         PVOID UserContext)
376
{
Eric Pouech's avatar
Eric Pouech committed
377
    struct module_pair  pair;
378 379 380 381 382
    char                buffer[sizeof(SYMBOL_INFO) + 256];
    SYMBOL_INFO*        sym_info = (SYMBOL_INFO*)buffer;
    const char*         tmp;
    struct symt*        type;
    void*               pos = NULL;
383 384
    DWORD64             size;

385 386 387
    TRACE("(%p %s %p %p)\n",
          hProcess, wine_dbgstr_longlong(BaseOfDll), EnumSymbolsCallback,
          UserContext);
388

389 390 391
    if (!(pair.pcs = process_find_by_handle(hProcess))) return FALSE;
    pair.requested = module_find_by_addr(pair.pcs, BaseOfDll, DMT_UNKNOWN);
    if (!module_get_debug(&pair)) return FALSE;
392 393 394 395

    sym_info->SizeOfStruct = sizeof(SYMBOL_INFO);
    sym_info->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);

Eric Pouech's avatar
Eric Pouech committed
396
    while ((pos = vector_iter_up(&pair.effective->vtypes, pos)))
397
    {
398
        type = *(struct symt**)pos;
399 400
        sym_info->TypeIndex = (DWORD)type;
        sym_info->info = 0; /* FIXME */
401 402
        symt_get_info(type, TI_GET_LENGTH, &size);
        sym_info->Size = size;
Eric Pouech's avatar
Eric Pouech committed
403
        sym_info->ModBase = pair.requested->module.BaseOfImage;
404 405 406 407 408
        sym_info->Flags = 0; /* FIXME */
        sym_info->Value = 0; /* FIXME */
        sym_info->Address = 0; /* FIXME */
        sym_info->Register = 0; /* FIXME */
        sym_info->Scope = 0; /* FIXME */
409 410 411 412
        sym_info->Tag = type->tag;
        tmp = symt_get_name(type);
        if (tmp)
        {
413 414 415 416 417 418
            sym_info->NameLen = min(strlen(tmp),sym_info->MaxNameLen-1);
            memcpy(sym_info->Name, tmp, sym_info->NameLen);
            sym_info->Name[sym_info->NameLen] = '\0';
        }
        else
           sym_info->Name[sym_info->NameLen = 0] = '\0';
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        if (!EnumSymbolsCallback(sym_info, sym_info->Size, UserContext)) break;
    }
    return TRUE;
}

/******************************************************************
 *		symt_get_info
 *
 * Retrieves inforamtion about a symt (either symbol or type)
 */
BOOL symt_get_info(const struct symt* type, IMAGEHLP_SYMBOL_TYPE_INFO req, 
                   void* pInfo)
{
    unsigned            len;

    if (!type) return FALSE;

/* helper to typecast pInfo to its expected type (_t) */
#define X(_t) (*((_t*)pInfo))

    switch (req)
    {
    case TI_FINDCHILDREN:
        {
            const struct vector*        v;
            struct symt**               pt;
            unsigned                    i;
            TI_FINDCHILDREN_PARAMS*     tifp = pInfo;

            switch (type->tag)
            {
450 451 452 453
            case SymTagUDT:          v = &((const struct symt_udt*)type)->vchildren; break;
            case SymTagEnum:         v = &((const struct symt_enum*)type)->vchildren; break;
            case SymTagFunctionType: v = &((const struct symt_function_signature*)type)->vchildren; break;
            case SymTagFunction:     v = &((const struct symt_function*)type)->vchildren; break;
454
            default:
455 456
                FIXME("Unsupported sym-tag %s for find-children\n", 
                      symt_get_tag_str(type->tag));
457 458 459 460 461 462 463 464 465 466 467 468 469 470
                return FALSE;
            }
            for (i = 0; i < tifp->Count; i++)
            {
                if (!(pt = vector_at(v, tifp->Start + i))) return FALSE;
                tifp->ChildId[i] = (DWORD)*pt;
            }
        }
        break;

    case TI_GET_ADDRESS:
        switch (type->tag)
        {
        case SymTagData:
471
            switch (((const struct symt_data*)type)->kind)
472 473 474
            {
            case DataIsGlobal:
            case DataIsFileStatic:
475
                X(ULONG64) = ((const struct symt_data*)type)->u.var.offset;
476 477 478 479 480
                break;
            default: return FALSE;
            }
            break;
        case SymTagFunction:
481
            X(ULONG64) = ((const struct symt_function*)type)->address;
482 483
            break;
        case SymTagPublicSymbol:
484
            X(ULONG64) = ((const struct symt_public*)type)->address;
485
            break;
486 487 488
        case SymTagFuncDebugStart:
        case SymTagFuncDebugEnd:
        case SymTagLabel:
489
            X(ULONG64) = ((const struct symt_function_point*)type)->parent->address + 
490
                ((const struct symt_function_point*)type)->loc.offset;
491 492
            break;
        case SymTagThunk:
493
            X(ULONG64) = ((const struct symt_thunk*)type)->address;
494
            break;
495 496 497
        case SymTagCompiland:
            X(ULONG64) = ((const struct symt_compiland*)type)->address;
            break;
498
        default:
499 500
            FIXME("Unsupported sym-tag %s for get-address\n", 
                  symt_get_tag_str(type->tag));
501 502 503 504 505
            return FALSE;
        }
        break;

    case TI_GET_BASETYPE:
506 507 508
        switch (type->tag)
        {
        case SymTagBaseType:
509
            X(DWORD) = ((const struct symt_basic*)type)->bt;
510 511 512 513 514 515 516
            break;
        case SymTagEnum:
            X(DWORD) = btInt;
            break;
        default:
            return FALSE;
        }
517 518 519
        break;

    case TI_GET_BITPOSITION:
520 521 522 523 524
        if (type->tag == SymTagData &&
            ((const struct symt_data*)type)->kind == DataIsMember &&
            ((const struct symt_data*)type)->u.member.length != 0)
            X(DWORD) = ((const struct symt_data*)type)->u.member.offset & 7;
        else return FALSE;
525 526 527 528 529 530
        break;

    case TI_GET_CHILDRENCOUNT:
        switch (type->tag)
        {
        case SymTagUDT:
531
            X(DWORD) = vector_length(&((const struct symt_udt*)type)->vchildren);
532 533
            break;
        case SymTagEnum:
534
            X(DWORD) = vector_length(&((const struct symt_enum*)type)->vchildren);
535
            break;
536
        case SymTagFunctionType:
537
            X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
538 539
            break;
        case SymTagFunction:
540
            X(DWORD) = vector_length(&((const struct symt_function*)type)->vchildren);
541 542 543
            break;
        case SymTagPointerType: /* MS does it that way */
        case SymTagArrayType: /* MS does it that way */
544
        case SymTagThunk: /* MS does it that way */
545 546
            X(DWORD) = 0;
            break;
547
        default:
548 549
            FIXME("Unsupported sym-tag %s for get-children-count\n", 
                  symt_get_tag_str(type->tag));
550
            /* fall through */
551
        case SymTagData:
552 553 554 555 556 557 558
        case SymTagPublicSymbol:
        case SymTagBaseType:
            return FALSE;
        }
        break;

    case TI_GET_COUNT:
559 560 561 562 563 564 565 566 567 568 569 570 571 572
        switch (type->tag)
        {
        case SymTagArrayType:
            X(DWORD) = ((const struct symt_array*)type)->end - 
                ((const struct symt_array*)type)->start + 1;
            break;
        case SymTagFunctionType:
            /* this seems to be wrong for (future) C++ methods, where 'this' parameter
             * should be included in this value (and not in GET_CHILDREN_COUNT)
             */
            X(DWORD) = vector_length(&((const struct symt_function_signature*)type)->vchildren);
            break;
        default: return FALSE;
        }
573 574 575 576
        break;

    case TI_GET_DATAKIND:
        if (type->tag != SymTagData) return FALSE;
577
        X(DWORD) = ((const struct symt_data*)type)->kind;
578 579 580 581 582 583
        break;

    case TI_GET_LENGTH:
        switch (type->tag)
        {
        case SymTagBaseType:
584
            X(DWORD64) = ((const struct symt_basic*)type)->size;
585 586
            break;
        case SymTagFunction:
587
            X(DWORD64) = ((const struct symt_function*)type)->size;
588 589
            break;
        case SymTagPointerType:
590
            X(DWORD64) = sizeof(void*);
591 592
            break;
        case SymTagUDT:
593
            X(DWORD64) = ((const struct symt_udt*)type)->size;
594 595
            break;
        case SymTagEnum:
596
            X(DWORD64) = sizeof(int); /* FIXME: should be size of base-type of enum !!! */
597 598
            break;
        case SymTagData:
599
            if (((const struct symt_data*)type)->kind != DataIsMember ||
600
                !((const struct symt_data*)type)->u.member.length)
601
                return FALSE;
602
            X(DWORD64) = ((const struct symt_data*)type)->u.member.length;
603 604
            break;
        case SymTagArrayType:   
605
            if (!symt_get_info(((const struct symt_array*)type)->base_type, 
606 607
                               TI_GET_LENGTH, pInfo))
                return FALSE;
608
            X(DWORD64) *= ((const struct symt_array*)type)->end - 
609
                ((const struct symt_array*)type)->start + 1;
610 611
            break;
        case SymTagPublicSymbol:
612
            X(DWORD64) = ((const struct symt_public*)type)->size;
613
            break;
614
        case SymTagTypedef:
615 616
            return symt_get_info(((const struct symt_typedef*)type)->type, TI_GET_LENGTH, pInfo);
        case SymTagThunk:
617
            X(DWORD64) = ((const struct symt_thunk*)type)->size;
618
            break;
619
        default:
620 621
            FIXME("Unsupported sym-tag %s for get-length\n", 
                  symt_get_tag_str(type->tag));
622 623
            /* fall through */
        case SymTagFunctionType:
624 625 626 627 628 629 630 631
            return 0;
        }
        break;

    case TI_GET_LEXICALPARENT:
        switch (type->tag)
        {
        case SymTagBlock:
632
            X(DWORD) = (DWORD)((const struct symt_block*)type)->container;
633 634
            break;
        case SymTagData:
635 636 637 638 639 640 641
            X(DWORD) = (DWORD)((const struct symt_data*)type)->container;
            break;
        case SymTagFunction:
            X(DWORD) = (DWORD)((const struct symt_function*)type)->container;
            break;
        case SymTagThunk:
            X(DWORD) = (DWORD)((const struct symt_thunk*)type)->container;
642
            break;
643 644 645
        case SymTagFunctionArgType:
            X(DWORD) = (DWORD)((const struct symt_function_arg_type*)type)->container;
            break;
646
        default:
647 648 649 650 651 652 653 654 655 656 657 658 659 660
            FIXME("Unsupported sym-tag %s for get-lexical-parent\n", 
                  symt_get_tag_str(type->tag));
            return FALSE;
        }
        break;

    case TI_GET_NESTED:
        switch (type->tag)
        {
        case SymTagUDT:
        case SymTagEnum:
            X(DWORD) = 0;
            break;
        default:
661 662 663 664 665 666 667 668
            return FALSE;
        }
        break;

    case TI_GET_OFFSET:
        switch (type->tag)
        {
        case SymTagData:
669
            switch (((const struct symt_data*)type)->kind)
670
            {
671 672
            case DataIsParam:
            case DataIsLocal:
673 674
                X(ULONG) = ((const struct symt_data*)type)->u.var.offset; 
                break;
675
            case DataIsMember:
676
                X(ULONG) = ((const struct symt_data*)type)->u.member.offset >> 3; 
677 678
                break;
            default:
679
                FIXME("Unknown kind (%u) for get-offset\n",     
680
                      ((const struct symt_data*)type)->kind);
681
                return FALSE;
682 683 684
            }
            break;
        default:
685 686
            FIXME("Unsupported sym-tag %s for get-offset\n", 
                  symt_get_tag_str(type->tag));
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
            return FALSE;
        }
        break;

    case TI_GET_SYMNAME:
        {
            const char* name = symt_get_name(type);
            if (!name) return FALSE;
            len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
            X(WCHAR*) = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
            if (!X(WCHAR*)) return FALSE;
            MultiByteToWideChar(CP_ACP, 0, name, -1, X(WCHAR*), len);
        }
        break;

    case TI_GET_SYMTAG:
        X(DWORD) = type->tag;
        break;

    case TI_GET_TYPE:
707
    case TI_GET_TYPEID:
708 709
        switch (type->tag)
        {
710
            /* hierarchical => hierarchical */
711
        case SymTagArrayType:
712
            X(DWORD) = (DWORD)((const struct symt_array*)type)->base_type;
713 714
            break;
        case SymTagPointerType:
715
            X(DWORD) = (DWORD)((const struct symt_pointer*)type)->pointsto;
716 717
            break;
        case SymTagFunctionType:
718
            X(DWORD) = (DWORD)((const struct symt_function_signature*)type)->rettype;
719
            break;
720
        case SymTagTypedef:
721
            X(DWORD) = (DWORD)((const struct symt_typedef*)type)->type;
722 723
            break;
            /* lexical => hierarchical */
724
        case SymTagData:
725
            X(DWORD) = (DWORD)((const struct symt_data*)type)->type; 
726
            break;
727
        case SymTagFunction:
728
            X(DWORD) = (DWORD)((const struct symt_function*)type)->type;
729
            break;
730 731 732 733
            /* FIXME: should also work for enums */
        case SymTagFunctionArgType:
            X(DWORD) = (DWORD)((const struct symt_function_arg_type*)type)->arg_type;
            break;
734
        default:
735 736
            FIXME("Unsupported sym-tag %s for get-type\n", 
                  symt_get_tag_str(type->tag));
737
        case SymTagPublicSymbol:
738
        case SymTagThunk:
739 740 741 742 743 744
            return FALSE;
        }
        break;

    case TI_GET_UDTKIND:
        if (type->tag != SymTagUDT) return FALSE;
745
        X(DWORD) = ((const struct symt_udt*)type)->kind;
746 747
        break;

748
    case TI_GET_VALUE:
749
        if (type->tag != SymTagData || ((const struct symt_data*)type)->kind != DataIsConstant)
750
            return FALSE;
751
        X(VARIANT) = ((const struct symt_data*)type)->u.value;
752 753
        break;

754 755 756 757 758 759 760 761 762
    case TI_GET_CALLING_CONVENTION:
        if (type->tag != SymTagFunctionType) return FALSE;
        if (((const struct symt_function_signature*)type)->call_conv == -1)
        {
            FIXME("No support for calling convention for this signature\n");
            X(DWORD) = CV_CALL_FAR_C; /* FIXME */
        }
        else X(DWORD) = ((const struct symt_function_signature*)type)->call_conv;
        break;
763 764 765 766
    case TI_GET_ARRAYINDEXTYPEID:
        if (type->tag != SymTagArrayType) return FALSE;
        X(DWORD) = (DWORD)((const struct symt_array*)type)->index_type;
        break;
767

768 769 770 771 772 773 774 775 776 777 778 779
#undef X

    case TI_GET_ADDRESSOFFSET:
    case TI_GET_CLASSPARENTID:
    case TI_GET_SYMINDEX:
    case TI_GET_THISADJUST:
    case TI_GET_VIRTUALBASECLASS:
    case TI_GET_VIRTUALBASEPOINTEROFFSET:
    case TI_GET_VIRTUALTABLESHAPEID:
    case TI_IS_EQUIV_TO:
        FIXME("Unsupported GetInfo request (%u)\n", req);
        return FALSE;
780 781 782
    default:
        FIXME("Unknown GetInfo request (%u)\n", req);
        return FALSE;
783 784 785 786 787 788 789 790 791
    }

    return TRUE;
}

/******************************************************************
 *		SymGetTypeInfo (DBGHELP.@)
 *
 */
792
BOOL WINAPI SymGetTypeInfo(HANDLE hProcess, DWORD64 ModBase,
793 794 795
                           ULONG TypeId, IMAGEHLP_SYMBOL_TYPE_INFO GetType,
                           PVOID pInfo)
{
Eric Pouech's avatar
Eric Pouech committed
796
    struct module_pair  pair;
797

798 799
    pair.pcs = process_find_by_handle(hProcess);
    if (!pair.pcs) return FALSE;
800

801 802
    pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
    if (!module_get_debug(&pair))
803
    {
804
        FIXME("Someone didn't properly set ModBase (%s)\n", wine_dbgstr_longlong(ModBase));
805 806
        return FALSE;
    }
807 808 809 810 811 812 813 814

    return symt_get_info((struct symt*)TypeId, GetType, pInfo);
}

/******************************************************************
 *		SymGetTypeFromName (DBGHELP.@)
 *
 */
815
BOOL WINAPI SymGetTypeFromName(HANDLE hProcess, ULONG64 BaseOfDll,
816
                               PCSTR Name, PSYMBOL_INFO Symbol)
817 818 819 820 821 822 823 824 825 826 827 828 829 830
{
    struct process*     pcs = process_find_by_handle(hProcess);
    struct module*      module;
    struct symt*        type;

    if (!pcs) return FALSE;
    module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
    if (!module) return FALSE;
    type = symt_find_type_by_name(module, SymTagNull, Name);
    if (!type) return FALSE;
    Symbol->TypeIndex = (DWORD)type;

    return TRUE;
}