-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
DwarfImageDebugInfo.cpp
1119 lines (921 loc) · 30.7 KB
/
DwarfImageDebugInfo.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
/*
* Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2012, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "DwarfImageDebugInfo.h"
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <algorithm>
#include <new>
#include <AutoDeleter.h>
#include <AutoLocker.h>
#include "Architecture.h"
#include "BasicFunctionDebugInfo.h"
#include "CLanguage.h"
#include "CompilationUnit.h"
#include "CppLanguage.h"
#include "CpuState.h"
#include "DebuggerInterface.h"
#include "DebugInfoEntries.h"
#include "Demangler.h"
#include "Dwarf.h"
#include "DwarfFile.h"
#include "DwarfFunctionDebugInfo.h"
#include "DwarfStackFrameDebugInfo.h"
#include "DwarfTargetInterface.h"
#include "DwarfTypeFactory.h"
#include "DwarfTypes.h"
#include "DwarfUtils.h"
#include "ElfFile.h"
#include "FileManager.h"
#include "FileSourceCode.h"
#include "FunctionID.h"
#include "FunctionInstance.h"
#include "GlobalTypeLookup.h"
#include "LocatableFile.h"
#include "Register.h"
#include "RegisterMap.h"
#include "SourceFile.h"
#include "StackFrame.h"
#include "Statement.h"
#include "StringUtils.h"
#include "SymbolInfo.h"
#include "TargetAddressRangeList.h"
#include "TeamMemory.h"
#include "Tracing.h"
#include "TypeLookupConstraints.h"
#include "UnsupportedLanguage.h"
#include "Variable.h"
namespace {
// #pragma mark - HasTypePredicate
template<typename EntryType>
struct HasTypePredicate {
inline bool operator()(EntryType* entry) const
{
return entry->GetType() != NULL;
}
};
}
// #pragma mark - BasicTargetInterface
struct DwarfImageDebugInfo::BasicTargetInterface : DwarfTargetInterface {
BasicTargetInterface(const Register* registers, int32 registerCount,
RegisterMap* fromDwarfMap, Architecture* architecture,
TeamMemory* teamMemory)
:
fRegisters(registers),
fRegisterCount(registerCount),
fFromDwarfMap(fromDwarfMap),
fArchitecture(architecture),
fTeamMemory(teamMemory)
{
fFromDwarfMap->AcquireReference();
}
~BasicTargetInterface()
{
fFromDwarfMap->ReleaseReference();
}
virtual uint32 CountRegisters() const
{
return fRegisterCount;
}
virtual uint32 RegisterValueType(uint32 index) const
{
const Register* reg = _RegisterAt(index);
return reg != NULL ? reg->ValueType() : 0;
}
virtual bool GetRegisterValue(uint32 index, BVariant& _value) const
{
return false;
}
virtual bool SetRegisterValue(uint32 index, const BVariant& value)
{
return false;
}
virtual bool IsCalleePreservedRegister(uint32 index) const
{
const Register* reg = _RegisterAt(index);
return reg != NULL && reg->IsCalleePreserved();
}
virtual status_t InitRegisterRules(CfaContext& context) const
{
return fArchitecture->InitRegisterRules(context);
}
virtual bool ReadMemory(target_addr_t address, void* buffer,
size_t size) const
{
ssize_t bytesRead = fTeamMemory->ReadMemory(address, buffer, size);
return bytesRead >= 0 && (size_t)bytesRead == size;
}
virtual bool ReadValueFromMemory(target_addr_t address,
uint32 valueType, BVariant& _value) const
{
return fArchitecture->ReadValueFromMemory(address, valueType, _value)
== B_OK;
}
virtual bool ReadValueFromMemory(target_addr_t addressSpace,
target_addr_t address, uint32 valueType, BVariant& _value) const
{
return fArchitecture->ReadValueFromMemory(addressSpace, address,
valueType, _value) == B_OK;
}
protected:
const Register* _RegisterAt(uint32 dwarfIndex) const
{
int32 index = fFromDwarfMap->MapRegisterIndex(dwarfIndex);
return index >= 0 && index < fRegisterCount ? fRegisters + index : NULL;
}
protected:
const Register* fRegisters;
int32 fRegisterCount;
RegisterMap* fFromDwarfMap;
Architecture* fArchitecture;
TeamMemory* fTeamMemory;
};
// #pragma mark - UnwindTargetInterface
struct DwarfImageDebugInfo::UnwindTargetInterface : BasicTargetInterface {
UnwindTargetInterface(const Register* registers, int32 registerCount,
RegisterMap* fromDwarfMap, RegisterMap* toDwarfMap, CpuState* cpuState,
Architecture* architecture, TeamMemory* teamMemory)
:
BasicTargetInterface(registers, registerCount, fromDwarfMap,
architecture, teamMemory),
fToDwarfMap(toDwarfMap),
fCpuState(cpuState)
{
fToDwarfMap->AcquireReference();
fCpuState->AcquireReference();
}
~UnwindTargetInterface()
{
fToDwarfMap->ReleaseReference();
fCpuState->ReleaseReference();
}
virtual bool GetRegisterValue(uint32 index, BVariant& _value) const
{
const Register* reg = _RegisterAt(index);
if (reg == NULL)
return false;
return fCpuState->GetRegisterValue(reg, _value);
}
virtual bool SetRegisterValue(uint32 index, const BVariant& value)
{
const Register* reg = _RegisterAt(index);
if (reg == NULL)
return false;
return fCpuState->SetRegisterValue(reg, value);
}
private:
RegisterMap* fToDwarfMap;
CpuState* fCpuState;
};
// #pragma mark - EntryListWrapper
/*! Wraps a DebugInfoEntryList, which is a typedef and thus cannot appear in
the header, since our policy disallows us to include DWARF headers there.
*/
struct DwarfImageDebugInfo::EntryListWrapper {
const DebugInfoEntryList& list;
EntryListWrapper(const DebugInfoEntryList& list)
:
list(list)
{
}
};
// #pragma mark - DwarfImageDebugInfo
DwarfImageDebugInfo::DwarfImageDebugInfo(const ImageInfo& imageInfo,
DebuggerInterface* interface, Architecture* architecture,
FileManager* fileManager, GlobalTypeLookup* typeLookup,
GlobalTypeCache* typeCache, DwarfFile* file)
:
fLock("dwarf image debug info"),
fImageInfo(imageInfo),
fDebuggerInterface(interface),
fArchitecture(architecture),
fFileManager(fileManager),
fTypeLookup(typeLookup),
fTypeCache(typeCache),
fFile(file),
fTextSegment(NULL),
fRelocationDelta(0),
fTextSectionStart(0),
fTextSectionEnd(0),
fPLTSectionStart(0),
fPLTSectionEnd(0)
{
fDebuggerInterface->AcquireReference();
fFile->AcquireReference();
fTypeCache->AcquireReference();
}
DwarfImageDebugInfo::~DwarfImageDebugInfo()
{
fDebuggerInterface->ReleaseReference();
fFile->ReleaseReference();
fTypeCache->ReleaseReference();
}
status_t
DwarfImageDebugInfo::Init()
{
status_t error = fLock.InitCheck();
if (error != B_OK)
return error;
fTextSegment = fFile->GetElfFile()->TextSegment();
if (fTextSegment == NULL)
return B_ENTRY_NOT_FOUND;
fRelocationDelta = fImageInfo.TextBase() - fTextSegment->LoadAddress();
ElfSection* section = fFile->GetElfFile()->FindSection(".text");
if (section != NULL) {
fTextSectionStart = section->LoadAddress() + fRelocationDelta;
fTextSectionEnd = fTextSectionStart + section->Size();
}
section = fFile->GetElfFile()->FindSection(".plt");
if (section != NULL) {
fPLTSectionStart = section->LoadAddress() + fRelocationDelta;
fPLTSectionEnd = fPLTSectionStart + section->Size();
}
return B_OK;
}
status_t
DwarfImageDebugInfo::GetFunctions(BObjectList<FunctionDebugInfo>& functions)
{
TRACE_IMAGES("DwarfImageDebugInfo::GetFunctions()\n");
TRACE_IMAGES(" %" B_PRId32 " compilation units\n",
fFile->CountCompilationUnits());
for (int32 i = 0; CompilationUnit* unit = fFile->CompilationUnitAt(i);
i++) {
DIECompileUnitBase* unitEntry = unit->UnitEntry();
// printf(" %s:\n", unitEntry->Name());
// printf(" address ranges:\n");
// TargetAddressRangeList* rangeList = unitEntry->AddressRanges();
// if (rangeList != NULL) {
// int32 count = rangeList->CountRanges();
// for (int32 i = 0; i < count; i++) {
// TargetAddressRange range = rangeList->RangeAt(i);
// printf(" %#llx - %#llx\n", range.Start(), range.End());
// }
// } else {
// printf(" %#llx - %#llx\n", (target_addr_t)unitEntry->LowPC(),
// (target_addr_t)unitEntry->HighPC());
// }
// printf(" functions:\n");
for (DebugInfoEntryList::ConstIterator it
= unitEntry->OtherChildren().GetIterator();
DebugInfoEntry* entry = it.Next();) {
if (entry->Tag() != DW_TAG_subprogram)
continue;
DIESubprogram* subprogramEntry = static_cast<DIESubprogram*>(entry);
// ignore declarations and inlined functions
if (subprogramEntry->IsDeclaration()
|| subprogramEntry->Inline() == DW_INL_inlined
|| subprogramEntry->Inline() == DW_INL_declared_inlined) {
continue;
}
// get the name
BString name;
DwarfUtils::GetFullyQualifiedDIEName(subprogramEntry, name);
if (name.Length() == 0)
continue;
// get the address ranges
TargetAddressRangeList* rangeList = fFile->ResolveRangeList(unit,
subprogramEntry->AddressRangesOffset());
if (rangeList == NULL) {
target_addr_t lowPC = subprogramEntry->LowPC();
target_addr_t highPC = subprogramEntry->HighPC();
if (lowPC >= highPC)
continue;
rangeList = new(std::nothrow) TargetAddressRangeList(
TargetAddressRange(lowPC, highPC - lowPC));
if (rangeList == NULL)
return B_NO_MEMORY;
// TODO: Clean up already added functions!
}
BReference<TargetAddressRangeList> rangeListReference(rangeList,
true);
// get the source location
const char* directoryPath = NULL;
const char* fileName = NULL;
int32 line = -1;
int32 column = -1;
DwarfUtils::GetDeclarationLocation(fFile, subprogramEntry,
directoryPath, fileName, line, column);
LocatableFile* file = NULL;
if (fileName != NULL) {
file = fFileManager->GetSourceFile(directoryPath,
fileName);
}
BReference<LocatableFile> fileReference(file, true);
// create and add the functions
DwarfFunctionDebugInfo* function
= new(std::nothrow) DwarfFunctionDebugInfo(this, unit,
subprogramEntry, rangeList, name, file,
SourceLocation(line, std::max(column, (int32)0)));
if (function == NULL || !functions.AddItem(function)) {
delete function;
return B_NO_MEMORY;
// TODO: Clean up already added functions!
}
// BString name;
// DwarfUtils::GetFullyQualifiedDIEName(subprogramEntry, name);
// printf(" subprogram entry: %p, name: %s, declaration: %d\n",
// subprogramEntry, name.String(),
// subprogramEntry->IsDeclaration());
//
// rangeList = subprogramEntry->AddressRanges();
// if (rangeList != NULL) {
// int32 count = rangeList->CountRanges();
// for (int32 i = 0; i < count; i++) {
// TargetAddressRange range = rangeList->RangeAt(i);
// printf(" %#llx - %#llx\n", range.Start(), range.End());
// }
// } else {
// printf(" %#llx - %#llx\n",
// (target_addr_t)subprogramEntry->LowPC(),
// (target_addr_t)subprogramEntry->HighPC());
// }
}
}
if (fFile->CountCompilationUnits() != 0)
return B_OK;
// if we had no compilation units, fall back to providing basic
// debug infos with DWARF-supported call frame unwinding
return SpecificImageDebugInfo::GetFunctionsFromSymbols(functions,
fDebuggerInterface, fImageInfo, this);
}
status_t
DwarfImageDebugInfo::GetType(GlobalTypeCache* cache,
const BString& name, const TypeLookupConstraints& constraints,
Type*& _type)
{
int32 registerCount = fArchitecture->CountRegisters();
const Register* registers = fArchitecture->Registers();
// get the DWARF -> architecture register map
RegisterMap* fromDwarfMap;
status_t error = fArchitecture->GetDwarfRegisterMaps(NULL, &fromDwarfMap);
if (error != B_OK)
return error;
BReference<RegisterMap> fromDwarfMapReference(fromDwarfMap, true);
// create the target interface
BasicTargetInterface *inputInterface
= new(std::nothrow) BasicTargetInterface(registers, registerCount,
fromDwarfMap, fArchitecture, fDebuggerInterface);
if (inputInterface == NULL)
return B_NO_MEMORY;
BReference<BasicTargetInterface> inputInterfaceReference(inputInterface,
true);
// iterate through all compilation units
for (int32 i = 0; CompilationUnit* unit = fFile->CompilationUnitAt(i);
i++) {
DwarfTypeContext* typeContext = NULL;
BReference<DwarfTypeContext> typeContextReference;
// iterate through all types of the compilation unit
for (DebugInfoEntryList::ConstIterator it
= unit->UnitEntry()->Types().GetIterator();
DIEType* typeEntry = dynamic_cast<DIEType*>(it.Next());) {
if (typeEntry->IsDeclaration())
continue;
if (constraints.HasTypeKind()) {
if (dwarf_tag_to_type_kind(typeEntry->Tag())
!= constraints.TypeKind())
continue;
if (!_EvaluateBaseTypeConstraints(typeEntry,
constraints))
continue;
}
if (constraints.HasSubtypeKind()
&& dwarf_tag_to_subtype_kind(typeEntry->Tag())
!= constraints.SubtypeKind())
continue;
BString typeEntryName;
DwarfUtils::GetFullyQualifiedDIEName(typeEntry, typeEntryName);
if (typeEntryName != name)
continue;
// The name matches and the entry is not just a declaration --
// create the type. First create the type context lazily.
if (typeContext == NULL) {
typeContext = new(std::nothrow)
DwarfTypeContext(fArchitecture, fImageInfo.ImageID(), fFile,
unit, NULL, 0, 0, fRelocationDelta, inputInterface,
fromDwarfMap);
if (typeContext == NULL)
return B_NO_MEMORY;
typeContextReference.SetTo(typeContext, true);
}
// create the type
DwarfType* type;
DwarfTypeFactory typeFactory(typeContext, fTypeLookup, cache);
error = typeFactory.CreateType(typeEntry, type);
if (error != B_OK)
continue;
_type = type;
return B_OK;
}
}
return B_ENTRY_NOT_FOUND;
}
AddressSectionType
DwarfImageDebugInfo::GetAddressSectionType(target_addr_t address)
{
if (address >= fTextSectionStart && address < fTextSectionEnd)
return ADDRESS_SECTION_TYPE_FUNCTION;
if (address >= fPLTSectionStart && address < fPLTSectionEnd)
return ADDRESS_SECTION_TYPE_PLT;
return ADDRESS_SECTION_TYPE_UNKNOWN;
}
status_t
DwarfImageDebugInfo::CreateFrame(Image* image,
FunctionInstance* functionInstance, CpuState* cpuState,
bool getFullFrameInfo, StackFrame*& _frame, CpuState*& _previousCpuState)
{
DwarfFunctionDebugInfo* function = dynamic_cast<DwarfFunctionDebugInfo*>(
functionInstance->GetFunctionDebugInfo());
FunctionID* functionID = functionInstance->GetFunctionID();
BReference<FunctionID> functionIDReference;
if (functionID != NULL)
functionIDReference.SetTo(functionID, true);
DIESubprogram* entry = function != NULL
? function->SubprogramEntry() : NULL;
TRACE_CFI("DwarfImageDebugInfo::CreateFrame(): subprogram DIE: %p, "
"function: %s\n", entry,
functionID->FunctionName().String());
int32 registerCount = fArchitecture->CountRegisters();
const Register* registers = fArchitecture->Registers();
// get the DWARF <-> architecture register maps
RegisterMap* toDwarfMap;
RegisterMap* fromDwarfMap;
status_t error = fArchitecture->GetDwarfRegisterMaps(&toDwarfMap,
&fromDwarfMap);
if (error != B_OK)
return error;
BReference<RegisterMap> toDwarfMapReference(toDwarfMap, true);
BReference<RegisterMap> fromDwarfMapReference(fromDwarfMap, true);
// create a clean CPU state for the previous frame
CpuState* previousCpuState;
error = fArchitecture->CreateCpuState(previousCpuState);
if (error != B_OK)
return error;
BReference<CpuState> previousCpuStateReference(previousCpuState, true);
// create the target interfaces
UnwindTargetInterface* inputInterface
= new(std::nothrow) UnwindTargetInterface(registers, registerCount,
fromDwarfMap, toDwarfMap, cpuState, fArchitecture,
fDebuggerInterface);
if (inputInterface == NULL)
return B_NO_MEMORY;
BReference<UnwindTargetInterface> inputInterfaceReference(inputInterface,
true);
UnwindTargetInterface* outputInterface
= new(std::nothrow) UnwindTargetInterface(registers, registerCount,
fromDwarfMap, toDwarfMap, previousCpuState, fArchitecture,
fDebuggerInterface);
if (outputInterface == NULL)
return B_NO_MEMORY;
BReference<UnwindTargetInterface> outputInterfaceReference(outputInterface,
true);
// do the unwinding
target_addr_t instructionPointer
= cpuState->InstructionPointer() - fRelocationDelta;
target_addr_t framePointer;
CompilationUnit* unit = function != NULL ? function->GetCompilationUnit()
: NULL;
error = fFile->UnwindCallFrame(unit, fArchitecture->AddressSize(), entry,
instructionPointer, inputInterface, outputInterface, framePointer);
if (error != B_OK) {
TRACE_CFI("Failed to unwind call frame: %s\n", strerror(error));
return B_UNSUPPORTED;
}
TRACE_CFI_ONLY(
TRACE_CFI("unwound registers:\n");
for (int32 i = 0; i < registerCount; i++) {
const Register* reg = registers + i;
BVariant value;
if (previousCpuState->GetRegisterValue(reg, value)) {
TRACE_CFI(" %3s: %#" B_PRIx64 "\n", reg->Name(),
value.ToUInt64());
} else
TRACE_CFI(" %3s: undefined\n", reg->Name());
}
)
// create the stack frame debug info
DIESubprogram* subprogramEntry = function != NULL ?
function->SubprogramEntry() : NULL;
DwarfStackFrameDebugInfo* stackFrameDebugInfo
= new(std::nothrow) DwarfStackFrameDebugInfo(fArchitecture,
fImageInfo.ImageID(), fFile, unit, subprogramEntry, fTypeLookup,
fTypeCache, instructionPointer, framePointer, fRelocationDelta,
inputInterface, fromDwarfMap);
if (stackFrameDebugInfo == NULL)
return B_NO_MEMORY;
BReference<DwarfStackFrameDebugInfo> stackFrameDebugInfoReference(
stackFrameDebugInfo, true);
error = stackFrameDebugInfo->Init();
if (error != B_OK)
return error;
// create the stack frame
StackFrame* frame = new(std::nothrow) StackFrame(STACK_FRAME_TYPE_STANDARD,
cpuState, framePointer, cpuState->InstructionPointer(),
stackFrameDebugInfo);
if (frame == NULL)
return B_NO_MEMORY;
BReference<StackFrame> frameReference(frame, true);
error = frame->Init();
if (error != B_OK)
return error;
frame->SetReturnAddress(previousCpuState->InstructionPointer());
// Note, this is correct, since we actually retrieved the return
// address. Our caller will fix the IP for us.
// The subprogram entry may not be available since this may be a case
// where .eh_frame was used to unwind the stack without other DWARF
// info being available.
if (subprogramEntry != NULL && getFullFrameInfo) {
// create function parameter objects
for (DebugInfoEntryList::ConstIterator it
= subprogramEntry->Parameters().GetIterator();
DebugInfoEntry* entry = it.Next();) {
if (entry->Tag() != DW_TAG_formal_parameter)
continue;
BString parameterName;
DwarfUtils::GetDIEName(entry, parameterName);
if (parameterName.Length() == 0)
continue;
DIEFormalParameter* parameterEntry
= dynamic_cast<DIEFormalParameter*>(entry);
Variable* parameter;
if (stackFrameDebugInfo->CreateParameter(functionID,
parameterEntry, parameter) != B_OK) {
continue;
}
BReference<Variable> parameterReference(parameter, true);
if (!frame->AddParameter(parameter))
return B_NO_MEMORY;
}
// create objects for the local variables
_CreateLocalVariables(unit, frame, functionID, *stackFrameDebugInfo,
instructionPointer, functionInstance->Address() - fRelocationDelta,
subprogramEntry->Variables(), subprogramEntry->Blocks());
}
_frame = frameReference.Detach();
_previousCpuState = previousCpuStateReference.Detach();
frame->SetPreviousCpuState(_previousCpuState);
return B_OK;
}
status_t
DwarfImageDebugInfo::GetStatement(FunctionDebugInfo* _function,
target_addr_t address, Statement*& _statement)
{
TRACE_CODE("DwarfImageDebugInfo::GetStatement(function: %p, address: %#"
B_PRIx64 ")\n", _function, address);
DwarfFunctionDebugInfo* function
= dynamic_cast<DwarfFunctionDebugInfo*>(_function);
if (function == NULL) {
TRACE_LINES(" -> no dwarf function\n");
return B_BAD_VALUE;
}
AutoLocker<BLocker> locker(fLock);
// check whether we have the source code
CompilationUnit* unit = function->GetCompilationUnit();
LocatableFile* file = function->SourceFile();
if (file == NULL) {
TRACE_CODE(" -> no source file\n");
// no source code -- rather return the assembly statement
return fArchitecture->GetStatement(function, address, _statement);
}
// get the index of the source file in the compilation unit for cheaper
// comparison below
int32 fileIndex = _GetSourceFileIndex(unit, file);
// Get the statement by executing the line number program for the
// compilation unit.
LineNumberProgram& program = unit->GetLineNumberProgram();
if (!program.IsValid()) {
TRACE_CODE(" -> no line number program\n");
return B_BAD_DATA;
}
// adjust address
address -= fRelocationDelta;
LineNumberProgram::State state;
program.GetInitialState(state);
target_addr_t statementAddress = 0;
int32 statementLine = -1;
int32 statementColumn = -1;
while (program.GetNextRow(state)) {
// skip statements of other files
if (state.file != fileIndex)
continue;
if (statementAddress != 0
&& (state.isStatement || state.isSequenceEnd)) {
target_addr_t endAddress = state.address;
if (address >= statementAddress && address < endAddress) {
ContiguousStatement* statement = new(std::nothrow)
ContiguousStatement(
SourceLocation(statementLine, statementColumn),
TargetAddressRange(fRelocationDelta + statementAddress,
endAddress - statementAddress));
if (statement == NULL)
return B_NO_MEMORY;
_statement = statement;
return B_OK;
}
statementAddress = 0;
}
if (state.isStatement) {
statementAddress = state.address;
statementLine = state.line - 1;
statementColumn = std::max(state.column - 1, (int32)0);
}
}
TRACE_CODE(" -> no line number program match\n");
return B_ENTRY_NOT_FOUND;
}
status_t
DwarfImageDebugInfo::GetStatementAtSourceLocation(FunctionDebugInfo* _function,
const SourceLocation& sourceLocation, Statement*& _statement)
{
DwarfFunctionDebugInfo* function
= dynamic_cast<DwarfFunctionDebugInfo*>(_function);
if (function == NULL)
return B_BAD_VALUE;
target_addr_t functionStartAddress = function->Address() - fRelocationDelta;
target_addr_t functionEndAddress = functionStartAddress + function->Size();
TRACE_LINES2("DwarfImageDebugInfo::GetStatementAtSourceLocation(%p, "
"(%" B_PRId32 ", %" B_PRId32 ")): function range: %#" B_PRIx64 " - %#"
B_PRIx64 "\n", function, sourceLocation.Line(), sourceLocation.Column(),
functionStartAddress, functionEndAddress);
AutoLocker<BLocker> locker(fLock);
// get the source file
LocatableFile* file = function->SourceFile();
if (file == NULL)
return B_ENTRY_NOT_FOUND;
CompilationUnit* unit = function->GetCompilationUnit();
// get the index of the source file in the compilation unit for cheaper
// comparison below
int32 fileIndex = _GetSourceFileIndex(unit, file);
// Get the statement by executing the line number program for the
// compilation unit.
LineNumberProgram& program = unit->GetLineNumberProgram();
if (!program.IsValid())
return B_BAD_DATA;
LineNumberProgram::State state;
program.GetInitialState(state);
target_addr_t statementAddress = 0;
int32 statementLine = -1;
int32 statementColumn = -1;
while (program.GetNextRow(state)) {
bool isOurFile = state.file == fileIndex;
if (statementAddress != 0
&& (!isOurFile || state.isStatement || state.isSequenceEnd)) {
target_addr_t endAddress = state.address;
if (statementAddress < endAddress) {
TRACE_LINES2(" statement: %#" B_PRIx64 " - %#" B_PRIx64
", location: (%" B_PRId32 ", %" B_PRId32 ")\n",
statementAddress, endAddress, statementLine,
statementColumn);
}
if (statementAddress < endAddress
&& statementAddress >= functionStartAddress
&& statementAddress < functionEndAddress
&& statementLine == (int32)sourceLocation.Line()
&& statementColumn == (int32)sourceLocation.Column()) {
TRACE_LINES2(" -> found statement!\n");
ContiguousStatement* statement = new(std::nothrow)
ContiguousStatement(
SourceLocation(statementLine, statementColumn),
TargetAddressRange(fRelocationDelta + statementAddress,
endAddress - statementAddress));
if (statement == NULL)
return B_NO_MEMORY;
_statement = statement;
return B_OK;
}
statementAddress = 0;
}
// skip statements of other files
if (!isOurFile)
continue;
if (state.isStatement) {
statementAddress = state.address;
statementLine = state.line - 1;
statementColumn = std::max(state.column - 1, (int32)0);
}
}
return B_ENTRY_NOT_FOUND;
}
status_t
DwarfImageDebugInfo::GetSourceLanguage(FunctionDebugInfo* _function,
SourceLanguage*& _language)
{
DwarfFunctionDebugInfo* function
= dynamic_cast<DwarfFunctionDebugInfo*>(_function);
if (function == NULL)
return B_BAD_VALUE;
SourceLanguage* language;
CompilationUnit* unit = function->GetCompilationUnit();
switch (unit->UnitEntry()->Language()) {
case DW_LANG_C89:
case DW_LANG_C:
case DW_LANG_C99:
language = new(std::nothrow) CLanguage;
break;
case DW_LANG_C_plus_plus:
language = new(std::nothrow) CppLanguage;
break;
case 0:
default:
language = new(std::nothrow) UnsupportedLanguage;
break;
}
if (language == NULL)
return B_NO_MEMORY;
_language = language;
return B_OK;
}
ssize_t
DwarfImageDebugInfo::ReadCode(target_addr_t address, void* buffer, size_t size)
{
target_addr_t offset = address - fRelocationDelta
- fTextSegment->LoadAddress() + fTextSegment->FileOffset();
ssize_t bytesRead = pread(fFile->GetElfFile()->FD(), buffer, size, offset);
return bytesRead >= 0 ? bytesRead : errno;
}
status_t
DwarfImageDebugInfo::AddSourceCodeInfo(LocatableFile* file,
FileSourceCode* sourceCode)
{
bool addedAny = false;
for (int32 i = 0; CompilationUnit* unit = fFile->CompilationUnitAt(i);
i++) {
int32 fileIndex = _GetSourceFileIndex(unit, file);
if (fileIndex < 0)
continue;
status_t error = _AddSourceCodeInfo(unit, sourceCode, fileIndex);
if (error == B_NO_MEMORY)
return error;
addedAny |= error == B_OK;
}
return addedAny ? B_OK : B_ENTRY_NOT_FOUND;
}
status_t
DwarfImageDebugInfo::_AddSourceCodeInfo(CompilationUnit* unit,
FileSourceCode* sourceCode, int32 fileIndex)
{
// Get the statements by executing the line number program for the
// compilation unit and filtering the rows for our source file.
LineNumberProgram& program = unit->GetLineNumberProgram();
if (!program.IsValid())
return B_BAD_DATA;
LineNumberProgram::State state;
program.GetInitialState(state);
target_addr_t statementAddress = 0;
int32 statementLine = -1;
int32 statementColumn = -1;
while (program.GetNextRow(state)) {
TRACE_LINES2(" %#" B_PRIx64 " (%" B_PRId32 ", %" B_PRId32 ", %"
B_PRId32 ") %d\n", state.address, state.file, state.line,
state.column, state.isStatement);
bool isOurFile = state.file == fileIndex;
if (statementAddress != 0
&& (!isOurFile || state.isStatement || state.isSequenceEnd)) {
target_addr_t endAddress = state.address;
if (endAddress > statementAddress) {
// add the statement
status_t error = sourceCode->AddSourceLocation(
SourceLocation(statementLine, statementColumn));
if (error != B_OK)
return error;
TRACE_LINES2(" -> statement: %#" B_PRIx64 " - %#" B_PRIx64
", source location: (%" B_PRId32 ", %" B_PRId32 ")\n",
statementAddress, endAddress, statementLine,
statementColumn);
}
statementAddress = 0;
}
// skip statements of other files
if (!isOurFile)
continue;
if (state.isStatement) {
statementAddress = state.address;
statementLine = state.line - 1;
statementColumn = std::max(state.column - 1, (int32)0);
}
}
return B_OK;
}
int32
DwarfImageDebugInfo::_GetSourceFileIndex(CompilationUnit* unit,
LocatableFile* sourceFile) const
{
// get the index of the source file in the compilation unit for cheaper
// comparison below
const char* directory;
for (int32 i = 0; const char* fileName = unit->FileAt(i, &directory); i++) {
LocatableFile* file = fFileManager->GetSourceFile(directory, fileName);
if (file != NULL) {
file->ReleaseReference();
if (file == sourceFile) {
return i + 1;
// indices are one-based
}
}
}
return -1;
}
status_t
DwarfImageDebugInfo::_CreateLocalVariables(CompilationUnit* unit,