-
Notifications
You must be signed in to change notification settings - Fork 23
/
sip.cpp
1311 lines (1161 loc) · 35.7 KB
/
sip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sip.hpp"
#include "tilbuild.hpp"
#include <dbg.hpp>
struct pdb_modinfo_t;
static source_item_t *new_pdb_symbol(
pdb_modinfo_t *pdb_module,
DWORD sym_id);
static source_item_t *new_pdb_symbol_or_delete(
pdb_modinfo_t *pdb_module,
pdb_sym_t *sym);
typedef qvector<source_item_ptr> source_items_vec_t;
//-------------------------------------------------------------------------
// will steal data from all 'child' instances passed to 'visit_child()',
// and create new pdb_sym_t objects (wrapped in source_item_t's) with
// that data.
struct source_items_vec_builder_t : public pdb_access_t::children_visitor_t
{
source_items_vec_t items;
pdb_modinfo_t *pdb_module;
source_items_vec_builder_t(pdb_modinfo_t *_mod) : pdb_module(_mod) {}
virtual HRESULT visit_child(pdb_sym_t &child) override;
};
//--------------------------------------------------------------------------
// Implementation of source information provider using PDB information
// and the DIA SDK
//--------------------------------------------------------------------------
// Information about a PDB module. Contains a type cache, that'll
// last the same time as the debugging session.
struct pdb_modinfo_t
{
pdb_ctx_t &pv;
pdb_modinfo_t()
: pv(*GET_MODULE_DATA(pdb_ctx_t)),
base(BADADDR),
size(0),
opened(false),
pdb_access(nullptr),
type_cache(nullptr),
use_pdbida(false)
{}
~pdb_modinfo_t()
{
delete type_cache;
#ifdef __NT__
// on windows with MSDIA provider, 'session_ref' owns the pdb_access
if ( use_pdbida )
#endif
{
delete pdb_access;
}
}
pdb_access_t *get_access() { return pdb_access; }
HRESULT open(const char *input_file, const char *user_spath, ea_t load_address);
source_item_t *find_static_item_in_module(const char *iname);
typedef std::map<qstring,DWORD> module_globals_t;
module_globals_t module_globals;
qstring path;
ea_t base;
asize_t size;
bool opened;
pdb_access_t *pdb_access;
#ifndef ENABLE_REMOTEPDB
pdb_session_ref_t session_ref;
#endif
til_builder_t *type_cache;
bool use_pdbida;
};
typedef std::map<ea_t, pdb_modinfo_t> pdb_modules_t;
//-------------------------------------------------------------------------
HRESULT pdb_modinfo_t::open(
const char *input_file,
const char *user_spath,
ea_t load_address)
{
QASSERT(30212, type_cache == nullptr);
pdbargs_t args;
args.input_path = input_file;
args.spath = user_spath;
args.loaded_base = load_address;
HRESULT hr = E_FAIL;
{
msg("PDB: using MSDIA provider\n");
#ifdef ENABLE_REMOTEPDB
if ( is_win32_remote_debugger_loaded() )
{
args.flags |= PDBFLG_DBG_MODULE;
remote_pdb_access_t *rpdb_access = new remote_pdb_access_t(
args,
"TESTER-SQUISH",
23946,
"");
hr = rpdb_access->open_connection();
if ( hr == S_OK )
pdb_access = rpdb_access;
else
delete rpdb_access;
}
#else // ENABLE_REMOTEPDB
hr = session_ref.open_session(args);
if ( hr == S_OK )
pdb_access = session_ref.session->pdb_access;
#endif // ENABLE_REMOTEPDB
}
if ( hr == S_OK )
{
type_cache = new til_builder_t(pv, CONST_CAST(til_t *)(get_idati()), nullptr);
type_cache->set_pdb_access(pdb_access);
}
return hr;
}
//-------------------------------------------------------------------------
source_item_t *pdb_modinfo_t::find_static_item_in_module(
const char *iname)
{
if ( !opened )
return nullptr;
DWORD id = 0;
module_globals_t::iterator p = module_globals.find(iname);
if ( p != module_globals.end() )
{
id = p->second;
}
else
{
struct ida_local iter_t : public pdb_access_t::children_visitor_t
{
pdb_modinfo_t *pdb_module;
const char *name;
DWORD id;
iter_t(
pdb_modinfo_t *_pdb_module,
const char *_name)
: pdb_module(_pdb_module), name(_name), id(0) {}
virtual HRESULT visit_child(pdb_sym_t &data) override
{
qstring cname;
if ( data.get_name(&cname) == S_OK && cname == name )
data.get_symIndexId(&id);
return id == 0 ? S_OK : E_FAIL; // 'id' still 0? keep iterating
}
};
iter_t iter(this, iname);
pdb_sym_t *global = get_access()->create_sym(get_access()->get_global_symbol_id());
pdb_sym_janitor_t janitor_global(global);
get_access()->iterate_children(*global, SymTagData, iter);
id = iter.id;
module_globals[iname] = id; // populate
}
return id != 0 ? new_pdb_symbol(this, id) : nullptr;
}
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
bool pdb_ctx_t::get_pdb_register_info(int *p_reg, uint64 *p_mask, int machine, int reg)
{
qstring name;
print_pdb_register(&name, machine, reg);
reg_info_t ri;
if ( !parse_reg_name(&ri, name.c_str()) )
return false;
*p_reg = ri.reg;
*p_mask = left_shift(uint64(1), 8*ri.size) - 1;
return true;
}
//--------------------------------------------------------------------------
struct pdb_source_file_t : public source_file_t
{
pdb_modinfo_t *pdb_module;
DWORD file_id;
qstring my_path;
pdb_source_file_t(pdb_modinfo_t *_mod, DWORD _fid)
: pdb_module(_mod), file_id(_fid) {}
srcinfo_provider_t *idaapi get_provider(void) const override;
virtual ~pdb_source_file_t(void) {}
virtual void idaapi release() override { delete this; }
const char *idaapi get_path(qstring *errbuf) override
{
if ( my_path.empty() )
{
pdb_module->get_access()->sip_retrieve_file_path(
&my_path,
errbuf,
file_id);
}
return my_path.c_str();
}
bool idaapi read_file(strvec_t *buf, qstring *errbuf) override
{
buf->clear();
const char *path = get_path(errbuf);
// Always favor file mapping first.
qstring mapbuf = path;
callui(ui_dbg_map_source_path, &mapbuf);
path = mapbuf.c_str();
if ( !qfileexist(path) )
{
if ( errbuf != nullptr )
errbuf->sprnt("source file not found: %s", path);
return false;
}
FILE *fp = fopenRT(path);
if ( fp == nullptr )
{
if ( errbuf != nullptr )
*errbuf = get_errdesc(path);
return false;
}
int tabsize = get_tab_size(path);
qstring line;
while ( qgetline(&line, fp) >= 0 )
{
simpleline_t &sl = buf->push_back();
sl.line.clear();
replace_tabs(&sl.line, line.c_str(), tabsize);
}
qfclose(fp);
return true;
}
TWidget *open_srcview(strvec_t ** /*strvec*/, TWidget ** /*pview*/, int, int) override
{
return nullptr;
}
};
//--------------------------------------------------------------------------
struct pdb_file_iterator : public _source_file_iterator
{
struct entry_t
{
pdb_modinfo_t *pdb_module;
DWORD file_id;
};
qvector<entry_t> entries;
int idx;
pdb_file_iterator() : idx(-1) {}
virtual ~pdb_file_iterator(void) {}
virtual void idaapi release(void) override { delete this; }
bool idaapi first(void) override
{
idx = -1;
return next();
}
bool idaapi next(void) override
{
++idx;
return idx < entries.size();
}
source_file_ptr idaapi operator *() override
{
const entry_t &e = entries[idx];
return source_file_ptr(
new pdb_source_file_t(e.pdb_module, e.file_id));
}
};
//--------------------------------------------------------------------------
// Dummy source item: provides no information.
struct dummy_item_t : public source_item_t
{
pdb_modinfo_t *pdb_module;
dummy_item_t(pdb_modinfo_t *_pdb_module) : pdb_module(_pdb_module) {}
virtual ~dummy_item_t(void) { pdb_module = nullptr; }
void idaapi release(void) override { delete this; }
source_file_iterator idaapi get_source_files(void) override { return source_file_iterator(nullptr); }
int idaapi get_lnnum() const override { return -1; }
int idaapi get_end_lnnum() const override { return -1; }
int idaapi get_colnum() const override { return -1; }
int idaapi get_end_colnum() const override { return -1; }
ea_t idaapi get_ea() const override { return BADADDR; }
asize_t idaapi get_size() const override { return 0; }
bool idaapi get_item_bounds(rangeset_t *set) const override
{
ea_t ea = get_ea();
if ( ea == BADADDR )
return false;
asize_t size = get_size(); //-V779 Unreachable code detected
set->add(range_t(ea, ea+size));
return true;
}
source_item_ptr idaapi get_parent(src_item_kind_t) const override { return source_item_ptr(nullptr); }
source_item_iterator idaapi create_children_iterator() override { return source_item_iterator(nullptr); }
bool idaapi get_hint(qstring *hint, const eval_ctx_t *, int *nlines) const override
{
// TODO: remove these test lines
*hint = "test";
*nlines = 1;
return true;
}
bool idaapi evaluate(const eval_ctx_t *, idc_value_t *, qstring *) const override { return false; }
// bool idaapi get_stkvar_info(char *, size_t, uval_t *, ea_t) const { return false; }
// bool idaapi get_regvar_info(char *, size_t) const { return false; }
// bool idaapi get_rrlvar_info(char *, size_t, uval_t *) const { return false; }
bool idaapi get_expr_tinfo(tinfo_t *) const override { return false; }
virtual bool idaapi get_location(argloc_t *, const eval_ctx_t *) const override { return false; }
virtual srcinfo_provider_t *idaapi get_provider(void) const override;
};
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
bool pdb_lnnums_t::get_item_bounds(rangeset_t *set) const
{
for ( size_t i = 0, sz = size(); i < sz; ++i )
{
const pdb_lnnum_t &ln = at(i);
set->add(ln.va, ln.va + ln.length);
}
return !set->empty();
}
//-------------------------------------------------------------------------
int pdb_lnnums_t::get_lnnum() const
{
return empty() ? -1 : at(0).lineNumber;
}
//-------------------------------------------------------------------------
int pdb_lnnums_t::get_colnum() const
{
return empty() ? -1 : at(0).columnNumber;
}
//-------------------------------------------------------------------------
int pdb_lnnums_t::get_end_lnnum() const
{
return empty() ? -1 : at(size() - 1).lineNumber; //should it be lineNumberEnd; ?
}
//-------------------------------------------------------------------------
int pdb_lnnums_t::get_end_colnum() const
{
return empty() ? -1 : at(size() - 1).columnNumber; //should it be columnNumberEnd; ?
}
//--------------------------------------------------------------------------
// pdb_item_iterator
//--------------------------------------------------------------------------
struct pdb_item_iterator : public _source_item_iterator
{
pdb_modinfo_t *pdb_module;
source_items_vec_t items;
int index;
pdb_item_iterator(pdb_modinfo_t *_mod, source_items_vec_t &_items)
: pdb_module(_mod), index(-1)
{
items.swap(_items);
}
virtual ~pdb_item_iterator(void) {}
void idaapi release(void) override
{
delete this;
}
bool idaapi first(void) override
{
index = -1;
return next();
}
bool idaapi next(void) override
{
++index;
return index < items.size();
}
source_item_ptr idaapi operator *() override
{
return items[index];
}
};
//-------------------------------------------------------------------------
// pdb_symbol_t
//--------------------------------------------------------------------------
// source item based on dia symbol
class pdb_symbol_t : public dummy_item_t
{
pdb_sym_t *sym;
mutable pdb_lnnums_t lnnums; // cached ptr to line number enumerator
src_item_kind_t kind;
bool own_sym;
bool init_lnnums() const
{
if ( !lnnums.inited )
{
ULONGLONG va;
if ( sym->get_virtualAddress(&va) == S_OK )
{
ULONGLONG length;
if ( sym->get_length(&length) == S_OK )
{
pdb_access_t *pa = pdb_module->get_access();
if ( pa->sip_retrieve_lines_by_va(&lnnums, va, length) == S_OK )
lnnums.inited = true;
}
}
}
return lnnums.inited;
}
public:
pdb_symbol_t(pdb_modinfo_t *_pdb_module,
pdb_sym_t *_sym,
bool _own_sym,
src_item_kind_t k)
: dummy_item_t(_pdb_module),
sym(_sym),
kind(k),
own_sym(_own_sym)
{
}
virtual ~pdb_symbol_t(void)
{
if ( own_sym )
delete sym;
}
pdb_sym_t *get_pdb_sym() { return sym; }
source_file_iterator idaapi get_source_files(void) override
{
pdb_file_iterator *ret = nullptr;
qvector<DWORD> ids;
HRESULT hr = pdb_module->get_access()->sip_retrieve_symbol_files(
&ids, *sym);
if ( hr == S_OK )
{
ret = new pdb_file_iterator();
for ( size_t i = 0; i < ids.size(); ++i )
{
pdb_file_iterator::entry_t &e = ret->entries.push_back();
e.pdb_module = pdb_module;
e.file_id = ids[i];
}
}
return source_file_iterator(ret);
}
bool idaapi get_name(qstring *buf) const override
{
return sym->get_name(buf) == S_OK;
}
int idaapi get_lnnum() const override
{
return init_lnnums() ? lnnums.get_lnnum() : 0;
}
int idaapi get_end_lnnum() const override
{
return init_lnnums() ? lnnums.get_end_lnnum() : 0;
}
int idaapi get_colnum() const override
{
return init_lnnums() ? lnnums.get_colnum() : 0;
}
int idaapi get_end_colnum() const override
{
if ( !init_lnnums() )
return 0;
return lnnums.get_end_colnum();
}
ea_t idaapi get_ea() const override
{
ULONGLONG va = ULONGLONG(-1);
return FAILED(sym->get_virtualAddress(&va)) ? BADADDR : va;
}
asize_t idaapi get_size() const override
{
ULONGLONG len = 0;
return FAILED(sym->get_length(&len)) ? BADADDR : len;
}
bool idaapi get_item_bounds(rangeset_t *set) const override
{
return init_lnnums() && lnnums.get_item_bounds(set);
}
source_item_ptr idaapi get_parent(src_item_kind_t /*max_kind*/) const override
{
source_item_t *ret = nullptr;
pdb_sym_t *lpar = pdb_module->get_access()->create_sym();
pdb_sym_janitor_t janitor_lpar(lpar);
DWORD par_id = 0;
if ( sym->get_lexicalParent(lpar) == S_OK
&& lpar->get_symIndexId(&par_id) == S_OK )
{
ret = new_pdb_symbol(pdb_module, par_id);
}
return source_item_ptr(ret);
}
source_item_iterator idaapi create_children_iterator() override;
// TODO: not implemented yet
/*bool idaapi get_hint(qstring *hint, const eval_ctx_t *ctx, int *nlines) const
{
return false;
}*/
bool idaapi evaluate(const eval_ctx_t * /*ctx*/, idc_value_t * /*res*/, qstring * /*errbuf*/) const override
{
// not implemented yet
return false;
}
virtual src_item_kind_t idaapi get_item_kind(const eval_ctx_t * /*ctx*/) const override
{
return kind;
}
virtual bool idaapi get_location(argloc_t *out, const eval_ctx_t *) const override
{
DWORD loctype = LocIsNull;
HRESULT hr = sym->get_locationType(&loctype);
if ( FAILED(hr) )
return false;
bool ok = false;
int machine = pdb_module->get_access()->get_machine_type();
switch ( loctype )
{
case LocIsRegRel:
{
DWORD dwReg = 0;
LONG lOffset;
if ( sym->get_registerId(&dwReg) == S_OK
&& sym->get_offset(&lOffset) == S_OK )
{
int regno;
uint64 mask;
if ( pdb_module->pv.get_pdb_register_info(®no, &mask, machine, dwReg) )
{
rrel_t *rrel = new rrel_t();
rrel->reg = regno;
rrel->off = lOffset;
out->consume_rrel(rrel);
ok = true;
}
}
}
break;
case LocIsEnregistered:
{
DWORD dwReg = 0;
if ( sym->get_registerId(&dwReg) == S_OK )
{
int regno;
uint64 mask;
if ( pdb_module->pv.get_pdb_register_info(®no, &mask, machine, dwReg) )
{
out->set_reg1(regno, 0); // off=0?
ok = true;
}
}
}
break;
default:
break;
}
return ok;
}
bool idaapi get_expr_tinfo(tinfo_t *tif) const override
{
til_builder_t::tpinfo_t tpi;
bool res = pdb_module->type_cache->retrieve_type(&tpi, *sym, nullptr);
*tif = tpi.type;
if ( (debug & IDA_DEBUG_SRCDBG) != 0 )
{
qstring type_str;
tpi.type.print(&type_str);
DWORD sym_id = 0;
sym->get_symIndexId(&sym_id);
qstring name;
deb(IDA_DEBUG_SRCDBG, "Retrieved type for %s (symbol #%u): %s\n",
get_name(&name) ? name.c_str() : "<unnamed>",
sym_id,
type_str.c_str());
}
return res;
}
bool idaapi equals(const source_item_t *othr) const override
{
DWORD this_id, other_id;
pdb_symbol_t *other = (pdb_symbol_t*) othr;
return other != nullptr
&& other->sym != nullptr
&& pdb_module == other->pdb_module
&& sym->get_symIndexId(&this_id) == S_OK
&& other->sym->get_symIndexId(&other_id) == S_OK
&& this_id == other_id;
}
};
//--------------------------------------------------------------------------
source_item_iterator idaapi pdb_symbol_t::create_children_iterator()
{
pdb_item_iterator *ret = nullptr;
source_items_vec_builder_t items_builder(pdb_module);
if ( pdb_module->get_access()->iterate_children(
*sym, SymTagNull, items_builder) == S_OK )
ret = new pdb_item_iterator(pdb_module, items_builder.items);
return source_item_iterator(ret);
}
//--------------------------------------------------------------------------
class pdb_lnnum_item_t : public dummy_item_t
{
pdb_lnnum_t *lnnum; // we do not own this pointer
public:
pdb_lnnum_item_t(pdb_modinfo_t *_pdb_module, pdb_lnnum_t *l)
: dummy_item_t(_pdb_module),
lnnum(l) {}
virtual ~pdb_lnnum_item_t(void) {}
virtual source_file_iterator idaapi get_source_files(void) override
{
pdb_file_iterator *ret = nullptr;
if ( lnnum->file_id != DWORD(-1) )
{
ret = new pdb_file_iterator();
pdb_file_iterator::entry_t &e = ret->entries.push_back();
e.pdb_module = pdb_module;
e.file_id = lnnum->file_id;
}
return source_file_iterator(ret);
}
virtual bool idaapi get_name(qstring *) const override
{
return false;
}
virtual int idaapi get_lnnum() const override
{
return lnnum->lineNumber;
}
virtual int idaapi get_end_lnnum() const override
{
return lnnum->lineNumberEnd;
}
virtual int idaapi get_colnum() const override
{
return lnnum->columnNumber;
}
virtual int idaapi get_end_colnum() const override
{
return lnnum->columnNumberEnd;
}
virtual ea_t idaapi get_ea() const override
{
return ea_t(lnnum->va);
}
virtual asize_t idaapi get_size() const override
{
return lnnum->length;
}
virtual src_item_kind_t idaapi get_item_kind(const eval_ctx_t * /*ctx*/) const override
{
return lnnum->statement ? SRCIT_STMT : SRCIT_EXPR;
}
virtual source_item_ptr idaapi get_parent(src_item_kind_t /*max_kind*/) const override
{
source_item_t *ret = nullptr;
ea_t ea = get_ea();
if ( ea != BADADDR )
{
source_items_vec_builder_t items_builder(pdb_module);
HRESULT hr = pdb_module->get_access()->sip_iterate_symbols_at_ea(
ea, /*size=*/ 1, SymTagFunction, items_builder);
if ( hr == S_OK && !items_builder.items.empty() )
{
DWORD sym_id = 0;
pdb_symbol_t &pit = (pdb_symbol_t &) *items_builder.items[0];
hr = pit.get_pdb_sym()->get_symIndexId(&sym_id);
if ( hr == S_OK )
ret = new_pdb_symbol(pdb_module, sym_id);
}
}
return source_item_ptr(ret);
}
bool idaapi equals(const source_item_t *othr) const override
{
pdb_lnnum_item_t *other = (pdb_lnnum_item_t*) othr;
return other != nullptr
&& other->lnnum != nullptr
&& lnnum->va != BADADDR
&& other->lnnum->va != BADADDR
&& lnnum->va == other->lnnum->va;
}
};
//-------------------------------------------------------------------------
static src_item_kind_t find_srcitem_kind(pdb_sym_t *sym)
{
src_item_kind_t kind = SRCIT_NONE;
DWORD tag = 0;
HRESULT hr = sym->get_symTag(&tag);
if ( hr == S_OK )
{
switch ( tag )
{
case SymTagFunction:
kind = SRCIT_FUNC;
break;
case SymTagBlock:
kind = SRCIT_STMT;
break;
case SymTagData:
case SymTagPublicSymbol:
{
DWORD loctype = LocIsNull;
sym->get_locationType(&loctype);
switch ( loctype )
{
case LocIsStatic:
case LocIsTLS:
kind = SRCIT_STTVAR;
break;
case LocIsRegRel:
DWORD dwReg;
if ( sym->get_registerId(&dwReg) == S_OK
&& (dwReg == CV_REG_EBP || dwReg == CV_AMD64_RSP) )
{
kind = SRCIT_LOCVAR;
}
break;
case LocIsEnregistered:
kind = SRCIT_LOCVAR;
break;
}
}
break;
}
}
return kind;
}
//--------------------------------------------------------------------------
static source_item_t *new_pdb_symbol(pdb_modinfo_t *pdb_module, DWORD sym_id)
{
pdb_sym_t *sym = pdb_module->get_access()->create_sym(sym_id);
src_item_kind_t kind = find_srcitem_kind(sym);
if ( kind != SRCIT_NONE )
return new pdb_symbol_t(pdb_module, sym, /*own=*/ true, kind);
delete sym;
return nullptr;
}
//--------------------------------------------------------------------------
static source_item_t *new_pdb_symbol_or_delete(pdb_modinfo_t *pdb_module, pdb_sym_t *sym)
{
src_item_kind_t kind = find_srcitem_kind(sym);
if ( kind != SRCIT_NONE )
return new pdb_symbol_t(pdb_module, sym, /*own=*/ true, kind);
delete sym;
return nullptr;
}
//--------------------------------------------------------------------------
class pdb_provider_t : public srcinfo_provider_t
{
pdb_ctx_t &pv;
pdb_modules_t modules;
qstring search_path;
pdb_modinfo_t *open_module(pdb_modules_t::iterator p)
{
pdb_modinfo_t &mod = p->second;
if ( !mod.opened )
{
msg("PDBSRC: loading symbols for '%s'...\n", mod.path.c_str());
HRESULT hr = mod.open(mod.path.c_str(), search_path.c_str(), mod.base);
if ( FAILED(hr) )
{ // failed to open the corresponding pdb file
modules.erase(p);
return nullptr;
}
mod.opened = true;
}
return &mod;
}
pdb_modinfo_t *find_module(ea_t ea)
{
deb(IDA_DEBUG_SRCDBG, "PDB: find_module(%a)\n", ea);
pdb_modules_t::iterator p = modules.lower_bound(ea);
if ( p == modules.end() || p->first > ea )
{
if ( p == modules.begin() )
return nullptr; // could not find the module
--p;
if ( p->first > ea || p->first+p->second.size <= ea )
return nullptr;
}
return open_module(p);
}
pdb_modinfo_t *find_module(const char *path)
{
deb(IDA_DEBUG_SRCDBG, "PDB: find_module(%s)\n", path);
pdb_modules_t::iterator p = modules.begin();
for ( ; p != modules.end(); ++p )
if ( p->second.path == path )
return &p->second;
return nullptr;
}
public:
bool idaapi enable_provider(bool enable) override;
const char *idaapi set_options(const char *keyword, int value_type, const void *value) override;
void idaapi add_module(const char *path, ea_t base, asize_t size) override;
void idaapi del_module(ea_t base) override;
void idaapi get_ready(void) override;
int idaapi get_change_flags(void) override;
source_item_iterator idaapi find_source_items(ea_t ea, asize_t size, src_item_kind_t level, bool) override;
source_item_iterator idaapi find_source_items(source_file_t *sf, int lnnum, int colnum) override;
source_file_iterator idaapi create_file_iterator(const char *filename) override;
source_item_iterator idaapi create_item_iterator(const source_file_t *sf) override;
bool idaapi apply_module_info(const char *path) override;
source_item_ptr idaapi find_static_item(const char *name, ea_t ea) override;
pdb_provider_t(pdb_ctx_t &_pv, const char *nm, const char *dnm)
: srcinfo_provider_t(nm, dnm),
pv(_pv)
{}
virtual ~pdb_provider_t(void) {}
};
//---------------------------------------------------------------------------
static bool is_pdb_supported(void)
{
// PE files.
filetype_t ftype = inf_get_filetype();
if ( ftype == f_PE )
return true;
// Otherwise check for debugger.
if ( dbg == nullptr )
return false;
// Win32 debugger.
qstring platform;
debapp_attrs_t pattrs;
if ( dbg->get_debapp_attrs(&pattrs) )
platform.swap(pattrs.platform);
else
platform = dbg->name;
if ( platform.find("win32") != qstring::npos )
return true;
// Some other debugger (e.g.: "gdb") with unknown filetype.
// This is needed to debug windows kernels under VMware.
if ( ftype == 0 )
return true;
return false;
}
//--------------------------------------------------------------------------
bool idaapi pdb_provider_t::enable_provider(bool enable)
{
if ( enable )
{
if ( !is_pdb_supported() )
return false;
pv.init_sympaths();
if ( pv.full_sympath.empty() )
search_path.qclear();
else
search_path = pv.full_sympath;
}
return enable;
}
//--------------------------------------------------------------------------
const char *idaapi pdb_provider_t::set_options(
const char * /*keyword*/,
int /*value_type*/,
const void * /*value*/)
{
// todo: add option to set search path
return IDPOPT_BADKEY;
}
//--------------------------------------------------------------------------
void idaapi pdb_provider_t::add_module(
const char *path,
ea_t base,
asize_t size)
{
deb(IDA_DEBUG_DEBUGGER, "PDB: add_module(%s, [%a -> %a))\n", path, base, ea_t(base + size));
pdb_modinfo_t &mod = modules[base];
mod.path = path;
mod.base = base;
mod.size = size;
// do not open the module immediately, we will do it only when we
// really need the module
mod.opened = false;
mod.type_cache = nullptr;
}
//--------------------------------------------------------------------------
void idaapi pdb_provider_t::del_module(ea_t base)
{
modules.erase(base);
}
//--------------------------------------------------------------------------
void idaapi pdb_provider_t::get_ready(void)
{
// nothing to do
}
//--------------------------------------------------------------------------
int idaapi pdb_provider_t::get_change_flags(void)
{
// nothing ever changes?
return 0;
}
//--------------------------------------------------------------------------
// Retrieve the line numbers into a map
// 'enumerator' will be freed by this function
static void lnnums_to_lnmap(lnmap_t *map, const pdb_lnnums_t &lnnums)
{
const size_t lncnt = lnnums.size();
if ( lncnt > 0 )
{
pdb_lnnum_vec_t vec;
vec.resize(lncnt);
for ( size_t i = 0; i < lncnt; ++i )
{
const pdb_lnnum_t &lnnum = lnnums[i];
(*map)[lnnum.lineNumber].push_back(lnnum);
}
}