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