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];
98 char * time = PrintNow();
100 //ide.outputView.debugBox.Logf();
101 Logf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
102 va_start(args, object);
103 PrintStdArgsToBuffer(string, sizeof(string), object, args);
111 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
112 static void __dpl(const char * file, int line, int indent, const char * format, ...)
115 char string[MAX_F_STRING];
117 char * time = PrintNow();
118 //static File f = null;
119 va_start(args, format);
120 vsnprintf(string, sizeof(string), format, args);
121 string[sizeof(string)-1] = 0;
124 char * time = PrintNow();
126 logName = PrintString(time, ".log");
128 f = FileOpen(logName, write);
131 /*f.Printf("%s %s:% 5d: ", time, file, line);
132 for(c = 0; c<indent; c++)
134 f.Printf("%s\n", string);*/
135 Logf("%s %s:% 5d: ", time, file, line);
136 for(c = 0; c<indent; c++)
138 Logf("%s\n", string);
143 public char * StripQuotes2(char * string, char * output)
147 bool quoted = false, escaped = false;
149 for(c = 0; (ch = string[c]); c++)
153 if(escaped || ch != '\"')
156 escaped = !escaped && ch == '\\';
170 // String Escape Copy
171 static void strescpy(char * d, char * s)
179 case '\n': d[k] = '\\'; d[++k] = 'n'; break;
180 case '\t': d[k] = '\\'; d[++k] = 't'; break;
181 case '\a': d[k] = '\\'; d[++k] = 'a'; break;
182 case '\b': d[k] = '\\'; d[++k] = 'b'; break;
183 case '\f': d[k] = '\\'; d[++k] = 'f'; break;
184 case '\r': d[k] = '\\'; d[++k] = 'r'; break;
185 case '\v': d[k] = '\\'; d[++k] = 'v'; break;
186 case '\\': d[k] = '\\'; d[++k] = '\\'; break;
187 case '\"': d[k] = '\\'; d[++k] = '\"'; break;
188 default: d[k] = s[j];
195 /*static char * CopyUnescapedSystemPath(char * p)
198 char * d = new char[len + 1];
199 UnescapeString(d, p, len);
200 #if defined(__WIN32__)
201 ChangeCh(d, '/', '\\');
206 static char * CopyUnescapedUnixPath(char * p)
209 char * d = new char[len + 1];
210 UnescapeString(d, p, len);
211 #if defined(__WIN32__)
212 ChangeCh(d, '\\', '/');
217 static char * CopyUnescapedString(char * s)
220 char * d = new char[len + 1];
221 UnescapeString(d, s, len);
225 static char * StripBrackets(char * string)
227 int length = strlen(string);
228 if(length > 1 && *string == '[' && string[length - 1] == ']')
231 string[length - 1] = '\0';
238 static char * StripCurlies(char * string)
240 int length = strlen(string);
241 if(length > 1 && *string == '{' && string[length - 1] == '}')
244 string[length - 1] = '\0';
251 /*static int StringGetInt(char * string, int start)
254 int i, len = strlen(string);
256 for(i = start; i < len && i < start + 8; i++)
258 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')
259 strncat(number, &string[i], 1);
266 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
270 bool quoted = false, escaped = false;
271 char * start = string, ch;
273 for(; (ch = *string); string++)
280 if(escaped || ch != '\"')
281 escaped = !escaped && ch == '\\';
287 else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
289 else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
291 else if(ch == seperator && !level)
300 //tokens[count] = start;
301 //tokens[count++] = start;
308 static bool TokenizeListItem(char * string, DebugListItem item)
310 char * equal = strstr(string, "=");
322 static bool CheckCommandAvailable(const char * command)
324 bool available = false;
326 char * name = new char[MAX_FILENAME];
327 char * pathVar = new char[maxPathLen];
329 GetEnvironment("PATH", pathVar, maxPathLen);
330 count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
331 strcpy(name, command);
335 const char * extensions[] = { "exe", "com", "bat", null };
336 for(e=0; extensions[e]; e++)
338 ChangeExtension(name, extensions[e], name);
340 for(c=0; c<count; c++)
342 FileListing fl { paths[c] };
345 if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
364 // define GdbGetLineSize = 1638400;
365 define GdbGetLineSize = 5638400;
366 #if defined(__unix__)
367 char progFifoPath[MAX_LOCATION];
368 char progFifoDir[MAX_LOCATION];
371 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
374 none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause, locationReached;
376 property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd || this == locationReached); } };
378 enum DebuggerAction { none, internal, restart, stop, selectFrame, advance }; //, bpValidation
381 unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit, locationReached
382 //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope,
383 //exited, exitedNormally, exitedSignalled;
387 none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad, internalEntry;
389 property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad || this == internalEntry); } };
390 property bool isUser { get { return (this == user || this == runToCursor); } };
392 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
393 enum DebuggerUserAction
395 none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepUntil, stepOut, runToCursor;
396 property bool breaksOnInternalBreakpoint { get { return (this == stepInto || this == stepOver || this == stepUntil); } };
400 none, run, _continue, next, until, advance, step, finish;
401 property bool suspendInternalBreakpoints { get { return (this == until || this == advance || this == step || this == finish); } };
404 FileDialog debuggerFileDialog { type = selectDir };
406 static DualPipe vgTargetHandle;
407 static File vgLogFile;
408 static char vgLogPath[MAX_LOCATION];
409 static DualPipe gdbHandle;
410 static DebugEvaluationData eval { };
412 static int targetProcessId;
414 static bool gdbReady;
415 static bool breakpointError;
419 Semaphore serialSemaphore { };
425 bool sentBreakInsert;
426 bool ignoreBreakpoints;
434 int activeFrameLevel;
443 GdbExecution gdbExecution;
444 DebuggerUserAction userAction;
447 DebuggerAction breakType;
449 //DebuggerCommand lastCommand; // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
451 GdbDataStop stopItem;
452 GdbDataBreakpoint bpItem;
455 List<Breakpoint> sysBPs { };
456 Breakpoint bpRunToCursor;
457 Breakpoint intBpEntry;
458 Breakpoint intBpMain;
459 Breakpoint intBpWinMain;
463 CompilerConfig currentCompiler;
464 ProjectConfig prjConfig;
467 CodeEditor codeEditor;
469 ValgrindLogThread vgLogThread { debugger = this };
470 ValgrindTargetThread vgTargetThread { debugger = this };
471 GdbThread gdbThread { debugger = this };
474 Map<String, bool> projectsLibraryLoaded { };
478 delay = 0.0, userData = this;
482 bool monitor = false;
483 DebuggerEvent curEvent = event;
484 GdbDataStop stopItem = this.stopItem;
485 Breakpoint bpUser = null;
486 Breakpoint bpInternal = null;
494 this.stopItem = null;
498 DynamicString bpReport { };
500 for(bp : sysBPs; bp.inserted)
502 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
505 if(bpRunToCursor && bpRunToCursor.inserted)
507 Breakpoint bp = bpRunToCursor;
508 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
511 for(bp : ide.workspace.breakpoints; bp.inserted)
513 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
517 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
522 Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
525 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")");
536 if(curEvent && curEvent != exit)
538 _dpl(0, "No stop item");
546 Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
555 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
556 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
557 if(activeFrame.level == activeFrameLevel)
563 // GdbCommand(0, false, "-break-info %s", bpItem.number);
575 Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
576 if(bp && bp.inserted && bp.bp.addr)
578 if(bp.type.isInternal)
582 if(stopItem && stopItem.frame)
584 if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
585 bpUser = bpRunToCursor;
588 for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
590 if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
602 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
603 if(bpUser && stopItem.frame.addr && strcmp(stopItem.frame.addr, bpUser.bp.addr))
604 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") address missmatch!");
607 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
608 if((bpUser && !ignoreBreakpoints) || (bpInternal && userAction.breaksOnInternalBreakpoint))
610 hitThread = stopItem.threadid;
614 signalThread = stopItem.threadid;
618 case locationReached:
620 ignoreBreakpoints = false;
622 case valgrindStartPause:
623 GdbExecContinue(true);
631 if(curEvent == signal)
635 ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
636 ide.outputView.debugBox.Logf(" %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
637 ide.outputView.Show();
638 ide.callStackView.Show();
641 else if(curEvent == breakEvent)
643 ide.threadsView.Show();
644 ide.callStackView.Show();
645 ide.callStackView.Activate();
647 else if(curEvent == hit)
649 if(BreakpointHit(stopItem, bpInternal, bpUser))
651 ide.AdjustDebugMenus();
652 if(bpUser && bpUser.type == runToCursor)
654 ignoreBreakpoints = false;
655 UnsetBreakpoint(bpUser);
656 delete bpRunToCursor;
661 if(breakType == advance && bpInternal && (bpInternal.type == internalMain || bpInternal.type == internalEntry))
664 GdbExecAdvance(breakString, 0);
669 GdbExecContinue(false);
675 if(monitor && curEvent.canBeMonitored)
678 activeThread = stopItem.threadid;
679 GdbCommand(0, false, "-thread-list-ids");
680 InternalSelectFrame(activeFrameLevel);
681 GoToStackFrameLine(activeFrameLevel, true, false);
683 ide.ShowCodeEditor();
684 ide.AdjustDebugMenus();
685 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
698 #ifdef GDB_DEBUG_CONSOLE
699 char lastGdbOutput[GdbGetLineSize];
701 #if defined(__unix__)
702 ProgramThread progThread { };
706 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
707 void ChangeUserAction(const char * file, int line, DebuggerUserAction value)
710 bool same = value == userAction;
711 __dpl2(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
716 #define _ChangeUserAction(value) userAction = value
720 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
721 void ChangeState(const char * file, int line, DebuggerState value)
723 #define _ChangeState(value) ChangeState(value)
724 void ChangeState(DebuggerState value)
727 bool same = value == state;
728 #if 0 //def _DEBUG_INST
729 __dpl2(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
732 if(!same) ide.AdjustDebugMenus();
737 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
739 stackFrames.Free(Frame::Free);
749 waitingForPID = false;
754 sentBreakInsert = false;
755 ignoreBreakpoints = false;
758 activeFrameLevel = 0;
775 bpRunToCursor = null;
777 delete currentCompiler;
780 WatchesReleaseCodeEditor();
783 projectsLibraryLoaded.Free();
785 /*GdbThread gdbThread
791 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
792 ideProcessId = Process_GetCurrentProcessId();
794 sysBPs.Add((intBpEntry = Breakpoint { type = internalEntry, enabled = false, level = -1 }));
795 sysBPs.Add((intBpMain = Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 }));
796 #if defined(__WIN32__)
797 sysBPs.Add((intBpWinMain = Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 }));
799 sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
800 sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
805 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
813 property bool isActive { get { return state == running || state == stopped; } }
814 property bool isPrepared { get { return state == loaded || state == running || state == stopped; } }
818 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
819 _ChangeUserAction(resume);
820 GdbExecContinue(true);
825 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
826 _ChangeUserAction(_break);
830 GdbDebugBreak(false);
836 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
837 _ChangeUserAction(stop);
844 GdbDebugBreak(false);
858 void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
860 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
861 _ChangeUserAction(restart);
862 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
866 bool GoToCodeLine(char * location)
869 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
870 codloc = CodeLocation::ParseCodeLocation(location);
873 CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, false, true, null, no, normal, false);
876 EditBox editBox = editor.editBox;
877 if(editBox.GoToLineNum(codloc.line - 1))
878 editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
885 bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
887 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
890 char sourceDir[MAX_LOCATION];
892 CodeEditor editor = null;
893 if(stackLevel == -1) // this (the two lines) is part of that fix that I would not put in for some time
895 for(frame = stackFrames.first; frame; frame = frame.next)
896 if(frame.level == stackLevel)
901 ide.callStackView.Show();
903 if(frame.absoluteFile)
904 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
905 if(!editor && frame.file)
906 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
907 if(!frame.absoluteFile && askForLocation && frame.file)
910 char title[MAX_LOCATION];
911 snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
912 title[sizeof(title)-1] = 0;
914 if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
916 AddSourceDir(sourceDir);
917 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
920 if(!editor && frame.absoluteFile)
921 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
923 ide.RepositionWindows(false);
925 if(editor && frame.line)
927 EditBox editBox = editor.editBox;
928 editBox.GoToLineNum(frame.line - 1);
929 editBox.GoToPosition(editBox.line, frame.line - 1, 0);
937 void SelectThread(int thread)
939 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
940 _ChangeUserAction(selectThread);
943 if(thread != activeThread)
945 activeFrameLevel = -1;
946 ide.callStackView.Clear();
947 GdbCommand(0, false, "-thread-select %d", thread);
949 InternalSelectFrame(activeFrameLevel);
950 GoToStackFrameLine(activeFrameLevel, true, false);
954 ide.callStackView.Show();
958 void SelectFrame(int frame)
960 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
961 _ChangeUserAction(selectFrame);
964 if(frame != activeFrameLevel)
966 InternalSelectFrame(frame);
973 void InternalSelectFrame(int frame)
975 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
976 activeFrameLevel = frame; // there is no active frame number in the gdb reply
977 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
978 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
979 if(activeFrame.level == activeFrameLevel)
983 void HandleExit(char * reason, char * code)
985 char verboseExitCode[128];
987 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
988 _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
993 snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
994 verboseExitCode[sizeof(verboseExitCode)-1] = 0;
997 verboseExitCode[0] = '\0';
1001 // ClearBreakDisplay();
1005 for(wh : ide.workspace.watches)
1007 if(wh.type) FreeType(wh.type);
1010 ide.watchesView.UpdateWatch(wh);
1014 #if defined(__unix__)
1017 progThread.terminate = true;
1020 fifoFile.CloseInput();
1030 char program[MAX_LOCATION];
1031 GetSystemPathBuffer(program, targetFile);
1033 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1034 else if(!strcmp(reason, "exited-normally"))
1035 ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
1036 else if(!strcmp(reason, "exited"))
1037 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1038 else if(!strcmp(reason, "exited-signalled"))
1039 ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
1041 ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
1046 DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool ignoreBreakpoints)
1048 DebuggerState result = none;
1049 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), ignoreBreakpoints(", ignoreBreakpoints, ")");
1050 if(restart && state == running && targetProcessId)
1052 breakType = DebuggerAction::restart;
1053 GdbDebugBreak(false);
1057 if(restart && state == stopped)
1059 if(needReset && state == loaded)
1060 GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
1062 if(result == none || result == terminated)
1064 ide.outputView.ShowClearSelectTab(debug);
1065 ide.outputView.debugBox.Logf($"Starting debug mode\n");
1072 for(bp : ide.workspace.breakpoints)
1078 if(GdbInit(compiler, config, bitDepth, useValgrind))
1083 this.ignoreBreakpoints = ignoreBreakpoints;
1088 void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1090 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1091 _ChangeUserAction(start);
1092 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
1096 void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1098 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1099 _ChangeUserAction(stepInto);
1100 switch(StartSession(compiler, config, bitDepth, useValgrind, false, false))
1102 case loaded: GdbExecRun(); break;
1103 case stopped: GdbExecStep(); break;
1107 void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1109 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1110 _ChangeUserAction(stepOver);
1111 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1113 case loaded: GdbExecRun(); break;
1114 case stopped: GdbExecNext(); break;
1118 void StepUntil(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1120 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepUntil()");
1121 _ChangeUserAction(stepUntil);
1122 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1124 case loaded: GdbExecRun(); break;
1125 case stopped: GdbExecUntil(null, 0); break;
1129 void StepOut(bool ignoreBreakpoints)
1131 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1132 _ChangeUserAction(stepOut);
1133 if(state == stopped)
1135 this.ignoreBreakpoints = ignoreBreakpoints;
1139 GdbExecContinue(true);
1143 void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, const char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel, bool oldImplementation)
1145 char relativeFilePath[MAX_LOCATION];
1146 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1147 _ChangeUserAction(runToCursor);
1148 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
1150 if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1152 UnsetBreakpoint(bpRunToCursor);
1153 delete bpRunToCursor;
1156 StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints);
1159 if(oldImplementation)
1161 bpRunToCursor = Breakpoint { };
1162 bpRunToCursor.absoluteFilePath = absoluteFilePath;
1163 bpRunToCursor.relativeFilePath = relativeFilePath;
1164 bpRunToCursor.line = lineNumber;
1165 bpRunToCursor.type = runToCursor;
1166 bpRunToCursor.enabled = true;
1167 bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1172 breakType = advance;
1173 breakString = PrintString(relativeFilePath, ":", lineNumber);
1176 else if(state == stopped)
1178 if(oldImplementation)
1179 GdbExecContinue(true);
1183 GdbExecUntil(absoluteFilePath, lineNumber);
1185 GdbExecAdvance(absoluteFilePath, lineNumber);
1190 void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1192 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1193 if(activeFrameLevel == -1)
1201 *error = signalOn && activeThread == signalThread;
1202 *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1203 *lineTopFrame = activeFrameLevel ? 1 : 0;
1207 int GetMarginIconsLineNumbers(const char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1209 char winFilePath[MAX_LOCATION];
1210 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1212 Iterator<Breakpoint> it { ide.workspace.breakpoints };
1213 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1214 while(it.Next() && count < max)
1216 Breakpoint bp = it.data;
1219 if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1221 lines[count] = bp.line;
1222 enabled[count] = bp.enabled;
1227 if(activeFrameLevel == -1)
1235 *error = signalOn && activeThread == signalThread;
1236 if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1237 *lineCursor = activeFrame.line;
1240 if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1242 else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1243 *lineTopFrame = stopItem.frame.line;
1247 if(*lineTopFrame == *lineCursor && *lineTopFrame)
1253 void ChangeWatch(DataRow row, char * expression)
1255 Watch wh = (Watch)row.tag;
1256 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1259 delete wh.expression;
1261 wh.expression = CopyString(expression);
1264 Iterator<Watch> it { ide.workspace.watches };
1266 ide.workspace.watches.Delete(it.pointer);
1272 row.tag = (int64)wh;
1273 ide.workspace.watches.Add(wh);
1275 wh.expression = CopyString(expression);
1277 ide.workspace.Save();
1278 //if(expression && state == stopped)
1283 void MoveIcons(const char * fileName, int lineNumber, int move, bool start)
1285 char winFilePath[MAX_LOCATION];
1286 const char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1289 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1290 for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1292 Breakpoint bp = (Breakpoint)bpLink.data;
1295 if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1297 if(bp.line > lineNumber || (bp.line == lineNumber && start))
1299 if(move < 0 && (bp.line < lineNumber - move))
1300 ide.workspace.RemoveBreakpoint(bp);
1304 ide.breakpointsView.UpdateBreakpoint(bp.row);
1305 ide.workspace.Save();
1311 // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1314 bool SourceDirDialog(const char * title, const char * startDir, const char * test, char * sourceDir)
1317 String srcDir = null;
1319 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1320 debuggerFileDialog.text = title;
1321 debuggerFileDialog.currentDirectory = startDir;
1322 debuggerFileDialog.master = ide;
1324 while(debuggerFileDialog.Modal())
1326 strcpy(sourceDir, debuggerFileDialog.filePath);
1327 if(!fstrcmp(ide.workspace.projectDir, sourceDir) &&
1328 MessageBox { type = yesNo, master = ide,
1329 contents = $"This is the project directory.\nWould you like to try again?",
1330 text = $"Invalid Source Directory" }.Modal() == no)
1334 for(dir : ide.workspace.sourceDirs)
1336 if(!fstrcmp(dir, sourceDir))
1344 MessageBox { type = yesNo, master = ide,
1345 contents = $"This source directory is already specified.\nWould you like to try again?",
1346 text = $"Invalid Source Directory" }.Modal() == no)
1352 char file[MAX_LOCATION];
1353 strcpy(file, sourceDir);
1354 PathCat(file, test);
1355 result = FileExists(file);
1357 MessageBox { type = yesNo, master = ide,
1358 contents = $"Unable to locate source file.\nWould you like to try again?",
1359 text = $"Invalid Source Directory" }.Modal() == no)
1373 void AddSourceDir(const char * sourceDir)
1375 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1376 ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1377 ide.workspace.Save();
1381 DebuggerState oldState = state;
1386 GdbDebugBreak(true);
1389 GdbCommand(0, false, "-environment-directory \"%s\"", sourceDir);
1392 if(oldState == running)
1393 GdbExecContinue(false);
1397 void ToggleBreakpoint(const char * fileName, int lineNumber)
1399 char absolutePath[MAX_LOCATION];
1400 Breakpoint bp = null;
1402 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1404 GetSlashPathBuffer(absolutePath, fileName);
1405 for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1414 ide.workspace.RemoveBreakpoint(bp);
1423 char relativePath[MAX_LOCATION];
1425 WorkspaceGetRelativePath(absolutePath, relativePath, &owner);
1427 if(!owner && !FileExists(absolutePath))
1429 char title[MAX_LOCATION];
1430 char directory[MAX_LOCATION];
1431 char sourceDir[MAX_LOCATION];
1432 StripLastDirectory(absolutePath, directory);
1433 snprintf(title, sizeof(title), $"Provide source files location directory for %s", relativePath);
1434 title[sizeof(title)-1] = 0;
1437 String srcDir = null;
1438 for(dir : ide.workspace.sourceDirs)
1440 if(IsPathInsideOf(absolutePath, dir))
1442 MakePathRelative(absolutePath, dir, relativePath);
1450 if(SourceDirDialog(title, directory, null, sourceDir))
1452 if(IsPathInsideOf(absolutePath, sourceDir))
1454 AddSourceDir(sourceDir);
1455 MakePathRelative(absolutePath, sourceDir, relativePath);
1458 else if(MessageBox { type = yesNo, master = ide,
1459 contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1460 text = $"Invalid Source Directory" }.Modal() == no)
1467 ide.workspace.bpCount++;
1468 bp = { line = lineNumber, type = user, enabled = true, level = -1, project = owner };
1469 ide.workspace.breakpoints.Add(bp);
1470 bp.absoluteFilePath = absolutePath;
1471 bp.relativeFilePath = relativePath;
1472 ide.breakpointsView.AddBreakpoint(bp);
1477 DebuggerState oldState = state;
1482 GdbDebugBreak(true);
1485 if(!SetBreakpoint(bp, false))
1486 SetBreakpoint(bp, true);
1489 if(oldState == running)
1490 GdbExecContinue(false);
1493 ide.workspace.Save();
1496 void UpdateRemovedBreakpoint(Breakpoint bp)
1498 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1499 if(targeted && bp.inserted)
1501 DebuggerState oldState = state;
1506 GdbDebugBreak(true);
1509 UnsetBreakpoint(bp);
1512 if(oldState == running)
1513 GdbExecContinue(false);
1519 void ParseFrame(Frame frame, char * string)
1522 Array<char *> frameTokens { minAllocSize = 50 };
1523 Array<char *> argsTokens { minAllocSize = 50 };
1524 Array<char *> argumentTokens { minAllocSize = 50 };
1525 DebugListItem item { };
1528 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1529 TokenizeList(string, ',', frameTokens);
1530 for(i = 0; i < frameTokens.count; i++)
1532 if(TokenizeListItem(frameTokens[i], item))
1534 StripQuotes(item.value, item.value);
1535 if(!strcmp(item.name, "level"))
1536 frame.level = atoi(item.value);
1537 else if(!strcmp(item.name, "addr"))
1538 frame.addr = item.value;
1539 else if(!strcmp(item.name, "func"))
1540 frame.func = item.value;
1541 else if(!strcmp(item.name, "args"))
1543 if(!strcmp(item.value, "[]"))
1544 frame.argsCount = 0;
1547 item.value = StripBrackets(item.value);
1548 TokenizeList(item.value, ',', argsTokens);
1549 for(j = 0; j < argsTokens.count; j++)
1551 argsTokens[j] = StripCurlies(argsTokens[j]);
1552 TokenizeList(argsTokens[j], ',', argumentTokens);
1553 for(k = 0; k < argumentTokens.count; k++)
1556 frame.args.Add(arg);
1557 if(TokenizeListItem(argumentTokens[k], item))
1559 if(!strcmp(item.name, "name"))
1561 StripQuotes(item.value, item.value);
1562 arg.name = item.value;
1564 else if(!strcmp(item.name, "value"))
1566 StripQuotes(item.value, item.value);
1567 arg.val = item.value;
1570 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1573 _dpl(0, "Bad frame args item");
1575 argumentTokens.RemoveAll();
1577 frame.argsCount = argsTokens.count;
1578 argsTokens.RemoveAll();
1581 else if(!strcmp(item.name, "from"))
1582 frame.from = item.value;
1583 else if(!strcmp(item.name, "file"))
1584 frame.file = item.value;
1585 else if(!strcmp(item.name, "line"))
1586 frame.line = atoi(item.value);
1587 else if(!strcmp(item.name, "fullname"))
1589 // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1590 Workspace ws = ide.workspace;
1593 String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1594 if(strcmp(frame.file, path))
1598 frame.absoluteFile = item.value;
1601 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1604 _dpl(0, "Bad frame");
1609 delete argumentTokens;
1613 Breakpoint GetBreakpointById(int id, bool * isInternal)
1615 Breakpoint bp = null;
1616 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1618 *isInternal = false;
1621 for(i : sysBPs; i.bp && i.bp.id == id)
1628 if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1632 for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1642 GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1645 GdbDataBreakpoint bp { };
1646 DebugListItem item { };
1647 Array<char *> bpTokens { minAllocSize = 16 };
1648 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1649 string = StripCurlies(string);
1650 TokenizeList(string, ',', bpTokens);
1651 for(i = 0; i < bpTokens.count; i++)
1653 if(TokenizeListItem(bpTokens[i], item))
1655 StripQuotes(item.value, item.value);
1656 if(!strcmp(item.name, "number"))
1658 if(!strchr(item.value, '.'))
1659 bp.id = atoi(item.value);
1660 bp.number = item.value;
1662 else if(!strcmp(item.name, "type"))
1663 bp.type = item.value;
1664 else if(!strcmp(item.name, "disp"))
1665 bp.disp = item.value;
1666 else if(!strcmp(item.name, "enabled"))
1667 bp.enabled = (!strcmpi(item.value, "y"));
1668 else if(!strcmp(item.name, "addr"))
1670 if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1673 Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1674 while(outTokens.count > ++c)
1676 GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1677 bpArray.Add(multBp);
1681 bp.addr = item.value;
1683 else if(!strcmp(item.name, "func"))
1684 bp.func = item.value;
1685 else if(!strcmp(item.name, "file"))
1686 bp.file = item.value;
1687 else if(!strcmp(item.name, "fullname"))
1688 bp.fullname = item.value;
1689 else if(!strcmp(item.name, "line"))
1690 bp.line = atoi(item.value);
1691 else if(!strcmp(item.name, "at"))
1693 else if(!strcmp(item.name, "times"))
1694 bp.times = atoi(item.value);
1695 else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1696 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1698 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1706 void ShowDebuggerViews()
1708 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1709 ide.outputView.Show();
1710 ide.outputView.SelectTab(debug);
1711 ide.threadsView.Show();
1712 ide.callStackView.Show();
1713 ide.watchesView.Show();
1717 void HideDebuggerViews()
1719 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1720 ide.RepositionWindows(true);
1723 bool ::GdbCommand(Time timeOut, bool focus, const char * format, ...)
1725 bool result = false;
1729 // TODO: Improve this limit
1730 static char string[MAX_F_STRING*4];
1732 va_start(args, format);
1733 vsnprintf(string, sizeof(string), format, args);
1734 string[sizeof(string)-1] = 0;
1738 ide.debugger.serialSemaphore.TryWait();
1740 #ifdef GDB_DEBUG_CONSOLE
1741 _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1743 #ifdef GDB_DEBUG_OUTPUT
1744 ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1746 #ifdef GDB_DEBUG_GUI
1748 ide.gdbDialog.AddCommand(string);
1751 strcat(string,"\n");
1752 gdbHandle.Puts(string);
1755 Process_ShowWindows(targetProcessId);
1761 startTime = GetTime();
1764 if(ide.debugger.serialSemaphore.TryWait())
1771 if(GetTime() - startTime > timeOut)
1779 ide.debugger.serialSemaphore.Wait();
1788 bool ValidateBreakpoint(Breakpoint bp)
1790 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1791 if(modules && bp.line && bp.bp)
1793 if(bp.bp.line != bp.line)
1799 ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1801 //UnsetBreakpoint(bp);
1802 //bp.enabled = false;
1808 ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1809 bp.line = bp.bp.line;
1816 void BreakpointsMaintenance()
1818 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::BreakpointsMaintenance()");
1821 if(gdbExecution.suspendInternalBreakpoints)
1823 for(bp : sysBPs; bp.inserted)
1824 UnsetBreakpoint(bp);
1828 DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1829 for(bp : sysBPs; !bp.inserted)
1831 bool insert = false;
1832 if(bp.type == internalModulesLoaded)
1834 char path[MAX_LOCATION];
1835 char name[MAX_LOCATION];
1836 char fixedModuleName[MAX_FILENAME];
1839 bool moduleLoadBlock = false;
1841 ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1842 snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1843 name[sizeof(name)-1] = 0;
1844 strcpy(path, ide.workspace.projectDir);
1845 PathCatSlash(path, objDir.dir);
1846 PathCatSlash(path, name);
1847 f = FileOpen(path, read);
1850 for(lineNumber = 1; !f.Eof(); lineNumber++)
1852 if(f.GetLine(line, sizeof(line) - 1))
1854 bool moduleLoadLine;
1855 TrimLSpaces(line, line);
1856 moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1857 if(!moduleLoadBlock && moduleLoadLine)
1858 moduleLoadBlock = true;
1859 else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1865 char relative[MAX_LOCATION];
1866 bp.absoluteFilePath = path;
1867 MakePathRelative(path, ide.workspace.projectDir, relative);
1868 bp.relativeFilePath = relative;
1869 bp.line = lineNumber;
1875 else if(bp.type == internalModuleLoad)
1879 for(prj : ide.workspace.projects)
1881 if(!strcmp(prj.moduleName, "ecere"))
1883 ProjectNode node = prj.topNode.Find("instance.c", false);
1886 char path[MAX_LOCATION];
1887 char relative[MAX_LOCATION];
1888 node.GetFullFilePath(path);
1889 bp.absoluteFilePath = path;
1890 MakePathRelative(path, prj.topNode.path, relative);
1891 bp.relativeFilePath = relative;
1903 if(!SetBreakpoint(bp, false))
1904 SetBreakpoint(bp, true);
1910 if(userAction != runToCursor && bpRunToCursor && bpRunToCursor.inserted)
1911 UnsetBreakpoint(bpRunToCursor);
1912 if(bpRunToCursor && !bpRunToCursor.inserted)
1914 if(!SetBreakpoint(bpRunToCursor, false))
1915 SetBreakpoint(bpRunToCursor, true);
1918 if(ignoreBreakpoints)
1920 for(bp : ide.workspace.breakpoints; bp.inserted)
1921 UnsetBreakpoint(bp);
1925 for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1929 if(!SetBreakpoint(bp, false))
1930 SetBreakpoint(bp, true);
1939 bp.bp = GdbDataBreakpoint { };
1946 void UnsetBreakpoint(Breakpoint bp)
1948 char * s = null; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1949 if(symbols && bp.inserted)
1951 GdbCommand(0, false, "-break-delete %s", bp.bp.number);
1952 bp.inserted = false;
1958 bool SetBreakpoint(Breakpoint bp, bool removePath)
1960 char * s = null; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
1961 breakpointError = false;
1962 if(symbols && bp.enabled && (!bp.project || bp.project.GetTargetType(bp.project.config) == staticLibrary || bp.project == ide.project || projectsLibraryLoaded[bp.project.name]))
1964 sentBreakInsert = true;
1966 GdbCommand(0, false, "-break-insert *%s", bp.address);
1969 char * location = bp.CopyLocationString(removePath);
1970 GdbCommand(0, false, "-break-insert %s", location);
1973 if(!breakpointError)
1975 char * address = null;
1976 if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
1979 GdbDataBreakpoint first = null;
1980 for(n : bpItem.multipleBPs)
1982 if(!fstrcmp(n.fullname, bp.absoluteFilePath) && !first)
1992 GdbCommand(0, false, "-break-disable %s", n.number);
1996 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2001 address = CopyString(first.addr);
2002 bpItem.addr = first.addr;
2003 bpItem.func = first.func;
2004 bpItem.file = first.file;
2005 bpItem.fullname = first.fullname;
2006 bpItem.line = first.line;
2007 //bpItem.thread-groups = first.thread-groups;*/
2010 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2012 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2013 bpItem.multipleBPs.Free();
2014 delete bpItem.multipleBPs;
2019 bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2021 ValidateBreakpoint(bp);
2025 UnsetBreakpoint(bp);
2026 bp.address = address;
2028 SetBreakpoint(bp, removePath);
2031 return !breakpointError;
2038 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2040 stackFrames.Free(Frame::Free);
2041 GdbCommand(0, false, "-stack-info-depth");
2043 GdbCommand(0, false, "-stack-info-depth 192");
2044 if(frameCount && frameCount <= 192)
2045 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2048 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2049 GdbCommand(0, false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2051 GdbCommand(0, false, "");
2056 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2059 char escaped[MAX_LOCATION];
2060 strescpy(escaped, targetFile);
2061 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
2068 const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2069 //GdbCommand(0, false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2070 printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2071 GdbCommand(0, false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2074 GdbCommand(0, false, "info target"); //GDB/MI Missing Implementation -file-list-symbol-files and -file-list-exec-sections
2076 /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2077 GdbCommand(0, false, "-environment-directory \"%s\"", prj.topNode.path);*/
2079 for(dir : ide.workspace.sourceDirs; dir && dir[0])
2081 bool interference = false;
2082 for(prj : ide.workspace.projects)
2084 if(!fstrcmp(prj.topNode.path, dir))
2086 interference = true;
2090 if(!interference && dir[0])
2091 GdbCommand(0, false, "-environment-directory \"%s\"", dir);
2099 /*void GdbTargetRelease()
2103 BreakpointsDeleteAll();
2104 GdbCommand(0, false, "file"); //GDB/MI Missing Implementation -target-detach
2110 void GdbDebugBreak(bool internal)
2112 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2116 breakType = DebuggerAction::internal;
2118 if(ide) ide.Update(null);
2120 if(Process_Break(targetProcessId)) //GdbCommand(0, false, "-exec-interrupt");
2121 serialSemaphore.Wait();
2124 _ChangeState(loaded);
2125 targetProcessId = 0;
2130 ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2135 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2140 ShowDebuggerViews();
2142 GdbExecContinue(true);
2143 else if(!GdbCommand(3, true, "-exec-run"))
2144 gdbExecution = none;
2147 void GdbExecContinue(bool focus)
2149 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2152 GdbCommand(0, focus, "-exec-continue");
2157 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2158 gdbExecution = next;
2160 GdbCommand(0, true, "-exec-next");
2163 void ForceUpdateCurrentFrame()
2166 GdbCommand(0, false, "-thread-list-ids");
2167 InternalSelectFrame(activeFrameLevel);
2168 GoToStackFrameLine(activeFrameLevel, true, false);
2170 ide.ShowCodeEditor();
2171 ide.AdjustDebugMenus();
2172 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
2176 void GdbExecUntil(const char * absoluteFilePath, int lineNumber)
2178 bool forceUpdate = false;
2179 char relativeFilePath[MAX_LOCATION];
2180 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecUntil()");
2181 gdbExecution = until;
2183 if(absoluteFilePath)
2185 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
2186 if(!GdbCommand(0.1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2188 GetLastDirectory(relativeFilePath, relativeFilePath);
2189 if(GdbCommand(1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2194 GdbCommand(0, true, "-exec-until");
2196 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2197 // (It may not be ideal, we may need to wait?)
2199 ForceUpdateCurrentFrame();
2202 void GdbExecAdvance(const char * absoluteFilePathOrLocation, int lineNumber)
2204 bool forceUpdate = false;
2205 char relativeFilePath[MAX_LOCATION];
2206 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecAdvance()");
2207 gdbExecution = advance;
2211 WorkspaceGetRelativePath(absoluteFilePathOrLocation, relativeFilePath, null);
2212 if(!GdbCommand(0.1, true, "advance %s:%d", relativeFilePath, lineNumber)) // should use -exec-advance -- GDB/MI implementation missing
2214 GetLastDirectory(relativeFilePath, relativeFilePath);
2215 if(GdbCommand(1, true, "advance %s:%d", relativeFilePath, lineNumber))
2221 if(!GdbCommand(0.1, true, "advance %s", absoluteFilePathOrLocation)) // should use -exec-advance -- GDB/MI implementation missing
2223 GetLastDirectory(absoluteFilePathOrLocation, relativeFilePath);
2224 if(GdbCommand(1, true, "advance %s", relativeFilePath))
2229 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2230 // (It may not be ideal, we may need to wait?)
2232 ForceUpdateCurrentFrame();
2237 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2238 gdbExecution = step;
2240 GdbCommand(0, true, "-exec-step");
2243 void GdbExecFinish()
2245 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2246 gdbExecution = finish;
2248 GdbCommand(0, true, "-exec-finish");
2251 void GdbExecCommon()
2253 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2254 BreakpointsMaintenance();
2257 #ifdef GDB_DEBUG_GUI
2258 void SendGDBCommand(const char * command)
2260 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2261 DebuggerState oldState = state;
2266 GdbDebugBreak(true);
2269 GdbCommand(0, false, command);
2272 if(oldState == running)
2273 GdbExecContinue(false);
2277 void ClearBreakDisplay()
2279 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2281 activeFrameLevel = -1;
2291 stackFrames.Free(Frame::Free);
2292 ide.callStackView.Clear();
2293 ide.threadsView.Clear();
2299 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2301 GdbCommand(0, false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2305 bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2308 char oldDirectory[MAX_LOCATION];
2309 char tempPath[MAX_LOCATION];
2310 char command[MAX_F_STRING*4];
2311 Project project = ide.project;
2312 DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2313 PathBackup pathBackup { };
2314 Map<String, String> envBackup { };
2316 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2317 if(currentCompiler != compiler)
2319 delete currentCompiler;
2320 currentCompiler = compiler;
2321 incref currentCompiler;
2324 this.bitDepth = bitDepth;
2325 usingValgrind = useValgrind;
2327 _ChangeState(loaded);
2329 sentBreakInsert = false;
2330 breakpointError = false;
2331 ignoreBreakpoints = false;
2337 projectsLibraryLoaded.Free();
2339 ide.outputView.ShowClearSelectTab(debug);
2340 ide.outputView.debugBox.Logf($"Starting debug mode\n");
2342 #ifdef GDB_DEBUG_OUTPUT
2343 ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2346 strcpy(tempPath, ide.workspace.projectDir);
2347 PathCatSlash(tempPath, targetDirExp.dir);
2349 targetDir = CopyString(tempPath);
2350 project.CatTargetFileName(tempPath, compiler, config);
2352 targetFile = CopyString(tempPath);
2354 GetWorkingDir(oldDirectory, MAX_LOCATION);
2355 if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2357 char temp[MAX_LOCATION];
2358 strcpy(temp, ide.workspace.projectDir);
2359 PathCatSlash(temp, ide.workspace.debugDir);
2360 ChangeWorkingDir(temp);
2363 ChangeWorkingDir(ide.workspace.projectDir);
2365 ide.SetPath(true, compiler, config, bitDepth);
2367 // TODO: This pollutes the environment, but at least it works
2368 // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2369 // What is the proper solution for this? DualPipeOpenEnv?
2370 // gdb set environment commands don't seem to take effect
2371 for(e : ide.workspace.environmentVars)
2373 // Backing up the environment variables until we use DualPipeOpenEnv()
2374 envBackup[e.name] = CopyString(getenv(e.name));
2375 SetEnvironment(e.name, e.string);
2380 char * clArgs = ide.workspace.commandLineArgs;
2381 const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2382 ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2383 int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2384 bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2385 vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2389 vgLogThread.Create();
2393 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2396 if(result && !CheckCommandAvailable(valgrindCommand))
2398 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2403 char * vgRedzoneSizeFlag = PrintString(" --redzone-size=", vgRedzoneSize);
2404 sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2405 valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSize > -1 ? vgRedzoneSizeFlag : "", vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2406 delete vgRedzoneSizeFlag;
2407 vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2410 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2416 incref vgTargetHandle;
2417 vgTargetThread.Create();
2419 targetProcessId = vgTargetHandle.GetProcessID();
2420 waitingForPID = false;
2421 if(!targetProcessId)
2423 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2430 serialSemaphore.Wait();
2437 if(compiler.targetPlatform == win32)
2440 #if !((defined(__WORDSIZE) && __WORDSIZE == 8) || defined(__x86_64__))
2443 bitDepth == 32 ? "i686-w64-mingw32-gdb" : "gdb"); // x86_64-w64-mingw32-gdb
2446 // We really should have a box to select GDB in the compiler/toolchain options
2447 strcpy(command, "gdb");
2448 if(!CheckCommandAvailable(command))
2450 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2455 strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2457 gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2460 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2470 gdbProcessId = gdbHandle.GetProcessID();
2473 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2480 serialSemaphore.Wait();
2483 GdbCommand(0, false, "-gdb-set verbose off");
2484 //GdbCommand(0, false, "-gdb-set exec-done-display on");
2485 GdbCommand(0, false, "-gdb-set step-mode off");
2486 GdbCommand(0, false, "-gdb-set unwindonsignal on");
2487 //GdbCommand(0, false, "-gdb-set shell on");
2488 GdbCommand(0, false, "set print elements 992");
2489 GdbCommand(0, false, "-gdb-set backtrace limit 100000");
2493 //_ChangeState(terminated);
2499 #if defined(__unix__)
2501 CreateTemporaryDir(progFifoDir, "ecereide");
2502 strcpy(progFifoPath, progFifoDir);
2503 PathCat(progFifoPath, "ideprogfifo");
2504 if(!mkfifo(progFifoPath, 0600))
2506 //fileCreated = true;
2511 ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2518 progThread.terminate = false;
2519 progThread.Create();
2523 #if defined(__WIN32__)
2524 GdbCommand(0, false, "-gdb-set new-console on");
2527 #if defined(__unix__)
2529 GdbCommand(0, false, "-inferior-tty-set %s", progFifoPath);
2533 GdbCommand(0, false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2535 for(e : ide.workspace.environmentVars)
2537 GdbCommand(0, false, "set environment %s=%s", e.name, e.string);
2542 ChangeWorkingDir(oldDirectory);
2546 SetEnvironment(&e, e);
2555 delete targetDirExp;
2561 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2562 if(gdbHandle && gdbProcessId)
2565 GdbCommand(0, false, "-gdb-exit");
2574 vgLogFile.CloseInput();
2575 if(vgLogThread.created)
2585 vgTargetThread.Wait();
2595 _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2600 for(bp : ide.workspace.breakpoints)
2606 bpRunToCursor.Reset();
2608 ide.outputView.debugBox.Logf($"Debugging stopped\n");
2610 #if defined(__unix__)
2611 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2613 progThread.terminate = true;
2616 fifoFile.CloseInput();
2622 DeleteFile(progFifoPath);
2623 progFifoPath[0] = '\0';
2632 bool WatchesLinkCodeEditor()
2634 bool goodFrame = activeFrame && activeFrame.absoluteFile;
2635 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesLinkCodeEditor()");
2636 if(codeEditor && (!goodFrame || fstrcmp(codeEditor.fileName, activeFrame.absoluteFile)))
2637 WatchesReleaseCodeEditor();
2639 if(!codeEditor && goodFrame)
2641 codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, false, false, null, no, normal, false);
2644 codeEditor.inUseDebug = true;
2648 return codeEditor != null;
2651 void WatchesReleaseCodeEditor()
2653 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesReleaseCodeEditor()");
2656 codeEditor.inUseDebug = false;
2657 if(!codeEditor.visible)
2658 codeEditor.Destroy(0);
2663 bool ResolveWatch(Watch wh)
2665 bool result = false;
2667 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2679 char watchmsg[MAX_F_STRING];
2681 if(state == stopped && !codeEditor)
2682 wh.value = CopyString($"No source file found for selected frame");
2683 //if(codeEditor && state == stopped || state != stopped)
2686 Module backupPrivateModule;
2687 Context backupContext;
2688 Class backupThisClass;
2691 backupPrivateModule = GetPrivateModule();
2692 backupContext = GetCurrentContext();
2693 backupThisClass = GetThisClass();
2696 SetPrivateModule(codeEditor.privateModule);
2697 SetCurrentContext(codeEditor.globalContext);
2698 SetTopContext(codeEditor.globalContext);
2699 SetGlobalContext(codeEditor.globalContext);
2700 SetGlobalData(&codeEditor.globalData);
2703 exp = ParseExpressionString(wh.expression);
2705 if(exp && !GetParseError())
2707 char expString[4096];
2709 PrintExpression(exp, expString);
2712 if(codeEditor && activeFrame)
2713 DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2714 ProcessExpressionType(exp);
2716 wh.type = exp.expType;
2719 DebugComputeExpression(exp);
2720 // e.g. Meters * Degrees has no type set yet for some reason
2721 if(!wh.type && exp.expType)
2723 wh.type = exp.expType;
2724 exp.expType.refCount++;
2727 // This makes Degrees { 45 } work
2728 if(exp.type == constantExp && exp.isConstant && exp.expType && exp.expType.kind == classType &&
2729 exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == unitClass && exp.expType._class.registered.base.type == unitClass)
2731 ApplyUnitConverters(exp);
2733 else if(exp.type == instanceExp && exp.instance.data)
2735 Symbol s = exp.instance._class ? exp.instance._class.symbol : null;
2736 Class c = s ? s.registered : null;
2740 bool needClass = false;
2741 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, exp.instance.data, tmp, null, &needClass);
2744 FreeExpContents(exp);
2745 exp.type = constantExp;
2746 exp.isConstant = true;
2747 exp.constant = CopyString(s);
2751 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == bitClass)
2753 Class c = exp.expType._class.registered;
2755 bool needClass = false;
2756 Operand op = GetOperand(exp);
2758 char * (* onGetString)(void *, void *, void *, void *, void *) = (void *)c._vTbl[__ecereVMethodID_class_OnGetString];
2761 if(op.type) op.type.refCount++;
2764 case charType: s = onGetString(c, &op.c, tmp, null, &needClass); break;
2765 case shortType: s = onGetString(c, &op.s, tmp, null, &needClass); break;
2766 case intType: s = onGetString(c, &op.i, tmp, null, &needClass); break;
2767 case int64Type: s = onGetString(c, &op.i64, tmp, null, &needClass); break;
2772 FreeExpContents(exp);
2773 exp.type = constantExp;
2774 exp.isConstant = true;
2775 exp.constant = CopyString(s);
2779 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass && exp.hasAddress)
2781 Class c = exp.expType._class.registered;
2782 char structString[1024];
2783 strcpy(structString, "*(struct ");
2784 FullClassNameCat(structString, c.fullName, false);
2785 strcat(structString, " *)");
2786 strcatf(structString, "0x%p", exp.address);
2787 GDBFallBack(exp, structString);
2789 byte * data = GdbReadMemory(exp.address, c.structSize);
2793 bool needClass = false;
2794 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, data, tmp, null, &needClass);
2797 FreeExpContents(exp);
2798 exp.type = constantExp;
2799 exp.isConstant = true;
2800 exp.constant = CopyString(s);
2807 if(ExpressionIsError(exp) && exp.type != functionCallErrorExp)
2809 GDBFallBack(exp, expString);
2812 /*if(exp.hasAddress)
2814 char temp[MAX_F_STRING];
2815 sprintf(temp, "0x%x", exp.address);
2816 wh.address = CopyString(temp);
2817 // wh.address = CopyStringf("0x%x", exp.address);
2822 Type dataType = exp.expType;
2825 char temp[MAX_F_STRING];
2826 switch(dataType.kind)
2829 sprintf(temp, "%i", exp.val.c);
2832 sprintf(temp, "%i", exp.val.s);
2837 sprintf(temp, "%i", exp.val.i);
2840 sprintf(temp, "%i", exp.val.i64);
2843 sprintf(temp, "%i", exp.val.p);
2848 long v = (long)exp.val.f;
2849 sprintf(temp, "%i", v);
2854 long v = (long)exp.val.d;
2855 sprintf(temp, "%i", v);
2860 wh.intVal = CopyString(temp);
2861 switch(dataType.kind)
2864 sprintf(temp, "0x%x", exp.val.c);
2867 sprintf(temp, "0x%x", exp.val.s);
2871 sprintf(temp, "0x%x", exp.val.i);
2874 sprintf(temp, "0x%x", exp.val.i64);
2877 sprintf(temp, "0x%x", exp.val.i64);
2880 sprintf(temp, "0x%x", exp.val.p);
2885 long v = (long)exp.val.f;
2886 sprintf(temp, "0x%x", v);
2891 long v = (long)exp.val.d;
2892 sprintf(temp, "0x%x", v);
2897 wh.hexVal = CopyString(temp);
2898 switch(dataType.kind)
2901 sprintf(temp, "0o%o", exp.val.c);
2904 sprintf(temp, "0o%o", exp.val.s);
2908 sprintf(temp, "0o%o", exp.val.i);
2911 sprintf(temp, "0o%o", exp.val.i64);
2914 sprintf(temp, "0o%o", exp.val.i64);
2917 sprintf(temp, "0o%o", exp.val.p);
2922 long v = (long)exp.val.f;
2923 sprintf(temp, "0o%o", v);
2928 long v = (long)exp.val.d;
2929 sprintf(temp, "0o%o", v);
2934 wh.octVal = CopyString(temp);
2937 // WHATS THIS HERE ?
2938 if(exp.type == constantExp && exp.constant)
2939 wh.constant = CopyString(exp.constant);
2945 case symbolErrorExp:
2946 snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2948 case memberSymbolErrorExp:
2950 Expression memberExp = exp.member.exp;
2951 Identifier memberID = exp.member.member;
2952 Type type = memberExp.expType;
2955 if(type.kind == structType || type.kind == unionType)
2957 char string[1024] = "";
2959 PrintTypeNoConst(type, string, false, true);
2960 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in %s \"%s\"",
2961 memberID ? memberID.string : "", type.kind == unionType ? "union" : "struct", type.name ? type.name : string);
2965 Class _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2966 char string[1024] = "";
2970 PrintTypeNoConst(type, string, false, true);
2971 classSym = FindClass(string);
2972 _class = classSym ? classSym.registered : null;
2975 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2977 // NOTE: This should probably never happen, only classes and struct can be dereferenced, a dereferenceErrorExp should be displayed instead
2978 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in type \"%s\"", memberID ? memberID.string : "", string);
2982 // NOTE: This should probably never happen, the error causing the unresolved expression should be displayed instead
2983 snprintf(watchmsg, sizeof(watchmsg), $"Accessing member \"%s\" from unresolved expression", memberID ? memberID.string : "");
2986 case memberPropertyErrorExp:
2988 Expression memberExp = exp.member.exp;
2989 Identifier memberID = exp.member.member;
2990 Type type = memberExp.expType;
2991 Class _class = (type && memberID) ? (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null) : null;
2992 if(_class && memberID && memberID.string)
2993 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\" in class \"%s\"", memberID.string, _class.name);
2995 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\"", wh.expression);
2998 case functionCallErrorExp:
2999 if(exp.call.exp && exp.call.exp.type == identifierExp && exp.call.exp.identifier.string)
3000 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for call to \"%s\"", exp.call.exp.identifier.string);
3002 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for \"%s\"", wh.expression);
3004 case memoryErrorExp:
3005 // Need to ensure when set to memoryErrorExp, constant is set
3006 snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
3008 case dereferenceErrorExp:
3009 snprintf(watchmsg, sizeof(watchmsg), $"Dereferencing error evaluating \"%s\"", wh.expression);
3011 case divideBy0ErrorExp:
3012 snprintf(watchmsg, sizeof(watchmsg), "%s", $"Integer division by 0");
3014 case noDebuggerErrorExp:
3015 snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
3017 case unknownErrorExp:
3018 // NOTE: This should never happen
3019 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
3022 // NOTE: This should never happen
3023 snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
3026 wh.value = CopyString(exp.string);
3029 // Temporary Code for displaying Strings
3030 if((exp.expType && ((exp.expType.kind == pointerType ||
3031 exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) ||
3032 (wh.type && wh.type.kind == classType && wh.type._class &&
3033 wh.type._class.registered && wh.type._class.registered.type == normalClass &&
3034 !strcmp(wh.type._class.registered.name, "String")))
3037 if(exp.expType.kind != arrayType || exp.hasAddress)
3041 //char temp[MAX_F_STRING * 32];
3043 //ExpressionType evalError = dummyExp;
3044 /*if(exp.expType.kind == arrayType)
3045 sprintf(temp, "(char*)0x%x", exp.address);
3047 sprintf(temp, "(char*)%s", exp.constant);*/
3049 //evaluation = Debugger::EvaluateExpression(temp, &evalError);
3050 // address = strtoul(exp.constant, null, 0);
3051 address = _strtoui64(exp.constant, null, 0);
3052 //_dpl(0, "0x", address);
3053 // snprintf(value, sizeof(value), "0x%08x ", address);
3055 if(address > 0xFFFFFFFFLL)
3056 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
3058 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
3059 value[sizeof(value)-1] = 0;
3062 strcat(value, $"Null string");
3065 String string = new char[4097];
3067 bool success = false;
3071 for(start = 0; !done && start + size <= 4096; start += size)
3076 // Try to read 256 bytes at a time, then half if that fails
3077 s = GdbReadMemory(address + start, size);
3081 memcpy(string + start, s, size);
3082 string[start + size] = 0;
3083 if(size == 1 || memchr(s, 0, size))
3097 int len = strlen(value);
3099 if(UTF8Validate(string))
3104 for(c = 0; (ch = string[c]); c++)
3107 value[len++] = '\0';
3112 ISO8859_1toUTF8(string, value + len, strlen(value) - len - 30);
3113 strcat(value, ") (ISO8859-1)");
3117 strcat(value, $"Empty string");
3120 strcat(value, $"Couldn't read memory");
3123 wh.value = CopyString(value);
3126 else if(wh.type && wh.type.kind == classType && wh.type._class &&
3127 wh.type._class.registered && wh.type._class.registered.type == enumClass)
3129 Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
3130 EnumClassData enumeration = (EnumClassData)enumClass.data;
3134 if(!strcmp(enumClass.dataTypeString, "uint64"))
3136 uint64 v = strtoull(exp.constant, null, 0);
3137 value = *(int64*)&v;
3140 value = strtoll(exp.constant, null, 0);
3142 for(item = enumeration.values.first; item; item = item.next)
3143 if(item.data == value)
3146 wh.value = CopyString(item.name);
3148 wh.value = PrintString($"Invalid Enum Value (", value, ")");
3151 else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class &&
3152 wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
3159 if(exp.constant[0] == '\'')
3161 if((int)((byte *)exp.constant)[1] > 127)
3164 value = UTF8GetChar(exp.constant + 1, &nb);
3165 if(nb < 2) value = exp.constant[1];
3166 signedValue = value;
3170 signedValue = exp.constant[1];
3172 // Precomp Syntax error with boot strap here:
3173 byte b = (byte)(char)signedValue;
3174 value = (unichar) b;
3180 if(wh.type.kind == charType && wh.type.isSigned)
3182 signedValue = (int)(char)strtol(exp.constant, null, 0);
3184 // Precomp Syntax error with boot strap here:
3185 byte b = (byte)(char)signedValue;
3186 value = (unichar) b;
3191 value = (uint)strtoul(exp.constant, null, 0);
3192 signedValue = (int)value;
3196 UTF32toUTF8Len(&value, 1, charString, 5);
3198 snprintf(string, sizeof(string), "\'\\0' (0)");
3199 else if(value == '\t')
3200 snprintf(string, sizeof(string), "\'\\t' (%d)", value);
3201 else if(value == '\n')
3202 snprintf(string, sizeof(string), "\'\\n' (%d)", value);
3203 else if(value == '\r')
3204 snprintf(string, sizeof(string), "\'\\r' (%d)", value);
3205 else if(wh.type.kind == charType && wh.type.isSigned)
3206 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
3207 else if(value > 256 || wh.type.kind != charType)
3209 if(value > 0x10FFFF || !GetCharCategory(value))
3210 snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3212 snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3215 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3216 string[sizeof(string)-1] = 0;
3218 wh.value = CopyString(string);
3223 wh.value = CopyString(exp.constant);
3229 bool genericError = true;
3230 if(exp.expType && exp.hasAddress)
3232 bool showAddress = false;
3233 switch(exp.expType.kind)
3245 Symbol s = exp.expType._class;
3248 Class c = s.registered;
3249 if(c.type == noHeadClass || c.type == normalClass)
3257 wh.value = PrintHexUInt64(exp.address);
3259 genericError = false;
3263 // NOTE: This should ideally be handled with a specific error message
3264 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
3270 snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3271 if(exp) FreeExpression(exp);
3274 SetPrivateModule(backupPrivateModule);
3275 SetCurrentContext(backupContext);
3276 SetTopContext(backupContext);
3277 SetGlobalContext(backupContext);
3278 SetThisClass(backupThisClass);
3281 // wh.value = CopyString("No source file found for selected frame");
3283 watchmsg[sizeof(watchmsg)-1] = 0;
3285 wh.value = CopyString(watchmsg);
3287 ide.watchesView.UpdateWatch(wh);
3291 void EvaluateWatches()
3293 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3294 WatchesLinkCodeEditor();
3295 if(state == stopped)
3297 for(wh : ide.workspace.watches)
3302 char * ::GdbEvaluateExpression(char * expression)
3304 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3307 GdbCommand(0, false, "-data-evaluate-expression \"%s\"", expression);
3309 ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3313 // to be removed... use GdbReadMemory that returns a byte array instead
3314 char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3316 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3321 _dpl(0, "GdbReadMemoryString called with size = 0!");
3323 // GdbCommand(0, false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3324 if(GetRuntimePlatform() == win32)
3325 GdbCommand(0, false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3327 GdbCommand(0, false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3329 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3333 byte * ::GdbReadMemory(uint64 address, int bytes)
3335 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3338 //GdbCommand(0, false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3339 if(GetRuntimePlatform() == win32)
3340 GdbCommand(0, false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3342 GdbCommand(0, false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3345 _dpl(0, "GdbReadMemory called with bytes = 0!");
3348 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3349 else if(eval.result && strcmp(eval.result, "N/A"))
3351 byte * result = new byte[bytes];
3352 char * string = eval.result;
3356 result[c++] = (byte)strtol(string, &string, 10);
3373 bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3376 char * s1 = null; char * s2 = null;
3377 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3378 "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3379 "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3380 "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3381 "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor, ")");
3382 delete s1; delete s2;
3386 bool conditionMet = true;
3387 if(bpUser.condition)
3389 if(WatchesLinkCodeEditor())
3390 conditionMet = ResolveWatch(bpUser.condition);
3392 conditionMet = false;
3407 if(stopItem.frame.line && bpUser.line != stopItem.frame.line)
3409 // updating user breakpoint on hit location difference
3410 // todo, print something?
3411 bpUser.line = stopItem.frame.line;
3412 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3413 ide.workspace.Save();
3416 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3421 if(bpInternal.type == internalModulesLoaded)
3423 if(userAction == stepOver)
3425 if((bpInternal.type == internalEntry && ((intBpMain && intBpMain.inserted) || (intBpWinMain && intBpWinMain.inserted))) ||
3426 (bpInternal.type == internalMain && intBpWinMain && intBpWinMain.inserted))
3429 if(!bpUser && !userAction.breaksOnInternalBreakpoint)
3431 if(userAction == stepOut)
3432 StepOut(ignoreBreakpoints);
3438 if(!bpUser && !bpInternal)
3444 void ValgrindTargetThreadExit()
3446 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValgrindTargetThreadExit()");
3449 vgTargetHandle.Wait();
3450 delete vgTargetHandle;
3452 HandleExit(null, null);
3455 void GdbThreadExit()
3457 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3458 if(state != terminated)
3460 _ChangeState(terminated);
3461 targetProcessId = 0;
3462 ClearBreakDisplay();
3467 serialSemaphore.Release();
3472 ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3473 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3475 HideDebuggerViews();
3477 //_ChangeState(terminated);
3481 void GdbThreadMain(char * output)
3485 Array<char *> outTokens { minAllocSize = 50 };
3486 Array<char *> subTokens { minAllocSize = 50 };
3487 DebugListItem item { };
3488 DebugListItem item2 { };
3489 bool setWaitingForPID = false;
3491 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3492 #ifdef GDB_DEBUG_CONSOLE
3493 // _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3496 #ifdef GDB_DEBUG_OUTPUT
3498 int len = strlen(output);
3506 for(c = 0; c < len / 1024; c++)
3508 strncpy(tmp, start, 1024);
3509 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3512 ide.outputView.gdbBox.Logf("out: %s\n", start);
3516 ide.outputView.gdbBox.Logf("out: %s\n", output);
3520 #ifdef GDB_DEBUG_CONSOLE
3521 strcpy(lastGdbOutput, output);
3523 #ifdef GDB_DEBUG_GUI
3524 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3531 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3534 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3537 if(!entryPoint && (t = strstr(output, "Entry point:")))
3539 char * addr = t + strlen("Entry point:");
3541 if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3544 while(isxdigit(*++t));
3546 for(bp : sysBPs; bp.type == internalEntry)
3549 bp.enabled = entryPoint = true;
3557 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3559 //if(outTokens.count == 1)
3564 _ChangeState(loaded);
3565 targetProcessId = 0;
3566 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3568 if(!strcmp(item.name, "reason"))
3570 char * reason = item.value;
3571 StripQuotes(reason, reason);
3572 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3575 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3577 StripQuotes(item2.value, item2.value);
3578 if(!strcmp(item2.name, "exit-code"))
3579 exitCode = item2.value;
3585 HandleExit(reason, exitCode);
3589 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3592 HandleExit(null, null);
3595 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3597 if(!strcmp(item.name, "bkpt"))
3599 sentBreakInsert = false;
3605 bpItem = ParseBreakpoint(item.value, outTokens);
3606 //breakType = bpValidation;
3608 else if(!strcmp(item.name, "depth"))
3610 StripQuotes(item.value, item.value);
3611 frameCount = atoi(item.value);
3613 stackFrames.Free(Frame::Free);
3615 else if(!strcmp(item.name, "stack"))
3618 if(stackFrames.count)
3619 ide.callStackView.Logf("...\n");
3622 item.value = StripBrackets(item.value);
3623 TokenizeList(item.value, ',', subTokens);
3624 for(i = 0; i < subTokens.count; i++)
3626 if(TokenizeListItem(subTokens[i], item))
3628 if(!strcmp(item.name, "frame"))
3631 stackFrames.Add(frame);
3632 item.value = StripCurlies(item.value);
3633 ParseFrame(frame, item.value);
3634 if(frame.file && frame.from)
3635 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3639 if(activeFrameLevel == -1)
3641 if(ide.projectView.IsModuleInProject(frame.file));
3643 if(frame.level != 0)
3645 //stopItem.frame = frame;
3646 breakType = selectFrame;
3649 activeFrame = frame;
3650 activeFrameLevel = frame.level;
3653 ide.callStackView.Logf("%3d ", frame.level);
3654 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3655 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3656 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3657 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3658 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3659 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3660 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3661 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3663 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3668 ide.callStackView.Logf("%3d ", frame.level);
3673 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3677 ide.callStackView.Logf("%s\n", frame.func);
3679 ide.callStackView.Logf($"unknown source\n");
3683 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3686 if(activeFrameLevel == -1)
3688 activeFrameLevel = 0;
3689 activeFrame = stackFrames.first;
3691 ide.callStackView.Home();
3693 subTokens.RemoveAll();
3695 /*else if(!strcmp(item.name, "frame"))
3698 item.value = StripCurlies(item.value);
3699 ParseFrame(&frame, item.value);
3701 else if(!strcmp(item.name, "thread-ids"))
3703 ide.threadsView.Clear();
3704 item.value = StripCurlies(item.value);
3705 TokenizeList(item.value, ',', subTokens);
3706 for(i = subTokens.count - 1; ; i--)
3708 if(TokenizeListItem(subTokens[i], item))
3710 if(!strcmp(item.name, "thread-id"))
3713 StripQuotes(item.value, item.value);
3714 value = atoi(item.value);
3715 ide.threadsView.Logf("%3d \n", value);
3718 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3723 ide.threadsView.Home();
3725 subTokens.RemoveAll();
3726 //if(!strcmp(outTokens[2], "number-of-threads"))
3728 else if(!strcmp(item.name, "new-thread-id"))
3730 StripQuotes(item.value, item.value);
3731 activeThread = atoi(item.value);
3733 else if(!strcmp(item.name, "value"))
3735 StripQuotes(item.value, item.value);
3736 eval.result = CopyString(item.value);
3737 eval.active = false;
3739 else if(!strcmp(item.name, "addr"))
3741 for(i = 2; i < outTokens.count; i++)
3743 if(TokenizeListItem(outTokens[i], item))
3745 if(!strcmp(item.name, "total-bytes"))
3747 StripQuotes(item.value, item.value);
3748 eval.bytes = atoi(item.value);
3750 else if(!strcmp(item.name, "next-row"))
3752 StripQuotes(item.value, item.value);
3753 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3755 else if(!strcmp(item.name, "memory"))
3759 //StripQuotes(item.value, item.value);
3760 item.value = StripBrackets(item.value);
3761 // this should be treated as a list...
3762 item.value = StripCurlies(item.value);
3763 TokenizeList(item.value, ',', subTokens);
3764 for(j = 0; j < subTokens.count; j++)
3766 if(TokenizeListItem(subTokens[j], item))
3768 if(!strcmp(item.name, "data"))
3770 item.value = StripBrackets(item.value);
3771 StripQuotes2(item.value, item.value);
3772 eval.result = CopyString(item.value);
3773 eval.active = false;
3777 subTokens.RemoveAll();
3782 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3783 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3785 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3788 else if(!strcmp(outTokens[0], "^running"))
3790 waitingForPID = true;
3791 setWaitingForPID = true;
3792 ClearBreakDisplay();
3794 else if(!strcmp(outTokens[0], "^exit"))
3796 _ChangeState(terminated);
3797 // ide.outputView.debugBox.Logf("Exit\n");
3798 // ide.Update(null);
3800 serialSemaphore.Release();
3802 else if(!strcmp(outTokens[0], "^error"))
3806 sentBreakInsert = false;
3807 breakpointError = true;
3810 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3812 if(!strcmp(item.name, "msg"))
3814 StripQuotes(item.value, item.value);
3817 eval.active = false;
3819 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3820 eval.error = symbolNotFound;
3821 else if(strstr(item.value, "Cannot access memory at address"))
3822 eval.error = memoryCantBeRead;
3824 eval.error = unknown;
3826 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3829 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3832 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3834 _ChangeState(stopped);
3835 gdbHandle.Printf("-exec-continue\n");
3837 else if(!strcmp(item.value, "ptrace: No such process."))
3839 _ChangeState(loaded);
3840 targetProcessId = 0;
3842 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3845 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3847 _ChangeState(loaded);
3848 targetProcessId = 0;
3850 else if(strstr(item.value, "No such file or directory."))
3852 _ChangeState(loaded);
3853 targetProcessId = 0;
3855 else if(strstr(item.value, "During startup program exited with code "))
3857 _ChangeState(loaded);
3858 targetProcessId = 0;
3863 if(strlen(item.value) < MAX_F_STRING)
3866 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3870 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3876 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3879 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3880 outTokens.RemoveAll();
3883 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3886 if(TokenizeList(output, ',', outTokens))
3888 if(!strcmp(outTokens[0], "=library-loaded"))
3889 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, false);
3890 else if(!strcmp(outTokens[0], "=shlibs-added"))
3891 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, true);
3892 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3893 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3894 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3895 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3896 !strcmp(outTokens[0], "=breakpoint-modified"))
3897 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3898 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3899 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3900 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3901 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3903 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3905 outTokens.RemoveAll();
3909 if(TokenizeList(output, ',', outTokens))
3911 if(!strcmp(outTokens[0],"*running"))
3913 waitingForPID = true;
3914 setWaitingForPID = true;
3916 else if(!strcmp(outTokens[0], "*stopped"))
3919 _ChangeState(stopped);
3921 for(tk = 1; tk < outTokens.count; tk++)
3923 if(TokenizeListItem(outTokens[tk], item))
3925 if(!strcmp(item.name, "reason"))
3927 char * reason = item.value;
3928 StripQuotes(reason, reason);
3929 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3932 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3935 StripQuotes(item2.value, item2.value);
3936 if(!strcmp(item2.name, "exit-code"))
3937 exitCode = item2.value;
3943 HandleExit(reason, exitCode);
3946 else if(!strcmp(reason, "breakpoint-hit") ||
3947 !strcmp(reason, "function-finished") ||
3948 !strcmp(reason, "end-stepping-range") ||
3949 !strcmp(reason, "location-reached") ||
3950 !strcmp(reason, "signal-received"))
3954 if(stopItem) _dpl(0, "problem");
3956 stopItem = GdbDataStop { };
3957 stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3959 for(i = tk+1; i < outTokens.count; i++)
3961 TokenizeListItem(outTokens[i], item);
3962 StripQuotes(item.value, item.value);
3963 if(!strcmp(item.name, "thread-id"))
3964 stopItem.threadid = atoi(item.value);
3965 else if(!strcmp(item.name, "frame"))
3967 item.value = StripCurlies(item.value);
3968 ParseFrame(stopItem.frame, item.value);
3970 else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3971 stopItem.bkptno = atoi(item.value);
3972 else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3973 stopItem.gdbResultVar = CopyString(item.value);
3974 else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3975 stopItem.returnValue = CopyString(item.value);
3976 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3977 stopItem.name = CopyString(item.value);
3978 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3979 stopItem.meaning = CopyString(item.value);
3980 else if(!strcmp(item.name, "stopped-threads"))
3981 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3982 else if(!strcmp(item.name, "core"))
3983 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3984 else if(!strcmp(item.name, "disp"))
3985 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3987 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3990 if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
4006 event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
4010 else if(!strcmp(reason, "watchpoint-trigger"))
4011 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
4012 else if(!strcmp(reason, "read-watchpoint-trigger"))
4013 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
4014 else if(!strcmp(reason, "access-watchpoint-trigger"))
4015 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
4016 else if(!strcmp(reason, "watchpoint-scope"))
4017 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
4019 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
4027 if(usingValgrind && event == none && !stopItem)
4028 event = valgrindStartPause;
4033 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
4034 outTokens.RemoveAll();
4037 if(!strcmpi(output, "(gdb) "))
4041 Time startTime = GetTime();
4042 char exeFile[MAX_LOCATION];
4043 int oldProcessID = targetProcessId;
4044 GetLastDirectory(targetFile, exeFile);
4046 while(!targetProcessId/*true*/)
4048 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
4049 if(targetProcessId) break;
4050 // Can't break on Peek(), GDB is giving us =library and other info before the process is listed in /proc
4051 // if(gdbHandle.Peek()) break;
4053 if(gdbHandle.Peek() && GetTime() - startTime > 2.5) // Give the process 2.5 seconds to show up in /proc
4058 _ChangeState(running);
4059 else if(!oldProcessID)
4061 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
4062 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
4063 gdbHandle.Printf("-gdb-exit\n");
4065 _ChangeState(terminated); //loaded;
4070 for(bp : ide.workspace.breakpoints)
4071 bp.inserted = false;
4074 bp.inserted = false;
4076 bpRunToCursor.inserted = false;
4078 ide.outputView.debugBox.Logf($"Debugging stopped\n");
4079 ClearBreakDisplay();
4081 #if defined(__unix__)
4082 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
4084 progThread.terminate = true;
4087 fifoFile.CloseInput();
4094 DeleteFile(progFifoPath);
4095 progFifoPath[0] = '\0';
4102 serialSemaphore.Release();
4105 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
4109 if(!strncmp(output, "&\"warning:", 10))
4112 content = strstr(output, "\"");
4113 StripQuotes(content, content);
4114 content = strstr(content, ":");
4120 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
4127 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
4129 if(!setWaitingForPID)
4130 waitingForPID = false;
4131 setWaitingForPID = false;
4139 // From GDB Output functions
4140 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens, bool shlibs)
4142 char path[MAX_LOCATION] = "";
4143 char file[MAX_FILENAME] = "";
4144 //bool symbolsLoaded = false;
4145 DebugListItem item { };
4146 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
4147 for(token : outTokens)
4149 if(TokenizeListItem(token, item))
4151 if(!strcmp(item.name, "target-name"))
4153 StripQuotes(item.value, path);
4154 MakeSystemPath(path);
4155 GetLastDirectory(path, file);
4157 else if(!strcmp(item.name, "symbols-loaded"))
4159 //symbolsLoaded = (atoi(item.value) == 1);
4161 else if(!strcmp(item.name, "shlib-info"))
4163 DebugListItem subItem { };
4164 Array<char *> tokens { minAllocSize = 50 };
4165 item.value = StripBrackets(item.value);
4166 TokenizeList(item.value, ',', tokens);
4169 if(TokenizeListItem(t, subItem))
4171 if(!strcmp(subItem.name, "path"))
4173 StripQuotes(subItem.value, path);
4174 MakeSystemPath(path);
4175 GetLastDirectory(path, file);
4176 //symbolsLoaded = true;
4187 if(path[0] && file[0])
4189 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
4193 char prjTargetPath[MAX_LOCATION];
4194 char prjTargetFile[MAX_FILENAME];
4195 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
4196 strcpy(prjTargetPath, prj.topNode.path);
4197 PathCat(prjTargetPath, targetDirExp.dir);
4198 delete targetDirExp;
4199 prjTargetFile[0] = '\0';
4200 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
4201 PathCat(prjTargetPath, prjTargetFile);
4202 MakeSystemPath(prjTargetPath);
4204 match = !fstrcmp(prjTargetFile, file);
4205 if(!match && (dot = strstr(prjTargetFile, ".so.")))
4207 char * dot3 = strstr(dot+4, ".");
4211 match = !fstrcmp(prjTargetFile, file);
4216 match = !fstrcmp(prjTargetFile, file);
4221 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
4222 /* -- 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)
4224 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
4226 match = !fstrcmp(prjTargetPath, path);
4227 if(!match && (dot = strstr(prjTargetPath, ".so.")))
4229 char * dot3 = strstr(dot+4, ".");
4233 match = !fstrcmp(prjTargetPath, path);
4238 match = !fstrcmp(prjTargetPath, path);
4242 projectsLibraryLoaded[prj.name] = true;
4244 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4251 void FGOBreakpointModified(Array<char *> outTokens)
4253 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4255 DebugListItem item { };
4256 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4258 if(!strcmp(item.name, "bkpt"))
4260 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4268 ExpressionType ::DebugEvalExpTypeError(char * result)
4270 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4275 case symbolNotFound:
4276 return symbolErrorExp;
4277 case memoryCantBeRead:
4278 return memoryErrorExp;
4280 return unknownErrorExp;
4283 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4286 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4287 if(ide.projectView && ide.debugger.state == stopped)
4289 result = GdbEvaluateExpression(expression);
4290 *error = DebugEvalExpTypeError(result);
4295 *error = noDebuggerErrorExp;
4300 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4303 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4304 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4305 if(!result || !strcmp(result, "N/A"))
4306 *error = memoryErrorExp;
4308 *error = DebugEvalExpTypeError(result);
4313 class ValgrindLogThread : Thread
4319 static char output[4096];
4320 bool lastLineEmpty = true;
4321 Array<char> dynamicBuffer { minAllocSize = 4096 };
4322 File oldValgrindHandle = vgLogFile;
4323 incref oldValgrindHandle;
4326 while(debugger.state != terminated && vgLogFile && vgLogFile.input)
4331 result = vgLogFile.Read(output, 1, sizeof(output));
4333 if(debugger.state == terminated || !vgLogFile) // || vgLogFile.Eof()
4340 for(c = 0; c<result; c++)
4342 if(output[c] == '\n')
4344 int pos = dynamicBuffer.size;
4345 dynamicBuffer.size += c - start;
4346 memcpy(&dynamicBuffer[pos], output + start, c - start);
4347 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4348 dynamicBuffer.size++;
4349 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4351 // printf("%s\n", dynamicBuffer.array);
4353 if(strstr(&dynamicBuffer[0], "vgdb me ..."))
4354 debugger.serialSemaphore.Release();
4356 char * s = strstr(&dynamicBuffer[0], "==");
4358 s = strstr(s+2, "== ");
4362 if(s[0] == '\0' && !lastLineEmpty)
4365 lastLineEmpty = true;
4366 dynamicBuffer[0] = '\0';
4375 if(strstr(s, "vgdb me ..."))
4377 if(strstr(s, "(action on error) vgdb me ..."))
4378 ide.outputView.debugBox.Logf($"...breaked on Valgrind error (F5 to resume)\n");
4385 if(strstr(s, "TO DEBUG THIS PROCESS USING GDB: start GDB like this"))
4391 if(strstr(s, "and then give GDB the following command"))
4397 if(strstr(s, "/path/to/gdb") || strstr(s, "target remote | /usr/lib/valgrind/../../bin/vgdb --pid="))
4403 if(strstr(s, "--pid is optional if only one valgrind process is running"))
4409 if((s = strstr(s, "; rerun with -h for copyright info")))
4421 if(lastLineEmpty && t[0] != '\0')
4422 lastLineEmpty = false;
4425 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4427 dynamicBuffer.size = 0;
4433 int pos = dynamicBuffer.size;
4434 dynamicBuffer.size += c - start;
4435 memcpy(&dynamicBuffer[pos], output + start, c - start);
4438 else if(debugger.state == stopped)
4441 printf("Got end of file from GDB!\n");
4448 delete dynamicBuffer;
4449 _dpl2(_dpct, dplchan::debuggerCall, 0, "ValgrindLogThreadExit");
4450 //if(oldValgrindHandle == vgLogFile)
4451 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4452 delete oldValgrindHandle;
4458 class ValgrindTargetThread : Thread
4464 static char output[4096];
4465 Array<char> dynamicBuffer { minAllocSize = 4096 };
4466 DualPipe oldValgrindHandle = vgTargetHandle;
4467 incref oldValgrindHandle;
4470 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4474 result = vgTargetHandle.Read(output, 1, sizeof(output));
4476 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4483 for(c = 0; c<result; c++)
4485 if(output[c] == '\n')
4487 int pos = dynamicBuffer.size;
4488 dynamicBuffer.size += c - start;
4489 memcpy(&dynamicBuffer[pos], output + start, c - start);
4490 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4491 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4492 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4493 dynamicBuffer.size++;
4494 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4496 // printf("%s\n", dynamicBuffer.array);
4498 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4500 dynamicBuffer.size = 0;
4506 int pos = dynamicBuffer.size;
4507 dynamicBuffer.size += c - start;
4508 memcpy(&dynamicBuffer[pos], output + start, c - start);
4514 printf("Got end of file from GDB!\n");
4518 delete dynamicBuffer;
4519 //if(oldValgrindHandle == vgTargetHandle)
4520 debugger.ValgrindTargetThreadExit();
4521 delete oldValgrindHandle;
4527 class GdbThread : Thread
4533 static char output[4096];
4534 Array<char> dynamicBuffer { minAllocSize = 4096 };
4535 DualPipe oldGdbHandle = gdbHandle;
4536 incref oldGdbHandle;
4539 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4543 result = gdbHandle.Read(output, 1, sizeof(output));
4545 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4552 for(c = 0; c<result; c++)
4554 if(output[c] == '\n')
4556 int pos = dynamicBuffer.size;
4557 dynamicBuffer.size += c - start;
4558 memcpy(&dynamicBuffer[pos], output + start, c - start);
4559 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4560 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4561 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4562 dynamicBuffer.size++;
4563 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4565 // _dpl(0, dynamicBuffer.array);
4567 debugger.GdbThreadMain(&dynamicBuffer[0]);
4568 dynamicBuffer.size = 0;
4574 int pos = dynamicBuffer.size;
4575 dynamicBuffer.size += c - start;
4576 memcpy(&dynamicBuffer[pos], output + start, c - start);
4582 _dpl(0, "Got end of file from GDB!");
4586 delete dynamicBuffer;
4587 //if(oldGdbHandle == gdbHandle)
4588 debugger.GdbThreadExit();
4589 delete oldGdbHandle;
4595 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4596 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4598 #if defined(__unix__)
4603 #include <sys/types.h>
4608 class ProgramThread : Thread
4613 //bool result = true;
4614 //bool fileCreated = false;
4615 //mode_t mask = 0600;
4616 static char output[1000];
4619 /*if(!mkfifo(progFifoPath, mask))
4626 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4630 if(FileExists(progFifoPath)) //fileCreated)
4632 fifoFile = FileOpen(progFifoPath, read);
4636 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4641 fd = fileno((FILE *)fifoFile.input);
4642 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4646 while(!terminate && fifoFile && !fifoFile.Eof())
4649 struct timeval time;
4657 /*selectResult = */select(fd + 1, &rs, null, null, &time);
4658 if(FD_ISSET(fd, &rs))
4660 int result = (int)read(fd, output, sizeof(output)-1);
4661 if(!result || (result < 0 && errno != EAGAIN))
4665 output[result] = '\0';
4666 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4669 ide.outputView.debugBox.Log(output);
4678 //fifoFile.CloseInput();
4681 ide.outputView.debugBox.Log("\n");
4685 if(FileExists(progFifoPath)) //fileCreated)
4687 DeleteFile(progFifoPath);
4688 progFifoPath[0] = '\0';
4696 class Argument : struct
4698 Argument prev, next;
4700 property char * name { set { delete name; if(value) name = CopyString(value); } }
4702 property char * val { set { delete val; if(value) val = CopyString(value); } }
4716 class Frame : struct
4721 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4723 property char * func { set { delete func; if(value) func = CopyString(value); } }
4727 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4729 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4730 char * absoluteFile;
4731 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4740 delete absoluteFile;
4741 args.Free(Argument::Free);
4750 class GdbDataStop : struct
4752 DebuggerReason reason;
4767 char * gdbResultVar;
4777 if(reason == signalReceived)
4782 else if(reason == functionFinished)
4784 delete gdbResultVar;
4788 if(frame) frame.Free();
4797 class GdbDataBreakpoint : struct
4801 property char * number { set { delete number; if(value) number = CopyString(value); } }
4803 property char * type { set { delete type; if(value) type = CopyString(value); } }
4805 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4808 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4810 property char * func { set { delete func; if(value) func = CopyString(value); } }
4812 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4814 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4817 property char * at { set { delete at; if(value) at = CopyString(value); } }
4820 Array<GdbDataBreakpoint> multipleBPs;
4825 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4836 if(multipleBPs) multipleBPs.Free();
4842 ~GdbDataBreakpoint()
4848 class Breakpoint : struct
4853 property const char * function { set { delete function; if(value) function = CopyString(value); } }
4854 char * relativeFilePath;
4855 property const char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4856 char * absoluteFilePath;
4857 property const char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4859 property const char * location { set { delete location; if(value) location = CopyString(value); } }
4868 BreakpointType type;
4870 GdbDataBreakpoint bp;
4873 property const char * address { set { delete address; if(value) address = CopyString(value); } }
4875 void ParseLocation()
4877 char * prjName = null;
4878 char * filePath = null;
4881 char fullPath[MAX_LOCATION];
4882 if(location[0] == '(' && location[1] && (file = strchr(location+2, ')')) && file[1])
4884 prjName = new char[file-location];
4885 strncpy(prjName, location+1, file-location-1);
4886 prjName[file-location-1] = '\0';
4891 if((line = strchr(file+1, ':')))
4893 filePath = new char[strlen(file)+1];
4894 strncpy(filePath, file, line-file);
4895 filePath[line-file] = '\0';
4899 filePath = CopyString(file);
4900 property::relativeFilePath = filePath;
4903 for(prj : ide.workspace.projects)
4905 if(!strcmp(prjName, prj.name))
4907 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4909 property::absoluteFilePath = fullPath;
4916 this.line = atoi(line);
4920 Project prj = ide.project;
4921 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4923 property::absoluteFilePath = fullPath;
4927 if(!absoluteFilePath)
4928 property::absoluteFilePath = "";
4933 char * CopyLocationString(bool removePath)
4936 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4937 bool removingPath = removePath && file;
4940 char * fileName = new char[MAX_FILENAME];
4941 GetLastDirectory(file, fileName);
4947 location = PrintString(file, ":", function);
4949 location = CopyString(function);
4952 location = PrintString(file, ":", line);
4958 char * CopyUserLocationString()
4961 char * loc = CopyLocationString(false);
4963 if(absoluteFilePath)
4965 for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4967 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4976 location = PrintString("(", prj.name, ")", loc);
4986 if(relativeFilePath && relativeFilePath[0])
4988 char * location = CopyUserLocationString();
4989 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4992 f.Printf(" ~ %s\n", condition.expression);
5000 delete relativeFilePath;
5001 delete absoluteFilePath;
5021 class Watch : struct
5032 f.Printf(" ~ %s\n", expression);
5056 class DebugListItem : struct
5062 struct DebugEvaluationData
5067 uint64 nextBlockAddress;
5069 DebuggerEvaluationError error;
5072 class CodeLocation : struct
5075 char * absoluteFile;
5078 CodeLocation ::ParseCodeLocation(char * location)
5082 char * colon = null;
5084 char loc[MAX_LOCATION];
5085 strcpy(loc, location);
5086 for(temp = loc; (temp = strstr(temp, ":")); temp++)
5094 int line = atoi(colon);
5097 CodeLocation codloc { line = line };
5098 codloc.file = CopyString(loc);
5099 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
5111 delete absoluteFile;
5120 void GDBFallBack(Expression exp, String expString)
5123 ExpressionType evalError = dummyExp;
5124 result = Debugger::EvaluateExpression(expString, &evalError);
5127 FreeExpContents(exp);
5128 exp.constant = result;
5129 exp.type = constantExp;
5133 static Project WorkspaceGetFileOwner(const char * absolutePath)
5135 Project owner = null;
5136 for(prj : ide.workspace.projects)
5138 if(prj.topNode.FindByFullPath(absolutePath, false))
5145 WorkspaceGetObjectFileNode(absolutePath, &owner);
5149 static ProjectNode WorkspaceGetObjectFileNode(const char * filePath, Project * project)
5151 ProjectNode node = null;
5152 char ext[MAX_EXTENSION];
5153 GetExtension(filePath, ext);
5156 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5159 char fileName[MAX_FILENAME];
5160 GetLastDirectory(filePath, fileName);
5163 DotMain dotMain = DotMain::FromFileName(fileName);
5164 for(prj : ide.workspace.projects)
5166 if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
5179 static ProjectNode ProjectGetObjectFileNode(Project project, const char * filePath)
5181 ProjectNode node = null;
5182 char ext[MAX_EXTENSION];
5183 GetExtension(filePath, ext);
5186 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5189 char fileName[MAX_FILENAME];
5190 GetLastDirectory(filePath, fileName);
5193 DotMain dotMain = DotMain::FromFileName(fileName);
5194 node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
5201 static void WorkspaceGetRelativePath(const char * absolutePath, char * relativePath, Project * owner)
5203 Project prj = WorkspaceGetFileOwner(absolutePath);
5207 prj = ide.workspace.projects.firstIterator.data;
5210 MakePathRelative(absolutePath, prj.topNode.path, relativePath);
5211 MakeSlashPath(relativePath);
5214 relativePath[0] = '\0';
5217 static bool ProjectGetAbsoluteFromRelativePath(Project project, const char * relativePath, char * absolutePath)
5219 ProjectNode node = project.topNode.FindWithPath(relativePath, false);
5221 node = ProjectGetObjectFileNode(project, relativePath);
5224 strcpy(absolutePath, node.project.topNode.path);
5225 PathCat(absolutePath, relativePath);
5226 MakeSlashPath(absolutePath);
5228 return node != null;