2
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3
* http://www.gnu.org/licenses/gpl-3.0.html
6
* $Id: debuggerstate.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
7
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/debuggergdb/debuggerstate.cpp $
11
#include <cbexception.h>
12
#include "debuggerstate.h"
13
#include <compilerfactory.h>
14
#include "debuggergdb.h"
15
#include "projectbuildtarget.h"
16
#include "cdb_driver.h"
17
#include "gdb_driver.h"
19
#include "projectmanager.h"
22
#include "cbproject.h"
25
DebuggerState::DebuggerState(DebuggerGDB* plugin)
32
DebuggerState::~DebuggerState()
36
bool DebuggerState::StartDriver(ProjectBuildTarget* target)
39
SetupBreakpointIndices();
40
wxString idx = target ? target->GetCompilerID() : CompilerFactory::GetDefaultCompilerID();
41
if (CompilerFactory::CompilerInheritsFrom(idx, _T("msvc*"))) // MSVC
42
m_pDriver = new CDB_driver(m_pPlugin);
44
m_pDriver = new GDB_driver(m_pPlugin);
48
void DebuggerState::StopDriver()
55
bool DebuggerState::HasDriver()
57
return m_pDriver != NULL;
60
DebuggerDriver* DebuggerState::GetDriver()
62
cbAssert(m_pDriver != NULL);
66
void DebuggerState::CleanUp()
69
m_pDriver->RemoveBreakpoint(0);
72
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
74
DebuggerBreakpoint* bp = m_Breakpoints[i];
77
m_Breakpoints.Clear();
80
// Re-number indices. Called before starting the debugging session
81
void DebuggerState::SetupBreakpointIndices()
84
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
86
DebuggerBreakpoint* bp = m_Breakpoints[i];
87
bp->index = ++m_BpAutoIndex;
91
// The compiler now uses absolute paths to source files so we don't need
92
// any absolute->relative filename conversions here anymore.
93
// Just adjust the path separators...
94
wxString DebuggerState::ConvertToValidFilename(const wxString& filename)
96
wxString fname = filename;
97
fname.Replace(_T("\\"), _T("/"));
99
} // end of ConvertToValidFilename
101
cbProject* DebuggerState::FindProjectForFile(const wxString& file)
103
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Searching for project containing: ") + file));
104
ProjectsArray* projects = Manager::Get()->GetProjectManager()->GetProjects();
105
for (size_t i = 0; i < projects->GetCount(); ++i)
107
cbProject* prj = projects->Item(i);
108
if (prj->GetFileByFilename(file, false, false))
110
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Got it: %s (%p)"), prj->GetTitle().c_str(), prj));
114
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Not found...")));
118
int DebuggerState::AddBreakpoint(const wxString& file, int line, bool temp, const wxString& lineText)
120
wxString bpfile = ConvertToValidFilename(file);
122
// do we have a bp there?
123
int idx = HasBreakpoint(bpfile, line);
124
// if yes, remove old breakpoint first
126
RemoveBreakpoint(idx, true);
128
// Manager::Get()->GetLogManager()->DebugLog(_T("add bp: file=%s, bpfile=%s"), file.c_str(), bpfile.c_str());
129
DebuggerBreakpoint* bp = new DebuggerBreakpoint;
130
bp->type = DebuggerBreakpoint::bptCode;
131
bp->filename = bpfile;
132
bp->filenameAsPassed = file;
134
bp->temporary = temp;
135
bp->lineText = lineText;
136
bp->userData = FindProjectForFile(file);
137
return AddBreakpoint(bp);
140
int DebuggerState::AddBreakpoint(const wxString& dataAddr, bool onRead, bool onWrite)
142
DebuggerBreakpoint* bp = new DebuggerBreakpoint;
143
bp->type = DebuggerBreakpoint::bptData;
144
bp->breakAddress = dataAddr;
145
bp->breakOnRead = onRead;
146
bp->breakOnWrite = onWrite;
147
return AddBreakpoint(bp);
150
int DebuggerState::AddBreakpoint(DebuggerBreakpoint* bp)
155
wxString bpfile = ConvertToValidFilename(bp->filename);
156
bp->filename = bpfile;
158
bp->index = ++m_BpAutoIndex;
159
m_Breakpoints.Add(bp);
161
// notify driver if it is active
163
m_pDriver->AddBreakpoint(bp);
167
DebuggerBreakpoint* DebuggerState::RemoveBreakpoint(DebuggerBreakpoint* bp, bool deleteit)
169
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
171
if (m_Breakpoints[i] == bp)
173
return RemoveBreakpoint(i, deleteit);
179
DebuggerBreakpoint* DebuggerState::RemoveBreakpoint(const wxString& file, int line, bool deleteit)
181
wxString bpfile = ConvertToValidFilename(file);
182
return RemoveBreakpoint(HasBreakpoint(bpfile, line), deleteit);
185
DebuggerBreakpoint* DebuggerState::RemoveBreakpoint(int idx, bool deleteit)
187
// do we have a valid index?
188
if (idx < 0 || idx >= (int)m_Breakpoints.GetCount())
190
// yes, remove it from the list
191
DebuggerBreakpoint* bp = m_Breakpoints[idx];
192
m_Breakpoints.RemoveAt(idx);
194
// notify driver if it is active
196
m_pDriver->RemoveBreakpoint(bp);
206
void DebuggerState::RemoveAllBreakpoints(const wxString& file, bool deleteit)
208
wxString bpfile = ConvertToValidFilename(file);
209
bool fileonly = !bpfile.IsEmpty();
210
for (int i = m_Breakpoints.GetCount() - 1; i >= 0; --i)
214
DebuggerBreakpoint* bp = m_Breakpoints[i];
215
if (bp->filename != bpfile && bp->filenameAsPassed != file)
218
RemoveBreakpoint(i, deleteit);
222
int DebuggerState::RemoveBreakpointsRange(const wxString& file, int startline, int endline)
225
wxString bpfile = ConvertToValidFilename(file);
226
for (int i = m_Breakpoints.GetCount() - 1; i >= 0; --i)
228
DebuggerBreakpoint* bp = m_Breakpoints[i];
229
if (bp->line >= startline && bp->line <= endline && (bp->filename == bpfile || bp->filenameAsPassed == file))
232
RemoveBreakpoint(i, true);
238
void DebuggerState::RemoveAllProjectBreakpoints(cbProject* prj)
240
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Removing all breakpoints of project: %p"), prj));
241
for (int i = m_Breakpoints.GetCount() - 1; i >= 0; --i)
243
DebuggerBreakpoint* bp = m_Breakpoints[i];
244
if (bp->userData == prj)
246
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Got one")));
247
RemoveBreakpoint(i, true);
252
void DebuggerState::ShiftBreakpoints(const wxString& file, int startline, int nroflines)
254
wxString bpfile = ConvertToValidFilename(file);
255
for (int i = m_Breakpoints.GetCount() - 1; i >= 0; --i)
257
DebuggerBreakpoint* bp = m_Breakpoints[i];
258
if (bp->line >= startline && (bp->filename == bpfile || bp->filenameAsPassed == file))
260
// notify driver if it is active
262
m_pDriver->RemoveBreakpoint(bp);
263
bp->line += nroflines;
264
// notify driver if it is active
266
m_pDriver->AddBreakpoint(bp);
271
int DebuggerState::HasBreakpoint(const wxString& file, int line)
273
wxString bpfile = ConvertToValidFilename(file);
274
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
276
DebuggerBreakpoint* bp = m_Breakpoints[i];
277
if ((bp->filename == bpfile || bp->filenameAsPassed == file) && bp->line == line)
283
int DebuggerState::HasBreakpoint(const wxString& dataAddr)
285
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
287
DebuggerBreakpoint* bp = m_Breakpoints[i];
288
if (bp->breakAddress == dataAddr)
294
DebuggerBreakpoint* DebuggerState::GetBreakpoint(int idx)
296
if (idx < 0 || idx >= (int)m_Breakpoints.GetCount())
298
return m_Breakpoints[idx];
301
DebuggerBreakpoint* DebuggerState::GetBreakpointByNumber(int num)
303
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
305
DebuggerBreakpoint* bp = m_Breakpoints[i];
306
if (bp->index == num)
312
void DebuggerState::ResetBreakpoint(int idx)
314
DebuggerBreakpoint* bp = RemoveBreakpoint(idx, false);
318
void DebuggerState::ResetBreakpoint(DebuggerBreakpoint* bp)
320
for (unsigned int i = 0; i < m_Breakpoints.GetCount(); ++i)
322
if (m_Breakpoints[i] == bp)
331
void DebuggerState::ApplyBreakpoints()
336
// remove any previously set temporary breakpoints
337
int i = (int)m_Breakpoints.GetCount() - 1;
340
DebuggerBreakpoint* bp = m_Breakpoints[i];
341
if (bp->temporary && bp->alreadySet)
342
m_Breakpoints.RemoveAt(i);
346
m_pPlugin->Log(_("Setting breakpoints"));
347
m_pDriver->RemoveBreakpoint(0); // clear all breakpoints
349
i = (int)m_Breakpoints.GetCount() - 1;
352
DebuggerBreakpoint* bp = m_Breakpoints[i];
353
m_pDriver->AddBreakpoint(bp);