Debugger: Add dedicated functions for global {un}init.
[haiku.git] / src / apps / debugger / jobs / LoadSourceCodeJob.cpp
blob6265a03ff3a7d1f849cd88bfa1c349d8e60a7077
1 /*
2 * Copyright 2012, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
5 */
7 #include "Jobs.h"
9 #include <AutoLocker.h>
11 #include "Architecture.h"
12 #include "DebuggerInterface.h"
13 #include "DisassembledCode.h"
14 #include "Function.h"
15 #include "FunctionInstance.h"
16 #include "FileSourceCode.h"
17 #include "Team.h"
18 #include "TeamDebugInfo.h"
21 LoadSourceCodeJob::LoadSourceCodeJob(
22 DebuggerInterface* debuggerInterface, Architecture* architecture,
23 Team* team, FunctionInstance* functionInstance, bool loadForFunction)
25 fKey(functionInstance, JOB_TYPE_LOAD_SOURCE_CODE),
26 fDebuggerInterface(debuggerInterface),
27 fArchitecture(architecture),
28 fTeam(team),
29 fFunctionInstance(functionInstance),
30 fLoadForFunction(loadForFunction)
32 fFunctionInstance->AcquireReference();
34 SetDescription("Loading source code for function %s",
35 fFunctionInstance->PrettyName().String());
39 LoadSourceCodeJob::~LoadSourceCodeJob()
41 fFunctionInstance->ReleaseReference();
45 const JobKey&
46 LoadSourceCodeJob::Key() const
48 return fKey;
52 status_t
53 LoadSourceCodeJob::Do()
55 // if requested, try loading the source code for the function
56 Function* function = fFunctionInstance->GetFunction();
57 if (fLoadForFunction) {
58 FileSourceCode* sourceCode;
59 status_t error = fTeam->DebugInfo()->LoadSourceCode(
60 function->SourceFile(), sourceCode);
62 AutoLocker<Team> locker(fTeam);
64 if (error == B_OK) {
65 function->SetSourceCode(sourceCode, FUNCTION_SOURCE_LOADED);
66 sourceCode->ReleaseReference();
67 return B_OK;
70 function->SetSourceCode(NULL, FUNCTION_SOURCE_UNAVAILABLE);
73 // Only try to load the function instance code, if it's not overridden yet.
74 AutoLocker<Team> locker(fTeam);
75 if (fFunctionInstance->SourceCodeState() != FUNCTION_SOURCE_LOADING)
76 return B_OK;
77 locker.Unlock();
79 // disassemble the function
80 DisassembledCode* sourceCode = NULL;
81 status_t error = fTeam->DebugInfo()->DisassembleFunction(fFunctionInstance,
82 sourceCode);
84 // set the result
85 locker.Lock();
86 if (error == B_OK) {
87 if (fFunctionInstance->SourceCodeState() == FUNCTION_SOURCE_LOADING) {
88 // various parts of the debugger expect functions to have only
89 // one of source or disassembly available. As such, if the current
90 // function had source code previously active, unset it when
91 // explicitly asked for disassembly. This needs to be done first
92 // since Function will clear the disassembled code states of all
93 // its child instances.
94 if (function->SourceCodeState() == FUNCTION_SOURCE_LOADED) {
95 FileSourceCode* sourceCode = function->GetSourceCode();
96 function->SetSourceCode(sourceCode,
97 FUNCTION_SOURCE_NOT_LOADED);
100 fFunctionInstance->SetSourceCode(sourceCode,
101 FUNCTION_SOURCE_LOADED);
102 sourceCode->ReleaseReference();
104 } else
105 fFunctionInstance->SetSourceCode(NULL, FUNCTION_SOURCE_UNAVAILABLE);
107 return error;