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