File: debuggermanager.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (254 lines) | stat: -rw-r--r-- 8,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : debuggermanager.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "debuggermanager.h"

#include "cl_command_event.h"
#include "cl_defs.h"
#include "cl_standard_paths.h"
#include "codelite_events.h"
#include "codelite_exports.h"
#include "debuggerconfigtool.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "wx/filename.h"

#include <wx/dir.h>
#include <wx/log.h>
#include <wx/msgdlg.h>

//---------------------------------------------------------
static DebuggerMgr* ms_instance = NULL;

wxDEFINE_EVENT(wxEVT_DEBUGGER_UPDATE_VIEWS, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_QUERY_LOCALS, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_LIST_CHILDREN, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_VAROBJ_EVALUATED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_VAROBJECT_CREATED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_DISASSEBLE_OUTPUT, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_DISASSEBLE_CURLINE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_QUERY_FILELINE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_TYPE_RESOLVE_ERROR, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_LIST_REGISTERS, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_LIST_FRAMES, clCommandEvent);
wxDEFINE_EVENT(wxEVT_DEBUGGER_FRAME_SELECTED, clCommandEvent);

DebuggerMgr::DebuggerMgr() {}

DebuggerMgr::~DebuggerMgr()
{
    std::vector<clDynamicLibrary*>::iterator iter = m_dl.begin();
    for(; iter != m_dl.end(); iter++) {
        (*iter)->Detach();
        delete(*iter);
    }

    m_dl.clear();
    m_debuggers.clear();
}

DebuggerMgr& DebuggerMgr::Get()
{
    if(!ms_instance) {
        ms_instance = new DebuggerMgr();
    }
    return *ms_instance;
}

void DebuggerMgr::Free()
{
    delete ms_instance;
    ms_instance = NULL;
}

bool DebuggerMgr::LoadDebuggers(IDebuggerObserver* observer)
{
    wxString ext;

#if defined(__WXMSW__) || defined(__CYGWIN__)
    ext = wxT("dll");

#elif defined(__WXMAC__)
    ext = wxT("dylib");

#else
    ext = wxT("so");

#endif

    wxString fileSpec(wxT("*.") + ext);

    // get list of dlls
    wxArrayString files;
#ifdef __WXGTK__
    wxString debuggersPath(PLUGINS_DIR, wxConvUTF8);
    debuggersPath += wxT("/debuggers");
#elif defined(__WXMSW__)
#ifdef USE_POSIX_LAYOUT
    wxString debuggersPath(clStandardPaths::Get().GetPluginsDirectory() + wxT("/debuggers"));
#else
    wxString debuggersPath(m_baseDir + wxT("/debuggers"));
#endif
#else
    // OSX
    wxFileName debuggersFolder(clStandardPaths::Get().GetDataDir(), "");
    debuggersFolder.AppendDir("debuggers");
    wxString debuggersPath(debuggersFolder.GetPath());
#endif

    clDEBUG() << "Loading debuggers from:" << debuggersPath;
    wxDir::GetAllFiles(debuggersPath, &files, fileSpec, wxDIR_FILES);

    for(size_t i = 0; i < files.GetCount(); i++) {
        clDynamicLibrary* dl = new clDynamicLibrary();
        wxString fileName(files.Item(i));

#if defined(__WXMSW__) && CL_DEBUG_BUILD
        // Under MSW loading a release plugin while in debug mode will cause a crash
        if(!fileName.EndsWith("-dbg.dll")) {
            continue;
        }
#elif defined(__WXMSW__)
        // filter debug plugins
        if(fileName.EndsWith("-dbg.dll")) {
            continue;
        }
#endif
        clDEBUG() << "Attempting to load debugger:" << fileName << endl;
        if(!dl->Load(fileName)) {
            clWARNING() << "Failed to load debugger:" << fileName << endl;
            if(!dl->GetError().IsEmpty()) {
                clWARNING() << dl->GetError() << endl;
            }
            wxDELETE(dl);
            continue;
        }

        bool success(false);
        GET_DBG_INFO_FUNC pfn = (GET_DBG_INFO_FUNC)dl->GetSymbol(wxT("GetDebuggerInfo"), &success);
        if(!success) {
            clLogMessage(wxT("Failed to find GetDebuggerInfo() in dll: ") + fileName);
            if(!dl->GetError().IsEmpty()) {
                clLogMessage(dl->GetError());
            }
            // dl->Unload();
            delete dl;
            continue;
        }

        DebuggerInfo info = pfn();
        // Call the init method to create an instance of the debugger
        success = false;
        GET_DBG_CREATE_FUNC pfnInitDbg = (GET_DBG_CREATE_FUNC)dl->GetSymbol(info.initFuncName, &success);
        if(!success) {
            clLogMessage(wxT("Failed to find init function in dll: ") + fileName);
            if(!dl->GetError().IsEmpty()) {
                clLogMessage(dl->GetError());
            }
            dl->Detach();
            delete dl;
            continue;
        }

        clLogMessage(wxT("Loaded debugger: ") + info.name + wxT(", Version: ") + info.version);
        IDebugger* dbg = pfnInitDbg();

        // set the environment
        dbg->SetEnvironment(m_env);
        dbg->SetObserver(observer);
        m_debuggers[info.name] = dbg;

        // keep the dynamic load library
        m_dl.push_back(dl);
    }
    return true;
}

wxArrayString DebuggerMgr::GetAvailableDebuggers()
{
    wxArrayString dbgs;
    dbgs.reserve(m_pluginsDebuggers.size() + m_debuggers.size());

    auto iter = m_debuggers.begin();
    for(; iter != m_debuggers.end(); iter++) {
        dbgs.Add(iter->first);
    }

    // append all the plugins that were registered themself as debugger
    for(const auto& vt : m_pluginsDebuggers) {
        dbgs.insert(dbgs.end(), vt.second.begin(), vt.second.end());
    }
    return dbgs;
}

IDebugger* DebuggerMgr::GetActiveDebugger()
{
    if(m_activeDebuggerName.IsEmpty()) {
        // no active debugger is set, use the first one
        auto iter = m_debuggers.begin();
        if(iter != m_debuggers.end()) {
            SetActiveDebugger(iter->first);
            return iter->second;
        }
        return NULL;
    }

    auto iter = m_debuggers.find(m_activeDebuggerName);
    if(iter != m_debuggers.end()) {
        return iter->second;
    }
    return NULL;
}

void DebuggerMgr::SetActiveDebugger(const wxString& name) { m_activeDebuggerName = name; }

void DebuggerMgr::SetDebuggerInformation(const wxString& name, const DebuggerInformation& info)
{
    DebuggerConfigTool::Get()->WriteObject(name, (SerializedObject*)&info);
}

bool DebuggerMgr::GetDebuggerInformation(const wxString& name, DebuggerInformation& info)
{
    return DebuggerConfigTool::Get()->ReadObject(name, &info);
}

bool DebuggerMgr::IsNativeDebuggerRunning() const
{
    auto iter = m_debuggers.find(m_activeDebuggerName);
    if(iter == m_debuggers.end()) {
        return false;
    }

    IDebugger* d = iter->second;
    return d && d->IsRunning();
}

void DebuggerMgr::RegisterDebuggers(const wxString& plugin_name, const wxArrayString& names)
{
    m_pluginsDebuggers.erase(plugin_name);
    m_pluginsDebuggers.insert({ plugin_name, names });
}

void DebuggerMgr::UnregisterDebuggers(const wxString& plugin_name) { m_pluginsDebuggers.erase(plugin_name); }