dbghelp: Improve speed of our hashtable implementation by remembering the last elemen...
[wine] / dlls / dbghelp / dbghelp_private.h
1 /*
2  * File dbghelp_private.h - dbghelp internal definitions
3  *
4  * Copyright (C) 1995, Alexandre Julliard
5  * Copyright (C) 1996, Eric Youngdale.
6  * Copyright (C) 1999-2000, Ulrich Weigand.
7  * Copyright (C) 2004-2007, Eric Pouech.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winver.h"
28 #include "dbghelp.h"
29 #include "objbase.h"
30 #include "oaidl.h"
31 #include "winnls.h"
32 #include "wine/list.h"
33 #include "wine/unicode.h"
34
35 #include "cvconst.h"
36
37 /* #define USE_STATS */
38
39 struct pool /* poor's man */
40 {
41     struct list arena_list;
42     struct list arena_full;
43     size_t      arena_size;
44 };
45
46 void     pool_init(struct pool* a, size_t arena_size);
47 void     pool_destroy(struct pool* a);
48 void*    pool_alloc(struct pool* a, size_t len);
49 char*    pool_strdup(struct pool* a, const char* str);
50
51 struct vector
52 {
53     void**      buckets;
54     unsigned    elt_size;
55     unsigned    shift;
56     unsigned    num_elts;
57     unsigned    num_buckets;
58     unsigned    buckets_allocated;
59 };
60
61 void     vector_init(struct vector* v, unsigned elt_sz, unsigned bucket_sz);
62 unsigned vector_length(const struct vector* v);
63 void*    vector_at(const struct vector* v, unsigned pos);
64 void*    vector_add(struct vector* v, struct pool* pool);
65
66 struct sparse_array
67 {
68     struct vector               key2index;
69     struct vector               elements;
70 };
71
72 void     sparse_array_init(struct sparse_array* sa, unsigned elt_sz, unsigned bucket_sz);
73 void*    sparse_array_find(const struct sparse_array* sa, unsigned long idx);
74 void*    sparse_array_add(struct sparse_array* sa, unsigned long key, struct pool* pool);
75 unsigned sparse_array_length(const struct sparse_array* sa);
76
77 struct hash_table_elt
78 {
79     const char*                 name;
80     struct hash_table_elt*      next;
81 };
82
83 struct hash_table_bucket
84 {
85     struct hash_table_elt*      first;
86     struct hash_table_elt*      last;
87 };
88
89 struct hash_table
90 {
91     unsigned                    num_elts;
92     unsigned                    num_buckets;
93     struct hash_table_bucket*   buckets;
94     struct pool*                pool;
95 };
96
97 void     hash_table_init(struct pool* pool, struct hash_table* ht,
98                          unsigned num_buckets);
99 void     hash_table_destroy(struct hash_table* ht);
100 void     hash_table_add(struct hash_table* ht, struct hash_table_elt* elt);
101
102 struct hash_table_iter
103 {
104     const struct hash_table*    ht;
105     struct hash_table_elt*      element;
106     int                         index;
107     int                         last;
108 };
109
110 void     hash_table_iter_init(const struct hash_table* ht,
111                               struct hash_table_iter* hti, const char* name);
112 void*    hash_table_iter_up(struct hash_table_iter* hti);
113
114 #define GET_ENTRY(__i, __t, __f) \
115     ((__t*)((char*)(__i) - FIELD_OFFSET(__t,__f)))
116
117
118 extern unsigned dbghelp_options;
119 /* some more Wine extensions */
120 #define SYMOPT_WINE_WITH_NATIVE_MODULES 0x40000000
121
122 enum location_kind {loc_error,          /* reg is the error code */
123                     loc_absolute,       /* offset is the location */
124                     loc_register,       /* reg is the location */
125                     loc_regrel,         /* [reg+offset] is the location */
126                     loc_user,           /* value is debug information dependent,
127                                            reg & offset can be used ad libidem */
128 };
129
130 enum location_error {loc_err_internal = -1,     /* internal while computing */
131                      loc_err_too_complex = -2,  /* couldn't compute location (even at runtime) */
132                      loc_err_out_of_scope = -3, /* variable isn't available at current address */
133                      loc_err_cant_read = -4,    /* couldn't read memory at given address */
134 };
135
136 struct location
137 {
138     unsigned            kind : 8,
139                         reg;
140     unsigned long       offset;
141 };
142
143 struct symt
144 {
145     enum SymTagEnum             tag;
146 };
147
148 struct symt_ht
149 {
150     struct symt                 symt;
151     struct hash_table_elt       hash_elt;        /* if global symbol or type */
152 };
153
154 /* lexical tree */
155 struct symt_block
156 {
157     struct symt                 symt;
158     unsigned long               address;
159     unsigned long               size;
160     struct symt*                container;      /* block, or func */
161     struct vector               vchildren;      /* sub-blocks & local variables */
162 };
163
164 struct symt_compiland
165 {
166     struct symt                 symt;
167     unsigned long               address;
168     unsigned                    source;
169     struct vector               vchildren;      /* global variables & functions */
170 };
171
172 struct symt_data
173 {
174     struct symt                 symt;
175     struct hash_table_elt       hash_elt;       /* if global symbol */
176     enum DataKind               kind;
177     struct symt*                container;
178     struct symt*                type;
179     union                                       /* depends on kind */
180     {
181         /* DataIs{Global, FileStatic}:
182          *      loc.kind is loc_absolute
183          *      loc.offset is address
184          * DataIs{Local,Param}:
185          *      with loc.kind
186          *              loc_absolute    not supported
187          *              loc_register    location is in register loc.reg
188          *              loc_regrel      location is at address loc.reg + loc.offset
189          *              >= loc_user     ask debug info provider for resolution
190          */
191         struct location         var;
192         /* DataIs{Member} (all values are in bits, not bytes) */
193         struct
194         {
195             long                        offset;
196             unsigned long               length;
197         } member;
198         /* DataIsConstant */
199         VARIANT                 value;
200     } u;
201 };
202
203 struct symt_function
204 {
205     struct symt                 symt;
206     struct hash_table_elt       hash_elt;       /* if global symbol */
207     unsigned long               address;
208     struct symt*                container;      /* compiland */
209     struct symt*                type;           /* points to function_signature */
210     unsigned long               size;
211     struct vector               vlines;
212     struct vector               vchildren;      /* locals, params, blocks, start/end, labels */
213 };
214
215 struct symt_hierarchy_point
216 {
217     struct symt                 symt;           /* either SymTagFunctionDebugStart, SymTagFunctionDebugEnd, SymTagLabel */
218     struct hash_table_elt       hash_elt;       /* if label (and in compiland's hash table if global) */
219     struct symt*                parent;         /* symt_function or symt_compiland */
220     struct location             loc;
221 };
222
223 struct symt_public
224 {
225     struct symt                 symt;
226     struct hash_table_elt       hash_elt;
227     struct symt*                container;      /* compiland */
228     unsigned long               address;
229     unsigned long               size;
230     unsigned                    in_code : 1,
231                                 is_function : 1;
232 };
233
234 struct symt_thunk
235 {
236     struct symt                 symt;
237     struct hash_table_elt       hash_elt;
238     struct symt*                container;      /* compiland */
239     unsigned long               address;
240     unsigned long               size;
241     THUNK_ORDINAL               ordinal;        /* FIXME: doesn't seem to be accessible */
242 };
243
244 /* class tree */
245 struct symt_array
246 {
247     struct symt                 symt;
248     int                         start;
249     int                         end;
250     struct symt*                base_type;
251     struct symt*                index_type;
252 };
253
254 struct symt_basic
255 {
256     struct symt                 symt;
257     struct hash_table_elt       hash_elt;
258     enum BasicType              bt;
259     unsigned long               size;
260 };
261
262 struct symt_enum
263 {
264     struct symt                 symt;
265     struct symt*                base_type;
266     const char*                 name;
267     struct vector               vchildren;
268 };
269
270 struct symt_function_signature
271 {
272     struct symt                 symt;
273     struct symt*                rettype;
274     struct vector               vchildren;
275     enum CV_call_e              call_conv;
276 };
277
278 struct symt_function_arg_type
279 {
280     struct symt                 symt;
281     struct symt*                arg_type;
282     struct symt*                container;
283 };
284
285 struct symt_pointer
286 {
287     struct symt                 symt;
288     struct symt*                pointsto;
289 };
290
291 struct symt_typedef
292 {
293     struct symt                 symt;
294     struct hash_table_elt       hash_elt;
295     struct symt*                type;
296 };
297
298 struct symt_udt
299 {
300     struct symt                 symt;
301     struct hash_table_elt       hash_elt;
302     enum UdtKind                kind;
303     int                         size;
304     struct vector               vchildren;
305 };
306
307 enum module_type
308 {
309     DMT_UNKNOWN,        /* for lookup, not actually used for a module */
310     DMT_ELF,            /* a real ELF shared module */
311     DMT_PE,             /* a native or builtin PE module */
312     DMT_MACHO,          /* a real Mach-O shared module */
313     DMT_PDB,            /* .PDB file */
314     DMT_DBG,            /* .DBG file */
315 };
316
317 struct process;
318
319 struct module
320 {
321     IMAGEHLP_MODULEW64          module;
322     /* ANSI copy of module.ModuleName for efficiency */
323     char                        module_name[MAX_PATH];
324     struct module*              next;
325     enum module_type            type : 16;
326     unsigned short              is_virtual : 1;
327
328     /* specific information for debug types */
329     struct elf_module_info*     elf_info;
330     struct dwarf2_module_info_s*dwarf2_info;
331
332     struct macho_module_info*   macho_info;
333
334     /* memory allocation pool */
335     struct pool                 pool;
336
337     /* symbols & symbol tables */
338     int                         sortlist_valid;
339     unsigned                    num_sorttab;    /* number of symbols with addresses */
340     struct symt_ht**            addr_sorttab;
341     struct hash_table           ht_symbols;
342     void                        (*loc_compute)(struct process* pcs,
343                                                const struct module* module,
344                                                const struct symt_function* func,
345                                                struct location* loc);
346
347     /* types */
348     struct hash_table           ht_types;
349     struct vector               vtypes;
350
351     /* source files */
352     unsigned                    sources_used;
353     unsigned                    sources_alloc;
354     char*                       sources;
355 };
356
357 struct process 
358 {
359     struct process*             next;
360     HANDLE                      handle;
361     WCHAR*                      search_path;
362     
363     PSYMBOL_REGISTERED_CALLBACK64       reg_cb;
364     BOOL                        reg_is_unicode;
365     DWORD64                     reg_user;
366
367     struct module*              lmodules;
368     unsigned long               dbg_hdr_addr;
369
370     IMAGEHLP_STACK_FRAME        ctx_frame;
371
372     unsigned                    buffer_size;
373     void*                       buffer;
374 };
375
376 struct line_info
377 {
378     unsigned long               is_first : 1,
379                                 is_last : 1,
380                                 is_source_file : 1,
381                                 line_number;
382     union
383     {
384         unsigned long               pc_offset;   /* if is_source_file isn't set */
385         unsigned                    source_file; /* if is_source_file is set */
386     } u;
387 };
388
389 struct module_pair
390 {
391     struct process*             pcs;
392     struct module*              requested; /* in:  to module_get_debug() */
393     struct module*              effective; /* out: module with debug info */
394 };
395
396 enum pdb_kind {PDB_JG, PDB_DS};
397
398 struct pdb_lookup
399 {
400     const char*                 filename;
401     DWORD                       age;
402     enum pdb_kind               kind;
403     union
404     {
405         struct
406         {
407             DWORD               timestamp;
408             struct PDB_JG_TOC*  toc;
409         } jg;
410         struct
411         {
412             GUID                guid;
413             struct PDB_DS_TOC*  toc;
414         } ds;
415     } u;
416 };
417
418 /* dbghelp.c */
419 extern struct process* process_find_by_handle(HANDLE hProcess);
420 extern HANDLE hMsvcrt;
421 extern BOOL         validate_addr64(DWORD64 addr);
422 extern BOOL         pcs_callback(const struct process* pcs, ULONG action, void* data);
423 extern void*        fetch_buffer(struct process* pcs, unsigned size);
424
425 /* crc32.c */
426 extern DWORD calc_crc32(int fd);
427
428 typedef BOOL (*enum_modules_cb)(const WCHAR*, unsigned long addr, void* user);
429
430 /* elf_module.c */
431 #define ELF_NO_MAP      ((const void*)-1)
432 extern BOOL         elf_enum_modules(HANDLE hProc, enum_modules_cb, void*);
433 extern BOOL         elf_fetch_file_info(const WCHAR* name, DWORD* base, DWORD* size, DWORD* checksum);
434 struct elf_file_map;
435 extern BOOL         elf_load_debug_info(struct module* module, struct elf_file_map* fmap);
436 extern struct module*
437                     elf_load_module(struct process* pcs, const WCHAR* name, unsigned long);
438 extern BOOL         elf_read_wine_loader_dbg_info(struct process* pcs);
439 extern BOOL         elf_synchronize_module_list(struct process* pcs);
440 struct elf_thunk_area;
441 extern int          elf_is_in_thunk_area(unsigned long addr, const struct elf_thunk_area* thunks);
442 extern DWORD WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS* addr);
443
444 /* macho_module.c */
445 #define MACHO_NO_MAP    ((const void*)-1)
446 extern BOOL         macho_enum_modules(HANDLE hProc, enum_modules_cb, void*);
447 extern BOOL         macho_fetch_file_info(const WCHAR* name, DWORD* base, DWORD* size, DWORD* checksum);
448 struct macho_file_map;
449 extern BOOL         macho_load_debug_info(struct module* module, struct macho_file_map* fmap);
450 extern struct module*
451                     macho_load_module(struct process* pcs, const WCHAR* name, unsigned long);
452 extern BOOL         macho_read_wine_loader_dbg_info(struct process* pcs);
453 extern BOOL         macho_synchronize_module_list(struct process* pcs);
454
455 /* module.c */
456 extern const WCHAR      S_ElfW[];
457 extern const WCHAR      S_WineLoaderW[];
458 extern const WCHAR      S_WineW[];
459 extern const WCHAR      S_SlashW[];
460
461 extern struct module*
462                     module_find_by_addr(const struct process* pcs, unsigned long addr,
463                                         enum module_type type);
464 extern struct module*
465                     module_find_by_nameA(const struct process* pcs,
466                                          const char* name);
467 extern struct module*
468                     module_is_already_loaded(const struct process* pcs,
469                                              const WCHAR* imgname);
470 extern BOOL         module_get_debug(struct module_pair*);
471 extern struct module*
472                     module_new(struct process* pcs, const WCHAR* name,
473                                enum module_type type, BOOL virtual,
474                                unsigned long addr, unsigned long size,
475                                unsigned long stamp, unsigned long checksum);
476 extern struct module*
477                     module_get_containee(const struct process* pcs,
478                                          const struct module* inner);
479 extern enum module_type
480                     module_get_type_by_name(const WCHAR* name);
481 extern void         module_reset_debug_info(struct module* module);
482 extern BOOL         module_remove(struct process* pcs,
483                                   struct module* module);
484 extern void         module_set_module(struct module* module, const WCHAR* name);
485
486 /* msc.c */
487 extern BOOL         pe_load_debug_directory(const struct process* pcs,
488                                             struct module* module, 
489                                             const BYTE* mapping,
490                                             const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
491                                             const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg);
492 extern BOOL         pdb_fetch_file_info(struct pdb_lookup* pdb_lookup);
493
494 /* path.c */
495 extern BOOL         path_find_symbol_file(const struct process* pcs, PCSTR full_path,
496                                           const GUID* guid, DWORD dw1, DWORD dw2, PSTR buffer,
497                                           BOOL* is_unmatched);
498
499 /* pe_module.c */
500 extern BOOL         pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth);
501 extern struct module*
502                     pe_load_native_module(struct process* pcs, const WCHAR* name,
503                                           HANDLE hFile, DWORD base, DWORD size);
504 extern struct module*
505                     pe_load_builtin_module(struct process* pcs, const WCHAR* name,
506                                            DWORD base, DWORD size);
507 extern BOOL         pe_load_debug_info(const struct process* pcs,
508                                        struct module* module);
509 /* source.c */
510 extern unsigned     source_new(struct module* module, const char* basedir, const char* source);
511 extern const char*  source_get(const struct module* module, unsigned idx);
512
513 /* stabs.c */
514 typedef void (*stabs_def_cb)(struct module* module, unsigned long load_offset,
515                                 const char* name, unsigned long offset,
516                                 BOOL is_public, BOOL is_global, unsigned char other,
517                                 struct symt_compiland* compiland, void* user);
518 extern BOOL         stabs_parse(struct module* module, unsigned long load_offset,
519                                 const void* stabs, int stablen,
520                                 const char* strs, int strtablen,
521                                 stabs_def_cb callback, void* user);
522
523 /* dwarf.c */
524 extern BOOL         dwarf2_parse(struct module* module, unsigned long load_offset,
525                                  const struct elf_thunk_area* thunks,
526                                  const unsigned char* debug, unsigned int debug_size, 
527                                  const unsigned char* abbrev, unsigned int abbrev_size, 
528                                  const unsigned char* str, unsigned int str_size,
529                                  const unsigned char* line, unsigned int line_size,
530                                  const unsigned char* loclist, unsigned int loclist_size);
531
532 /* symbol.c */
533 extern const char*  symt_get_name(const struct symt* sym);
534 extern int          symt_cmp_addr(const void* p1, const void* p2);
535 extern void         copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si);
536 extern struct symt_ht*
537                     symt_find_nearest(struct module* module, DWORD addr);
538 extern struct symt_compiland*
539                     symt_new_compiland(struct module* module, unsigned long address,
540                                        unsigned src_idx);
541 extern struct symt_public*
542                     symt_new_public(struct module* module, 
543                                     struct symt_compiland* parent, 
544                                     const char* typename,
545                                     unsigned long address, unsigned size,
546                                     BOOL in_code, BOOL is_func);
547 extern struct symt_data*
548                     symt_new_global_variable(struct module* module, 
549                                              struct symt_compiland* parent,
550                                              const char* name, unsigned is_static,
551                                              unsigned long addr, unsigned long size, 
552                                              struct symt* type);
553 extern struct symt_function*
554                     symt_new_function(struct module* module,
555                                       struct symt_compiland* parent,
556                                       const char* name,
557                                       unsigned long addr, unsigned long size,
558                                       struct symt* type);
559 extern BOOL         symt_normalize_function(struct module* module, 
560                                             struct symt_function* func);
561 extern void         symt_add_func_line(struct module* module,
562                                        struct symt_function* func, 
563                                        unsigned source_idx, int line_num, 
564                                        unsigned long offset);
565 extern struct symt_data*
566                     symt_add_func_local(struct module* module, 
567                                         struct symt_function* func, 
568                                         enum DataKind dt, const struct location* loc,
569                                         struct symt_block* block,
570                                         struct symt* type, const char* name);
571 extern struct symt_block*
572                     symt_open_func_block(struct module* module, 
573                                          struct symt_function* func,
574                                          struct symt_block* block, 
575                                          unsigned pc, unsigned len);
576 extern struct symt_block*
577                     symt_close_func_block(struct module* module, 
578                                           struct symt_function* func,
579                                           struct symt_block* block, unsigned pc);
580 extern struct symt_hierarchy_point*
581                     symt_add_function_point(struct module* module, 
582                                             struct symt_function* func,
583                                             enum SymTagEnum point, 
584                                             const struct location* loc,
585                                             const char* name);
586 extern BOOL         symt_fill_func_line_info(const struct module* module,
587                                              const struct symt_function* func,
588                                              DWORD addr, IMAGEHLP_LINE* line);
589 extern BOOL         symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line);
590 extern struct symt_thunk*
591                     symt_new_thunk(struct module* module, 
592                                    struct symt_compiland* parent,
593                                    const char* name, THUNK_ORDINAL ord,
594                                    unsigned long addr, unsigned long size);
595 extern struct symt_data*
596                     symt_new_constant(struct module* module,
597                                       struct symt_compiland* parent,
598                                       const char* name, struct symt* type,
599                                       const VARIANT* v);
600 extern struct symt_hierarchy_point*
601                     symt_new_label(struct module* module,
602                                    struct symt_compiland* compiland,
603                                    const char* name, unsigned long address);
604
605 /* type.c */
606 extern void         symt_init_basic(struct module* module);
607 extern BOOL         symt_get_info(const struct symt* type,
608                                   IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo);
609 extern struct symt_basic*
610                     symt_new_basic(struct module* module, enum BasicType, 
611                                    const char* typename, unsigned size);
612 extern struct symt_udt*
613                     symt_new_udt(struct module* module, const char* typename,
614                                  unsigned size, enum UdtKind kind);
615 extern BOOL         symt_set_udt_size(struct module* module,
616                                       struct symt_udt* type, unsigned size);
617 extern BOOL         symt_add_udt_element(struct module* module, 
618                                          struct symt_udt* udt_type, 
619                                          const char* name,
620                                          struct symt* elt_type, unsigned offset, 
621                                          unsigned size);
622 extern struct symt_enum*
623                     symt_new_enum(struct module* module, const char* typename,
624                                   struct symt* basetype);
625 extern BOOL         symt_add_enum_element(struct module* module, 
626                                           struct symt_enum* enum_type, 
627                                           const char* name, int value);
628 extern struct symt_array*
629                     symt_new_array(struct module* module, int min, int max, 
630                                    struct symt* base, struct symt* index);
631 extern struct symt_function_signature*
632                     symt_new_function_signature(struct module* module, 
633                                                 struct symt* ret_type,
634                                                 enum CV_call_e call_conv);
635 extern BOOL         symt_add_function_signature_parameter(struct module* module,
636                                                           struct symt_function_signature* sig,
637                                                           struct symt* param);
638 extern struct symt_pointer*
639                     symt_new_pointer(struct module* module, 
640                                      struct symt* ref_type);
641 extern struct symt_typedef*
642                     symt_new_typedef(struct module* module, struct symt* ref, 
643                                      const char* name);