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