xref: /haiku/src/kits/debugger/dwarf/BaseUnit.cpp (revision 3c16ba4e780846dc1973050c97b54d39c2eb854f)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2013, Rene Gollent, rene@gollent.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "BaseUnit.h"
9 
10 #include <new>
11 
12 #include "DebugInfoEntries.h"
13 
14 
BaseUnit(off_t headerOffset,off_t contentOffset,off_t totalSize,off_t abbreviationOffset,uint8 addressSize,bool isBigEndian,bool isDwarf64)15 BaseUnit::BaseUnit(off_t headerOffset, off_t contentOffset,
16 	off_t totalSize, off_t abbreviationOffset, uint8 addressSize,
17 	bool isBigEndian, bool isDwarf64)
18 	:
19 	fHeaderOffset(headerOffset),
20 	fContentOffset(contentOffset),
21 	fTotalSize(totalSize),
22 	fAbbreviationOffset(abbreviationOffset),
23 	fAbbreviationTable(NULL),
24 	fAddressSize(addressSize),
25 	fIsBigEndian(isBigEndian),
26 	fIsDwarf64(isDwarf64)
27 {
28 }
29 
30 
~BaseUnit()31 BaseUnit::~BaseUnit()
32 {
33 	for (int32 i = 0; i < fEntries.Count(); i++)
34 		delete fEntries[i];
35 }
36 
37 
38 void
SetAbbreviationTable(AbbreviationTable * abbreviationTable)39 BaseUnit::SetAbbreviationTable(AbbreviationTable* abbreviationTable)
40 {
41 	fAbbreviationTable = abbreviationTable;
42 }
43 
44 
45 status_t
AddDebugInfoEntry(DebugInfoEntry * entry,off_t offset)46 BaseUnit::AddDebugInfoEntry(DebugInfoEntry* entry, off_t offset)
47 {
48 	if (!fEntries.Add(entry))
49 		return B_NO_MEMORY;
50 	if (!fEntryOffsets.Add(offset)) {
51 		fEntries.Remove(fEntries.Count() - 1);
52 		return B_NO_MEMORY;
53 	}
54 
55 	return B_OK;
56 }
57 
58 
59 bool
ContainsAbsoluteOffset(off_t offset) const60 BaseUnit::ContainsAbsoluteOffset(off_t offset) const
61 {
62 	return fHeaderOffset <= offset && fHeaderOffset + fTotalSize > offset;
63 }
64 
65 
66 void
SetSourceLanguage(const SourceLanguageInfo * language)67 BaseUnit::SetSourceLanguage(const SourceLanguageInfo* language)
68 {
69 	fSourceLanguage = language;
70 }
71 
72 
73 int
CountEntries() const74 BaseUnit::CountEntries() const
75 {
76 	return fEntries.Count();
77 }
78 
79 
80 void
GetEntryAt(int index,DebugInfoEntry * & entry,off_t & offset) const81 BaseUnit::GetEntryAt(int index, DebugInfoEntry*& entry,
82 	off_t& offset) const
83 {
84 	entry = fEntries[index];
85 	offset = fEntryOffsets[index];
86 }
87 
88 
89 DebugInfoEntry*
EntryForOffset(off_t offset) const90 BaseUnit::EntryForOffset(off_t offset) const
91 {
92 	if (fEntries.IsEmpty())
93 		return NULL;
94 
95 	// binary search
96 	int lower = 0;
97 	int upper = fEntries.Count() - 1;
98 	while (lower < upper) {
99 		int mid = (lower + upper + 1) / 2;
100 		if (fEntryOffsets[mid] > offset)
101 			upper = mid - 1;
102 		else
103 			lower = mid;
104 	}
105 
106 	return fEntryOffsets[lower] == offset ? fEntries[lower] : NULL;
107 }
108