Debug.cpp

Zbigniew Rebacz, 05/16/2014 04:17 PM

Download (7.4 KB)

 
1
#include "ide.h"
2

    
3
void Ide::RunArgs() {
4
        WithRunLayout<TopWindow> dlg;
5
        CtrlLayoutOKCancel(dlg, "Run options");
6

    
7
        SelectDirButton dir_browse("Run in folder");
8
        dir_browse.Attach(dlg.dir);
9
        dlg.dir = rundir.ToWString();
10

    
11
        dlg.arg <<= runarg;
12

    
13
        {
14
                StringStream ss(recent_runarg);
15
                dlg.arg.SerializeList(ss);
16
        }
17

    
18
        SaveFileButton stdout_browse("Save STDOUT as");
19
        stdout_browse.Type("Text files (*.txt)", "*.txt").AllFilesType();
20
        stdout_browse.Attach(dlg.stdout_file);
21

    
22
        {
23
                StringStream ss(recent_stdout_file);
24
                dlg.stdout_file.SerializeList(ss);
25
                dlg.stdout_file <<= stdout_file;
26
        }
27

    
28
        dlg.runmode <<= runmode;
29
        dlg.external = runexternal;
30
        dlg.forceconsole = forceconsole;
31
        dlg.runmode <<= dlg.Breaker(222);
32

    
33
        for(;;) {
34
                int rm = ~dlg.runmode;
35
                dlg.stdout_file.Enable(rm == RUN_FILE || rm == RUN_FILE_CONSOLE);
36
                switch(dlg.Run()) {
37
                case IDOK:
38
                        rundir  = dlg.dir;
39
                        runarg  = ~dlg.arg;
40
                        runmode = ~dlg.runmode;
41
                        runexternal = dlg.external;
42
                        forceconsole = dlg.forceconsole;
43
                        stdout_file = ~dlg.stdout_file;
44
                        dlg.arg.AddHistory();
45
                        {
46
                                StringStream ss;
47
                                dlg.arg.SerializeList(ss);
48
                                recent_runarg = ss;
49
                        }
50
                        {
51
                                StringStream ss;
52
                                dlg.stdout_file.SerializeList(ss);
53
                                recent_stdout_file = ss;
54
                        }
55
                        return;
56

    
57
                case IDCANCEL:
58
                        return;
59
                }
60
        }
61
}
62

    
63
One<Host> Ide::CreateHostRunDir()
64
{
65
        One<Host> h = CreateHost(false);
66
        if(IsNull(rundir))
67
                h->ChDir(GetFileFolder(target));
68
        else
69
                h->ChDir(rundir);
70
        return h;
71
}
72

    
73
void Ide::BuildAndExecute()
74
{
75
        if(Build()) {
76
                int time = msecs();
77
                One<Host> h = CreateHostRunDir();
78
                h->ChDir(Nvl(rundir, GetFileFolder(target)));
79
                String cmdline;
80
                if(!runexternal)
81
                        cmdline << '\"' << h->GetHostPath(target) << "\" ";
82
                cmdline << ToSystemCharset(runarg);
83
                int exitcode;
84
                switch(runmode) {
85
                case RUN_WINDOW:
86
                        HideBottom();
87
                        h->Launch(cmdline, FindIndex(SplitFlags(mainconfigparam, true), "GUI") < 0 || forceconsole);
88
                        break;
89
                case RUN_CONSOLE:
90
                        ShowConsole();
91
                        PutConsole(String().Cat() << "Executing: " << cmdline);
92
                        console.Sync();
93
                        exitcode = h->ExecuteWithInput(cmdline);
94
                        PutConsole("Finished in " + GetPrintTime(time) + ", exit code: " + AsString(exitcode));
95
                        break;
96
                case RUN_FILE: {
97
                                HideBottom();
98
                                String fn;
99
                                if(IsNull(stdout_file))
100
                                        fn = ForceExt(target, ".ol");
101
                                else
102
                                        fn = stdout_file;
103
                                FileOut out(fn);
104
                                if(!out) {
105
                                        PromptOK("Unable to open output file [* " + DeQtf(stdout_file) + "] !");
106
                                        return;
107
                                }
108
                                if(h->Execute(cmdline, out) >= 0) {
109
                                        out.Close();
110
                                        EditFile(fn);
111
                                }
112
                        }
113
                }
114
        }
115
}
116

    
117
void Ide::BuildAndDebug0(const String& srcfile)
118
{
119
        if(Build()) {
120
                One<Host> h = CreateHostRunDir();
121
                h->ChDir(GetFileFolder(target));
122
                VectorMap<String, String> bm = GetMethodVars(method);
123
                String dbg = bm.Get("DEBUGGER", Null);
124
                if(IsNull(dbg)) {
125
                        if(bm.Get("BUILDER", Null) == "MSC71") {
126
                                String sln = ForceExt(target, ".sln");
127
                                if(GetFileLength(sln) > 0)
128
                                        h->Launch("devenv \"" + h->GetHostPath(sln) + "\" "
129
                                        // + "\"" + h->GetHostPath(srcfile) + "\"" //TRC, 2011/09/26: wrong devenv argument
130
                                        );
131
                                else
132
                                        h->Launch("devenv \"" + h->GetHostPath(target)
133
                                        //+ "\" \"" + h->GetHostPath(srcfile) //TRC, 2011/09/26: wrong devenv argument
134
                                        + "\" /debugexe "
135
                                        );
136
                                return;
137
                        }
138
                        dbg = "gdb";
139
                }
140
                else
141
                        h->Launch('\"' + dbg + "\" \""
142
//                                  + h->GetHostPath(srcfile) + ' '
143
                                  + h->GetHostPath(target) + "\"", true);
144
        }
145
}
146

    
147
void Ide::BuildAndExtDebug()
148
{
149
        BuildAndDebug0(Null);
150
}
151

    
152
void Ide::BuildAndExtDebugFile()
153
{
154
        BuildAndDebug0(editfile);
155
}
156

    
157
One<Debugger> GdbCreate(One<Host> rval_ host, const String& exefile, const String& cmdline, bool console);
158
One<Debugger> Gdb_MI2Create(One<Host> rval_ host, const String& exefile, const String& cmdline, bool console);
159
#ifdef PLATFORM_WIN32
160
One<Debugger> CdbCreate(One<Host> rval_ host, const String& exefile, const String& cmdline);
161
One<Debugger> PdbCreate(One<Host> rval_ host, const String& exefile, const String& cmdline);
162
#endif
163

    
164
void Ide::BuildAndDebug(bool runto)
165
{
166
        VectorMap<String, String> bm = GetMethodVars(method);
167
        String builder = bm.Get("BUILDER", "");
168
        if(!Build())
169
                return;
170
        if(!FileExists(target))
171
                return;
172
        if(designer)
173
                EditAsText();
174
        One<Host> host = CreateHostRunDir();
175
        host->ChDir(Nvl(rundir, GetFileFolder(target)));
176
        HideBottom();
177
        editor.Disable();
178

    
179
        bool console = FindIndex(SplitFlags(mainconfigparam, true), "GUI") < 0 || forceconsole;
180
        if (console && !debug_console_apps_in_console)
181
                console = false;
182
        
183
#ifdef COMPILER_MSC
184
        if(builder == "GCC")
185
                if(gdbSelector)
186
                        debugger = Gdb_MI2Create(host, target, runarg, console);
187
                else
188
                        debugger = GdbCreate(host, target, runarg, console);
189
        else
190
                debugger = PdbCreate(host, target, runarg);
191
#else
192
        if(gdbSelector)
193
                debugger = Gdb_MI2Create(pick(host), target, runarg, console);
194
        else
195
                debugger = GdbCreate(pick(host), target, runarg, console);
196
#endif
197
        if(!debugger) {
198
                IdeEndDebug();
199
                SetBar();
200
                editor.Enable();
201
                return;
202
        }
203
        debuglock = 0;
204
        const Workspace& wspc = IdeWorkspace();
205
        for(int i = 0; i < wspc.GetCount(); i++) {
206
                const Package& pk = wspc.GetPackage(i);
207
                String n = wspc[i];
208
                for(int i = 0; i < pk.file.GetCount(); i++) {
209
                        String file = SourcePath(n, pk.file[i]);
210
                        LineInfo& ln = Filedata(file).lineinfo;
211
                        for(int i = 0; i < ln.GetCount(); i++) {
212
                                LineInfoRecord& lr = ln[i];
213
                                if(!lr.breakpoint.IsEmpty())
214
                                        if(!debugger->SetBreakpoint(file, lr.lineno, lr.breakpoint)) {
215
                                                lr.breakpoint = "\xe";
216
                                                if(PathIsEqual(file, editfile))
217
                                                        editor.SetBreakpoint(lr.lineno, "\xe");
218
                                        }
219
                        }
220
                }
221
        }
222
        SetBar();
223
        editor.Enable();
224
        if(runto) {
225
                if(!debugger->RunTo())
226
                        IdeEndDebug();
227
        }
228
        else
229
                debugger->Run();
230
}
231

    
232
void Ide::DebugClearBreakpoints()
233
{
234
        const Workspace& wspc = IdeWorkspace();
235
        for(int i = 0; i < wspc.GetCount(); i++) {
236
                const Package& pk = wspc.GetPackage(i);
237
                String n = wspc[i];
238
                for(int i = 0; i < pk.file.GetCount(); i++) {
239
                        String file = SourcePath(n, pk.file[i]);
240
                        LineInfo& ln = Filedata(file).lineinfo;
241
                        if(debugger)
242
                                for(int i = 0; i < ln.GetCount(); i++) {
243
                                        const LineInfoRecord& lr = ln[i];
244
                                        if(!lr.breakpoint.IsEmpty())
245
                                                debugger->SetBreakpoint(file, lr.lineno, "");
246
                                }
247
                        ClearBreakpoints(ln);
248
                }
249
        }
250
        editor.ClearBreakpoints();
251
}
252

    
253
void Ide::OnBreakpoint(int i)
254
{
255
        if(!editfile.IsEmpty() && !designer && debugger) {
256
                String q = editor.GetBreakpoint(i);
257
                if(q[0] != 0xe && !debugger->SetBreakpoint(editfile, i, q))
258
                        if(!q.IsEmpty())
259
                                editor.SetBreakpoint(i, "\xe");
260
        }
261
}
262

    
263
void Ide::DebugToggleBreak()
264
{
265
        if(editfile.IsEmpty() || designer)
266
                return;
267
        int ln = editor.GetCursorLine();
268
        String brk = editor.GetBreakpoint(ln);
269
        if(!brk.IsEmpty())
270
                editor.SetBreakpoint(ln, Null);
271
        else
272
                editor.SetBreakpoint(ln, "1");
273
        editor.RefreshFrame();
274
}
275

    
276
void Ide::ConditionalBreak()
277
{
278
        if(editfile.IsEmpty() || designer)
279
                return;
280
        int ln = editor.GetCursorLine();
281
        String brk = editor.GetBreakpoint(ln);
282
        if(EditText(brk, "Conditional breakpoint", "Condition"))
283
                editor.SetBreakpoint(ln, brk);
284
        editor.RefreshFrame();
285
}
286

    
287
void Ide::StopDebug()
288
{
289
        if(debugger)
290
                debugger->Stop();
291
        console.Kill();
292
        PosSync();
293
}
294

    
295
String Ide::GetLogPath()
296
{
297
        if(target.GetCount() == 0)
298
                return Null;
299
#ifdef PLATFORM_WIN32
300
        return ForceExt(target, ".log");
301
#else
302
        String p = GetFileTitle(target);
303
        return GetHomeDirFile(".upp/" + p + "/" + p + ".log");
304
#endif
305
}
306

    
307
void Ide::OpenLog()
308
{
309
        String p = GetLogPath();
310
        if(FileExists(p))
311
                EditFile(p);
312
}
313

    
314
bool Ide::EditorTip(CodeEditor::MouseTip& mt)
315
{
316
        return debugger && debugger->Tip(editor.ReadIdBack(mt.pos), mt);
317
}