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