dbghelp: Rename some things to be less ELF-centric.
[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
84 {
85     unsigned                    num_elts;
86     unsigned                    num_buckets;
87     struct hash_table_elt**     buckets;
88     struct pool*                pool;
89 };
90
91 void     hash_table_init(struct pool* pool, struct hash_table* ht,
92                          unsigned num_buckets);
93 void     hash_table_destroy(struct hash_table* ht);
94 void     hash_table_add(struct hash_table* ht, struct hash_table_elt* elt);
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) - FIELD_OFFSET(__t,__f)))
110
111
112 extern unsigned dbghelp_options;
113 /* some more Wine extensions */
114 #define SYMOPT_WINE_WITH_NATIVE_MODULES 0x40000000
115
116 enum location_kind {loc_error,          /* reg is the error code */
117                     loc_absolute,       /* offset is the location */
118                     loc_register,       /* reg is the location */
119                     loc_regrel,         /* [reg+offset] is the location */
120                     loc_user,           /* value is debug information dependent,
121                                            reg & offset can be used ad libidem */
122 };
123
124 enum location_error {loc_err_internal = -1,     /* internal while computing */
125                      loc_err_too_complex = -2,  /* couldn't compute location (even at runtime) */
126                      loc_err_out_of_scope = -3, /* variable isn't available at current address */
127                      loc_err_cant_read = -4,    /* couldn't read memory at given address */
128 };
129
130 struct location
131 {
132     unsigned            kind : 8,
133                         reg;
134     unsigned long       offset;
135 };
136
137 struct symt
138 {
139     enum SymTagEnum             tag;
140 };
141
142 struct symt_ht
143 {
144     struct symt                 symt;
145     struct hash_table_elt       hash_elt;        /* if global symbol or type */
146 };
147
148 /* lexical tree */
149 struct symt_block
150 {
151     struct symt                 symt;
152     unsigned long               address;
153     unsigned long               size;
154     struct symt*                container;      /* block, or func */
155     struct vector               vchildren;      /* sub-blocks & local variables */
156 };
157
158 struct symt_compiland
159 {
160     struct symt                 symt;
161     unsigned long               address;
162     unsigned                    source;
163     struct vector               vchildren;      /* global variables & functions */
164 };
165
166 struct symt_data
167 {
168     struct symt                 symt;
169     struct hash_table_elt       hash_elt;       /* if global symbol */
170     enum DataKind               kind;
171     struct symt*                container;
172     struct symt*                type;
173     union                                       /* depends on kind */
174     {
175         /* DataIs{Global, FileStatic}:
176          *      loc.kind is loc_absolute
177          *      loc.offset is address
178          * DataIs{Local,Param}:
179          *      with loc.kind
180          *              loc_absolute    not supported
181          *              loc_register    location is in register loc.reg
182          *              loc_regrel      location is at address loc.reg + loc.offset
183          *              >= loc_user     ask debug info provider for resolution
184          */
185         struct location         var;
186         /* DataIs{Member} (all values are in bits, not bytes) */
187         struct
188         {
189             long                        offset;
190             unsigned long               length;
191         } member;
192         /* DataIsConstant */
193         VARIANT                 value;
194     } u;
195 };
196
197 struct symt_function
198 {
199     struct symt                 symt;
200     struct hash_table_elt       hash_elt;       /* if global symbol */
201     unsigned long               address;
202     struct symt*                container;      /* compiland */
203     struct symt*                type;           /* points to function_signature */
204     unsigned long               size;
205     struct vector               vlines;
206     struct vector               vchildren;      /* locals, params, blocks, start/end, labels */
207 };
208
209 struct symt_hierarchy_point
210 {
211     struct symt                 symt;           /* either SymTagFunctionDebugStart, SymTagFunctionDebugEnd, SymTagLabel */
212     struct hash_table_elt       hash_elt;       /* if label (and in compiland's hash table if global) */
213     struct symt*                parent;         /* symt_function or symt_compiland */
214     struct location             loc;
215 };
216
217 struct symt_public
218 {
219     struct symt                 symt;
220     struct hash_table_elt       hash_elt;
221     struct symt*                container;      /* compiland */
222     unsigned long               address;
223     unsigned long               size;
224     unsigned                    in_code : 1,
225                                 is_function : 1;
226 };
227
228 struct symt_thunk
229 {
230     struct symt                 symt;
231     struct hash_table_elt       hash_elt;
232     struct symt*                container;      /* compiland */
233     unsigned long               address;
234     unsigned long               size;
235     THUNK_ORDINAL               ordinal;        /* FIXME: doesn't seem to be accessible */
236 };
237
238 /* class tree */
239 struct symt_array
240 {
241     struct symt                 symt;
242     int                         start;
243     int                         end;
244     struct symt*                base_type;
245     struct symt*                index_type;
246 };
247
248 struct symt_basic
249 {
250     struct symt                 symt;
251     struct hash_table_elt       hash_elt;
252     enum BasicType              bt;
253     unsigned long               size;
254 };
255
256 struct symt_enum
257 {
258     struct symt                 symt;
259     struct symt*                base_type;
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     DMT_DBG,            /* .DBG file */
308 };
309
310 struct process;
311
312 struct module
313 {
314     IMAGEHLP_MODULEW64          module;
315     /* ANSI copy of module.ModuleName for efficiency */
316     char                        module_name[MAX_PATH];
317     struct module*              next;
318     enum module_type            type : 16;
319     unsigned short              is_virtual : 1;
320
321     /* specific information for debug types */
322     struct elf_module_info*     elf_info;
323     struct dwarf2_module_info_s*dwarf2_info;
324
325     /* memory allocation pool */
326     struct pool                 pool;
327
328     /* symbols & symbol tables */
329     int                         sortlist_valid;
330     unsigned                    num_sorttab;    /* number of symbols with addresses */
331     struct symt_ht**            addr_sorttab;
332     struct hash_table           ht_symbols;
333     void                        (*loc_compute)(struct process* pcs,
334                                                const struct module* module,
335                                                const struct symt_function* func,
336                                                struct location* loc);
337
338     /* types */
339     struct hash_table           ht_types;
340     struct vector               vtypes;
341
342     /* source files */
343     unsigned                    sources_used;
344     unsigned                    sources_alloc;
345     char*                       sources;
346 };
347
348 struct process 
349 {
350     struct process*             next;
351     HANDLE                      handle;
352     WCHAR*                      search_path;
353     
354     PSYMBOL_REGISTERED_CALLBACK64       reg_cb;
355     BOOL                        reg_is_unicode;
356     DWORD64                     reg_user;
357
358     struct module*              lmodules;
359     unsigned long               dbg_hdr_addr;
360
361     IMAGEHLP_STACK_FRAME        ctx_frame;
362
363     unsigned                    buffer_size;
364     void*                       buffer;
365 };
366
367 struct line_info
368 {
369     unsigned long               is_first : 1,
370                                 is_last : 1,
371                                 is_source_file : 1,
372                                 line_number;
373     union
374     {
375         unsigned long               pc_offset;   /* if is_source_file isn't set */
376         unsigned                    source_file; /* if is_source_file is set */
377     } u;
378 };
379
380 struct module_pair
381 {
382     struct process*             pcs;
383     struct module*              requested; /* in:  to module_get_debug() */
384     struct module*              effective; /* out: module with debug info */
385 };
386
387 enum pdb_kind {PDB_JG, PDB_DS};
388
389 struct pdb_lookup
390 {
391     const char*                 filename;
392     DWORD                       age;
393     enum pdb_kind               kind;
394     union
395     {
396         struct
397         {
398             DWORD               timestamp;
399             struct PDB_JG_TOC*  toc;
400         } jg;
401         struct
402         {
403             GUID                guid;
404             struct PDB_DS_TOC*  toc;
405         } ds;
406     } u;
407 };
408
409 /* dbghelp.c */
410 extern struct process* process_find_by_handle(HANDLE hProcess);
411 extern HANDLE hMsvcrt;
412 extern BOOL         validate_addr64(DWORD64 addr);
413 extern BOOL         pcs_callback(const struct process* pcs, ULONG action, void* data);
414 extern void*        fetch_buffer(struct process* pcs, unsigned size);
415
416 typedef BOOL (*enum_modules_cb)(const WCHAR*, unsigned long addr, void* user);
417
418 /* elf_module.c */
419 #define ELF_NO_MAP      ((const void*)0xffffffff)
420 extern BOOL         elf_enum_modules(HANDLE hProc, enum_modules_cb, void*);
421 extern BOOL         elf_fetch_file_info(const WCHAR* name, DWORD* base, DWORD* size, DWORD* checksum);
422 struct elf_file_map;
423 extern BOOL         elf_load_debug_info(struct module* module, struct elf_file_map* fmap);
424 extern struct module*
425                     elf_load_module(struct process* pcs, const WCHAR* name, unsigned long);
426 extern BOOL         elf_read_wine_loader_dbg_info(struct process* pcs);
427 extern BOOL         elf_synchronize_module_list(struct process* pcs);
428 struct elf_thunk_area;
429 extern int          elf_is_in_thunk_area(unsigned long addr, const struct elf_thunk_area* thunks);
430 extern DWORD WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS* addr);
431
432 /* module.c */
433 extern const WCHAR      S_ElfW[];
434 extern const WCHAR      S_WineLoaderW[];
435 extern const WCHAR      S_WineW[];
436 extern const WCHAR      S_SlashW[];
437
438 extern struct module*
439                     module_find_by_addr(const struct process* pcs, unsigned long addr,
440                                         enum module_type type);
441 extern struct module*
442                     module_find_by_nameA(const struct process* pcs,
443                                          const char* name);
444 extern struct module*
445                     module_is_already_loaded(const struct process* pcs,
446                                              const WCHAR* imgname);
447 extern BOOL         module_get_debug(struct module_pair*);
448 extern struct module*
449                     module_new(struct process* pcs, const WCHAR* name,
450                                enum module_type type, BOOL virtual,
451                                unsigned long addr, unsigned long size,
452                                unsigned long stamp, unsigned long checksum);
453 extern struct module*
454                     module_get_containee(const struct process* pcs,
455                                          const struct module* inner);
456 extern enum module_type
457                     module_get_type_by_name(const WCHAR* name);
458 extern void         module_reset_debug_info(struct module* module);
459 extern BOOL         module_remove(struct process* pcs,
460                                   struct module* module);
461 extern void         module_set_module(struct module* module, const WCHAR* name);
462
463 /* msc.c */
464 extern BOOL         pe_load_debug_directory(const struct process* pcs,
465                                             struct module* module, 
466                                             const BYTE* mapping,
467                                             const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
468                                             const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg);
469 extern BOOL         pdb_fetch_file_info(struct pdb_lookup* pdb_lookup);
470
471 /* path.c */
472 extern BOOL         path_find_symbol_file(const struct process* pcs, PCSTR full_path,
473                                           const GUID* guid, DWORD dw1, DWORD dw2, PSTR buffer,
474                                           BOOL* is_unmatched);
475
476 /* pe_module.c */
477 extern BOOL         pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth);
478 extern struct module*
479                     pe_load_native_module(struct process* pcs, const WCHAR* name,
480                                           HANDLE hFile, DWORD base, DWORD size);
481 extern struct module*
482                     pe_load_builtin_module(struct process* pcs, const WCHAR* name,
483                                            DWORD base, DWORD size);
484 extern BOOL         pe_load_debug_info(const struct process* pcs,
485                                        struct module* module);
486 /* source.c */
487 extern unsigned     source_new(struct module* module, const char* basedir, const char* source);
488 extern const char*  source_get(const struct module* module, unsigned idx);
489
490 /* stabs.c */
491 typedef void (*stabs_def_cb)(struct module* module, unsigned long load_offset,
492                                 const char* name, unsigned long offset,
493                                 BOOL is_public, BOOL is_global, unsigned char other,
494                                 struct symt_compiland* compiland, void* user);
495 extern BOOL         stabs_parse(struct module* module, unsigned long load_offset,
496                                 const void* stabs, int stablen,
497                                 const char* strs, int strtablen,
498                                 stabs_def_cb callback, void* user);
499
500 /* dwarf.c */
501 extern BOOL         dwarf2_parse(struct module* module, unsigned long load_offset,
502                                  const struct elf_thunk_area* thunks,
503                                  const unsigned char* debug, unsigned int debug_size, 
504                                  const unsigned char* abbrev, unsigned int abbrev_size, 
505                                  const unsigned char* str, unsigned int str_size,
506                                  const unsigned char* line, unsigned int line_size,
507                                  const unsigned char* loclist, unsigned int loclist_size);
508
509 /* symbol.c */
510 extern const char*  symt_get_name(const struct symt* sym);
511 extern int          symt_cmp_addr(const void* p1, const void* p2);
512 extern void         copy_symbolW(SYMBOL_INFOW* siw, const SYMBOL_INFO* si);
513 extern struct symt_ht*
514                     symt_find_nearest(struct module* module, DWORD addr);
515 extern struct symt_compiland*
516                     symt_new_compiland(struct module* module, unsigned long address,
517                                        unsigned src_idx);
518 extern struct symt_public*
519                     symt_new_public(struct module* module, 
520                                     struct symt_compiland* parent, 
521                                     const char* typename,
522                                     unsigned long address, unsigned size,
523                                     BOOL in_code, BOOL is_func);
524 extern struct symt_data*
525                     symt_new_global_variable(struct module* module, 
526                                              struct symt_compiland* parent,
527                                              const char* name, unsigned is_static,
528                                              unsigned long addr, unsigned long size, 
529                                              struct symt* type);
530 extern struct symt_function*
531                     symt_new_function(struct module* module,
532                                       struct symt_compiland* parent,
533                                       const char* name,
534                                       unsigned long addr, unsigned long size,
535                                       struct symt* type);
536 extern BOOL         symt_normalize_function(struct module* module, 
537                                             struct symt_function* func);
538 extern void         symt_add_func_line(struct module* module,
539                                        struct symt_function* func, 
540                                        unsigned source_idx, int line_num, 
541                                        unsigned long offset);
542 extern struct symt_data*
543                     symt_add_func_local(struct module* module, 
544                                         struct symt_function* func, 
545                                         enum DataKind dt, const struct location* loc,
546                                         struct symt_block* block,
547                                         struct symt* type, const char* name);
548 extern struct symt_block*
549                     symt_open_func_block(struct module* module, 
550                                          struct symt_function* func,
551                                          struct symt_block* block, 
552                                          unsigned pc, unsigned len);
553 extern struct symt_block*
554                     symt_close_func_block(struct module* module, 
555                                           struct symt_function* func,
556                                           struct symt_block* block, unsigned pc);
557 extern struct symt_hierarchy_point*
558                     symt_add_function_point(struct module* module, 
559                                             struct symt_function* func,
560                                             enum SymTagEnum point, 
561                                             const struct location* loc,
562                                             const char* name);
563 extern BOOL         symt_fill_func_line_info(const struct module* module,
564                                              const struct symt_function* func,
565                                              DWORD addr, IMAGEHLP_LINE* line);
566 extern BOOL         symt_get_func_line_next(const struct module* module, PIMAGEHLP_LINE line);
567 extern struct symt_thunk*
568                     symt_new_thunk(struct module* module, 
569                                    struct symt_compiland* parent,
570                                    const char* name, THUNK_ORDINAL ord,
571                                    unsigned long addr, unsigned long size);
572 extern struct symt_data*
573                     symt_new_constant(struct module* module,
574                                       struct symt_compiland* parent,
575                                       const char* name, struct symt* type,
576                                       const VARIANT* v);
577 extern struct symt_hierarchy_point*
578                     symt_new_label(struct module* module,
579                                    struct symt_compiland* compiland,
580                                    const char* name, unsigned long address);
581
582 /* type.c */
583 extern void         symt_init_basic(struct module* module);
584 extern BOOL         symt_get_info(const struct symt* type,
585                                   IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo);
586 extern struct symt_basic*
587                     symt_new_basic(struct module* module, enum BasicType, 
588                                    const char* typename, unsigned size);
589 extern struct symt_udt*
590                     symt_new_udt(struct module* module, const char* typename,
591                                  unsigned size, enum UdtKind kind);
592 extern BOOL         symt_set_udt_size(struct module* module,
593                                       struct symt_udt* type, unsigned size);
594 extern BOOL         symt_add_udt_element(struct module* module, 
595                                          struct symt_udt* udt_type, 
596                                          const char* name,
597                                          struct symt* elt_type, unsigned offset, 
598                                          unsigned size);
599 extern struct symt_enum*
600                     symt_new_enum(struct module* module, const char* typename,
601                                   struct symt* basetype);
602 extern BOOL         symt_add_enum_element(struct module* module, 
603                                           struct symt_enum* enum_type, 
604                                           const char* name, int value);
605 extern struct symt_array*
606                     symt_new_array(struct module* module, int min, int max, 
607                                    struct symt* base, struct symt* index);
608 extern struct symt_function_signature*
609                     symt_new_function_signature(struct module* module, 
610                                                 struct symt* ret_type,
611                                                 enum CV_call_e call_conv);
612 extern BOOL         symt_add_function_signature_parameter(struct module* module,
613                                                           struct symt_function_signature* sig,
614                                                           struct symt* param);
615 extern struct symt_pointer*
616                     symt_new_pointer(struct module* module, 
617                                      struct symt* ref_type);
618 extern struct symt_typedef*
619                     symt_new_typedef(struct module* module, struct symt* ref, 
620                                      const char* name);