- fixed some bugs in StackWalk (claimed for but forgotten in last
[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 "oaidl.h"
30
31 #include "cvconst.h"
32
33 /* #define USE_STATS */
34
35 struct pool /* poor's man */
36 {
37     struct pool_arena*  first;
38     unsigned            arena_size;
39 };
40
41 void     pool_init(struct pool* a, unsigned arena_size);
42 void     pool_destroy(struct pool* a);
43 void*    pool_alloc(struct pool* a, unsigned len);
44 /* void*    pool_realloc(struct pool* a, void* p,
45    unsigned old_size, unsigned new_size); */
46 char*    pool_strdup(struct pool* a, const char* str);
47
48 struct vector
49 {
50     void**      buckets;
51     unsigned    elt_size : 24, shift : 8;
52     unsigned    num_elts : 20, num_buckets : 12;
53 };
54
55 void     vector_init(struct vector* v, unsigned elt_sz, unsigned bucket_sz);
56 unsigned vector_length(const struct vector* v);
57 void*    vector_at(const struct vector* v, unsigned pos);
58 void*    vector_add(struct vector* v, struct pool* pool);
59 /*void     vector_pool_normalize(struct vector* v, struct pool* pool); */
60 void*    vector_iter_up(const struct vector* v, void* elt);
61 void*    vector_iter_down(const struct vector* v, void* elt);
62
63 struct hash_table_elt
64 {
65     const char*                 name;
66     struct hash_table_elt*      next;
67 };
68
69 struct hash_table
70 {
71     unsigned                    num_buckets;
72     struct hash_table_elt**     buckets;
73 };
74
75 void     hash_table_init(struct pool* pool, struct hash_table* ht,
76                          unsigned num_buckets);
77 void     hash_table_destroy(struct hash_table* ht);
78 void     hash_table_add(struct hash_table* ht, struct hash_table_elt* elt);
79 void*    hash_table_find(const struct hash_table* ht, const char* name);
80 unsigned hash_table_hash(const char* name, unsigned num_buckets);
81
82 struct hash_table_iter
83 {
84     const struct hash_table*    ht;
85     struct hash_table_elt*      element;
86     int                         index;
87     int                         last;
88 };
89
90 void     hash_table_iter_init(const struct hash_table* ht,
91                               struct hash_table_iter* hti, const char* name);
92 void*    hash_table_iter_up(struct hash_table_iter* hti);
93
94 #define GET_ENTRY(__i, __t, __f) \
95     ((__t*)((char*)(__i) - (unsigned int)(&((__t*)0)->__f)))
96
97
98 extern unsigned dbghelp_options;
99
100 struct symt
101 {
102     enum SymTagEnum             tag;
103 };
104
105 struct symt_ht
106 {
107     struct symt                 symt;
108     struct hash_table_elt       hash_elt;        /* if global symbol or type */
109 };
110
111 /* lexical tree */
112 struct symt_block
113 {
114     struct symt                 symt;
115     unsigned long               address;
116     unsigned long               size;
117     struct symt*                container;      /* block, or func */
118     struct vector               vchildren;      /* sub-blocks & local variables */
119 };
120
121 struct symt_compiland
122 {
123     struct symt                 symt;
124     unsigned                    source;
125     struct vector               vchildren;      /* global variables & functions */
126 };
127
128 struct symt_data
129 {
130     struct symt                 symt;
131     struct hash_table_elt       hash_elt;       /* if global symbol */
132     enum DataKind               kind;
133     struct symt*                container;
134     struct symt*                type;
135     union                                       /* depends on kind */
136     {
137         unsigned long           address;        /* DataIs{Global, FileStatic} */
138         struct
139         {
140             long                        offset; /* DataIs{Member,Local,Param} in bits*/
141             unsigned long               length; /* DataIs{Member} in bits */
142             unsigned long               reg_id; /* DataIs{Local} (0 if frame relative) */
143         } s;
144         VARIANT                 value;          /* DataIsConstant */
145     } u;
146 };
147
148 struct symt_function
149 {
150     struct symt                 symt;
151     struct hash_table_elt       hash_elt;       /* if global symbol */
152     unsigned long               addr;
153     struct symt*                container;      /* compiland */
154     struct symt*                type;           /* points to function_signature */
155     unsigned long               size;
156     struct vector               vlines;
157     struct vector               vchildren;      /* locals, params, blocks, start/end, labels */
158 };
159
160 struct symt_function_point
161 {
162     struct symt                 symt;           /* either SymTagFunctionDebugStart, SymTagFunctionDebugEnd, SymTagLabel */
163     struct symt_function*       parent;
164     unsigned long               offset;
165     const char*                 name;           /* for labels */
166 };
167
168 struct symt_public
169 {
170     struct symt                 symt;
171     struct hash_table_elt       hash_elt;
172     struct symt*                container;      /* compiland */
173     unsigned long               address;
174     unsigned long               size;
175     unsigned                    in_code : 1,
176                                 is_function : 1;
177 };
178
179 /* class tree */
180 struct symt_array
181 {
182     struct symt                 symt;
183     int                         start;
184     int                         end;
185     struct symt*                basetype;
186 };
187
188 struct symt_basic
189 {
190     struct symt                 symt;
191     struct hash_table_elt       hash_elt;
192     enum BasicType              bt;
193     unsigned long               size;
194 };
195
196 struct symt_enum
197 {
198     struct symt                 symt;
199     const char*                 name;
200     struct vector               vchildren;
201 };
202
203 struct symt_function_signature
204 {
205     struct symt                 symt;
206     struct symt*                rettype;
207     struct vector               vchildren;
208 };
209
210 struct symt_pointer
211 {
212     struct symt                 symt;
213     struct symt*                pointsto;
214 };
215
216 struct symt_typedef
217 {
218     struct symt                 symt;
219     struct hash_table_elt       hash_elt;
220     struct symt*                type;
221 };
222
223 struct symt_udt
224 {
225     struct symt                 symt;
226     struct hash_table_elt       hash_elt;
227     enum UdtKind                kind;
228     int                         size;
229     struct vector               vchildren;
230 };
231
232 enum DbgModuleType {DMT_UNKNOWN, DMT_ELF, DMT_NE, DMT_PE};
233
234 struct module
235 {
236     IMAGEHLP_MODULE             module;
237     struct module*              next;
238     enum DbgModuleType          type;
239     unsigned short              elf_mark : 1;
240     struct tagELF_DBG_INFO*     elf_dbg_info;
241     
242     /* memory allocation pool */
243     struct pool                 pool;
244
245     /* symbol tables */
246     int                         sortlist_valid;
247     struct symt_ht**            addr_sorttab;
248     struct hash_table           ht_symbols;
249
250     /* types */
251     struct hash_table           ht_types;
252
253     /* source files */
254     unsigned                    sources_used;
255     unsigned                    sources_alloc;
256     char*                       sources;
257 };
258
259 struct process 
260 {
261     struct process*             next;
262     HANDLE                      handle;
263     char*                       search_path;
264
265     struct module*              lmodules;
266     unsigned long               dbg_hdr_addr;
267
268     IMAGEHLP_STACK_FRAME        ctx_frame;
269 };
270
271 /* dbghelp.c */
272 extern struct process* process_find_by_handle(HANDLE hProcess);
273
274 /* elf_module.c */
275 extern SYM_TYPE     elf_load_debug_info(struct module* module);
276 extern struct module*
277                     elf_load_module(struct process* pcs, const char* name);
278 extern unsigned long
279                     elf_read_wine_loader_dbg_info(struct process* pcs);
280 extern BOOL         elf_synchronize_module_list(struct process* pcs);
281
282 /* memory.c */
283 struct memory_access
284 {
285     BOOL        (*read_mem)(HANDLE hProcess, DWORD addr, void* buf, DWORD len);
286     BOOL        (*write_mem)(HANDLE hProcess, DWORD addr, void* buf, DWORD len);
287 };
288
289 extern struct memory_access mem_access;
290 #define read_mem(p,a,b,l) (mem_access.read_mem)((p),(a),(b),(l))
291 #define write_mem(p,a,b,l) (mem_access.write_mem)((p),(a),(b),(l))
292 extern unsigned long WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS* addr);
293
294 /* module.c */
295 extern struct module*
296                     module_find_by_addr(const struct process* pcs, unsigned long addr,
297                                         enum DbgModuleType type);
298 extern struct module*
299                     module_find_by_name(const struct process* pcs, 
300                                         const char* name, enum DbgModuleType type);
301 extern struct module*
302                     module_get_debug(const struct process* pcs, struct module*);
303 extern struct module*
304                     module_new(struct process* pcs, const char* name, 
305                                enum DbgModuleType type, unsigned long addr, 
306                                unsigned long size, unsigned long stamp, 
307                                unsigned long checksum);
308
309 extern BOOL         module_remove(struct process* pcs, 
310                                   struct module* module);
311 /* msc.c */
312 extern SYM_TYPE     pe_load_debug_directory(const struct process* pcs, 
313                                             struct module* module, 
314                                             const BYTE* file_map,
315                                             PIMAGE_DEBUG_DIRECTORY dbg, int nDbg);
316 /* pe_module.c */
317 extern struct module*
318                     pe_load_module(struct process* pcs, char* name,
319                                    HANDLE hFile, DWORD base, DWORD size);
320 extern struct module*
321                     pe_load_module_from_pcs(struct process* pcs, const char* name, 
322                                             const char* mod_name, DWORD base, DWORD size);
323 extern SYM_TYPE     pe_load_debug_info(const struct process* pcs, 
324                                        struct module* module);
325 /* source.c */
326 extern unsigned     source_new(struct module* module, const char* source);
327 extern const char*  source_get(const struct module* module, unsigned idx);
328
329 /* stabs.c */
330 extern SYM_TYPE     stabs_parse(struct module* module, const char* addr, 
331                                 unsigned long load_offset,
332                                 unsigned int staboff, int stablen,
333                                 unsigned int strtaboff, int strtablen);
334
335 /* symbol.c */
336 extern const char*  symt_get_name(const struct symt* sym);
337 extern int          symt_cmp_addr(const void* p1, const void* p2);
338 extern struct symt_compiland*
339                     symt_new_compiland(struct module* module, 
340                                        const char* filename);
341 extern struct symt_public*
342                     symt_new_public(struct module* module, 
343                                     struct symt_compiland* parent, 
344                                     const char* typename,
345                                     unsigned long address, unsigned size,
346                                     BOOL in_code, BOOL is_func);
347 extern struct symt_data*
348                     symt_new_global_variable(struct module* module, 
349                                              struct symt_compiland* parent,
350                                              const char* name, unsigned is_static,
351                                              unsigned long addr, unsigned long size, 
352                                              struct symt* type);
353 extern struct symt_function*
354                     symt_new_function(struct module* module, 
355                                       struct symt_compiland* parent,
356                                       const char* name,
357                                       unsigned long addr, unsigned long size,        
358                                       struct symt* type);
359 extern BOOL         symt_normalize_function(struct module* module, 
360                                             struct symt_function* func);
361 extern void         symt_add_func_line(struct module* module,
362                                        struct symt_function* func, 
363                                        unsigned source_idx, int line_num, 
364                                        unsigned long offset);
365 extern struct symt_data*
366                     symt_add_func_local(struct module* module, 
367                                         struct symt_function* func, 
368                                         int regno, int offset,
369                                         struct symt_block* block,
370                                         struct symt* type, const char* name);
371 extern struct symt_block*
372                     symt_open_func_block(struct module* module, 
373                                          struct symt_function* func,
374                                          struct symt_block* block, 
375                                          unsigned pc, unsigned len);
376 extern struct symt_block*
377                     symt_close_func_block(struct module* module, 
378                                           struct symt_function* func,
379                                           struct symt_block* block, unsigned pc);
380 extern struct symt_function_point*
381                     symt_add_function_point(struct module* module, 
382                                             struct symt_function* func,
383                                             enum SymTagEnum point, 
384                                             unsigned offset, const char* name);
385 extern BOOL         symt_fill_func_line_info(struct module* module,
386                                              struct symt_function* func, 
387                                              DWORD addr, IMAGEHLP_LINE* line);
388 extern BOOL         symt_get_func_line_next(struct module* module, PIMAGEHLP_LINE line);
389
390 /* type.c */
391 extern void         symt_init_basic(struct module* module);
392 extern BOOL         symt_get_info(const struct symt* type,
393                                   IMAGEHLP_SYMBOL_TYPE_INFO req, void* pInfo);
394 extern struct symt_basic*
395                     symt_new_basic(struct module* module, enum BasicType, 
396                                    const char* typename, unsigned size);
397 extern struct symt_udt*
398                     symt_new_udt(struct module* module, const char* typename,
399                                  unsigned size, enum UdtKind kind);
400 extern BOOL         symt_set_udt_size(struct module* module,
401                                       struct symt_udt* type, unsigned size);
402 extern BOOL         symt_add_udt_element(struct module* module, 
403                                          struct symt_udt* udt_type, 
404                                          const char* name,
405                                          struct symt* elt_type, unsigned offset, 
406                                          unsigned size);
407 extern struct symt_enum*
408                     symt_new_enum(struct module* module, const char* typename);
409 extern BOOL         symt_add_enum_element(struct module* module, 
410                                           struct symt_enum* enum_type, 
411                                           const char* name, int value);
412 extern struct symt_array*
413                     symt_new_array(struct module* module, int min, int max, 
414                                    struct symt* base);
415 extern struct symt_function_signature*
416                     symt_new_function_signature(struct module* module, 
417                                                 struct symt* ret_type);
418 extern BOOL         symt_add_function_signature_parameter(struct module* module,
419                                                           struct symt_function_signature* sig,
420                                                           struct symt* param);
421 extern struct symt_pointer*
422                     symt_new_pointer(struct module* module, 
423                                      struct symt* ref_type);
424 extern struct symt_typedef*
425                     symt_new_typedef(struct module* module, struct symt* ref, 
426                                      const char* name);