2 public import static "ecere"
3 public import static "ec"
15 #define GDB_DEBUG_CONSOLE
19 extern char * strrchr(const char * s, int c);
22 #define strlen _strlen
26 #include <string.h> // For memchr
34 #include <sys/time.h> // Required on Apple...
48 sprintf(s[0], "%04d", now.year);
49 sprintf(s[1], "%02d", now.month+1);
50 sprintf(s[2], "%02d", now.day);
51 sprintf(s[3], "%02d", now.hour);
52 sprintf(s[4], "%02d", now.minute);
53 sprintf(s[5], "%02d", now.second);
54 time = PrintString("*", s[0], s[1], s[2], "-", s[3], s[4], s[5], "*");
60 // use =0 to disable printing of specific channels
62 static enum dplchan { none, gdbProtoIgnored=0/*1*/, gdbProtoUnknown=2, gdbOutput=3/*3*/, gdbCommand=4/*4*/, debuggerCall=0/*5*/, debuggerProblem=6,
63 debuggerUserAction=7,debuggerState=8, debuggerBreakpoints=9, debuggerWatches=0/*10*/, debuggerTemp=0 };
65 static enum dplchan { none, gdbProtoIgnored=0, gdbProtoUnknown=0, gdbOutput=0, gdbCommand=0, debuggerCall=0, debuggerProblem=0,
66 debuggerUserAction=0,debuggerState=0, debuggerBreakpoints=0, debuggerWatches=0, debuggerTemp=0 };
68 static const char * _dpct[] = {
70 "GDB Protocol Ignored",
71 "GDB Protocol ***Unknown***",
75 "Debugger ***Problem***",
76 "Debugger::ChangeUserAction",
77 "Debugger::ChangeState",
80 "-----> Temporary Message",
84 // TODO if(strlen(item.value) < MAX_F_STRING)
88 #define _dpl2(...) __dpl2(__FILE__, __LINE__, ##__VA_ARGS__)
92 static void __dpl2(const char * file, int line, const char ** channels, int channel, int indent, typed_object object, ...)
94 bool chan = channel && channels && channels[channel];
97 char string[MAX_F_STRING];
99 char * time = PrintNow();
101 //ide.outputView.debugBox.Logf();
102 Logf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
103 va_start(args, object);
104 len = PrintStdArgsToBuffer(string, sizeof(string), object, args);
112 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
113 static void __dpl(const char * file, int line, int indent, const char * format, ...)
116 char string[MAX_F_STRING];
118 char * time = PrintNow();
119 //static File f = null;
120 va_start(args, format);
121 vsnprintf(string, sizeof(string), format, args);
122 string[sizeof(string)-1] = 0;
125 char * time = PrintNow();
127 logName = PrintString(time, ".log");
129 f = FileOpen(logName, write);
132 /*f.Printf("%s %s:% 5d: ", time, file, line);
133 for(c = 0; c<indent; c++)
135 f.Printf("%s\n", string);*/
136 Logf("%s %s:% 5d: ", time, file, line);
137 for(c = 0; c<indent; c++)
139 Logf("%s\n", string);
144 public char * StripQuotes2(char * string, char * output)
148 bool quoted = false, escaped = false;
150 for(c = 0; ch = string[c]; c++)
154 if(escaped || ch != '\"')
157 escaped = !escaped && ch == '\\';
171 // String Escape Copy
172 static void strescpy(char * d, char * s)
180 case '\n': d[k] = '\\'; d[++k] = 'n'; break;
181 case '\t': d[k] = '\\'; d[++k] = 't'; break;
182 case '\a': d[k] = '\\'; d[++k] = 'a'; break;
183 case '\b': d[k] = '\\'; d[++k] = 'b'; break;
184 case '\f': d[k] = '\\'; d[++k] = 'f'; break;
185 case '\r': d[k] = '\\'; d[++k] = 'r'; break;
186 case '\v': d[k] = '\\'; d[++k] = 'v'; break;
187 case '\\': d[k] = '\\'; d[++k] = '\\'; break;
188 case '\"': d[k] = '\\'; d[++k] = '\"'; break;
189 default: d[k] = s[j];
196 /*static char * CopyUnescapedSystemPath(char * p)
199 char * d = new char[len + 1];
200 UnescapeString(d, p, len);
201 #if defined(__WIN32__)
202 ChangeCh(d, '/', '\\');
207 static char * CopyUnescapedUnixPath(char * p)
210 char * d = new char[len + 1];
211 UnescapeString(d, p, len);
212 #if defined(__WIN32__)
213 ChangeCh(d, '\\', '/');
218 static char * CopyUnescapedString(char * s)
221 char * d = new char[len + 1];
222 UnescapeString(d, s, len);
226 static char * StripBrackets(char * string)
228 int length = strlen(string);
229 if(length > 1 && *string == '[' && string[length - 1] == ']')
232 string[length - 1] = '\0';
239 static char * StripCurlies(char * string)
241 int length = strlen(string);
242 if(length > 1 && *string == '{' && string[length - 1] == '}')
245 string[length - 1] = '\0';
252 /*static int StringGetInt(char * string, int start)
255 int i, len = strlen(string);
257 for(i = start; i < len && i < start + 8; i++)
259 if(string[i] == '0' || string[i] == '1' || string[i] == '2' || string[i] == '3' || string[i] == '4' || string[i] == '5' || string[i] == '6' || string[i] == '7' || string[i] == '8' || string[i] == '9')
260 strncat(number, &string[i], 1);
267 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
271 bool quoted = false, escaped = false;
272 char * start = string, ch;
274 for(; (ch = *string); string++)
281 if(escaped || ch != '\"')
282 escaped = !escaped && ch == '\\';
288 else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
290 else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
292 else if(ch == seperator && !level)
301 //tokens[count] = start;
302 //tokens[count++] = start;
309 static bool TokenizeListItem(char * string, DebugListItem item)
311 char * equal = strstr(string, "=");
323 static bool CheckCommandAvailable(const char * command)
325 bool available = false;
327 char * name = new char[MAX_FILENAME];
328 char * pathVar = new char[maxPathLen];
330 GetEnvironment("PATH", pathVar, maxPathLen);
331 count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
332 strcpy(name, command);
336 const char * extensions[] = { "exe", "com", "bat", null };
337 for(e=0; extensions[e]; e++)
339 ChangeExtension(name, extensions[e], name);
341 for(c=0; c<count; c++)
343 FileListing fl { paths[c] };
346 if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
365 // define GdbGetLineSize = 1638400;
366 define GdbGetLineSize = 5638400;
367 #if defined(__unix__)
368 char progFifoPath[MAX_LOCATION];
369 char progFifoDir[MAX_LOCATION];
372 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
375 none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause, locationReached;
377 property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd || this == locationReached); } };
379 enum DebuggerAction { none, internal, restart, stop, selectFrame, advance }; //, bpValidation
382 unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit, locationReached
383 //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope,
384 //exited, exitedNormally, exitedSignalled;
388 none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad, internalEntry;
390 property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad || this == internalEntry); } };
391 property bool isUser { get { return (this == user || this == runToCursor); } };
393 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
394 enum DebuggerUserAction
396 none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepUntil, stepOut, runToCursor;
397 property bool breaksOnInternalBreakpoint { get { return (this == stepInto || this == stepOver || this == stepUntil); } };
401 none, run, _continue, next, until, advance, step, finish;
402 property bool suspendInternalBreakpoints { get { return (this == until || this == advance || this == step || this == finish); } };
405 FileDialog debuggerFileDialog { type = selectDir };
407 static DualPipe vgTargetHandle;
408 static File vgLogFile;
409 static char vgLogPath[MAX_LOCATION];
410 static DualPipe gdbHandle;
411 static DebugEvaluationData eval { };
413 static int targetProcessId;
415 static bool gdbReady;
416 static bool breakpointError;
420 Semaphore serialSemaphore { };
426 bool sentBreakInsert;
427 bool ignoreBreakpoints;
435 int activeFrameLevel;
444 GdbExecution gdbExecution;
445 DebuggerUserAction userAction;
448 DebuggerAction breakType;
450 //DebuggerCommand lastCommand; // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
452 GdbDataStop stopItem;
453 GdbDataBreakpoint bpItem;
456 List<Breakpoint> sysBPs { };
457 Breakpoint bpRunToCursor;
458 Breakpoint intBpEntry;
459 Breakpoint intBpMain;
460 Breakpoint intBpWinMain;
464 CompilerConfig currentCompiler;
465 ProjectConfig prjConfig;
468 CodeEditor codeEditor;
470 ValgrindLogThread vgLogThread { debugger = this };
471 ValgrindTargetThread vgTargetThread { debugger = this };
472 GdbThread gdbThread { debugger = this };
475 Map<String, bool> projectsLibraryLoaded { };
479 delay = 0.0, userData = this;
483 bool monitor = false;
484 DebuggerEvent curEvent = event;
485 GdbDataStop stopItem = this.stopItem;
486 Breakpoint bpUser = null;
487 Breakpoint bpInternal = null;
495 this.stopItem = null;
499 DynamicString bpReport { };
501 for(bp : sysBPs; bp.inserted)
503 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
506 if(bpRunToCursor && bpRunToCursor.inserted)
508 Breakpoint bp = bpRunToCursor;
509 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
512 for(bp : ide.workspace.breakpoints; bp.inserted)
514 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
518 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
523 Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
526 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")");
537 if(curEvent && curEvent != exit)
539 _dpl(0, "No stop item");
547 Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
556 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
557 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
558 if(activeFrame.level == activeFrameLevel)
564 // GdbCommand(0, false, "-break-info %s", bpItem.number);
576 Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
577 if(bp && bp.inserted && bp.bp.addr)
579 if(bp.type.isInternal)
583 if(stopItem && stopItem.frame)
585 if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
586 bpUser = bpRunToCursor;
589 for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
591 if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
603 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
604 if(bpUser && stopItem.frame.addr && strcmp(stopItem.frame.addr, bpUser.bp.addr))
605 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") address missmatch!");
608 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
609 if((bpUser && !ignoreBreakpoints) || (bpInternal && userAction.breaksOnInternalBreakpoint))
611 hitThread = stopItem.threadid;
615 signalThread = stopItem.threadid;
619 case locationReached:
621 ignoreBreakpoints = false;
623 case valgrindStartPause:
624 GdbExecContinue(true);
632 if(curEvent == signal)
636 ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
637 ide.outputView.debugBox.Logf(" %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
638 ide.outputView.Show();
639 ide.callStackView.Show();
642 else if(curEvent == breakEvent)
644 ide.threadsView.Show();
645 ide.callStackView.Show();
646 ide.callStackView.Activate();
648 else if(curEvent == hit)
650 if(BreakpointHit(stopItem, bpInternal, bpUser))
652 ide.AdjustDebugMenus();
653 if(bpUser && bpUser.type == runToCursor)
655 ignoreBreakpoints = false;
656 UnsetBreakpoint(bpUser);
657 delete bpRunToCursor;
662 if(breakType == advance && bpInternal && (bpInternal.type == internalMain || bpInternal.type == internalEntry))
665 GdbExecAdvance(breakString, 0);
670 GdbExecContinue(false);
676 if(monitor && curEvent.canBeMonitored)
679 activeThread = stopItem.threadid;
680 GdbCommand(0, false, "-thread-list-ids");
681 InternalSelectFrame(activeFrameLevel);
682 GoToStackFrameLine(activeFrameLevel, true, false);
684 ide.ShowCodeEditor();
685 ide.AdjustDebugMenus();
686 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
699 #ifdef GDB_DEBUG_CONSOLE
700 char lastGdbOutput[GdbGetLineSize];
702 #if defined(__unix__)
703 ProgramThread progThread { };
707 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
708 void ChangeUserAction(const char * file, int line, DebuggerUserAction value)
710 bool same = value == userAction;
712 __dpl2(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
717 #define _ChangeUserAction(value) userAction = value
721 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
722 void ChangeState(const char * file, int line, DebuggerState value)
724 #define _ChangeState(value) ChangeState(value)
725 void ChangeState(DebuggerState value)
728 bool same = value == state;
729 #if 0 //def _DEBUG_INST
730 __dpl2(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
733 if(!same) ide.AdjustDebugMenus();
738 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
740 stackFrames.Free(Frame::Free);
750 waitingForPID = false;
755 sentBreakInsert = false;
756 ignoreBreakpoints = false;
759 activeFrameLevel = 0;
776 bpRunToCursor = null;
778 delete currentCompiler;
781 WatchesReleaseCodeEditor();
784 projectsLibraryLoaded.Free();
786 /*GdbThread gdbThread
792 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
793 ideProcessId = Process_GetCurrentProcessId();
795 sysBPs.Add((intBpEntry = Breakpoint { type = internalEntry, enabled = false, level = -1 }));
796 sysBPs.Add((intBpMain = Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 }));
797 #if defined(__WIN32__)
798 sysBPs.Add((intBpWinMain = Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 }));
800 sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
801 sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
806 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
814 property bool isActive { get { return state == running || state == stopped; } }
815 property bool isPrepared { get { return state == loaded || state == running || state == stopped; } }
819 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
820 _ChangeUserAction(resume);
821 GdbExecContinue(true);
826 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
827 _ChangeUserAction(_break);
831 GdbDebugBreak(false);
837 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
838 _ChangeUserAction(stop);
845 GdbDebugBreak(false);
859 void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
861 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
862 _ChangeUserAction(restart);
863 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
867 bool GoToCodeLine(char * location)
870 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
871 codloc = CodeLocation::ParseCodeLocation(location);
874 CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, false, true, null, no, normal, false);
877 EditBox editBox = editor.editBox;
878 if(editBox.GoToLineNum(codloc.line - 1))
879 editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
886 bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
888 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
891 char filePath[MAX_LOCATION];
892 char sourceDir[MAX_LOCATION];
894 CodeEditor editor = null;
895 if(stackLevel == -1) // this (the two lines) is part of that fix that I would not put in for some time
897 for(frame = stackFrames.first; frame; frame = frame.next)
898 if(frame.level == stackLevel)
903 ide.callStackView.Show();
905 if(frame.absoluteFile)
906 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
907 if(!editor && frame.file)
908 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
909 if(!frame.absoluteFile && askForLocation && frame.file)
912 char title[MAX_LOCATION];
913 snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
914 title[sizeof(title)-1] = 0;
916 if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
918 AddSourceDir(sourceDir);
919 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
922 if(!editor && frame.absoluteFile)
923 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
925 ide.RepositionWindows(false);
927 if(editor && frame.line)
929 EditBox editBox = editor.editBox;
930 editBox.GoToLineNum(frame.line - 1);
931 editBox.GoToPosition(editBox.line, frame.line - 1, 0);
939 void SelectThread(int thread)
941 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
942 _ChangeUserAction(selectThread);
945 if(thread != activeThread)
947 activeFrameLevel = -1;
948 ide.callStackView.Clear();
949 GdbCommand(0, false, "-thread-select %d", thread);
951 InternalSelectFrame(activeFrameLevel);
952 GoToStackFrameLine(activeFrameLevel, true, false);
956 ide.callStackView.Show();
960 void SelectFrame(int frame)
962 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
963 _ChangeUserAction(selectFrame);
966 if(frame != activeFrameLevel)
968 InternalSelectFrame(frame);
975 void InternalSelectFrame(int frame)
977 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
978 activeFrameLevel = frame; // there is no active frame number in the gdb reply
979 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
980 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
981 if(activeFrame.level == activeFrameLevel)
985 void HandleExit(char * reason, char * code)
987 bool returnedExitCode = false;
988 char verboseExitCode[128];
990 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
991 _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
996 snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
997 verboseExitCode[sizeof(verboseExitCode)-1] = 0;
1000 verboseExitCode[0] = '\0';
1004 // ClearBreakDisplay();
1008 for(wh : ide.workspace.watches)
1010 if(wh.type) FreeType(wh.type);
1013 ide.watchesView.UpdateWatch(wh);
1017 #if defined(__unix__)
1020 progThread.terminate = true;
1023 fifoFile.CloseInput();
1033 char program[MAX_LOCATION];
1034 GetSystemPathBuffer(program, targetFile);
1036 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1037 else if(!strcmp(reason, "exited-normally"))
1038 ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
1039 else if(!strcmp(reason, "exited"))
1040 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1041 else if(!strcmp(reason, "exited-signalled"))
1042 ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
1044 ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
1049 DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool ignoreBreakpoints)
1051 DebuggerState result = none;
1052 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), ignoreBreakpoints(", ignoreBreakpoints, ")");
1053 if(restart && state == running && targetProcessId)
1055 breakType = DebuggerAction::restart;
1056 GdbDebugBreak(false);
1060 if(restart && state == stopped)
1062 if(needReset && state == loaded)
1063 GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
1065 if(result == none || result == terminated)
1067 ide.outputView.ShowClearSelectTab(debug);
1068 ide.outputView.debugBox.Logf($"Starting debug mode\n");
1075 for(bp : ide.workspace.breakpoints)
1081 if(GdbInit(compiler, config, bitDepth, useValgrind))
1086 this.ignoreBreakpoints = ignoreBreakpoints;
1091 void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1093 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1094 _ChangeUserAction(start);
1095 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
1099 void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1101 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1102 _ChangeUserAction(stepInto);
1103 switch(StartSession(compiler, config, bitDepth, useValgrind, false, false))
1105 case loaded: GdbExecRun(); break;
1106 case stopped: GdbExecStep(); break;
1110 void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1112 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1113 _ChangeUserAction(stepOver);
1114 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1116 case loaded: GdbExecRun(); break;
1117 case stopped: GdbExecNext(); break;
1121 void StepUntil(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1123 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepUntil()");
1124 _ChangeUserAction(stepUntil);
1125 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1127 case loaded: GdbExecRun(); break;
1128 case stopped: GdbExecUntil(null, 0); break;
1132 void StepOut(bool ignoreBreakpoints)
1134 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1135 _ChangeUserAction(stepOut);
1136 if(state == stopped)
1138 this.ignoreBreakpoints = ignoreBreakpoints;
1142 GdbExecContinue(true);
1146 void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, const char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel, bool oldImplementation)
1148 char relativeFilePath[MAX_LOCATION];
1149 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1150 _ChangeUserAction(runToCursor);
1151 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
1153 if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1155 UnsetBreakpoint(bpRunToCursor);
1156 delete bpRunToCursor;
1159 StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints);
1162 if(oldImplementation)
1164 bpRunToCursor = Breakpoint { };
1165 bpRunToCursor.absoluteFilePath = absoluteFilePath;
1166 bpRunToCursor.relativeFilePath = relativeFilePath;
1167 bpRunToCursor.line = lineNumber;
1168 bpRunToCursor.type = runToCursor;
1169 bpRunToCursor.enabled = true;
1170 bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1175 breakType = advance;
1176 breakString = PrintString(relativeFilePath, ":", lineNumber);
1179 else if(state == stopped)
1181 if(oldImplementation)
1182 GdbExecContinue(true);
1186 GdbExecUntil(absoluteFilePath, lineNumber);
1188 GdbExecAdvance(absoluteFilePath, lineNumber);
1193 void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1195 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1196 if(activeFrameLevel == -1)
1204 *error = signalOn && activeThread == signalThread;
1205 *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1206 *lineTopFrame = activeFrameLevel ? 1 : 0;
1210 int GetMarginIconsLineNumbers(const char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1212 char winFilePath[MAX_LOCATION];
1213 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1215 Iterator<Breakpoint> it { ide.workspace.breakpoints };
1216 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1217 while(it.Next() && count < max)
1219 Breakpoint bp = it.data;
1222 if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1224 lines[count] = bp.line;
1225 enabled[count] = bp.enabled;
1230 if(activeFrameLevel == -1)
1238 *error = signalOn && activeThread == signalThread;
1239 if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1240 *lineCursor = activeFrame.line;
1243 if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1245 else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1246 *lineTopFrame = stopItem.frame.line;
1250 if(*lineTopFrame == *lineCursor && *lineTopFrame)
1256 void ChangeWatch(DataRow row, char * expression)
1258 Watch wh = (Watch)row.tag;
1259 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1262 delete wh.expression;
1264 wh.expression = CopyString(expression);
1267 Iterator<Watch> it { ide.workspace.watches };
1269 ide.workspace.watches.Delete(it.pointer);
1275 row.tag = (int64)wh;
1276 ide.workspace.watches.Add(wh);
1278 wh.expression = CopyString(expression);
1280 ide.workspace.Save();
1281 //if(expression && state == stopped)
1286 void MoveIcons(const char * fileName, int lineNumber, int move, bool start)
1288 char winFilePath[MAX_LOCATION];
1289 const char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1292 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1293 for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1295 Breakpoint bp = (Breakpoint)bpLink.data;
1298 if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1300 if(bp.line > lineNumber || (bp.line == lineNumber && start))
1302 if(move < 0 && (bp.line < lineNumber - move))
1303 ide.workspace.RemoveBreakpoint(bp);
1307 ide.breakpointsView.UpdateBreakpoint(bp.row);
1308 ide.workspace.Save();
1314 // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1317 bool SourceDirDialog(const char * title, const char * startDir, const char * test, char * sourceDir)
1321 String srcDir = null;
1323 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1324 debuggerFileDialog.text = title;
1325 debuggerFileDialog.currentDirectory = startDir;
1326 debuggerFileDialog.master = ide;
1328 while(debuggerFileDialog.Modal())
1330 strcpy(sourceDir, debuggerFileDialog.filePath);
1331 if(!fstrcmp(ide.workspace.projectDir, sourceDir) &&
1332 MessageBox { type = yesNo, master = ide,
1333 contents = $"This is the project directory.\nWould you like to try again?",
1334 text = $"Invalid Source Directory" }.Modal() == no)
1338 for(dir : ide.workspace.sourceDirs)
1340 if(!fstrcmp(dir, sourceDir))
1348 MessageBox { type = yesNo, master = ide,
1349 contents = $"This source directory is already specified.\nWould you like to try again?",
1350 text = $"Invalid Source Directory" }.Modal() == no)
1356 char file[MAX_LOCATION];
1357 strcpy(file, sourceDir);
1358 PathCat(file, test);
1359 result = FileExists(file);
1361 MessageBox { type = yesNo, master = ide,
1362 contents = $"Unable to locate source file.\nWould you like to try again?",
1363 text = $"Invalid Source Directory" }.Modal() == no)
1377 void AddSourceDir(const char * sourceDir)
1379 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1380 ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1381 ide.workspace.Save();
1385 DebuggerState oldState = state;
1390 GdbDebugBreak(true);
1393 GdbCommand(0, false, "-environment-directory \"%s\"", sourceDir);
1396 if(oldState == running)
1397 GdbExecContinue(false);
1401 void ToggleBreakpoint(const char * fileName, int lineNumber)
1403 char absolutePath[MAX_LOCATION];
1404 Breakpoint bp = null;
1406 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1408 GetSlashPathBuffer(absolutePath, fileName);
1409 for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1418 ide.workspace.RemoveBreakpoint(bp);
1427 char relativePath[MAX_LOCATION];
1429 WorkspaceGetRelativePath(absolutePath, relativePath, &owner);
1431 if(!owner && !FileExists(absolutePath))
1433 char title[MAX_LOCATION];
1434 char directory[MAX_LOCATION];
1435 char sourceDir[MAX_LOCATION];
1436 StripLastDirectory(absolutePath, directory);
1437 snprintf(title, sizeof(title), $"Provide source files location directory for %s", relativePath);
1438 title[sizeof(title)-1] = 0;
1441 String srcDir = null;
1442 for(dir : ide.workspace.sourceDirs)
1444 if(IsPathInsideOf(absolutePath, dir))
1446 MakePathRelative(absolutePath, dir, relativePath);
1454 if(SourceDirDialog(title, directory, null, sourceDir))
1456 if(IsPathInsideOf(absolutePath, sourceDir))
1458 AddSourceDir(sourceDir);
1459 MakePathRelative(absolutePath, sourceDir, relativePath);
1462 else if(MessageBox { type = yesNo, master = ide,
1463 contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1464 text = $"Invalid Source Directory" }.Modal() == no)
1471 ide.workspace.bpCount++;
1472 bp = { line = lineNumber, type = user, enabled = true, level = -1, project = owner };
1473 ide.workspace.breakpoints.Add(bp);
1474 bp.absoluteFilePath = absolutePath;
1475 bp.relativeFilePath = relativePath;
1476 ide.breakpointsView.AddBreakpoint(bp);
1481 DebuggerState oldState = state;
1486 GdbDebugBreak(true);
1489 if(!SetBreakpoint(bp, false))
1490 SetBreakpoint(bp, true);
1493 if(oldState == running)
1494 GdbExecContinue(false);
1497 ide.workspace.Save();
1500 void UpdateRemovedBreakpoint(Breakpoint bp)
1502 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1503 if(targeted && bp.inserted)
1505 DebuggerState oldState = state;
1510 GdbDebugBreak(true);
1513 UnsetBreakpoint(bp);
1516 if(oldState == running)
1517 GdbExecContinue(false);
1523 void ParseFrame(Frame frame, char * string)
1526 Array<char *> frameTokens { minAllocSize = 50 };
1527 Array<char *> argsTokens { minAllocSize = 50 };
1528 Array<char *> argumentTokens { minAllocSize = 50 };
1529 DebugListItem item { };
1532 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1533 TokenizeList(string, ',', frameTokens);
1534 for(i = 0; i < frameTokens.count; i++)
1536 if(TokenizeListItem(frameTokens[i], item))
1538 StripQuotes(item.value, item.value);
1539 if(!strcmp(item.name, "level"))
1540 frame.level = atoi(item.value);
1541 else if(!strcmp(item.name, "addr"))
1542 frame.addr = item.value;
1543 else if(!strcmp(item.name, "func"))
1544 frame.func = item.value;
1545 else if(!strcmp(item.name, "args"))
1547 if(!strcmp(item.value, "[]"))
1548 frame.argsCount = 0;
1551 item.value = StripBrackets(item.value);
1552 TokenizeList(item.value, ',', argsTokens);
1553 for(j = 0; j < argsTokens.count; j++)
1555 argsTokens[j] = StripCurlies(argsTokens[j]);
1556 TokenizeList(argsTokens[j], ',', argumentTokens);
1557 for(k = 0; k < argumentTokens.count; k++)
1560 frame.args.Add(arg);
1561 if(TokenizeListItem(argumentTokens[k], item))
1563 if(!strcmp(item.name, "name"))
1565 StripQuotes(item.value, item.value);
1566 arg.name = item.value;
1568 else if(!strcmp(item.name, "value"))
1570 StripQuotes(item.value, item.value);
1571 arg.val = item.value;
1574 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1577 _dpl(0, "Bad frame args item");
1579 argumentTokens.RemoveAll();
1581 frame.argsCount = argsTokens.count;
1582 argsTokens.RemoveAll();
1585 else if(!strcmp(item.name, "from"))
1586 frame.from = item.value;
1587 else if(!strcmp(item.name, "file"))
1588 frame.file = item.value;
1589 else if(!strcmp(item.name, "line"))
1590 frame.line = atoi(item.value);
1591 else if(!strcmp(item.name, "fullname"))
1593 // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1594 Workspace ws = ide.workspace;
1597 String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1598 if(strcmp(frame.file, path))
1602 frame.absoluteFile = item.value;
1605 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1608 _dpl(0, "Bad frame");
1613 delete argumentTokens;
1617 Breakpoint GetBreakpointById(int id, bool * isInternal)
1619 Breakpoint bp = null;
1620 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1622 *isInternal = false;
1625 for(i : sysBPs; i.bp && i.bp.id == id)
1632 if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1636 for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1646 GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1649 GdbDataBreakpoint bp { };
1650 DebugListItem item { };
1651 Array<char *> bpTokens { minAllocSize = 16 };
1652 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1653 string = StripCurlies(string);
1654 TokenizeList(string, ',', bpTokens);
1655 for(i = 0; i < bpTokens.count; i++)
1657 if(TokenizeListItem(bpTokens[i], item))
1659 StripQuotes(item.value, item.value);
1660 if(!strcmp(item.name, "number"))
1662 if(!strchr(item.value, '.'))
1663 bp.id = atoi(item.value);
1664 bp.number = item.value;
1666 else if(!strcmp(item.name, "type"))
1667 bp.type = item.value;
1668 else if(!strcmp(item.name, "disp"))
1669 bp.disp = item.value;
1670 else if(!strcmp(item.name, "enabled"))
1671 bp.enabled = (!strcmpi(item.value, "y"));
1672 else if(!strcmp(item.name, "addr"))
1674 if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1677 Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1678 while(outTokens.count > ++c)
1680 GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1681 bpArray.Add(multBp);
1685 bp.addr = item.value;
1687 else if(!strcmp(item.name, "func"))
1688 bp.func = item.value;
1689 else if(!strcmp(item.name, "file"))
1690 bp.file = item.value;
1691 else if(!strcmp(item.name, "fullname"))
1692 bp.fullname = item.value;
1693 else if(!strcmp(item.name, "line"))
1694 bp.line = atoi(item.value);
1695 else if(!strcmp(item.name, "at"))
1697 else if(!strcmp(item.name, "times"))
1698 bp.times = atoi(item.value);
1699 else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1700 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1702 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1710 void ShowDebuggerViews()
1712 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1713 ide.outputView.Show();
1714 ide.outputView.SelectTab(debug);
1715 ide.threadsView.Show();
1716 ide.callStackView.Show();
1717 ide.watchesView.Show();
1721 void HideDebuggerViews()
1723 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1724 ide.RepositionWindows(true);
1727 bool ::GdbCommand(Time timeOut, bool focus, const char * format, ...)
1729 bool result = false;
1733 // TODO: Improve this limit
1734 static char string[MAX_F_STRING*4];
1736 va_start(args, format);
1737 vsnprintf(string, sizeof(string), format, args);
1738 string[sizeof(string)-1] = 0;
1742 ide.debugger.serialSemaphore.TryWait();
1744 #ifdef GDB_DEBUG_CONSOLE
1745 _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1747 #ifdef GDB_DEBUG_OUTPUT
1748 ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1750 #ifdef GDB_DEBUG_GUI
1752 ide.gdbDialog.AddCommand(string);
1755 strcat(string,"\n");
1756 gdbHandle.Puts(string);
1759 Process_ShowWindows(targetProcessId);
1765 startTime = GetTime();
1768 if(ide.debugger.serialSemaphore.TryWait())
1775 if(GetTime() - startTime > timeOut)
1783 ide.debugger.serialSemaphore.Wait();
1792 bool ValidateBreakpoint(Breakpoint bp)
1794 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1795 if(modules && bp.line && bp.bp)
1797 if(bp.bp.line != bp.line)
1803 ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1805 //UnsetBreakpoint(bp);
1806 //bp.enabled = false;
1812 ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1813 bp.line = bp.bp.line;
1820 void BreakpointsMaintenance()
1822 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::BreakpointsMaintenance()");
1825 if(gdbExecution.suspendInternalBreakpoints)
1827 for(bp : sysBPs; bp.inserted)
1828 UnsetBreakpoint(bp);
1832 DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1833 for(bp : sysBPs; !bp.inserted)
1835 bool insert = false;
1836 if(bp.type == internalModulesLoaded)
1838 char path[MAX_LOCATION];
1839 char name[MAX_LOCATION];
1840 char fixedModuleName[MAX_FILENAME];
1843 bool moduleLoadBlock = false;
1845 ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1846 snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1847 name[sizeof(name)-1] = 0;
1848 strcpy(path, ide.workspace.projectDir);
1849 PathCatSlash(path, objDir.dir);
1850 PathCatSlash(path, name);
1851 f = FileOpen(path, read);
1854 for(lineNumber = 1; !f.Eof(); lineNumber++)
1856 if(f.GetLine(line, sizeof(line) - 1))
1858 bool moduleLoadLine;
1859 TrimLSpaces(line, line);
1860 moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1861 if(!moduleLoadBlock && moduleLoadLine)
1862 moduleLoadBlock = true;
1863 else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1869 char relative[MAX_LOCATION];
1870 bp.absoluteFilePath = path;
1871 MakePathRelative(path, ide.workspace.projectDir, relative);
1872 bp.relativeFilePath = relative;
1873 bp.line = lineNumber;
1879 else if(bp.type == internalModuleLoad)
1883 for(prj : ide.workspace.projects)
1885 if(!strcmp(prj.moduleName, "ecere"))
1887 ProjectNode node = prj.topNode.Find("instance.c", false);
1890 char path[MAX_LOCATION];
1891 char relative[MAX_LOCATION];
1892 node.GetFullFilePath(path);
1893 bp.absoluteFilePath = path;
1894 MakePathRelative(path, prj.topNode.path, relative);
1895 bp.relativeFilePath = relative;
1907 if(!SetBreakpoint(bp, false))
1908 SetBreakpoint(bp, true);
1914 if(userAction != runToCursor && bpRunToCursor && bpRunToCursor.inserted)
1915 UnsetBreakpoint(bpRunToCursor);
1916 if(bpRunToCursor && !bpRunToCursor.inserted)
1918 if(!SetBreakpoint(bpRunToCursor, false))
1919 SetBreakpoint(bpRunToCursor, true);
1922 if(ignoreBreakpoints)
1924 for(bp : ide.workspace.breakpoints; bp.inserted)
1925 UnsetBreakpoint(bp);
1929 for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1933 if(!SetBreakpoint(bp, false))
1934 SetBreakpoint(bp, true);
1943 bp.bp = GdbDataBreakpoint { };
1950 void UnsetBreakpoint(Breakpoint bp)
1952 char * s = null; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1953 if(symbols && bp.inserted)
1955 GdbCommand(0, false, "-break-delete %s", bp.bp.number);
1956 bp.inserted = false;
1962 bool SetBreakpoint(Breakpoint bp, bool removePath)
1964 char * s = null; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
1965 breakpointError = false;
1966 if(symbols && bp.enabled && (!bp.project || bp.project.GetTargetType(bp.project.config) == staticLibrary || bp.project == ide.project || projectsLibraryLoaded[bp.project.name]))
1968 sentBreakInsert = true;
1970 GdbCommand(0, false, "-break-insert *%s", bp.address);
1973 char * location = bp.CopyLocationString(removePath);
1974 GdbCommand(0, false, "-break-insert %s", location);
1977 if(!breakpointError)
1979 char * address = null;
1980 if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
1983 GdbDataBreakpoint first = null;
1984 for(n : bpItem.multipleBPs)
1986 if(!fstrcmp(n.fullname, bp.absoluteFilePath) && !first)
1996 GdbCommand(0, false, "-break-disable %s", n.number);
2000 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2005 address = CopyString(first.addr);
2006 bpItem.addr = first.addr;
2007 bpItem.func = first.func;
2008 bpItem.file = first.file;
2009 bpItem.fullname = first.fullname;
2010 bpItem.line = first.line;
2011 //bpItem.thread-groups = first.thread-groups;*/
2014 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2016 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2017 bpItem.multipleBPs.Free();
2018 delete bpItem.multipleBPs;
2023 bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2025 ValidateBreakpoint(bp);
2029 UnsetBreakpoint(bp);
2030 bp.address = address;
2032 SetBreakpoint(bp, removePath);
2035 return !breakpointError;
2042 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2044 stackFrames.Free(Frame::Free);
2045 GdbCommand(0, false, "-stack-info-depth");
2047 GdbCommand(0, false, "-stack-info-depth 192");
2048 if(frameCount && frameCount <= 192)
2049 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2052 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2053 GdbCommand(0, false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2055 GdbCommand(0, false, "");
2060 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2063 char escaped[MAX_LOCATION];
2064 strescpy(escaped, targetFile);
2065 GdbCommand(0, false, "file \"%s\"", escaped); //GDB/MI Missing Implementation in 5.1.1 but we now have -file-exec-and-symbols / -file-exec-file / -file-symbol-file
2072 const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2073 //GdbCommand(0, false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2074 printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2075 GdbCommand(0, false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2078 GdbCommand(0, false, "info target"); //GDB/MI Missing Implementation -file-list-symbol-files and -file-list-exec-sections
2080 /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2081 GdbCommand(0, false, "-environment-directory \"%s\"", prj.topNode.path);*/
2083 for(dir : ide.workspace.sourceDirs; dir && dir[0])
2085 bool interference = false;
2086 for(prj : ide.workspace.projects)
2088 if(!fstrcmp(prj.topNode.path, dir))
2090 interference = true;
2094 if(!interference && dir[0])
2095 GdbCommand(0, false, "-environment-directory \"%s\"", dir);
2103 /*void GdbTargetRelease()
2107 BreakpointsDeleteAll();
2108 GdbCommand(0, false, "file"); //GDB/MI Missing Implementation -target-detach
2114 void GdbDebugBreak(bool internal)
2116 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2120 breakType = DebuggerAction::internal;
2122 if(ide) ide.Update(null);
2124 if(Process_Break(targetProcessId)) //GdbCommand(0, false, "-exec-interrupt");
2125 serialSemaphore.Wait();
2128 _ChangeState(loaded);
2129 targetProcessId = 0;
2134 ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2139 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2144 ShowDebuggerViews();
2146 GdbExecContinue(true);
2147 else if(!GdbCommand(3, true, "-exec-run"))
2148 gdbExecution = none;
2151 void GdbExecContinue(bool focus)
2153 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2156 GdbCommand(0, focus, "-exec-continue");
2161 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2162 gdbExecution = next;
2164 GdbCommand(0, true, "-exec-next");
2167 void ForceUpdateCurrentFrame()
2170 GdbCommand(0, false, "-thread-list-ids");
2171 InternalSelectFrame(activeFrameLevel);
2172 GoToStackFrameLine(activeFrameLevel, true, false);
2174 ide.ShowCodeEditor();
2175 ide.AdjustDebugMenus();
2176 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
2180 void GdbExecUntil(const char * absoluteFilePath, int lineNumber)
2182 bool forceUpdate = false;
2183 char relativeFilePath[MAX_LOCATION];
2184 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecUntil()");
2185 gdbExecution = until;
2187 if(absoluteFilePath)
2189 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
2190 if(!GdbCommand(0.1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2192 GetLastDirectory(relativeFilePath, relativeFilePath);
2193 if(GdbCommand(1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2198 GdbCommand(0, true, "-exec-until");
2200 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2201 // (It may not be ideal, we may need to wait?)
2203 ForceUpdateCurrentFrame();
2206 void GdbExecAdvance(const char * absoluteFilePathOrLocation, int lineNumber)
2208 bool forceUpdate = false;
2209 char relativeFilePath[MAX_LOCATION];
2210 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecAdvance()");
2211 gdbExecution = advance;
2215 WorkspaceGetRelativePath(absoluteFilePathOrLocation, relativeFilePath, null);
2216 if(!GdbCommand(0.1, true, "advance %s:%d", relativeFilePath, lineNumber)) // should use -exec-advance -- GDB/MI implementation missing
2218 GetLastDirectory(relativeFilePath, relativeFilePath);
2219 if(GdbCommand(1, true, "advance %s:%d", relativeFilePath, lineNumber))
2225 if(!GdbCommand(0.1, true, "advance %s", absoluteFilePathOrLocation)) // should use -exec-advance -- GDB/MI implementation missing
2227 GetLastDirectory(absoluteFilePathOrLocation, relativeFilePath);
2228 if(GdbCommand(1, true, "advance %s", relativeFilePath))
2233 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2234 // (It may not be ideal, we may need to wait?)
2236 ForceUpdateCurrentFrame();
2241 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2242 gdbExecution = step;
2244 GdbCommand(0, true, "-exec-step");
2247 void GdbExecFinish()
2249 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2250 gdbExecution = finish;
2252 GdbCommand(0, true, "-exec-finish");
2255 void GdbExecCommon()
2257 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2258 BreakpointsMaintenance();
2261 #ifdef GDB_DEBUG_GUI
2262 void SendGDBCommand(const char * command)
2264 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2265 DebuggerState oldState = state;
2270 GdbDebugBreak(true);
2273 GdbCommand(0, false, command);
2276 if(oldState == running)
2277 GdbExecContinue(false);
2281 void ClearBreakDisplay()
2283 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2285 activeFrameLevel = -1;
2295 stackFrames.Free(Frame::Free);
2296 ide.callStackView.Clear();
2297 ide.threadsView.Clear();
2303 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2305 GdbCommand(0, false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2309 bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2312 char oldDirectory[MAX_LOCATION];
2313 char tempPath[MAX_LOCATION];
2314 char command[MAX_F_STRING*4];
2315 Project project = ide.project;
2316 DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2317 PathBackup pathBackup { };
2318 Map<String, String> envBackup { };
2320 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2321 if(currentCompiler != compiler)
2323 delete currentCompiler;
2324 currentCompiler = compiler;
2325 incref currentCompiler;
2328 this.bitDepth = bitDepth;
2329 usingValgrind = useValgrind;
2331 _ChangeState(loaded);
2333 sentBreakInsert = false;
2334 breakpointError = false;
2335 ignoreBreakpoints = false;
2341 projectsLibraryLoaded.Free();
2343 ide.outputView.ShowClearSelectTab(debug);
2344 ide.outputView.debugBox.Logf($"Starting debug mode\n");
2346 #ifdef GDB_DEBUG_OUTPUT
2347 ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2350 strcpy(tempPath, ide.workspace.projectDir);
2351 PathCatSlash(tempPath, targetDirExp.dir);
2353 targetDir = CopyString(tempPath);
2354 project.CatTargetFileName(tempPath, compiler, config);
2356 targetFile = CopyString(tempPath);
2358 GetWorkingDir(oldDirectory, MAX_LOCATION);
2359 if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2361 char temp[MAX_LOCATION];
2362 strcpy(temp, ide.workspace.projectDir);
2363 PathCatSlash(temp, ide.workspace.debugDir);
2364 ChangeWorkingDir(temp);
2367 ChangeWorkingDir(ide.workspace.projectDir);
2369 ide.SetPath(true, compiler, config, bitDepth);
2371 // TODO: This pollutes the environment, but at least it works
2372 // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2373 // What is the proper solution for this? DualPipeOpenEnv?
2374 // gdb set environment commands don't seem to take effect
2375 for(e : ide.workspace.environmentVars)
2377 // Backing up the environment variables until we use DualPipeOpenEnv()
2378 envBackup[e.name] = CopyString(getenv(e.name));
2379 SetEnvironment(e.name, e.string);
2384 char * clArgs = ide.workspace.commandLineArgs;
2385 const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2386 ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2387 int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2388 bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2389 vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2393 vgLogThread.Create();
2397 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2400 if(result && !CheckCommandAvailable(valgrindCommand))
2402 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2407 char * vgRedzoneSizeFlag = PrintString(" --redzone-size=", vgRedzoneSize);
2408 sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2409 valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSize > -1 ? vgRedzoneSizeFlag : "", vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2410 delete vgRedzoneSizeFlag;
2411 vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2414 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2420 incref vgTargetHandle;
2421 vgTargetThread.Create();
2423 targetProcessId = vgTargetHandle.GetProcessID();
2424 waitingForPID = false;
2425 if(!targetProcessId)
2427 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2434 serialSemaphore.Wait();
2441 if(compiler.targetPlatform == win32)
2444 #if !((defined(__WORDSIZE) && __WORDSIZE == 8) || defined(__x86_64__))
2447 bitDepth == 32 ? "i686-w64-mingw32-gdb" : "gdb"); // x86_64-w64-mingw32-gdb
2450 // We really should have a box to select GDB in the compiler/toolchain options
2451 strcpy(command, "gdb");
2452 if(!CheckCommandAvailable(command))
2454 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2459 strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2461 gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2464 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2474 gdbProcessId = gdbHandle.GetProcessID();
2477 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2484 serialSemaphore.Wait();
2487 GdbCommand(0, false, "-gdb-set verbose off");
2488 //GdbCommand(0, false, "-gdb-set exec-done-display on");
2489 GdbCommand(0, false, "-gdb-set step-mode off");
2490 GdbCommand(0, false, "-gdb-set unwindonsignal on");
2491 //GdbCommand(0, false, "-gdb-set shell on");
2492 GdbCommand(0, false, "set print elements 992");
2493 GdbCommand(0, false, "-gdb-set backtrace limit 100000");
2497 //_ChangeState(terminated);
2503 #if defined(__unix__)
2505 CreateTemporaryDir(progFifoDir, "ecereide");
2506 strcpy(progFifoPath, progFifoDir);
2507 PathCat(progFifoPath, "ideprogfifo");
2508 if(!mkfifo(progFifoPath, 0600))
2510 //fileCreated = true;
2515 ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2522 progThread.terminate = false;
2523 progThread.Create();
2527 #if defined(__WIN32__)
2528 GdbCommand(0, false, "-gdb-set new-console on");
2531 #if defined(__unix__)
2533 GdbCommand(0, false, "-inferior-tty-set %s", progFifoPath);
2537 GdbCommand(0, false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2539 for(e : ide.workspace.environmentVars)
2541 GdbCommand(0, false, "set environment %s=%s", e.name, e.string);
2546 ChangeWorkingDir(oldDirectory);
2550 SetEnvironment(&e, e);
2559 delete targetDirExp;
2565 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2566 if(gdbHandle && gdbProcessId)
2569 GdbCommand(0, false, "-gdb-exit");
2578 vgLogFile.CloseInput();
2579 if(vgLogThread.created)
2589 vgTargetThread.Wait();
2599 _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2604 for(bp : ide.workspace.breakpoints)
2610 bpRunToCursor.Reset();
2612 ide.outputView.debugBox.Logf($"Debugging stopped\n");
2614 #if defined(__unix__)
2615 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2617 progThread.terminate = true;
2620 fifoFile.CloseInput();
2626 DeleteFile(progFifoPath);
2627 progFifoPath[0] = '\0';
2636 bool WatchesLinkCodeEditor()
2638 bool goodFrame = activeFrame && activeFrame.absoluteFile;
2639 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesLinkCodeEditor()");
2640 if(codeEditor && (!goodFrame || fstrcmp(codeEditor.fileName, activeFrame.absoluteFile)))
2641 WatchesReleaseCodeEditor();
2643 if(!codeEditor && goodFrame)
2645 codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, false, false, null, no, normal, false);
2648 codeEditor.inUseDebug = true;
2652 return codeEditor != null;
2655 void WatchesReleaseCodeEditor()
2657 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesReleaseCodeEditor()");
2660 codeEditor.inUseDebug = false;
2661 if(!codeEditor.visible)
2662 codeEditor.Destroy(0);
2667 bool ResolveWatch(Watch wh)
2669 bool result = false;
2671 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2683 char watchmsg[MAX_F_STRING];
2685 if(state == stopped && !codeEditor)
2686 wh.value = CopyString($"No source file found for selected frame");
2687 //if(codeEditor && state == stopped || state != stopped)
2690 Module backupPrivateModule;
2691 Context backupContext;
2692 Class backupThisClass;
2695 backupPrivateModule = GetPrivateModule();
2696 backupContext = GetCurrentContext();
2697 backupThisClass = GetThisClass();
2700 SetPrivateModule(codeEditor.privateModule);
2701 SetCurrentContext(codeEditor.globalContext);
2702 SetTopContext(codeEditor.globalContext);
2703 SetGlobalContext(codeEditor.globalContext);
2704 SetGlobalData(&codeEditor.globalData);
2707 exp = ParseExpressionString(wh.expression);
2709 if(exp && !GetParseError())
2711 char expString[4096];
2713 PrintExpression(exp, expString);
2716 if(codeEditor && activeFrame)
2717 DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2718 ProcessExpressionType(exp);
2720 wh.type = exp.expType;
2723 DebugComputeExpression(exp);
2724 // e.g. Meters * Degrees has no type set yet for some reason
2725 if(!wh.type && exp.expType)
2727 wh.type = exp.expType;
2728 exp.expType.refCount++;
2731 // This makes Degrees { 45 } work
2732 if(exp.type == constantExp && exp.isConstant && exp.expType && exp.expType.kind == classType &&
2733 exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == unitClass && exp.expType._class.registered.base.type == unitClass)
2735 ApplyUnitConverters(exp);
2737 else if(exp.type == instanceExp && exp.instance.data)
2739 Symbol s = exp.instance._class ? exp.instance._class.symbol : null;
2740 Class c = s ? s.registered : null;
2744 bool needClass = false;
2745 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, exp.instance.data, tmp, null, &needClass);
2748 FreeExpContents(exp);
2749 exp.type = constantExp;
2750 exp.isConstant = true;
2751 exp.constant = CopyString(s);
2755 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == bitClass)
2757 Class c = exp.expType._class.registered;
2759 bool needClass = false;
2760 Operand op = GetOperand(exp);
2762 char * (* onGetString)(void *, void *, void *, void *, void *) = (void *)c._vTbl[__ecereVMethodID_class_OnGetString];
2765 if(op.type) op.type.refCount++;
2768 case charType: s = onGetString(c, &op.c, tmp, null, &needClass); break;
2769 case shortType: s = onGetString(c, &op.s, tmp, null, &needClass); break;
2770 case intType: s = onGetString(c, &op.i, tmp, null, &needClass); break;
2771 case int64Type: s = onGetString(c, &op.i64, tmp, null, &needClass); break;
2776 FreeExpContents(exp);
2777 exp.type = constantExp;
2778 exp.isConstant = true;
2779 exp.constant = CopyString(s);
2783 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass && exp.hasAddress)
2785 Class c = exp.expType._class.registered;
2786 char structString[1024];
2787 strcpy(structString, "*(struct ");
2788 FullClassNameCat(structString, c.fullName, false);
2789 strcat(structString, " *)");
2790 strcatf(structString, "0x%p", exp.address);
2791 GDBFallBack(exp, structString);
2793 byte * data = GdbReadMemory(exp.address, c.structSize);
2797 bool needClass = false;
2798 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, data, tmp, null, &needClass);
2801 FreeExpContents(exp);
2802 exp.type = constantExp;
2803 exp.isConstant = true;
2804 exp.constant = CopyString(s);
2811 if(ExpressionIsError(exp) && exp.type != functionCallErrorExp)
2813 GDBFallBack(exp, expString);
2816 /*if(exp.hasAddress)
2818 char temp[MAX_F_STRING];
2819 sprintf(temp, "0x%x", exp.address);
2820 wh.address = CopyString(temp);
2821 // wh.address = CopyStringf("0x%x", exp.address);
2826 Type dataType = exp.expType;
2829 char temp[MAX_F_STRING];
2830 switch(dataType.kind)
2833 sprintf(temp, "%i", exp.val.c);
2836 sprintf(temp, "%i", exp.val.s);
2841 sprintf(temp, "%i", exp.val.i);
2844 sprintf(temp, "%i", exp.val.i64);
2847 sprintf(temp, "%i", exp.val.p);
2852 long v = (long)exp.val.f;
2853 sprintf(temp, "%i", v);
2858 long v = (long)exp.val.d;
2859 sprintf(temp, "%i", v);
2864 wh.intVal = CopyString(temp);
2865 switch(dataType.kind)
2868 sprintf(temp, "0x%x", exp.val.c);
2871 sprintf(temp, "0x%x", exp.val.s);
2875 sprintf(temp, "0x%x", exp.val.i);
2878 sprintf(temp, "0x%x", exp.val.i64);
2881 sprintf(temp, "0x%x", exp.val.i64);
2884 sprintf(temp, "0x%x", exp.val.p);
2889 long v = (long)exp.val.f;
2890 sprintf(temp, "0x%x", v);
2895 long v = (long)exp.val.d;
2896 sprintf(temp, "0x%x", v);
2901 wh.hexVal = CopyString(temp);
2902 switch(dataType.kind)
2905 sprintf(temp, "0o%o", exp.val.c);
2908 sprintf(temp, "0o%o", exp.val.s);
2912 sprintf(temp, "0o%o", exp.val.i);
2915 sprintf(temp, "0o%o", exp.val.i64);
2918 sprintf(temp, "0o%o", exp.val.i64);
2921 sprintf(temp, "0o%o", exp.val.p);
2926 long v = (long)exp.val.f;
2927 sprintf(temp, "0o%o", v);
2932 long v = (long)exp.val.d;
2933 sprintf(temp, "0o%o", v);
2938 wh.octVal = CopyString(temp);
2941 // WHATS THIS HERE ?
2942 if(exp.type == constantExp && exp.constant)
2943 wh.constant = CopyString(exp.constant);
2949 case symbolErrorExp:
2950 snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2952 case memberSymbolErrorExp:
2955 Expression memberExp = exp.member.exp;
2956 Identifier memberID = exp.member.member;
2957 Type type = memberExp.expType;
2960 if(type.kind == structType || type.kind == unionType)
2962 char string[1024] = "";
2964 PrintTypeNoConst(type, string, false, true);
2965 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in %s \"%s\"",
2966 memberID ? memberID.string : "", type.kind == unionType ? "union" : "struct", type.name ? type.name : string);
2970 Class _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2971 char string[1024] = "";
2975 PrintTypeNoConst(type, string, false, true);
2976 classSym = FindClass(string);
2977 _class = classSym ? classSym.registered : null;
2980 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2982 // NOTE: This should probably never happen, only classes and struct can be dereferenced, a dereferenceErrorExp should be displayed instead
2983 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in type \"%s\"", memberID ? memberID.string : "", string);
2987 // NOTE: This should probably never happen, the error causing the unresolved expression should be displayed instead
2988 snprintf(watchmsg, sizeof(watchmsg), $"Accessing member \"%s\" from unresolved expression", memberID ? memberID.string : "");
2991 case memberPropertyErrorExp:
2993 Expression memberExp = exp.member.exp;
2994 Identifier memberID = exp.member.member;
2995 Type type = memberExp.expType;
2996 Class _class = (type && memberID) ? (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null) : null;
2999 if(_class && memberID && memberID.string)
3000 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\" in class \"%s\"", memberID.string, _class.name);
3002 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\"", wh.expression);
3005 case functionCallErrorExp:
3006 if(exp.call.exp && exp.call.exp.type == identifierExp && exp.call.exp.identifier.string)
3007 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for call to \"%s\"", exp.call.exp.identifier.string);
3009 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for \"%s\"", wh.expression);
3011 case memoryErrorExp:
3012 // Need to ensure when set to memoryErrorExp, constant is set
3013 snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
3015 case dereferenceErrorExp:
3016 snprintf(watchmsg, sizeof(watchmsg), $"Dereferencing error evaluating \"%s\"", wh.expression);
3018 case divideBy0ErrorExp:
3019 snprintf(watchmsg, sizeof(watchmsg), $"Integer division by 0");
3021 case noDebuggerErrorExp:
3022 snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
3024 case unknownErrorExp:
3025 // NOTE: This should never happen
3026 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
3029 // NOTE: This should never happen
3030 snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
3033 wh.value = CopyString(exp.string);
3036 // Temporary Code for displaying Strings
3037 if((exp.expType && ((exp.expType.kind == pointerType ||
3038 exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) ||
3039 (wh.type && wh.type.kind == classType && wh.type._class &&
3040 wh.type._class.registered && wh.type._class.registered.type == normalClass &&
3041 !strcmp(wh.type._class.registered.name, "String")))
3044 if(exp.expType.kind != arrayType || exp.hasAddress)
3048 //char temp[MAX_F_STRING * 32];
3050 ExpressionType evalError = dummyExp;
3051 /*if(exp.expType.kind == arrayType)
3052 sprintf(temp, "(char*)0x%x", exp.address);
3054 sprintf(temp, "(char*)%s", exp.constant);*/
3056 //evaluation = Debugger::EvaluateExpression(temp, &evalError);
3057 // address = strtoul(exp.constant, null, 0);
3058 address = _strtoui64(exp.constant, null, 0);
3059 //_dpl(0, "0x", address);
3060 // snprintf(value, sizeof(value), "0x%08x ", address);
3062 if(address > 0xFFFFFFFFLL)
3063 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
3065 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
3066 value[sizeof(value)-1] = 0;
3069 strcat(value, $"Null string");
3072 String string = new char[4097];
3074 bool success = false;
3078 for(start = 0; !done && start + size <= 4096; start += size)
3083 // Try to read 256 bytes at a time, then half if that fails
3084 s = GdbReadMemory(address + start, size);
3088 memcpy(string + start, s, size);
3089 string[start + size] = 0;
3090 if(size == 1 || memchr(s, 0, size))
3104 int len = strlen(value);
3106 if(UTF8Validate(string))
3111 for(c = 0; (ch = string[c]); c++)
3114 value[len++] = '\0';
3119 ISO8859_1toUTF8(string, value + len, strlen(value) - len - 30);
3120 strcat(value, ") (ISO8859-1)");
3124 strcat(value, $"Empty string");
3127 strcat(value, $"Couldn't read memory");
3130 wh.value = CopyString(value);
3133 else if(wh.type && wh.type.kind == classType && wh.type._class &&
3134 wh.type._class.registered && wh.type._class.registered.type == enumClass)
3136 uint64 value = strtoul(exp.constant, null, 0);
3137 Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
3138 EnumClassData enumeration = (EnumClassData)enumClass.data;
3140 for(item = enumeration.values.first; item; item = item.next)
3141 if((int)item.data == value)
3144 wh.value = CopyString(item.name);
3146 wh.value = PrintString($"Invalid Enum Value (", value, ")");
3149 else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class &&
3150 wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
3157 if(exp.constant[0] == '\'')
3159 if((int)((byte *)exp.constant)[1] > 127)
3162 value = UTF8GetChar(exp.constant + 1, &nb);
3163 if(nb < 2) value = exp.constant[1];
3164 signedValue = value;
3168 signedValue = exp.constant[1];
3170 // Precomp Syntax error with boot strap here:
3171 byte b = (byte)(char)signedValue;
3172 value = (unichar) b;
3178 if(wh.type.kind == charType && wh.type.isSigned)
3180 signedValue = (int)(char)strtol(exp.constant, null, 0);
3182 // Precomp Syntax error with boot strap here:
3183 byte b = (byte)(char)signedValue;
3184 value = (unichar) b;
3189 value = (uint)strtoul(exp.constant, null, 0);
3190 signedValue = (int)value;
3194 UTF32toUTF8Len(&value, 1, charString, 5);
3196 snprintf(string, sizeof(string), "\'\\0' (0)");
3197 else if(value == '\t')
3198 snprintf(string, sizeof(string), "\'\\t' (%d)", value);
3199 else if(value == '\n')
3200 snprintf(string, sizeof(string), "\'\\n' (%d)", value);
3201 else if(value == '\r')
3202 snprintf(string, sizeof(string), "\'\\r' (%d)", value);
3203 else if(wh.type.kind == charType && wh.type.isSigned)
3204 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
3205 else if(value > 256 || wh.type.kind != charType)
3207 if(value > 0x10FFFF || !GetCharCategory(value))
3208 snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3210 snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3213 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3214 string[sizeof(string)-1] = 0;
3216 wh.value = CopyString(string);
3221 wh.value = CopyString(exp.constant);
3227 bool genericError = true;
3228 if(exp.expType && exp.hasAddress)
3230 bool showAddress = false;
3231 switch(exp.expType.kind)
3243 Symbol s = exp.expType._class;
3246 Class c = s.registered;
3247 if(c.type == noHeadClass || c.type == normalClass)
3255 wh.value = PrintHexUInt64(exp.address);
3257 genericError = false;
3261 // NOTE: This should ideally be handled with a specific error message
3262 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
3268 snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3269 if(exp) FreeExpression(exp);
3272 SetPrivateModule(backupPrivateModule);
3273 SetCurrentContext(backupContext);
3274 SetTopContext(backupContext);
3275 SetGlobalContext(backupContext);
3276 SetThisClass(backupThisClass);
3279 // wh.value = CopyString("No source file found for selected frame");
3281 watchmsg[sizeof(watchmsg)-1] = 0;
3283 wh.value = CopyString(watchmsg);
3285 ide.watchesView.UpdateWatch(wh);
3289 void EvaluateWatches()
3291 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3292 WatchesLinkCodeEditor();
3293 if(state == stopped)
3295 for(wh : ide.workspace.watches)
3300 char * ::GdbEvaluateExpression(char * expression)
3302 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3305 GdbCommand(0, false, "-data-evaluate-expression \"%s\"", expression);
3307 ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3311 // to be removed... use GdbReadMemory that returns a byte array instead
3312 char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3314 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3319 _dpl(0, "GdbReadMemoryString called with size = 0!");
3321 // GdbCommand(0, false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3322 if(GetRuntimePlatform() == win32)
3323 GdbCommand(0, false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3325 GdbCommand(0, false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3327 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3331 byte * ::GdbReadMemory(uint64 address, int bytes)
3333 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3336 //GdbCommand(0, false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3337 if(GetRuntimePlatform() == win32)
3338 GdbCommand(0, false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3340 GdbCommand(0, false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3343 _dpl(0, "GdbReadMemory called with bytes = 0!");
3346 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3347 else if(eval.result && strcmp(eval.result, "N/A"))
3349 byte * result = new byte[bytes];
3350 char * string = eval.result;
3354 result[c++] = (byte)strtol(string, &string, 10);
3371 bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3374 char * s1 = null; char * s2 = null;
3375 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3376 "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3377 "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3378 "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3379 "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor, ")");
3380 delete s1; delete s2;
3384 bool conditionMet = true;
3385 if(bpUser.condition)
3387 if(WatchesLinkCodeEditor())
3388 conditionMet = ResolveWatch(bpUser.condition);
3390 conditionMet = false;
3405 if(stopItem.frame.line && bpUser.line != stopItem.frame.line)
3407 // updating user breakpoint on hit location difference
3408 // todo, print something?
3409 bpUser.line = stopItem.frame.line;
3410 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3411 ide.workspace.Save();
3414 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3419 if(bpInternal.type == internalModulesLoaded)
3421 if(userAction == stepOver)
3423 if((bpInternal.type == internalEntry && ((intBpMain && intBpMain.inserted) || (intBpWinMain && intBpWinMain.inserted))) ||
3424 (bpInternal.type == internalMain && intBpWinMain && intBpWinMain.inserted))
3427 if(!bpUser && !userAction.breaksOnInternalBreakpoint)
3429 if(userAction == stepOut)
3430 StepOut(ignoreBreakpoints);
3436 if(!bpUser && !bpInternal)
3442 void ValgrindTargetThreadExit()
3444 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValgrindTargetThreadExit()");
3447 vgTargetHandle.Wait();
3448 delete vgTargetHandle;
3450 HandleExit(null, null);
3453 void GdbThreadExit()
3455 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3456 if(state != terminated)
3458 _ChangeState(terminated);
3459 targetProcessId = 0;
3460 ClearBreakDisplay();
3465 serialSemaphore.Release();
3470 ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3471 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3473 HideDebuggerViews();
3475 //_ChangeState(terminated);
3479 void GdbThreadMain(char * output)
3483 Array<char *> outTokens { minAllocSize = 50 };
3484 Array<char *> subTokens { minAllocSize = 50 };
3485 DebugListItem item { };
3486 DebugListItem item2 { };
3487 bool setWaitingForPID = false;
3489 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3490 #ifdef GDB_DEBUG_CONSOLE
3491 // _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3494 #ifdef GDB_DEBUG_OUTPUT
3496 int len = strlen(output);
3504 for(c = 0; c < len / 1024; c++)
3506 strncpy(tmp, start, 1024);
3507 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3510 ide.outputView.gdbBox.Logf("out: %s\n", start);
3514 ide.outputView.gdbBox.Logf("out: %s\n", output);
3518 #ifdef GDB_DEBUG_CONSOLE
3519 strcpy(lastGdbOutput, output);
3521 #ifdef GDB_DEBUG_GUI
3522 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3529 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3532 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3535 if(!entryPoint && (t = strstr(output, "Entry point:")))
3537 char * addr = t + strlen("Entry point:");
3539 if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3542 while(isxdigit(*++t));
3544 for(bp : sysBPs; bp.type == internalEntry)
3547 bp.enabled = entryPoint = true;
3555 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3557 //if(outTokens.count == 1)
3562 _ChangeState(loaded);
3563 targetProcessId = 0;
3564 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3566 if(!strcmp(item.name, "reason"))
3568 char * reason = item.value;
3569 StripQuotes(reason, reason);
3570 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3573 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3575 StripQuotes(item2.value, item2.value);
3576 if(!strcmp(item2.name, "exit-code"))
3577 exitCode = item2.value;
3583 HandleExit(reason, exitCode);
3587 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3590 HandleExit(null, null);
3593 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3595 if(!strcmp(item.name, "bkpt"))
3597 sentBreakInsert = false;
3603 bpItem = ParseBreakpoint(item.value, outTokens);
3604 //breakType = bpValidation;
3606 else if(!strcmp(item.name, "depth"))
3608 StripQuotes(item.value, item.value);
3609 frameCount = atoi(item.value);
3611 stackFrames.Free(Frame::Free);
3613 else if(!strcmp(item.name, "stack"))
3616 if(stackFrames.count)
3617 ide.callStackView.Logf("...\n");
3620 item.value = StripBrackets(item.value);
3621 TokenizeList(item.value, ',', subTokens);
3622 for(i = 0; i < subTokens.count; i++)
3624 if(TokenizeListItem(subTokens[i], item))
3626 if(!strcmp(item.name, "frame"))
3629 stackFrames.Add(frame);
3630 item.value = StripCurlies(item.value);
3631 ParseFrame(frame, item.value);
3632 if(frame.file && frame.from)
3633 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3637 if(activeFrameLevel == -1)
3639 if(ide.projectView.IsModuleInProject(frame.file));
3641 if(frame.level != 0)
3643 //stopItem.frame = frame;
3644 breakType = selectFrame;
3647 activeFrame = frame;
3648 activeFrameLevel = frame.level;
3651 ide.callStackView.Logf("%3d ", frame.level);
3652 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3653 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3654 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3655 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3656 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3657 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3658 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3659 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3661 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3666 ide.callStackView.Logf("%3d ", frame.level);
3671 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3675 ide.callStackView.Logf("%s\n", frame.func);
3677 ide.callStackView.Logf($"unknown source\n");
3681 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3684 if(activeFrameLevel == -1)
3686 activeFrameLevel = 0;
3687 activeFrame = stackFrames.first;
3689 ide.callStackView.Home();
3691 subTokens.RemoveAll();
3693 /*else if(!strcmp(item.name, "frame"))
3696 item.value = StripCurlies(item.value);
3697 ParseFrame(&frame, item.value);
3699 else if(!strcmp(item.name, "thread-ids"))
3701 ide.threadsView.Clear();
3702 item.value = StripCurlies(item.value);
3703 TokenizeList(item.value, ',', subTokens);
3704 for(i = subTokens.count - 1; ; i--)
3706 if(TokenizeListItem(subTokens[i], item))
3708 if(!strcmp(item.name, "thread-id"))
3711 StripQuotes(item.value, item.value);
3712 value = atoi(item.value);
3713 ide.threadsView.Logf("%3d \n", value);
3716 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3721 ide.threadsView.Home();
3723 subTokens.RemoveAll();
3724 //if(!strcmp(outTokens[2], "number-of-threads"))
3726 else if(!strcmp(item.name, "new-thread-id"))
3728 StripQuotes(item.value, item.value);
3729 activeThread = atoi(item.value);
3731 else if(!strcmp(item.name, "value"))
3733 StripQuotes(item.value, item.value);
3734 eval.result = CopyString(item.value);
3735 eval.active = false;
3737 else if(!strcmp(item.name, "addr"))
3739 for(i = 2; i < outTokens.count; i++)
3741 if(TokenizeListItem(outTokens[i], item))
3743 if(!strcmp(item.name, "total-bytes"))
3745 StripQuotes(item.value, item.value);
3746 eval.bytes = atoi(item.value);
3748 else if(!strcmp(item.name, "next-row"))
3750 StripQuotes(item.value, item.value);
3751 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3753 else if(!strcmp(item.name, "memory"))
3757 //StripQuotes(item.value, item.value);
3758 item.value = StripBrackets(item.value);
3759 // this should be treated as a list...
3760 item.value = StripCurlies(item.value);
3761 TokenizeList(item.value, ',', subTokens);
3762 for(j = 0; j < subTokens.count; j++)
3764 if(TokenizeListItem(subTokens[j], item))
3766 if(!strcmp(item.name, "data"))
3768 item.value = StripBrackets(item.value);
3769 StripQuotes2(item.value, item.value);
3770 eval.result = CopyString(item.value);
3771 eval.active = false;
3775 subTokens.RemoveAll();
3780 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3781 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3783 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3786 else if(!strcmp(outTokens[0], "^running"))
3788 waitingForPID = true;
3789 setWaitingForPID = true;
3790 ClearBreakDisplay();
3792 else if(!strcmp(outTokens[0], "^exit"))
3794 _ChangeState(terminated);
3795 // ide.outputView.debugBox.Logf("Exit\n");
3796 // ide.Update(null);
3798 serialSemaphore.Release();
3800 else if(!strcmp(outTokens[0], "^error"))
3804 sentBreakInsert = false;
3805 breakpointError = true;
3808 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3810 if(!strcmp(item.name, "msg"))
3812 StripQuotes(item.value, item.value);
3815 eval.active = false;
3817 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3818 eval.error = symbolNotFound;
3819 else if(strstr(item.value, "Cannot access memory at address"))
3820 eval.error = memoryCantBeRead;
3822 eval.error = unknown;
3824 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3827 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3830 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3832 _ChangeState(stopped);
3833 gdbHandle.Printf("-exec-continue\n");
3835 else if(!strcmp(item.value, "ptrace: No such process."))
3837 _ChangeState(loaded);
3838 targetProcessId = 0;
3840 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3843 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3845 _ChangeState(loaded);
3846 targetProcessId = 0;
3848 else if(strstr(item.value, "No such file or directory."))
3850 _ChangeState(loaded);
3851 targetProcessId = 0;
3853 else if(strstr(item.value, "During startup program exited with code "))
3855 _ChangeState(loaded);
3856 targetProcessId = 0;
3861 if(strlen(item.value) < MAX_F_STRING)
3864 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3868 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3874 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3877 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3878 outTokens.RemoveAll();
3881 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3884 if(TokenizeList(output, ',', outTokens))
3886 if(!strcmp(outTokens[0], "=library-loaded"))
3887 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, false);
3888 else if(!strcmp(outTokens[0], "=shlibs-added"))
3889 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, true);
3890 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3891 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3892 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3893 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3894 !strcmp(outTokens[0], "=breakpoint-modified"))
3895 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3896 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3897 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3898 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3899 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3901 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3903 outTokens.RemoveAll();
3907 if(TokenizeList(output, ',', outTokens))
3909 if(!strcmp(outTokens[0],"*running"))
3911 waitingForPID = true;
3912 setWaitingForPID = true;
3914 else if(!strcmp(outTokens[0], "*stopped"))
3917 _ChangeState(stopped);
3919 for(tk = 1; tk < outTokens.count; tk++)
3921 if(TokenizeListItem(outTokens[tk], item))
3923 if(!strcmp(item.name, "reason"))
3925 char * reason = item.value;
3926 StripQuotes(reason, reason);
3927 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3930 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3933 StripQuotes(item2.value, item2.value);
3934 if(!strcmp(item2.name, "exit-code"))
3935 exitCode = item2.value;
3941 HandleExit(reason, exitCode);
3944 else if(!strcmp(reason, "breakpoint-hit") ||
3945 !strcmp(reason, "function-finished") ||
3946 !strcmp(reason, "end-stepping-range") ||
3947 !strcmp(reason, "location-reached") ||
3948 !strcmp(reason, "signal-received"))
3952 if(stopItem) _dpl(0, "problem");
3954 stopItem = GdbDataStop { };
3955 stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3957 for(i = tk+1; i < outTokens.count; i++)
3959 TokenizeListItem(outTokens[i], item);
3960 StripQuotes(item.value, item.value);
3961 if(!strcmp(item.name, "thread-id"))
3962 stopItem.threadid = atoi(item.value);
3963 else if(!strcmp(item.name, "frame"))
3965 item.value = StripCurlies(item.value);
3966 ParseFrame(stopItem.frame, item.value);
3968 else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3969 stopItem.bkptno = atoi(item.value);
3970 else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3971 stopItem.gdbResultVar = CopyString(item.value);
3972 else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3973 stopItem.returnValue = CopyString(item.value);
3974 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3975 stopItem.name = CopyString(item.value);
3976 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3977 stopItem.meaning = CopyString(item.value);
3978 else if(!strcmp(item.name, "stopped-threads"))
3979 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3980 else if(!strcmp(item.name, "core"))
3981 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3982 else if(!strcmp(item.name, "disp"))
3983 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3985 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3988 if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
4004 event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
4008 else if(!strcmp(reason, "watchpoint-trigger"))
4009 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
4010 else if(!strcmp(reason, "read-watchpoint-trigger"))
4011 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
4012 else if(!strcmp(reason, "access-watchpoint-trigger"))
4013 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
4014 else if(!strcmp(reason, "watchpoint-scope"))
4015 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
4017 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
4025 if(usingValgrind && event == none && !stopItem)
4026 event = valgrindStartPause;
4031 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
4032 outTokens.RemoveAll();
4035 if(!strcmpi(output, "(gdb) "))
4039 Time startTime = GetTime();
4040 char exeFile[MAX_LOCATION];
4041 int oldProcessID = targetProcessId;
4042 GetLastDirectory(targetFile, exeFile);
4044 while(!targetProcessId/*true*/)
4046 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
4047 if(targetProcessId) break;
4048 // Can't break on Peek(), GDB is giving us =library and other info before the process is listed in /proc
4049 // if(gdbHandle.Peek()) break;
4051 if(gdbHandle.Peek() && GetTime() - startTime > 2.5) // Give the process 2.5 seconds to show up in /proc
4056 _ChangeState(running);
4057 else if(!oldProcessID)
4059 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
4060 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
4061 gdbHandle.Printf("-gdb-exit\n");
4063 _ChangeState(terminated); //loaded;
4068 for(bp : ide.workspace.breakpoints)
4069 bp.inserted = false;
4072 bp.inserted = false;
4074 bpRunToCursor.inserted = false;
4076 ide.outputView.debugBox.Logf($"Debugging stopped\n");
4077 ClearBreakDisplay();
4079 #if defined(__unix__)
4080 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
4082 progThread.terminate = true;
4085 fifoFile.CloseInput();
4092 DeleteFile(progFifoPath);
4093 progFifoPath[0] = '\0';
4100 serialSemaphore.Release();
4103 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
4107 if(!strncmp(output, "&\"warning:", 10))
4110 content = strstr(output, "\"");
4111 StripQuotes(content, content);
4112 content = strstr(content, ":");
4118 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
4125 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
4127 if(!setWaitingForPID)
4128 waitingForPID = false;
4129 setWaitingForPID = false;
4137 // From GDB Output functions
4138 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens, bool shlibs)
4140 char path[MAX_LOCATION] = "";
4141 char file[MAX_FILENAME] = "";
4142 bool symbolsLoaded = false;
4143 DebugListItem item { };
4144 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
4145 for(token : outTokens)
4147 if(TokenizeListItem(token, item))
4149 if(!strcmp(item.name, "target-name"))
4151 StripQuotes(item.value, path);
4152 MakeSystemPath(path);
4153 GetLastDirectory(path, file);
4155 else if(!strcmp(item.name, "symbols-loaded"))
4157 symbolsLoaded = (atoi(item.value) == 1);
4159 else if(!strcmp(item.name, "shlib-info"))
4161 DebugListItem subItem { };
4162 Array<char *> tokens { minAllocSize = 50 };
4163 item.value = StripBrackets(item.value);
4164 TokenizeList(item.value, ',', tokens);
4167 if(TokenizeListItem(t, subItem))
4169 if(!strcmp(subItem.name, "path"))
4171 StripQuotes(subItem.value, path);
4172 MakeSystemPath(path);
4173 GetLastDirectory(path, file);
4174 symbolsLoaded = true;
4185 if(path[0] && file[0])
4187 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
4191 char prjTargetPath[MAX_LOCATION];
4192 char prjTargetFile[MAX_FILENAME];
4193 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
4194 strcpy(prjTargetPath, prj.topNode.path);
4195 PathCat(prjTargetPath, targetDirExp.dir);
4196 delete targetDirExp;
4197 prjTargetFile[0] = '\0';
4198 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
4199 PathCat(prjTargetPath, prjTargetFile);
4200 MakeSystemPath(prjTargetPath);
4202 match = !fstrcmp(prjTargetFile, file);
4203 if(!match && (dot = strstr(prjTargetFile, ".so.")))
4205 char * dot3 = strstr(dot+4, ".");
4209 match = !fstrcmp(prjTargetFile, file);
4214 match = !fstrcmp(prjTargetFile, file);
4219 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
4220 /* -- this is disabled because we can't trust gdb's symbols-loaded="0" field for =library-loaded (http://sourceware.org/bugzilla/show_bug.cgi?id=10693)
4222 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
4224 match = !fstrcmp(prjTargetPath, path);
4225 if(!match && (dot = strstr(prjTargetPath, ".so.")))
4227 char * dot3 = strstr(dot+4, ".");
4231 match = !fstrcmp(prjTargetPath, path);
4236 match = !fstrcmp(prjTargetPath, path);
4240 projectsLibraryLoaded[prj.name] = true;
4242 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4249 void FGOBreakpointModified(Array<char *> outTokens)
4251 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4253 DebugListItem item { };
4254 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4256 if(!strcmp(item.name, "bkpt"))
4258 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4266 ExpressionType ::DebugEvalExpTypeError(char * result)
4268 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4273 case symbolNotFound:
4274 return symbolErrorExp;
4275 case memoryCantBeRead:
4276 return memoryErrorExp;
4278 return unknownErrorExp;
4281 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4284 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4285 if(ide.projectView && ide.debugger.state == stopped)
4287 result = GdbEvaluateExpression(expression);
4288 *error = DebugEvalExpTypeError(result);
4293 *error = noDebuggerErrorExp;
4298 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4301 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4302 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4303 if(!result || !strcmp(result, "N/A"))
4304 *error = memoryErrorExp;
4306 *error = DebugEvalExpTypeError(result);
4311 class ValgrindLogThread : Thread
4317 static char output[4096];
4318 bool lastLineEmpty = true;
4319 Array<char> dynamicBuffer { minAllocSize = 4096 };
4320 File oldValgrindHandle = vgLogFile;
4321 incref oldValgrindHandle;
4324 while(debugger.state != terminated && vgLogFile && vgLogFile.input)
4329 result = vgLogFile.Read(output, 1, sizeof(output));
4331 if(debugger.state == terminated || !vgLogFile) // || vgLogFile.Eof()
4338 for(c = 0; c<result; c++)
4340 if(output[c] == '\n')
4342 int pos = dynamicBuffer.size;
4343 dynamicBuffer.size += c - start;
4344 memcpy(&dynamicBuffer[pos], output + start, c - start);
4345 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4346 dynamicBuffer.size++;
4347 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4349 // printf("%s\n", dynamicBuffer.array);
4351 if(strstr(&dynamicBuffer[0], "vgdb me ..."))
4352 debugger.serialSemaphore.Release();
4354 char * s = strstr(&dynamicBuffer[0], "==");
4356 s = strstr(s+2, "== ");
4360 if(s[0] == '\0' && !lastLineEmpty)
4363 lastLineEmpty = true;
4364 dynamicBuffer[0] = '\0';
4373 if(strstr(s, "vgdb me ..."))
4375 if(strstr(s, "(action on error) vgdb me ..."))
4376 ide.outputView.debugBox.Logf($"...breaked on Valgrind error (F5 to resume)\n");
4383 if(strstr(s, "TO DEBUG THIS PROCESS USING GDB: start GDB like this"))
4389 if(strstr(s, "and then give GDB the following command"))
4395 if(strstr(s, "/path/to/gdb") || strstr(s, "target remote | /usr/lib/valgrind/../../bin/vgdb --pid="))
4401 if(strstr(s, "--pid is optional if only one valgrind process is running"))
4407 if((s = strstr(s, "; rerun with -h for copyright info")))
4419 if(lastLineEmpty && t[0] != '\0')
4420 lastLineEmpty = false;
4423 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4425 dynamicBuffer.size = 0;
4431 int pos = dynamicBuffer.size;
4432 dynamicBuffer.size += c - start;
4433 memcpy(&dynamicBuffer[pos], output + start, c - start);
4436 else if(debugger.state == stopped)
4439 printf("Got end of file from GDB!\n");
4446 delete dynamicBuffer;
4447 _dpl2(_dpct, dplchan::debuggerCall, 0, "ValgrindLogThreadExit");
4448 //if(oldValgrindHandle == vgLogFile)
4449 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4450 delete oldValgrindHandle;
4456 class ValgrindTargetThread : Thread
4462 static char output[4096];
4463 Array<char> dynamicBuffer { minAllocSize = 4096 };
4464 DualPipe oldValgrindHandle = vgTargetHandle;
4465 incref oldValgrindHandle;
4468 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4472 result = vgTargetHandle.Read(output, 1, sizeof(output));
4474 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4481 for(c = 0; c<result; c++)
4483 if(output[c] == '\n')
4485 int pos = dynamicBuffer.size;
4486 dynamicBuffer.size += c - start;
4487 memcpy(&dynamicBuffer[pos], output + start, c - start);
4488 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4489 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4490 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4491 dynamicBuffer.size++;
4492 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4494 // printf("%s\n", dynamicBuffer.array);
4496 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4498 dynamicBuffer.size = 0;
4504 int pos = dynamicBuffer.size;
4505 dynamicBuffer.size += c - start;
4506 memcpy(&dynamicBuffer[pos], output + start, c - start);
4512 printf("Got end of file from GDB!\n");
4516 delete dynamicBuffer;
4517 //if(oldValgrindHandle == vgTargetHandle)
4518 debugger.ValgrindTargetThreadExit();
4519 delete oldValgrindHandle;
4525 class GdbThread : Thread
4531 static char output[4096];
4532 Array<char> dynamicBuffer { minAllocSize = 4096 };
4533 DualPipe oldGdbHandle = gdbHandle;
4534 incref oldGdbHandle;
4537 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4541 result = gdbHandle.Read(output, 1, sizeof(output));
4543 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4550 for(c = 0; c<result; c++)
4552 if(output[c] == '\n')
4554 int pos = dynamicBuffer.size;
4555 dynamicBuffer.size += c - start;
4556 memcpy(&dynamicBuffer[pos], output + start, c - start);
4557 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4558 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4559 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4560 dynamicBuffer.size++;
4561 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4563 // _dpl(0, dynamicBuffer.array);
4565 debugger.GdbThreadMain(&dynamicBuffer[0]);
4566 dynamicBuffer.size = 0;
4572 int pos = dynamicBuffer.size;
4573 dynamicBuffer.size += c - start;
4574 memcpy(&dynamicBuffer[pos], output + start, c - start);
4580 _dpl(0, "Got end of file from GDB!");
4584 delete dynamicBuffer;
4585 //if(oldGdbHandle == gdbHandle)
4586 debugger.GdbThreadExit();
4587 delete oldGdbHandle;
4593 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4594 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4596 #if defined(__unix__)
4601 #include <sys/types.h>
4606 class ProgramThread : Thread
4612 bool fileCreated = false;
4614 static char output[1000];
4617 /*if(!mkfifo(progFifoPath, mask))
4624 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4628 if(FileExists(progFifoPath)) //fileCreated)
4630 fifoFile = FileOpen(progFifoPath, read);
4634 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4639 fd = fileno((FILE *)fifoFile.input);
4640 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4644 while(!terminate && fifoFile && !fifoFile.Eof())
4647 struct timeval time;
4655 selectResult = select(fd + 1, &rs, null, null, &time);
4656 if(FD_ISSET(fd, &rs))
4658 int result = (int)read(fd, output, sizeof(output)-1);
4659 if(!result || (result < 0 && errno != EAGAIN))
4663 output[result] = '\0';
4664 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4667 ide.outputView.debugBox.Log(output);
4676 //fifoFile.CloseInput();
4679 ide.outputView.debugBox.Log("\n");
4683 if(FileExists(progFifoPath)) //fileCreated)
4685 DeleteFile(progFifoPath);
4686 progFifoPath[0] = '\0';
4694 class Argument : struct
4696 Argument prev, next;
4698 property char * name { set { delete name; if(value) name = CopyString(value); } }
4700 property char * val { set { delete val; if(value) val = CopyString(value); } }
4714 class Frame : struct
4719 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4721 property char * func { set { delete func; if(value) func = CopyString(value); } }
4725 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4727 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4728 char * absoluteFile;
4729 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4738 delete absoluteFile;
4739 args.Free(Argument::Free);
4748 class GdbDataStop : struct
4750 DebuggerReason reason;
4765 char * gdbResultVar;
4775 if(reason == signalReceived)
4780 else if(reason == functionFinished)
4782 delete gdbResultVar;
4786 if(frame) frame.Free();
4795 class GdbDataBreakpoint : struct
4799 property char * number { set { delete number; if(value) number = CopyString(value); } }
4801 property char * type { set { delete type; if(value) type = CopyString(value); } }
4803 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4806 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4808 property char * func { set { delete func; if(value) func = CopyString(value); } }
4810 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4812 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4815 property char * at { set { delete at; if(value) at = CopyString(value); } }
4818 Array<GdbDataBreakpoint> multipleBPs;
4823 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4834 if(multipleBPs) multipleBPs.Free();
4840 ~GdbDataBreakpoint()
4846 class Breakpoint : struct
4851 property const char * function { set { delete function; if(value) function = CopyString(value); } }
4852 char * relativeFilePath;
4853 property const char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4854 char * absoluteFilePath;
4855 property const char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4857 property const char * location { set { delete location; if(value) location = CopyString(value); } }
4866 BreakpointType type;
4868 GdbDataBreakpoint bp;
4871 property const char * address { set { delete address; if(value) address = CopyString(value); } }
4873 void ParseLocation()
4875 char * prjName = null;
4876 char * filePath = null;
4879 char fullPath[MAX_LOCATION];
4880 if(location[0] == '(' && location[1] && (file = strchr(location+2, ')')) && file[1])
4882 prjName = new char[file-location];
4883 strncpy(prjName, location+1, file-location-1);
4884 prjName[file-location-1] = '\0';
4889 if((line = strchr(file+1, ':')))
4891 filePath = new char[strlen(file)+1];
4892 strncpy(filePath, file, line-file);
4893 filePath[line-file] = '\0';
4897 filePath = CopyString(file);
4898 property::relativeFilePath = filePath;
4901 for(prj : ide.workspace.projects)
4903 if(!strcmp(prjName, prj.name))
4905 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4907 property::absoluteFilePath = fullPath;
4914 this.line = atoi(line);
4918 Project prj = ide.project;
4919 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4921 property::absoluteFilePath = fullPath;
4925 if(!absoluteFilePath)
4926 property::absoluteFilePath = "";
4931 char * CopyLocationString(bool removePath)
4934 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4935 bool removingPath = removePath && file;
4938 char * fileName = new char[MAX_FILENAME];
4939 GetLastDirectory(file, fileName);
4945 location = PrintString(file, ":", function);
4947 location = CopyString(function);
4950 location = PrintString(file, ":", line);
4956 char * CopyUserLocationString()
4959 char * loc = CopyLocationString(false);
4961 if(absoluteFilePath)
4963 for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4965 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4974 location = PrintString("(", prj.name, ")", loc);
4984 if(relativeFilePath && relativeFilePath[0])
4986 char * location = CopyUserLocationString();
4987 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4990 f.Printf(" ~ %s\n", condition.expression);
4998 delete relativeFilePath;
4999 delete absoluteFilePath;
5019 class Watch : struct
5030 f.Printf(" ~ %s\n", expression);
5054 class DebugListItem : struct
5060 struct DebugEvaluationData
5065 uint64 nextBlockAddress;
5067 DebuggerEvaluationError error;
5070 class CodeLocation : struct
5073 char * absoluteFile;
5076 CodeLocation ::ParseCodeLocation(char * location)
5080 char * colon = null;
5082 char loc[MAX_LOCATION];
5083 strcpy(loc, location);
5084 for(temp = loc; temp = strstr(temp, ":"); temp++)
5092 int line = atoi(colon);
5095 CodeLocation codloc { line = line };
5096 codloc.file = CopyString(loc);
5097 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
5109 delete absoluteFile;
5118 void GDBFallBack(Expression exp, String expString)
5121 ExpressionType evalError = dummyExp;
5122 result = Debugger::EvaluateExpression(expString, &evalError);
5125 FreeExpContents(exp);
5126 exp.constant = result;
5127 exp.type = constantExp;
5131 static Project WorkspaceGetFileOwner(const char * absolutePath)
5133 Project owner = null;
5134 for(prj : ide.workspace.projects)
5136 if(prj.topNode.FindByFullPath(absolutePath, false))
5143 WorkspaceGetObjectFileNode(absolutePath, &owner);
5147 static ProjectNode WorkspaceGetObjectFileNode(const char * filePath, Project * project)
5149 ProjectNode node = null;
5150 char ext[MAX_EXTENSION];
5151 GetExtension(filePath, ext);
5154 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5157 char fileName[MAX_FILENAME];
5158 GetLastDirectory(filePath, fileName);
5161 DotMain dotMain = DotMain::FromFileName(fileName);
5162 for(prj : ide.workspace.projects)
5164 if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
5177 static ProjectNode ProjectGetObjectFileNode(Project project, const char * filePath)
5179 ProjectNode node = null;
5180 char ext[MAX_EXTENSION];
5181 GetExtension(filePath, ext);
5184 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5187 char fileName[MAX_FILENAME];
5188 GetLastDirectory(filePath, fileName);
5191 DotMain dotMain = DotMain::FromFileName(fileName);
5192 node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
5199 static void WorkspaceGetRelativePath(const char * absolutePath, char * relativePath, Project * owner)
5201 Project prj = WorkspaceGetFileOwner(absolutePath);
5205 prj = ide.workspace.projects.firstIterator.data;
5208 MakePathRelative(absolutePath, prj.topNode.path, relativePath);
5209 MakeSlashPath(relativePath);
5212 relativePath[0] = '\0';
5215 static bool ProjectGetAbsoluteFromRelativePath(Project project, const char * relativePath, char * absolutePath)
5217 ProjectNode node = project.topNode.FindWithPath(relativePath, false);
5219 node = ProjectGetObjectFileNode(project, relativePath);
5222 strcpy(absolutePath, node.project.topNode.path);
5223 PathCat(absolutePath, relativePath);
5224 MakeSlashPath(absolutePath);
5226 return node != null;