xref: /haiku/src/kits/debugger/debug_info/FunctionInstance.cpp (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*fce4895dSRene Gollent  * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent  */
5*fce4895dSRene Gollent 
6*fce4895dSRene Gollent #include "FunctionInstance.h"
7*fce4895dSRene Gollent 
8*fce4895dSRene Gollent #include <new>
9*fce4895dSRene Gollent 
10*fce4895dSRene Gollent #include "DisassembledCode.h"
11*fce4895dSRene Gollent #include "Function.h"
12*fce4895dSRene Gollent #include "FunctionID.h"
13*fce4895dSRene Gollent #include "ImageDebugInfo.h"
14*fce4895dSRene Gollent #include "LocatableFile.h"
15*fce4895dSRene Gollent 
16*fce4895dSRene Gollent 
17*fce4895dSRene Gollent FunctionInstance::FunctionInstance(ImageDebugInfo* imageDebugInfo,
18*fce4895dSRene Gollent 	FunctionDebugInfo* functionDebugInfo)
19*fce4895dSRene Gollent 	:
20*fce4895dSRene Gollent 	fImageDebugInfo(imageDebugInfo),
21*fce4895dSRene Gollent 	fFunction(NULL),
22*fce4895dSRene Gollent 	fFunctionDebugInfo(functionDebugInfo),
23*fce4895dSRene Gollent 	fSourceCode(NULL),
24*fce4895dSRene Gollent 	fSourceCodeState(FUNCTION_SOURCE_NOT_LOADED)
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent 	fFunctionDebugInfo->AcquireReference();
27*fce4895dSRene Gollent 	// TODO: What about fImageDebugInfo? We must be careful regarding cyclic
28*fce4895dSRene Gollent 	// references.
29*fce4895dSRene Gollent }
30*fce4895dSRene Gollent 
31*fce4895dSRene Gollent 
32*fce4895dSRene Gollent FunctionInstance::~FunctionInstance()
33*fce4895dSRene Gollent {
34*fce4895dSRene Gollent 	SetFunction(NULL);
35*fce4895dSRene Gollent 	fFunctionDebugInfo->ReleaseReference();
36*fce4895dSRene Gollent }
37*fce4895dSRene Gollent 
38*fce4895dSRene Gollent 
39*fce4895dSRene Gollent FunctionID*
40*fce4895dSRene Gollent FunctionInstance::GetFunctionID() const
41*fce4895dSRene Gollent {
42*fce4895dSRene Gollent 	if (LocatableFile* file = SourceFile()) {
43*fce4895dSRene Gollent 		BString path;
44*fce4895dSRene Gollent 		file->GetPath(path);
45*fce4895dSRene Gollent 		return new(std::nothrow) SourceFunctionID(path, Name());
46*fce4895dSRene Gollent 	}
47*fce4895dSRene Gollent 
48*fce4895dSRene Gollent 	return new(std::nothrow) ImageFunctionID(
49*fce4895dSRene Gollent 		GetImageDebugInfo()->GetImageInfo().Name(), Name());
50*fce4895dSRene Gollent }
51*fce4895dSRene Gollent 
52*fce4895dSRene Gollent 
53*fce4895dSRene Gollent void
54*fce4895dSRene Gollent FunctionInstance::SetFunction(Function* function)
55*fce4895dSRene Gollent {
56*fce4895dSRene Gollent 	if (fFunction != NULL)
57*fce4895dSRene Gollent 		fFunction->ReleaseReference();
58*fce4895dSRene Gollent 
59*fce4895dSRene Gollent 	fFunction = function;
60*fce4895dSRene Gollent 
61*fce4895dSRene Gollent 	if (fFunction != NULL)
62*fce4895dSRene Gollent 		fFunction->AcquireReference();
63*fce4895dSRene Gollent }
64*fce4895dSRene Gollent 
65*fce4895dSRene Gollent 
66*fce4895dSRene Gollent void
67*fce4895dSRene Gollent FunctionInstance::SetSourceCode(DisassembledCode* source,
68*fce4895dSRene Gollent 	function_source_state state)
69*fce4895dSRene Gollent {
70*fce4895dSRene Gollent 	if (source == fSourceCode && state == fSourceCodeState)
71*fce4895dSRene Gollent 		return;
72*fce4895dSRene Gollent 
73*fce4895dSRene Gollent 	if (fSourceCode != NULL)
74*fce4895dSRene Gollent 		fSourceCode->ReleaseReference();
75*fce4895dSRene Gollent 
76*fce4895dSRene Gollent 	fSourceCode = source;
77*fce4895dSRene Gollent 	fSourceCodeState = state;
78*fce4895dSRene Gollent 
79*fce4895dSRene Gollent 	if (fSourceCode != NULL)
80*fce4895dSRene Gollent 		fSourceCode->AcquireReference();
81*fce4895dSRene Gollent 
82*fce4895dSRene Gollent 	if (fFunction != NULL)
83*fce4895dSRene Gollent 		fFunction->NotifySourceCodeChanged();
84*fce4895dSRene Gollent }
85