xref: /haiku/src/kits/debugger/debug_info/DwarfTeamDebugInfo.cpp (revision 2cad94c1c30b6223ad8c08710b26e071d32e9979)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2014, Rene Gollent, rene@gollent.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include "DwarfTeamDebugInfo.h"
8 
9 #include <new>
10 
11 #include <string.h>
12 
13 #include "arch/Architecture.h"
14 #include "DebuggerInterface.h"
15 #include "DwarfFile.h"
16 #include "DwarfImageDebugInfo.h"
17 #include "DwarfImageDebugInfoLoadingState.h"
18 #include "DwarfManager.h"
19 #include "GlobalTypeLookup.h"
20 #include "ImageDebugInfoLoadingState.h"
21 #include "LocatableFile.h"
22 
23 
24 DwarfTeamDebugInfo::DwarfTeamDebugInfo(Architecture* architecture,
25 	DebuggerInterface* interface, FileManager* fileManager,
26 	GlobalTypeLookup* typeLookup, GlobalTypeCache* typeCache)
27 	:
28 	fArchitecture(architecture),
29 	fDebuggerInterface(interface),
30 	fFileManager(fileManager),
31 	fManager(NULL),
32 	fTypeLookup(typeLookup),
33 	fTypeCache(typeCache)
34 {
35 	fDebuggerInterface->AcquireReference();
36 	fTypeCache->AcquireReference();
37 }
38 
39 
40 DwarfTeamDebugInfo::~DwarfTeamDebugInfo()
41 {
42 	fDebuggerInterface->ReleaseReference();
43 	fTypeCache->ReleaseReference();
44 	delete fManager;
45 }
46 
47 
48 status_t
49 DwarfTeamDebugInfo::Init()
50 {
51 	fManager = new(std::nothrow) DwarfManager(fArchitecture->AddressSize());
52 	if (fManager == NULL)
53 		return B_NO_MEMORY;
54 
55 	status_t error = fManager->Init();
56 	if (error != B_OK)
57 		return error;
58 
59 	return B_OK;
60 }
61 
62 
63 status_t
64 DwarfTeamDebugInfo::CreateImageDebugInfo(const ImageInfo& imageInfo,
65 	LocatableFile* imageFile, ImageDebugInfoLoadingState& _state,
66 	SpecificImageDebugInfo*& _imageDebugInfo)
67 {
68 	// We only like images whose file we can play with.
69 	BString filePath;
70 	if (imageFile == NULL || !imageFile->GetLocatedPath(filePath))
71 		return B_ENTRY_NOT_FOUND;
72 
73 	// try to load the DWARF file
74 	DwarfImageDebugInfoLoadingState* dwarfState;
75 	if (_state.HasSpecificDebugInfoLoadingState()) {
76 		dwarfState = dynamic_cast<DwarfImageDebugInfoLoadingState*>(
77 			_state.GetSpecificDebugInfoLoadingState());
78 		if (dwarfState == NULL)
79 			return B_BAD_VALUE;
80 	} else {
81 	 	dwarfState = new(std::nothrow) DwarfImageDebugInfoLoadingState();
82 		if (dwarfState == NULL)
83 			return B_NO_MEMORY;
84 		_state.SetSpecificDebugInfoLoadingState(dwarfState);
85 	}
86 
87 	status_t error = fManager->LoadFile(filePath, dwarfState->GetFileState());
88 	if (error != B_OK)
89 		return error;
90 
91 	error = fManager->FinishLoading();
92 	if (error != B_OK)
93 		return error;
94 
95 	// create the image debug info
96 	DwarfImageDebugInfo* debugInfo = new(std::nothrow) DwarfImageDebugInfo(
97 		imageInfo, fDebuggerInterface, fArchitecture, fFileManager,
98 		fTypeLookup, fTypeCache, dwarfState->GetFileState().dwarfFile);
99 	if (debugInfo == NULL)
100 		return B_NO_MEMORY;
101 
102 	error = debugInfo->Init();
103 	if (error != B_OK) {
104 		delete debugInfo;
105 		return error;
106 	}
107 
108 	_imageDebugInfo = debugInfo;
109 	return B_OK;
110 }
111