1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2014-2016, 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
24DwarfTeamDebugInfo::DwarfTeamDebugInfo(Architecture* architecture,
25	DebuggerInterface* interface, FileManager* fileManager,
26	GlobalTypeLookup* typeLookup, TeamFunctionSourceInformation* sourceInfo,
27	GlobalTypeCache* typeCache)
28	:
29	fArchitecture(architecture),
30	fDebuggerInterface(interface),
31	fFileManager(fileManager),
32	fManager(NULL),
33	fTypeLookup(typeLookup),
34	fSourceInfo(sourceInfo),
35	fTypeCache(typeCache)
36{
37	fDebuggerInterface->AcquireReference();
38	fTypeCache->AcquireReference();
39}
40
41
42DwarfTeamDebugInfo::~DwarfTeamDebugInfo()
43{
44	fDebuggerInterface->ReleaseReference();
45	fTypeCache->ReleaseReference();
46	delete fManager;
47}
48
49
50status_t
51DwarfTeamDebugInfo::Init()
52{
53	fManager = new(std::nothrow) DwarfManager(fArchitecture->AddressSize(), fArchitecture->IsBigEndian());
54	if (fManager == NULL)
55		return B_NO_MEMORY;
56
57	status_t error = fManager->Init();
58	if (error != B_OK)
59		return error;
60
61	return B_OK;
62}
63
64
65status_t
66DwarfTeamDebugInfo::CreateImageDebugInfo(const ImageInfo& imageInfo,
67	LocatableFile* imageFile, ImageDebugInfoLoadingState& _state,
68	SpecificImageDebugInfo*& _imageDebugInfo)
69{
70	// We only like images whose file we can play with.
71	BString filePath;
72	if (imageFile == NULL || !imageFile->GetLocatedPath(filePath))
73		return B_ENTRY_NOT_FOUND;
74
75	// try to load the DWARF file
76	DwarfImageDebugInfoLoadingState* dwarfState;
77	if (_state.HasSpecificDebugInfoLoadingState()) {
78		dwarfState = dynamic_cast<DwarfImageDebugInfoLoadingState*>(
79			_state.GetSpecificDebugInfoLoadingState());
80		if (dwarfState == NULL)
81			return B_BAD_VALUE;
82	} else {
83	 	dwarfState = new(std::nothrow) DwarfImageDebugInfoLoadingState();
84		if (dwarfState == NULL)
85			return B_NO_MEMORY;
86		_state.SetSpecificDebugInfoLoadingState(dwarfState);
87	}
88
89	status_t error = fManager->LoadFile(filePath, dwarfState->GetFileState());
90	if (error != B_OK)
91		return error;
92
93	error = fManager->FinishLoading();
94	if (error != B_OK)
95		return error;
96
97	// create the image debug info
98	DwarfImageDebugInfo* debugInfo = new(std::nothrow) DwarfImageDebugInfo(
99		imageInfo, fDebuggerInterface, fArchitecture, fFileManager,
100		fTypeLookup, fTypeCache, fSourceInfo, dwarfState->GetFileState().dwarfFile);
101	if (debugInfo == NULL)
102		return B_NO_MEMORY;
103
104	error = debugInfo->Init();
105	if (error != B_OK) {
106		delete debugInfo;
107		return error;
108	}
109
110	_imageDebugInfo = debugInfo;
111	return B_OK;
112}
113