dbghelp: Added helper to define the module's name.
[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, 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
32 #include "cvconst.h"
33
34 /* #define USE_STATS */
35
36 struct pool /* poor's man */
37 {
38     struct pool_arena*  first;
39     unsigned            arena_size;
40 };
41
42 void     pool_init(struct pool* a, unsigned arena_size);
43 void     pool_destroy(struct pool* a);
44 void*    pool_alloc(struct pool* a, unsigned len);
45 /* void*    pool_realloc(struct pool* a, void* p,
46    unsigned old_size, unsigned new_size); */
47 char*    pool_strdup(struct pool* a, const char* str);
48
49 struct vector
50 {
51     void**      buckets;
52     unsigned    elt_size;
53     unsigned    shift;
54     unsigned    num_elts;
55     unsigned    num_buckets;
56 };
57
58 void     vector_init(struct vector* v, unsigned elt_sz, unsigned bucket_sz);
59 unsigned vector_length(const struct vector* v);
60 void*    vector_at(const struct vector* v, unsigned pos);
61 void*    vector_add(struct vector* v, struct pool* pool);
62 /*void     vector_pool_normalize(struct vector* v, struct pool* pool); */
63 void*    vector_iter_up(const struct vector* v, void* elt);
64 void*    vector_iter_down(const struct vector* v, void* elt);
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
84 {
85     unsigned                    num_elts;
86     unsigned                    num_buckets;
87     struct hash_table_elt**     buckets;
88 };
89
90 void     hash_table_init(struct pool* pool, struct hash_table* ht,
91                          unsigned num_buckets);
92 void     hash_table_destroy(struct hash_table* ht);
93 void     hash_table_add(struct hash_table* ht, struct hash_table_elt* elt);
94 void*    hash_table_find(const struct hash_table* ht, const char* name);
95 unsigned hash_table_hash(const char* name, unsigned num_buckets);
96
97 struct hash_table_iter
98 {
99     const struct hash_table*    ht;
100     struct hash_table_elt*      element;
101     int                         index;
102     int                         last;
103 };
104
105 void     hash_table_iter_init(const struct hash_table* ht,
106                               struct hash_table_iter* hti, const char* name);
107 void*    hash_table_iter_up(struct hash_table_iter* hti);
108
109 #define GET_ENTRY(__i, __t, __f) \
110     ((__t*)((char*)(__i) - (unsigned int)(&((__t*)0)->__f)))
111
112
113 extern unsigned dbghelp_options;
114 /* some more Wine extensions */
115 #define SYMOPT_WINE_WITH_ELF_MODULES 0x40000000
116
117 enum location_kind {loc_error,          /* reg is the error code */
118                     loc_absolute,       /* offset is the location */
119                     loc_register,       /* reg is the location */
120                     loc_regrel,         /* [reg+offset] is the location */
121                     loc_user,           /* value is debug information dependent,
122                                            reg & offset can be used ad libidem */
123 };
124
125 enum location_error {loc_err_internal = -1,     /* internal while computing */
126                      loc_err_too_complex = -2,  /* couldn't compute location (even at runtime) */
127                      loc_err_out_of_scope = -3, /* variable isn't available at current address */
128                      loc_err_cant_read = -4,    /* couldn't read memory at given address */
129 };
130
131 struct location
132 {
133     unsigned            kind : 8,
134                         reg;
135     unsigned long       offset;
136 };
137
138 struct symt
139 {
140     enum SymTagEnum             tag;
141 };
142
143 struct symt_ht
144 {
145     struct symt                 symt;
146     struct hash_table_elt       hash_elt;        /* if global symbol or type */
147 };
148
149 /* lexical tree */
150 struct symt_block
151 {
152     struct symt                 symt;
153     unsigned long               address;
154     unsigned long               size;
155     struct symt*                container;      /* block, or func */
156     struct vector               vchildren;      /* sub-blocks & local variables */
157 };
158
159 struct symt_compiland
160 {
161     struct symt                 symt;
162     unsigned long               address;
163     unsigned                    source;
164     struct vector               vchildren;      /* global variables & functions */
165 };
166
167 struct symt_data
168 {
169     struct symt                 symt;
170     struct hash_table_elt       hash_elt;       /* if global symbol */
171     enum DataKind               kind;
172     struct symt*                container;
173     struct symt*                type;
174     union                                       /* depends on kind */
175     {
176         /* DataIs{Global, FileStatic}:
177          *      loc.kind is loc_absolute
178          *      loc.offset is address
179          * DataIs{Local,Param}:
180          *      with loc.kind
181          *              loc_absolute    not supported
182          *              loc_register    location is in register loc.reg
183          *              loc_regrel      location is at address loc.reg + loc.offset
184          *              >= loc_user     ask debug info provider for resolution
185          */
186         struct location         var;
187         /* DataIs{Member} (all values are in bits, not bytes) */
188         struct
189         {
190             long                        offset;
191             unsigned long               length;
192         } member;
193         /* DataIsConstant */
194         VARIANT                 value;
195     } u;
196 };
197
198 struct symt_function
199 {
200     struct symt                 symt;
201     struct hash_table_elt       hash_elt;       /* if global symbol */
202     unsigned long               address;
203     struct symt*                container;      /* compiland */
204     struct symt*                type;           /* points to function_signature */
205     unsigned long               size;
206     struct vector               vlines;
207     struct vector               vchildren;      /* locals, params, blocks, start/end, labels */
208 };
209
210 struct symt_function_point
211 {
212     struct symt                 symt;           /* either SymTagFunctionDebugStart, SymTagFunctionDebugEnd, SymTagLabel */
213     struct symt_function*       parent;
214     struct location             loc;
215     const char*                 name;           /* for labels */
216 };
217
218 struct symt_public
219 {
220     struct symt                 symt;
221     struct hash_table_elt       hash_elt;
222     struct symt*                container;      /* compiland */
223     unsigned long               address;
224     unsigned long               size;
225     unsigned                    in_code : 1,
226                                 is_function : 1;
227 };
228
229 struct symt_thunk
230 {
231     struct symt                 symt;
232     struct hash_table_elt       hash_elt;
233     struct symt*                container;      /* compiland */
234     unsigned long               address;
235     unsigned long               size;
236     THUNK_ORDINAL               ordinal;        /* FIXME: doesn't seem to be accessible */
237 };
238
239 /* class tree */
240 struct symt_array
241 {
242     struct symt                 symt;
243     int                         start;
244     int                         end;
245     struct symt*                base_type;
246     struct symt*                index_type;
247 };
248
249 struct symt_basic
250 {
251     struct symt                 symt;
252     struct hash_table_elt       hash_elt;
253     enum BasicType              bt;
254     unsigned long               size;
255 };
256
257 struct symt_enum
258 {
259     struct symt                 symt;
260     const char*                 name;
261     struct vector               vchildren;
262 };
263
264 struct symt_function_signature
265 {
266     struct symt                 symt;
267     struct symt*                rettype;
268     struct vector               vchildren;
269     enum CV_call_e              call_conv;
270 };
271
272 struct symt_function_arg_type
273 {
274     struct symt                 symt;
275     struct symt*                arg_type;
276     struct symt*                container;
277 };
278
279 struct symt_pointer
280 {
281     struct symt                 symt;
282     struct symt*                pointsto;
283 };
284
285 struct symt_typedef
286 {
287     struct symt                 symt;
288     struct hash_table_elt       hash_elt;
289     struct symt*                type;
290 };
291
292 struct symt_udt
293 {
294     struct symt                 symt;
295     struct hash_table_elt       hash_elt;
296     enum UdtKind                kind;
297     int                         size;
298     struct vector               vchildren;
299 };
300
301 enum module_type
302 {
303     DMT_UNKNOWN,        /* for lookup, not actually used for a module */
304     DMT_ELF,            /* a real ELF shared module */
305     DMT_PE,             /* a native or builtin PE module */
306     DMT_PDB,            /* PDB file */
307 };
308
309 struct process;
310
311 struct module
312 {
313     IMAGEHLP_MODULE64           module;
314     struct module*              next;
315     enum module_type            type : 16;
316     unsigned short              is_virtual : 1;
317     struct elf_module_info*     elf_info;
318     struct dwarf2_module_info_s*dwarf2_info;
319     
320     /* memory allocation pool */
321     struct pool                 pool;
322
323     /* symbols & symbol tables */
324     int                         sortlist_valid;
325     unsigned                    num_sorttab;    /* number of symbols with addresses */
326     struct symt_ht**            addr_sorttab;
327     struct hash_table           ht_symbols;
328     void                        (*loc_compute)(struct process* pcs,
329                                                const struct module* module,
330                                                const struct symt_function* func,
331                                                struct location* loc);
332
333     /* types */
334     struct hash_table           ht_types;
335     struct vector               vtypes;
336
337     /* source files */
338     unsigned                    sources_used;
339     unsigned                    sources_alloc;
340     char*                       sources;
341 };
342
343 struct process 
344 {
345     struct process*             next;
346     HANDLE                      handle;
347     WCHAR*                      search_path;
348     
349     PSYMBOL_REGISTERED_CALLBACK64       reg_cb;
350     BOOL                        reg_is_unicode;
351     DWORD64                     reg_user;
352
353     struct module*              lmodules;
354     unsigned long               dbg_hdr_addr;
355
356     IMAGEHLP_STACK_FRAME        ctx_frame;
357
358     unsigned                    buffer_size;
359     void*                       buffer;
360 };
361
362 struct line_info
363 {
364     unsigned long               is_first : 1,
365                                 is_last : 1,
366                                 is_source_file : 1,
367                                 line_number;
368     union
369     {
370         unsigned long               pc_offset;   /* if is_source_file isn't set */
371         unsigned                    source_file; /* if is_source_file is set */
372     } u;
373 };
374
375 struct module_pair
376 {
377     struct process*             pcs;
378     struct module*              requested; /* in:  to module_get_debug() */
379     struct module*              effective; /* out: module with debug info */
380 };
381
382 enum pdb_kind {PDB_JG, PDB_DS};
383
384 struct pdb_lookup
385 {
386     const char*                 filename;
387     DWORD                       age;
388     enum pdb_kind               kind;
389     union
390     {
391         struct
392         {
393             DWORD               timestamp;
394             struct PDB_JG_TOC*  toc;
395         } jg;
396         struct
397         {
398             GUID                guid;
399             struct PDB_DS_TOC*  toc;
400         } ds;
401     } u;
402 };
403
404 /* dbghelp.c */
405 extern struct process* process_find_by_handle(HANDLE hProcess);
406 extern HANDLE hMsvcrt;
407 extern BOOL         validate_addr64(DWORD64 addr);
408 extern BOOL         pcs_callback(const struct process* pcs, ULONG action, void* data);
409 extern void*        fetch_buffer(struct process* pcs, unsigned size);
410
411 /* elf_module.c */
412 #define ELF_NO_MAP      ((const void*)0xffffffff)
413 typedef BOOL (*elf_enum_modules_cb)(const char*, unsigned long addr, void* user);
414 extern BOOL         elf_enum_modules(HANDLE hProc, elf_enum_modules_cb, void*);
415 extern BOOL         elf_fetch_file_info(const char* name, DWORD* base, DWORD* size, DWORD* checksum);
416 struct elf_file_map;
417 extern BOOL         elf_load_debug_info(struct module* module, struct elf_file_map* fmap);
418 extern struct module*
419                     elf_load_module(struct process* pcs, const char* name, unsigned long);
420 extern BOOL         elf_read_wine_loader_dbg_info(struct process* pcs);
421 extern BOOL         elf_synchronize_module_list(struct process* pcs);
422 struct elf_thunk_area;
423 extern int          elf_is_in_thunk_area(unsigned long addr, const struct elf_thunk_area* thunks);
424 extern DWORD WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS* addr);
425
426 /* module.c */
427 extern struct module*
428                     module_find_by_addr(const struct process* pcs, unsigned long addr,
429                                         enum module_type type);
430 extern struct module*
431                     module_find_by_name(const struct process* pcs, 
432                                         const char* name, enum module_type type);
433 extern BOOL         module_get_debug(struct module_pair*);
434 extern struct module*
435                     module_new(struct process* pcs, const char* name, 
436                                enum module_type type, BOOL virtual,
437                                unsigned long addr, unsigned long size,
438                                unsigned long stamp, unsigned long checksum);
439 extern struct module*
440                     module_get_container(const struct process* pcs,
441                                          const struct module* inner);
442 extern struct module*
443                     module_get_containee(const struct process* pcs,
444                                          const struct module* inner);
445 extern enum module_type
446                     module_get_type_by_name(const char* name);
447 extern void         module_reset_debug_info(struct module* module);
448 extern BOOL         module_remove(struct process* pcs, 
449                                   struct module* module);
450 extern void         module_set_module(struct module* module, const char* name);
451
452 /* msc.c */
453 extern BOOL         pe_load_debug_directory(const struct process* pcs, 
454                                             struct module* module, 
455                                             const BYTE* mapping,
456                                             const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
457                                             const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg);
458 extern BOOL         pdb_fetch_file_info(struct pdb_lookup* pdb_lookup);
459
460 /* pe_module.c */
461 extern BOOL         pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth);
462 extern struct module*
463                     pe_load_module(struct process* pcs, const char* name,
464                                    HANDLE hFile, DWORD base, DWORD size);
465 extern struct module*
466                     pe_load_module_from_pcs(struct process* pcs, const char* name, 
467                                             const char* mod_name, DWORD base, DWORD size);
468 extern BOOL         pe_load_debug_info(const struct process* pcs, 
469                                        struct module* module);
470 /* source.c */
471 extern unsigned     source_new(struct module* module, const char* basedir, const char* source);
472 extern const char*  source_get(const struct module* module, unsigned idx);
473
474 /* stabs.c */
475 extern BOOL         stabs_parse(struct module* module, unsigned long load_offset,
476                                 const void* stabs, int stablen,
477                                 const char* strs, int strtablen);
478
479 /* dwarf.c */
480 extern BOOL         dwarf2_parse(struct module* module, unsigned long load_offset,
481                                  const struct elf_thunk_area* thunks,
482                                  const unsigned char* debug, unsigned int debug_size, 
483                                  const unsigned char* abbrev, unsigned int abbrev_size, 
484                                  const unsigned char* str, unsigned int str_size,
485                                  const unsigned char* line, unsigned int line_size,
486                                  const unsigned char* loclist, unsigned int loclist_size);
487
488 /* symbol.c */
489 extern const char*  symt_get_name(const struct symt* sym);
490 extern int          symt_cmp_addr(const void* p1, const void* p2);
491 extern struct symt_ht*
492                     symt_find_nearest(struct module* module, DWORD addr);
493 extern struct symt_compiland*
494                     symt_new_compiland(struct module* module, unsigned long address,
495                                        unsigned src_idx);
496 extern struct symt_public*
497                     symt_new_public(struct module* module, 
498                                     struct symt_compiland* parent, 
499                                     const char* typename,
500                                     unsigned long address, unsigned size,
501                                     BOOL in_code, BOOL is_func);
502 extern struct symt_data*
503                     symt_new_global_variable(struct module* module, 
504                                              struct symt_compiland* parent,
505                                              const char* name, unsigned is_static,
506                                              unsigned long addr, unsigned long size, 
507                                              struct symt* type);
508 extern struct symt_function*
509                     symt_new_function(struct module* module,
510                                       struct symt_compiland* parent,
511                                       const char* name,
512                                       unsigned long addr, unsigned long size,
513                                       struct symt* type);
514 extern BOOL         symt_normalize_function(struct module* module, 
515                                             struct symt_function* func);
516 extern void         symt_add_func_line(struct module* module,
517                                        struct symt_function* func, 
518                                        unsigned source_idx, int line_num, 
519                                        unsigned long offset);
520 extern struct symt_data*
521                     symt_add_func_local(struct module* module, 
522                                         struct symt_function* func, 
523                                         enum DataKind dt, const struct location* loc,
524                                         struct symt_block* block,
525                                         struct symt* type, const char* name);
526 extern struct symt_block*
527                     symt_open_func_block(struct module* module, 
528                                          struct symt_function* func,
529                                          struct symt_block* block, 
530                                          unsigned pc, unsigned len);
531 extern struct symt_block*
532                     symt_close_func_block(struct module* module, 
533                                           struct symt_function* func,
534                                           struct symt_block* block, unsigned pc);
535 extern struct symt_function_point*
536                     symt_add_function_point(struct module* module, 
537                                             struct symt_function* func,
538                                             enum SymTagEnum point, 
539                                             const struct location* loc,
540                                             const char* name);
541 extern BOOL         symt_fill_func_line_info(struct module* module,
542                                              struct symt_function* func, 
543                                              DWORD addr, IMAGEHLP_LINE* line);
544 extern BOOL         symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line);
545 extern struct symt_thunk*
546                     symt_new_thunk(struct module* module, 
547                                    struct symt_compiland* parent,
548                                    const char* name, THUNK_ORDINAL ord,
549                                    unsigned long addr, unsigned long size);
550 extern struct symt_data*
551                     symt_new_constant(struct module* module,
552                                       struct symt_compiland* parent,
553                                       const char* name, struct symt* type,
554                                       const VARIANT* v);
555
556 /* type.c */
557 extern void         symt_init_basic(struct module* module);
558 extern BOOL         symt_get_info(const struct symt* type,
559                                   IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo);
560 extern struct symt_basic*
561                     symt_new_basic(struct module* module, enum BasicType, 
562                                    const char* typename, unsigned size);
563 extern struct symt_udt*
564                     symt_new_udt(struct module* module, const char* typename,
565                                  unsigned size, enum UdtKind kind);
566 extern BOOL         symt_set_udt_size(struct module* module,
567                                       struct symt_udt* type, unsigned size);
568 extern BOOL         symt_add_udt_element(struct module* module, 
569                                          struct symt_udt* udt_type, 
570                                          const char* name,
571                                          struct symt* elt_type, unsigned offset, 
572                                          unsigned size);
573 extern struct symt_enum*
574                     symt_new_enum(struct module* module, const char* typename);
575 extern BOOL         symt_add_enum_element(struct module* module, 
576                                           struct symt_enum* enum_type, 
577                                           const char* name, int value);
578 extern struct symt_array*
579                     symt_new_array(struct module* module, int min, int max, 
580                                    struct symt* base, struct symt* index);
581 extern struct symt_function_signature*
582                     symt_new_function_signature(struct module* module, 
583                                                 struct symt* ret_type,
584                                                 enum CV_call_e call_conv);
585 extern BOOL         symt_add_function_signature_parameter(struct module* module,
586                                                           struct symt_function_signature* sig,
587                                                           struct symt* param);
588 extern struct symt_pointer*
589                     symt_new_pointer(struct module* module, 
590                                      struct symt* ref_type);
591 extern struct symt_typedef*
592                     symt_new_typedef(struct module* module, struct symt* ref, 
593                                      const char* name);