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
33 #include <sys/time.h> // Required on Apple...
47 sprintf(s[0], "%04d", now.year);
48 sprintf(s[1], "%02d", now.month+1);
49 sprintf(s[2], "%02d", now.day);
50 sprintf(s[3], "%02d", now.hour);
51 sprintf(s[4], "%02d", now.minute);
52 sprintf(s[5], "%02d", now.second);
53 time = PrintString("*", s[0], s[1], s[2], "-", s[3], s[4], s[5], "*");
59 // use =0 to disable printing of specific channels
61 static enum dplchan { none, gdbProtoIgnored=0/*1*/, gdbProtoUnknown=2, gdbOutput=3/*3*/, gdbCommand=4/*4*/, debuggerCall=0/*5*/, debuggerProblem=6,
62 debuggerUserAction=7,debuggerState=8, debuggerBreakpoints=9, debuggerWatches=0/*10*/, debuggerTemp=0 };
64 static enum dplchan { none, gdbProtoIgnored=0, gdbProtoUnknown=0, gdbOutput=0, gdbCommand=0, debuggerCall=0, debuggerProblem=0,
65 debuggerUserAction=0,debuggerState=0, debuggerBreakpoints=0, debuggerWatches=0, debuggerTemp=0 };
67 static char * _dpct[] = {
69 "GDB Protocol Ignored",
70 "GDB Protocol ***Unknown***",
74 "Debugger ***Problem***",
75 "Debugger::ChangeUserAction",
76 "Debugger::ChangeState",
79 "-----> Temporary Message",
83 // TODO if(strlen(item.value) < MAX_F_STRING)
87 #define _dpl2(...) __dpl2(__FILE__, __LINE__, ##__VA_ARGS__)
91 static void __dpl2(char * file, int line, char ** channels, int channel, int indent, typed_object object, ...)
93 bool chan = channel && channels && channels[channel];
96 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 len = PrintStdArgsToBuffer(string, sizeof(string), object, args);
111 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
112 static void __dpl(char * file, int line, int indent, 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)
224 static char * CopyUnescapedSystemPath(char * p)
226 char * d = new char[strlen(p) + 1];
228 #if defined(__WIN32__)
229 ChangeCh(d, '/', '\\');
234 static char * CopyUnescapedUnixPath(char * p)
236 char * d = new char[strlen(p) + 1];
238 #if defined(__WIN32__)
239 ChangeCh(d, '\\', '/');
244 static char * CopyUnescapedString(char * s)
246 char * d = new char[strlen(s) + 1];
251 // String Unescape Copy
253 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
254 // Seems very similar to ReadString in pass15.ec (which also misses numeric escape codes :) )
256 static void struscpy(char * d, char * s)
308 static char * StripBrackets(char * string)
310 int length = strlen(string);
311 if(length > 1 && *string == '[' && string[length - 1] == ']')
314 string[length - 1] = '\0';
321 static char * StripCurlies(char * string)
323 int length = strlen(string);
324 if(length > 1 && *string == '{' && string[length - 1] == '}')
327 string[length - 1] = '\0';
334 static int StringGetInt(char * string, int start)
337 int i, len = strlen(string);
339 for(i = start; i < len && i < start + 8; i++)
341 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')
342 strncat(number, &string[i], 1);
349 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
353 bool quoted = false, escaped = false;
354 char * start = string, ch;
356 for(; (ch = *string); string++)
363 if(escaped || ch != '\"')
364 escaped = !escaped && ch == '\\';
370 else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
372 else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
374 else if(ch == seperator && !level)
383 //tokens[count] = start;
384 //tokens[count++] = start;
391 static bool TokenizeListItem(char * string, DebugListItem item)
393 char * equal = strstr(string, "=");
407 static bool CheckCommandAvailable(const char * command)
409 bool available = false;
411 char * name = new char[MAX_FILENAME];
412 char * pathVar = new char[maxPathLen];
414 GetEnvironment("PATH", pathVar, maxPathLen);
415 count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
416 strcpy(name, command);
420 const char * extensions[] = { "exe", "com", "bat", null };
421 for(e=0; extensions[e]; e++)
423 ChangeExtension(name, extensions[e], name);
425 for(c=0; c<count; c++)
427 FileListing fl { paths[c] };
430 if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
448 // define GdbGetLineSize = 1638400;
449 define GdbGetLineSize = 5638400;
450 #if defined(__unix__)
451 char progFifoPath[MAX_LOCATION];
452 char progFifoDir[MAX_LOCATION];
455 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
458 none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause, locationReached;
460 property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd || this == locationReached); } };
462 enum DebuggerAction { none, internal, restart, stop, selectFrame, advance }; //, bpValidation
465 unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit, locationReached
466 //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope,
467 //exited, exitedNormally, exitedSignalled;
471 none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad, internalEntry;
473 property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad || this == internalEntry); } };
474 property bool isUser { get { return (this == user || this == runToCursor); } };
476 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
477 enum DebuggerUserAction
479 none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepUntil, stepOut, runToCursor;
480 property bool breaksOnInternalBreakpoint { get { return (this == stepInto || this == stepOver || this == stepUntil); } };
484 none, run, _continue, next, until, advance, step, finish;
485 property bool suspendInternalBreakpoints { get { return (this == until || this == advance || this == step || this == finish); } };
488 FileDialog debuggerFileDialog { type = selectDir };
490 static DualPipe vgTargetHandle;
491 static File vgLogFile;
492 static char vgLogPath[MAX_LOCATION];
493 static DualPipe gdbHandle;
494 static DebugEvaluationData eval { };
496 static int targetProcessId;
498 static bool gdbReady;
499 static bool breakpointError;
503 Semaphore serialSemaphore { };
509 bool sentBreakInsert;
510 bool ignoreBreakpoints;
518 int activeFrameLevel;
527 GdbExecution gdbExecution;
528 DebuggerUserAction userAction;
531 DebuggerAction breakType;
533 //DebuggerCommand lastCommand; // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
535 GdbDataStop stopItem;
536 GdbDataBreakpoint bpItem;
539 List<Breakpoint> sysBPs { };
540 Breakpoint bpRunToCursor;
541 Breakpoint intBpEntry;
542 Breakpoint intBpMain;
543 Breakpoint intBpWinMain;
547 CompilerConfig currentCompiler;
548 ProjectConfig prjConfig;
551 CodeEditor codeEditor;
553 ValgrindLogThread vgLogThread { debugger = this };
554 ValgrindTargetThread vgTargetThread { debugger = this };
555 GdbThread gdbThread { debugger = this };
558 Map<String, bool> projectsLibraryLoaded { };
562 delay = 0.0, userData = this;
566 bool monitor = false;
567 DebuggerEvent curEvent = event;
568 GdbDataStop stopItem = this.stopItem;
569 Breakpoint bpUser = null;
570 Breakpoint bpInternal = null;
578 this.stopItem = null;
582 DynamicString bpReport { };
584 for(bp : sysBPs; bp.inserted)
586 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
589 if(bpRunToCursor && bpRunToCursor.inserted)
591 Breakpoint bp = bpRunToCursor;
592 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
595 for(bp : ide.workspace.breakpoints; bp.inserted)
597 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
601 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
606 Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
608 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")"); delete s;
616 if(curEvent && curEvent != exit)
618 _dpl(0, "No stop item");
626 Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
635 GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
636 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
637 if(activeFrame.level == activeFrameLevel)
643 // GdbCommand(false, "-break-info %s", bpItem.number);
655 Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
656 if(bp && bp.inserted && bp.bp.addr)
658 if(bp.type.isInternal)
662 if(stopItem && stopItem.frame)
664 if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
665 bpUser = bpRunToCursor;
668 for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
670 if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
682 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
683 if(bpUser && strcmp(stopItem.frame.addr, bpUser.bp.addr))
684 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") address missmatch!");
687 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
688 if((bpUser && !ignoreBreakpoints) || (bpInternal && userAction.breaksOnInternalBreakpoint))
690 hitThread = stopItem.threadid;
694 signalThread = stopItem.threadid;
698 case locationReached:
700 ignoreBreakpoints = false;
702 case valgrindStartPause:
703 GdbExecContinue(true);
711 if(curEvent == signal)
715 ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
716 ide.outputView.debugBox.Logf(" %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
717 ide.outputView.Show();
718 ide.callStackView.Show();
721 else if(curEvent == breakEvent)
723 ide.threadsView.Show();
724 ide.callStackView.Show();
725 ide.callStackView.Activate();
727 else if(curEvent == hit)
729 if(BreakpointHit(stopItem, bpInternal, bpUser))
731 ide.AdjustDebugMenus();
732 if(bpUser && bpUser.type == runToCursor)
734 ignoreBreakpoints = false;
735 UnsetBreakpoint(bpUser);
736 delete bpRunToCursor;
741 if(breakType == advance && bpInternal && (bpInternal.type == internalMain || bpInternal.type == internalEntry))
744 GdbExecAdvance(breakString, 0);
749 GdbExecContinue(false);
755 if(monitor && curEvent.canBeMonitored)
758 activeThread = stopItem.threadid;
759 GdbCommand(false, "-thread-list-ids");
760 InternalSelectFrame(activeFrameLevel);
761 GoToStackFrameLine(activeFrameLevel, true, false);
763 ide.ShowCodeEditor();
764 ide.AdjustDebugMenus();
765 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
778 #ifdef GDB_DEBUG_CONSOLE
779 char lastGdbOutput[GdbGetLineSize];
781 #if defined(__unix__)
782 ProgramThread progThread { };
786 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
787 void ChangeUserAction(char * file, int line, DebuggerUserAction value)
789 bool same = value == userAction;
790 __dpl2(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
794 #define _ChangeUserAction(value) userAction = value
798 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
799 void ChangeState(char * file, int line, DebuggerState value)
801 #define _ChangeState(value) ChangeState(value)
802 void ChangeState(DebuggerState value)
805 bool same = value == state;
807 __dpl2(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
810 if(!same) ide.AdjustDebugMenus();
815 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
817 stackFrames.Free(Frame::Free);
827 waitingForPID = false;
832 sentBreakInsert = false;
833 ignoreBreakpoints = false;
836 activeFrameLevel = 0;
853 bpRunToCursor = null;
855 delete currentCompiler;
858 WatchesReleaseCodeEditor();
861 projectsLibraryLoaded.Free();
863 /*GdbThread gdbThread
869 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
870 ideProcessId = Process_GetCurrentProcessId();
872 sysBPs.Add((intBpEntry = Breakpoint { type = internalEntry, enabled = false, level = -1 }));
873 sysBPs.Add((intBpMain = Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 }));
874 #if defined(__WIN32__)
875 sysBPs.Add((intBpWinMain = Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 }));
877 sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
878 sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
883 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
891 property bool isActive { get { return state == running || state == stopped; } }
892 property bool isPrepared { get { return state == loaded || state == running || state == stopped; } }
896 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
897 _ChangeUserAction(resume);
898 GdbExecContinue(true);
903 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
904 _ChangeUserAction(_break);
908 GdbDebugBreak(false);
914 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
915 _ChangeUserAction(stop);
922 GdbDebugBreak(false);
936 void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
938 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
939 _ChangeUserAction(restart);
940 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
944 bool GoToCodeLine(char * location)
947 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
948 codloc = CodeLocation::ParseCodeLocation(location);
951 CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, normal, true, null, no, normal, false);
954 EditBox editBox = editor.editBox;
955 editBox.GoToLineNum(codloc.line - 1);
956 editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
963 bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
965 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
968 char filePath[MAX_LOCATION];
969 char sourceDir[MAX_LOCATION];
971 CodeEditor editor = null;
972 if(stackLevel == -1) // this (the two lines) is part of that fix that I would not put in for some time
974 for(frame = stackFrames.first; frame; frame = frame.next)
975 if(frame.level == stackLevel)
980 ide.callStackView.Show();
982 if(frame.absoluteFile)
983 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
984 if(!editor && frame.file)
985 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
986 if(!frame.absoluteFile && askForLocation && frame.file)
989 char title[MAX_LOCATION];
990 snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
991 title[sizeof(title)-1] = 0;
993 if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
995 AddSourceDir(sourceDir);
996 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
999 if(!editor && frame.absoluteFile)
1000 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
1002 ide.RepositionWindows(false);
1004 if(editor && frame.line)
1006 EditBox editBox = editor.editBox;
1007 editBox.GoToLineNum(frame.line - 1);
1008 editBox.GoToPosition(editBox.line, frame.line - 1, 0);
1016 void SelectThread(int thread)
1018 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
1019 _ChangeUserAction(selectThread);
1020 if(state == stopped)
1022 if(thread != activeThread)
1024 activeFrameLevel = -1;
1025 ide.callStackView.Clear();
1026 GdbCommand(false, "-thread-select %d", thread);
1028 InternalSelectFrame(activeFrameLevel);
1029 GoToStackFrameLine(activeFrameLevel, true, false);
1033 ide.callStackView.Show();
1037 void SelectFrame(int frame)
1039 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
1040 _ChangeUserAction(selectFrame);
1041 if(state == stopped)
1043 if(frame != activeFrameLevel)
1045 InternalSelectFrame(frame);
1052 void InternalSelectFrame(int frame)
1054 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
1055 activeFrameLevel = frame; // there is no active frame number in the gdb reply
1056 GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
1057 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
1058 if(activeFrame.level == activeFrameLevel)
1062 void HandleExit(char * reason, char * code)
1064 bool returnedExitCode = false;
1065 char verboseExitCode[128];
1067 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
1068 _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
1069 targetProcessId = 0;
1073 snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
1074 verboseExitCode[sizeof(verboseExitCode)-1] = 0;
1077 verboseExitCode[0] = '\0';
1081 // ClearBreakDisplay();
1085 for(wh : ide.workspace.watches)
1087 if(wh.type) FreeType(wh.type);
1090 ide.watchesView.UpdateWatch(wh);
1094 #if defined(__unix__)
1097 progThread.terminate = true;
1100 fifoFile.CloseInput();
1110 char program[MAX_LOCATION];
1111 GetSystemPathBuffer(program, targetFile);
1113 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1114 else if(!strcmp(reason, "exited-normally"))
1115 ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
1116 else if(!strcmp(reason, "exited"))
1117 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1118 else if(!strcmp(reason, "exited-signalled"))
1119 ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
1121 ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
1126 DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool ignoreBreakpoints)
1128 DebuggerState result = none;
1129 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), ignoreBreakpoints(", ignoreBreakpoints, ")");
1130 if(restart && state == running && targetProcessId)
1132 breakType = DebuggerAction::restart;
1133 GdbDebugBreak(false);
1137 if(restart && state == stopped)
1139 if(needReset && state == loaded)
1140 GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
1142 if(result == none || result == terminated)
1144 ide.outputView.ShowClearSelectTab(debug);
1145 ide.outputView.debugBox.Logf($"Starting debug mode\n");
1152 for(bp : ide.workspace.breakpoints)
1158 if(GdbInit(compiler, config, bitDepth, useValgrind))
1163 this.ignoreBreakpoints = ignoreBreakpoints;
1168 void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1170 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1171 _ChangeUserAction(start);
1172 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
1176 void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1178 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1179 _ChangeUserAction(stepInto);
1180 switch(StartSession(compiler, config, bitDepth, useValgrind, false, false))
1182 case loaded: GdbExecRun(); break;
1183 case stopped: GdbExecStep(); break;
1187 void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1189 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1190 _ChangeUserAction(stepOver);
1191 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1193 case loaded: GdbExecRun(); break;
1194 case stopped: GdbExecNext(); break;
1198 void StepUntil(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1200 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepUntil()");
1201 _ChangeUserAction(stepUntil);
1202 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1204 case loaded: GdbExecRun(); break;
1205 case stopped: GdbExecUntil(null, 0); break;
1209 void StepOut(bool ignoreBreakpoints)
1211 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1212 _ChangeUserAction(stepOut);
1213 if(state == stopped)
1215 this.ignoreBreakpoints = ignoreBreakpoints;
1219 GdbExecContinue(true);
1223 void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel, bool oldImplementation)
1225 char relativeFilePath[MAX_LOCATION];
1226 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1227 _ChangeUserAction(runToCursor);
1228 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
1230 if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1232 UnsetBreakpoint(bpRunToCursor);
1233 delete bpRunToCursor;
1236 StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints);
1239 if(oldImplementation)
1241 bpRunToCursor = Breakpoint { };
1242 bpRunToCursor.absoluteFilePath = absoluteFilePath;
1243 bpRunToCursor.relativeFilePath = relativeFilePath;
1244 bpRunToCursor.line = lineNumber;
1245 bpRunToCursor.type = runToCursor;
1246 bpRunToCursor.enabled = true;
1247 bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1252 breakType = advance;
1253 breakString = PrintString(relativeFilePath, ":", lineNumber);
1256 else if(state == stopped)
1258 if(oldImplementation)
1259 GdbExecContinue(true);
1263 GdbExecUntil(absoluteFilePath, lineNumber);
1265 GdbExecAdvance(absoluteFilePath, lineNumber);
1270 void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1272 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1273 if(activeFrameLevel == -1)
1281 *error = signalOn && activeThread == signalThread;
1282 *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1283 *lineTopFrame = activeFrameLevel ? 1 : 0;
1287 int GetMarginIconsLineNumbers(char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1289 char winFilePath[MAX_LOCATION];
1290 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1292 Iterator<Breakpoint> it { ide.workspace.breakpoints };
1293 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1294 while(it.Next() && count < max)
1296 Breakpoint bp = it.data;
1299 if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1301 lines[count] = bp.line;
1302 enabled[count] = bp.enabled;
1307 if(activeFrameLevel == -1)
1315 *error = signalOn && activeThread == signalThread;
1316 if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1317 *lineCursor = activeFrame.line;
1320 if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1322 else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1323 *lineTopFrame = stopItem.frame.line;
1327 if(*lineTopFrame == *lineCursor && *lineTopFrame)
1333 void ChangeWatch(DataRow row, char * expression)
1335 Watch wh = (Watch)row.tag;
1336 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1339 delete wh.expression;
1341 wh.expression = CopyString(expression);
1344 Iterator<Watch> it { ide.workspace.watches };
1346 ide.workspace.watches.Delete(it.pointer);
1352 row.tag = (int64)wh;
1353 ide.workspace.watches.Add(wh);
1355 wh.expression = CopyString(expression);
1357 ide.workspace.Save();
1358 //if(expression && state == stopped)
1363 void MoveIcons(char * fileName, int lineNumber, int move, bool start)
1365 char winFilePath[MAX_LOCATION];
1366 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1369 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1370 for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1372 Breakpoint bp = (Breakpoint)bpLink.data;
1375 if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1377 if(bp.line > lineNumber || (bp.line == lineNumber && start))
1379 if(move < 0 && (bp.line < lineNumber - move))
1380 ide.workspace.RemoveBreakpoint(bp);
1384 ide.breakpointsView.UpdateBreakpoint(bp.row);
1385 ide.workspace.Save();
1391 // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1394 bool SourceDirDialog(char * title, char * startDir, char * test, char * sourceDir)
1398 String srcDir = null;
1400 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1401 debuggerFileDialog.text = title;
1402 debuggerFileDialog.currentDirectory = startDir;
1403 debuggerFileDialog.master = ide;
1405 while(debuggerFileDialog.Modal())
1407 strcpy(sourceDir, debuggerFileDialog.filePath);
1408 if(!fstrcmp(ide.workspace.projectDir, sourceDir) &&
1409 MessageBox { type = yesNo, master = ide,
1410 contents = $"This is the project directory.\nWould you like to try again?",
1411 text = $"Invalid Source Directory" }.Modal() == no)
1415 for(dir : ide.workspace.sourceDirs)
1417 if(!fstrcmp(dir, sourceDir))
1425 MessageBox { type = yesNo, master = ide,
1426 contents = $"This source directory is already specified.\nWould you like to try again?",
1427 text = $"Invalid Source Directory" }.Modal() == no)
1433 char file[MAX_LOCATION];
1434 strcpy(file, sourceDir);
1435 PathCat(file, test);
1436 result = FileExists(file);
1438 MessageBox { type = yesNo, master = ide,
1439 contents = $"Unable to locate source file.\nWould you like to try again?",
1440 text = $"Invalid Source Directory" }.Modal() == no)
1454 void AddSourceDir(char * sourceDir)
1456 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1457 ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1458 ide.workspace.Save();
1462 DebuggerState oldState = state;
1467 GdbDebugBreak(true);
1470 GdbCommand(false, "-environment-directory \"%s\"", sourceDir);
1473 if(oldState == running)
1474 GdbExecContinue(false);
1478 void ToggleBreakpoint(char * fileName, int lineNumber)
1480 char absolutePath[MAX_LOCATION];
1481 Breakpoint bp = null;
1483 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1485 GetSlashPathBuffer(absolutePath, fileName);
1486 for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1495 ide.workspace.RemoveBreakpoint(bp);
1504 char relativePath[MAX_LOCATION];
1506 WorkspaceGetRelativePath(absolutePath, relativePath, &owner);
1510 char title[MAX_LOCATION];
1511 char directory[MAX_LOCATION];
1512 char sourceDir[MAX_LOCATION];
1513 StripLastDirectory(absolutePath, directory);
1514 snprintf(title, sizeof(title), $"Provide source files location directory for %s", absolutePath);
1515 title[sizeof(title)-1] = 0;
1518 String srcDir = null;
1519 for(dir : ide.workspace.sourceDirs)
1521 if(IsPathInsideOf(absolutePath, dir))
1523 MakePathRelative(absolutePath, dir, relativePath);
1531 if(SourceDirDialog(title, directory, null, sourceDir))
1533 if(IsPathInsideOf(absolutePath, sourceDir))
1535 AddSourceDir(sourceDir);
1536 MakePathRelative(absolutePath, sourceDir, relativePath);
1539 else if(MessageBox { type = yesNo, master = ide,
1540 contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1541 text = $"Invalid Source Directory" }.Modal() == no)
1548 ide.workspace.bpCount++;
1549 bp = { line = lineNumber, type = user, enabled = true, level = -1, project = owner };
1550 ide.workspace.breakpoints.Add(bp);
1551 bp.absoluteFilePath = absolutePath;
1552 bp.relativeFilePath = relativePath;
1553 ide.breakpointsView.AddBreakpoint(bp);
1558 DebuggerState oldState = state;
1563 GdbDebugBreak(true);
1566 SetBreakpoint(bp, false);
1569 if(oldState == running)
1570 GdbExecContinue(false);
1573 ide.workspace.Save();
1576 void UpdateRemovedBreakpoint(Breakpoint bp)
1578 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1579 if(targeted && bp.inserted)
1581 DebuggerState oldState = state;
1586 GdbDebugBreak(true);
1589 UnsetBreakpoint(bp);
1592 if(oldState == running)
1593 GdbExecContinue(false);
1599 void ParseFrame(Frame frame, char * string)
1602 Array<char *> frameTokens { minAllocSize = 50 };
1603 Array<char *> argsTokens { minAllocSize = 50 };
1604 Array<char *> argumentTokens { minAllocSize = 50 };
1605 DebugListItem item { };
1608 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1609 TokenizeList(string, ',', frameTokens);
1610 for(i = 0; i < frameTokens.count; i++)
1612 if(TokenizeListItem(frameTokens[i], item))
1614 StripQuotes(item.value, item.value);
1615 if(!strcmp(item.name, "level"))
1616 frame.level = atoi(item.value);
1617 else if(!strcmp(item.name, "addr"))
1618 frame.addr = item.value;
1619 else if(!strcmp(item.name, "func"))
1620 frame.func = item.value;
1621 else if(!strcmp(item.name, "args"))
1623 if(!strcmp(item.value, "[]"))
1624 frame.argsCount = 0;
1627 item.value = StripBrackets(item.value);
1628 TokenizeList(item.value, ',', argsTokens);
1629 for(j = 0; j < argsTokens.count; j++)
1631 argsTokens[j] = StripCurlies(argsTokens[j]);
1632 TokenizeList(argsTokens[j], ',', argumentTokens);
1633 for(k = 0; k < argumentTokens.count; k++)
1636 frame.args.Add(arg);
1637 if(TokenizeListItem(argumentTokens[k], item))
1639 if(!strcmp(item.name, "name"))
1641 StripQuotes(item.value, item.value);
1642 arg.name = item.value;
1644 else if(!strcmp(item.name, "value"))
1646 StripQuotes(item.value, item.value);
1647 arg.val = item.value;
1650 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1653 _dpl(0, "Bad frame args item");
1655 argumentTokens.RemoveAll();
1657 frame.argsCount = argsTokens.count;
1658 argsTokens.RemoveAll();
1661 else if(!strcmp(item.name, "from"))
1662 frame.from = item.value;
1663 else if(!strcmp(item.name, "file"))
1664 frame.file = item.value;
1665 else if(!strcmp(item.name, "line"))
1666 frame.line = atoi(item.value);
1667 else if(!strcmp(item.name, "fullname"))
1668 frame.absoluteFile = item.value;
1670 // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1671 String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1672 if(strcmp(frame.file, path))
1675 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
1680 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1683 _dpl(0, "Bad frame");
1688 delete argumentTokens;
1692 Breakpoint GetBreakpointById(int id, bool * isInternal)
1694 Breakpoint bp = null;
1695 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1697 *isInternal = false;
1700 for(i : sysBPs; i.bp && i.bp.id == id)
1707 if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1711 for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1721 GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1724 GdbDataBreakpoint bp { };
1725 DebugListItem item { };
1726 Array<char *> bpTokens { minAllocSize = 16 };
1727 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1728 string = StripCurlies(string);
1729 TokenizeList(string, ',', bpTokens);
1730 for(i = 0; i < bpTokens.count; i++)
1732 if(TokenizeListItem(bpTokens[i], item))
1734 StripQuotes(item.value, item.value);
1735 if(!strcmp(item.name, "number"))
1737 if(!strchr(item.value, '.'))
1738 bp.id = atoi(item.value);
1739 bp.number = item.value;
1741 else if(!strcmp(item.name, "type"))
1742 bp.type = item.value;
1743 else if(!strcmp(item.name, "disp"))
1744 bp.disp = item.value;
1745 else if(!strcmp(item.name, "enabled"))
1746 bp.enabled = (!strcmpi(item.value, "y"));
1747 else if(!strcmp(item.name, "addr"))
1749 if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1752 Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1753 while(outTokens.count > ++c)
1755 GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1756 bpArray.Add(multBp);
1760 bp.addr = item.value;
1762 else if(!strcmp(item.name, "func"))
1763 bp.func = item.value;
1764 else if(!strcmp(item.name, "file"))
1765 bp.file = item.value;
1766 else if(!strcmp(item.name, "fullname"))
1767 bp.fullname = item.value;
1768 else if(!strcmp(item.name, "line"))
1769 bp.line = atoi(item.value);
1770 else if(!strcmp(item.name, "at"))
1772 else if(!strcmp(item.name, "times"))
1773 bp.times = atoi(item.value);
1774 else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1775 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1777 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1783 void ShowDebuggerViews()
1785 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1786 ide.outputView.Show();
1787 ide.outputView.SelectTab(debug);
1788 ide.threadsView.Show();
1789 ide.callStackView.Show();
1790 ide.watchesView.Show();
1794 void HideDebuggerViews()
1796 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1797 ide.RepositionWindows(true);
1800 void ::GdbCommand(bool focus, char * format, ...)
1804 // TODO: Improve this limit
1805 static char string[MAX_F_STRING*4];
1807 va_start(args, format);
1808 vsnprintf(string, sizeof(string), format, args);
1809 string[sizeof(string)-1] = 0;
1813 ide.debugger.serialSemaphore.TryWait();
1815 #ifdef GDB_DEBUG_CONSOLE
1816 _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1818 #ifdef GDB_DEBUG_OUTPUT
1819 ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1821 #ifdef GDB_DEBUG_GUI
1823 ide.gdbDialog.AddCommand(string);
1826 strcat(string,"\n");
1827 gdbHandle.Puts(string);
1830 Process_ShowWindows(targetProcessId);
1833 ide.debugger.serialSemaphore.Wait();
1838 bool ValidateBreakpoint(Breakpoint bp)
1840 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1841 if(modules && bp.line && bp.bp)
1843 if(bp.bp.line != bp.line)
1849 ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1851 //UnsetBreakpoint(bp);
1852 //bp.enabled = false;
1858 ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1859 bp.line = bp.bp.line;
1866 void BreakpointsMaintenance()
1868 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::BreakpointsMaintenance()");
1871 if(gdbExecution.suspendInternalBreakpoints)
1873 for(bp : sysBPs; bp.inserted)
1874 UnsetBreakpoint(bp);
1878 DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1879 for(bp : sysBPs; !bp.inserted)
1881 bool insert = false;
1882 if(bp.type == internalModulesLoaded)
1884 char path[MAX_LOCATION];
1885 char name[MAX_LOCATION];
1886 char fixedModuleName[MAX_FILENAME];
1889 bool moduleLoadBlock = false;
1891 ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1892 snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1893 name[sizeof(name)-1] = 0;
1894 strcpy(path, ide.workspace.projectDir);
1895 PathCatSlash(path, objDir.dir);
1896 PathCatSlash(path, name);
1897 f = FileOpen(path, read);
1900 for(lineNumber = 1; !f.Eof(); lineNumber++)
1902 if(f.GetLine(line, sizeof(line) - 1))
1904 bool moduleLoadLine;
1905 TrimLSpaces(line, line);
1906 moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1907 if(!moduleLoadBlock && moduleLoadLine)
1908 moduleLoadBlock = true;
1909 else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1915 char relative[MAX_LOCATION];
1916 bp.absoluteFilePath = path;
1917 MakePathRelative(path, ide.workspace.projectDir, relative);
1918 bp.relativeFilePath = relative;
1919 bp.line = lineNumber;
1925 else if(bp.type == internalModuleLoad)
1929 for(prj : ide.workspace.projects)
1931 if(!strcmp(prj.moduleName, "ecere"))
1933 ProjectNode node = prj.topNode.Find("instance.c", false);
1936 char path[MAX_LOCATION];
1937 char relative[MAX_LOCATION];
1938 node.GetFullFilePath(path);
1939 bp.absoluteFilePath = path;
1940 MakePathRelative(path, prj.topNode.path, relative);
1941 bp.relativeFilePath = relative;
1952 SetBreakpoint(bp, false);
1957 if(userAction != runToCursor && bpRunToCursor && bpRunToCursor.inserted)
1958 UnsetBreakpoint(bpRunToCursor);
1959 if(bpRunToCursor && !bpRunToCursor.inserted)
1960 SetBreakpoint(bpRunToCursor, false);
1962 if(ignoreBreakpoints)
1964 for(bp : ide.workspace.breakpoints; bp.inserted)
1965 UnsetBreakpoint(bp);
1969 for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1973 if(!SetBreakpoint(bp, false))
1974 SetBreakpoint(bp, true);
1982 bp.bp = GdbDataBreakpoint { };
1989 void UnsetBreakpoint(Breakpoint bp)
1991 char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1992 if(symbols && bp.inserted)
1994 GdbCommand(false, "-break-delete %s", bp.bp.number);
1995 bp.inserted = false;
2000 bool SetBreakpoint(Breakpoint bp, bool removePath)
2002 char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
2003 breakpointError = false;
2004 if(symbols && bp.enabled && (!bp.project || bp.project == ide.project || projectsLibraryLoaded[bp.project.name]))
2006 sentBreakInsert = true;
2008 GdbCommand(false, "-break-insert *%s", bp.address);
2011 char * location = bp.CopyLocationString(removePath);
2012 GdbCommand(false, "-break-insert %s", location);
2015 if(!breakpointError)
2017 char * address = null;
2018 if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
2021 GdbDataBreakpoint first = null;
2022 for(n : bpItem.multipleBPs)
2024 if(!fstrcmp(n.fullname, bp.absoluteFilePath) && !first)
2034 GdbCommand(false, "-break-disable %s", n.number);
2038 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2043 address = CopyString(first.addr);
2044 bpItem.addr = first.addr;
2045 bpItem.func = first.func;
2046 bpItem.file = first.file;
2047 bpItem.fullname = first.fullname;
2048 bpItem.line = first.line;
2049 //bpItem.thread-groups = first.thread-groups;*/
2052 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2054 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2055 bpItem.multipleBPs.Free();
2056 delete bpItem.multipleBPs;
2060 bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2062 ValidateBreakpoint(bp);
2066 UnsetBreakpoint(bp);
2067 bp.address = address;
2069 SetBreakpoint(bp, removePath);
2073 return !breakpointError;
2078 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2080 stackFrames.Free(Frame::Free);
2081 GdbCommand(false, "-stack-info-depth");
2083 GdbCommand(false, "-stack-info-depth 192");
2084 if(frameCount && frameCount <= 192)
2085 GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2088 GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2089 GdbCommand(false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2091 GdbCommand(false, "");
2096 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2099 char escaped[MAX_LOCATION];
2100 strescpy(escaped, targetFile);
2101 GdbCommand(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
2108 const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2109 //GdbCommand(false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2110 printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2111 GdbCommand(false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2114 GdbCommand(false, "info target"); //GDB/MI Missing Implementation -file-list-symbol-files and -file-list-exec-sections
2116 /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2117 GdbCommand(false, "-environment-directory \"%s\"", prj.topNode.path);*/
2119 for(dir : ide.workspace.sourceDirs; dir && dir[0])
2121 bool interference = false;
2122 for(prj : ide.workspace.projects)
2124 if(!fstrcmp(prj.topNode.path, dir))
2126 interference = true;
2130 if(!interference && dir[0])
2131 GdbCommand(false, "-environment-directory \"%s\"", dir);
2139 /*void GdbTargetRelease()
2143 BreakpointsDeleteAll();
2144 GdbCommand(false, "file"); //GDB/MI Missing Implementation -target-detach
2150 void GdbDebugBreak(bool internal)
2152 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2156 breakType = DebuggerAction::internal;
2158 if(ide) ide.Update(null);
2160 if(Process_Break(targetProcessId)) //GdbCommand(false, "-exec-interrupt");
2161 serialSemaphore.Wait();
2164 _ChangeState(loaded);
2165 targetProcessId = 0;
2170 ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2175 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2180 ShowDebuggerViews();
2182 GdbExecContinue(true);
2184 GdbCommand(true, "-exec-run");
2187 void GdbExecContinue(bool focus)
2189 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2192 GdbCommand(focus, "-exec-continue");
2197 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2198 gdbExecution = next;
2200 GdbCommand(true, "-exec-next");
2203 void GdbExecUntil(char * absoluteFilePath, int lineNumber)
2205 char relativeFilePath[MAX_LOCATION];
2206 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecUntil()");
2207 gdbExecution = until;
2209 if(absoluteFilePath)
2211 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
2212 GdbCommand(true, "-exec-until %s:%d", relativeFilePath, lineNumber);
2215 GdbCommand(true, "-exec-until");
2218 void GdbExecAdvance(char * absoluteFilePathOrLocation, int lineNumber)
2220 char relativeFilePath[MAX_LOCATION];
2221 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecAdvance()");
2222 gdbExecution = advance;
2226 WorkspaceGetRelativePath(absoluteFilePathOrLocation, relativeFilePath, null);
2227 GdbCommand(true, "advance %s:%d", relativeFilePath, lineNumber); // should use -exec-advance -- GDB/MI implementation missing
2230 GdbCommand(true, "advance %s", absoluteFilePathOrLocation); // should use -exec-advance -- GDB/MI implementation missing
2235 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2236 gdbExecution = step;
2238 GdbCommand(true, "-exec-step");
2241 void GdbExecFinish()
2243 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2244 gdbExecution = finish;
2246 GdbCommand(true, "-exec-finish");
2249 void GdbExecCommon()
2251 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2252 BreakpointsMaintenance();
2255 #ifdef GDB_DEBUG_GUI
2256 void SendGDBCommand(char * command)
2258 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2259 DebuggerState oldState = state;
2264 GdbDebugBreak(true);
2267 GdbCommand(false, command);
2270 if(oldState == running)
2271 GdbExecContinue(false);
2275 void ClearBreakDisplay()
2277 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2279 activeFrameLevel = -1;
2289 stackFrames.Free(Frame::Free);
2290 ide.callStackView.Clear();
2291 ide.threadsView.Clear();
2297 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2299 GdbCommand(false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2303 bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2306 char oldDirectory[MAX_LOCATION];
2307 char tempPath[MAX_LOCATION];
2308 char command[MAX_F_STRING*4];
2309 Project project = ide.project;
2310 DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2311 PathBackup pathBackup { };
2313 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2314 if(currentCompiler != compiler)
2316 delete currentCompiler;
2317 currentCompiler = compiler;
2318 incref currentCompiler;
2321 this.bitDepth = bitDepth;
2322 usingValgrind = useValgrind;
2324 _ChangeState(loaded);
2326 sentBreakInsert = false;
2327 breakpointError = false;
2328 ignoreBreakpoints = false;
2333 projectsLibraryLoaded.Free();
2335 ide.outputView.ShowClearSelectTab(debug);
2336 ide.outputView.debugBox.Logf($"Starting debug mode\n");
2338 #ifdef GDB_DEBUG_OUTPUT
2339 ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2342 strcpy(tempPath, ide.workspace.projectDir);
2343 PathCatSlash(tempPath, targetDirExp.dir);
2345 targetDir = CopyString(tempPath);
2346 project.CatTargetFileName(tempPath, compiler, config);
2348 targetFile = CopyString(tempPath);
2350 GetWorkingDir(oldDirectory, MAX_LOCATION);
2351 if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2353 char temp[MAX_LOCATION];
2354 strcpy(temp, ide.workspace.projectDir);
2355 PathCatSlash(temp, ide.workspace.debugDir);
2356 ChangeWorkingDir(temp);
2359 ChangeWorkingDir(ide.workspace.projectDir);
2361 ide.SetPath(true, compiler, config, bitDepth);
2363 // TODO: This pollutes the environment, but at least it works
2364 // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2365 // What is the proper solution for this? DualPipeOpenEnv?
2366 // gdb set environment commands don't seem to take effect
2367 for(e : ide.workspace.environmentVars)
2369 SetEnvironment(e.name, e.string);
2374 char * clArgs = ide.workspace.commandLineArgs;
2375 const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2376 ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2377 int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2378 bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2379 vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2383 vgLogThread.Create();
2387 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2390 if(result && !CheckCommandAvailable(valgrindCommand))
2392 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2397 char * vgRedzoneSizeFlag = vgRedzoneSize == -1 ? "" : PrintString(" --redzone-size=", vgRedzoneSize);
2398 sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2399 valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSizeFlag, vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2400 if(vgRedzoneSize != -1)
2401 delete vgRedzoneSizeFlag;
2402 vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2405 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2411 incref vgTargetHandle;
2412 vgTargetThread.Create();
2414 targetProcessId = vgTargetHandle.GetProcessID();
2415 waitingForPID = false;
2416 if(!targetProcessId)
2418 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2425 serialSemaphore.Wait();
2433 (compiler.targetPlatform == win32 && bitDepth == 64) ? "x86_64-w64-mingw32-gdb" :
2434 (compiler.targetPlatform == win32 && bitDepth == 32) ? "i686-w64-mingw32-gdb" :
2436 if(!CheckCommandAvailable(command))
2438 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2443 strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2445 gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2448 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2458 gdbProcessId = gdbHandle.GetProcessID();
2461 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2468 serialSemaphore.Wait();
2471 GdbCommand(false, "-gdb-set verbose off");
2472 //GdbCommand(false, "-gdb-set exec-done-display on");
2473 GdbCommand(false, "-gdb-set step-mode off");
2474 GdbCommand(false, "-gdb-set unwindonsignal on");
2475 //GdbCommand(false, "-gdb-set shell on");
2476 GdbCommand(false, "set print elements 992");
2477 GdbCommand(false, "-gdb-set backtrace limit 100000");
2481 //_ChangeState(terminated);
2487 #if defined(__unix__)
2489 CreateTemporaryDir(progFifoDir, "ecereide");
2490 strcpy(progFifoPath, progFifoDir);
2491 PathCat(progFifoPath, "ideprogfifo");
2492 if(!mkfifo(progFifoPath, 0600))
2494 //fileCreated = true;
2499 ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2506 progThread.terminate = false;
2507 progThread.Create();
2511 #if defined(__WIN32__)
2512 GdbCommand(false, "-gdb-set new-console on");
2515 #if defined(__unix__)
2517 GdbCommand(false, "-inferior-tty-set %s", progFifoPath);
2521 GdbCommand(false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2523 for(e : ide.workspace.environmentVars)
2525 GdbCommand(false, "set environment %s=%s", e.name, e.string);
2530 ChangeWorkingDir(oldDirectory);
2536 delete targetDirExp;
2542 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2543 if(gdbHandle && gdbProcessId)
2546 GdbCommand(false, "-gdb-exit");
2561 _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2567 for(bp : ide.workspace.breakpoints)
2573 bpRunToCursor.Reset();
2575 ide.outputView.debugBox.Logf($"Debugging stopped\n");
2576 ClearBreakDisplay();
2579 #if defined(__unix__)
2580 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2582 progThread.terminate = true;
2585 fifoFile.CloseInput();
2591 DeleteFile(progFifoPath);
2592 progFifoPath[0] = '\0';
2598 bool WatchesLinkCodeEditor()
2600 bool goodFrame = activeFrame && activeFrame.absoluteFile;
2601 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesLinkCodeEditor()");
2602 if(codeEditor && (!goodFrame || fstrcmp(codeEditor.fileName, activeFrame.absoluteFile)))
2603 WatchesReleaseCodeEditor();
2605 if(!codeEditor && goodFrame)
2607 codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, normal, false, null, no, normal, false);
2610 codeEditor.inUseDebug = true;
2614 return codeEditor != null;
2617 void WatchesReleaseCodeEditor()
2619 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesReleaseCodeEditor()");
2622 codeEditor.inUseDebug = false;
2623 if(!codeEditor.visible)
2624 codeEditor.Destroy(0);
2629 bool ResolveWatch(Watch wh)
2631 bool result = false;
2633 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2645 char watchmsg[MAX_F_STRING];
2646 if(state == stopped && !codeEditor)
2647 wh.value = CopyString($"No source file found for selected frame");
2648 //if(codeEditor && state == stopped || state != stopped)
2651 Module backupPrivateModule;
2652 Context backupContext;
2653 Class backupThisClass;
2657 backupPrivateModule = GetPrivateModule();
2658 backupContext = GetCurrentContext();
2659 backupThisClass = GetThisClass();
2662 SetPrivateModule(codeEditor.privateModule);
2663 SetCurrentContext(codeEditor.globalContext);
2664 SetTopContext(codeEditor.globalContext);
2665 SetGlobalContext(codeEditor.globalContext);
2666 SetGlobalData(&codeEditor.globalData);
2669 exp = ParseExpressionString(wh.expression);
2671 if(exp && !parseError)
2673 char expString[4096];
2675 PrintExpression(exp, expString);
2677 if(GetPrivateModule())
2680 DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2681 ProcessExpressionType(exp);
2683 wh.type = exp.expType;
2686 DebugComputeExpression(exp);
2687 if(ExpressionIsError(exp))
2689 GDBFallBack(exp, expString);
2692 /*if(exp.hasAddress)
2694 char temp[MAX_F_STRING];
2695 sprintf(temp, "0x%x", exp.address);
2696 wh.address = CopyString(temp);
2697 // wh.address = CopyStringf("0x%x", exp.address);
2702 Type dataType = exp.expType;
2705 char temp[MAX_F_STRING];
2706 switch(dataType.kind)
2709 sprintf(temp, "%i", exp.val.c);
2712 sprintf(temp, "%i", exp.val.s);
2717 sprintf(temp, "%i", exp.val.i);
2720 sprintf(temp, "%i", exp.val.i64);
2723 sprintf(temp, "%i", exp.val.p);
2728 long v = (long)exp.val.f;
2729 sprintf(temp, "%i", v);
2734 long v = (long)exp.val.d;
2735 sprintf(temp, "%i", v);
2740 wh.intVal = CopyString(temp);
2741 switch(dataType.kind)
2744 sprintf(temp, "0x%x", exp.val.c);
2747 sprintf(temp, "0x%x", exp.val.s);
2751 sprintf(temp, "0x%x", exp.val.i);
2754 sprintf(temp, "0x%x", exp.val.i64);
2757 sprintf(temp, "0x%x", exp.val.i64);
2760 sprintf(temp, "0x%x", exp.val.p);
2765 long v = (long)exp.val.f;
2766 sprintf(temp, "0x%x", v);
2771 long v = (long)exp.val.d;
2772 sprintf(temp, "0x%x", v);
2777 wh.hexVal = CopyString(temp);
2778 switch(dataType.kind)
2781 sprintf(temp, "0o%o", exp.val.c);
2784 sprintf(temp, "0o%o", exp.val.s);
2788 sprintf(temp, "0o%o", exp.val.i);
2791 sprintf(temp, "0o%o", exp.val.i64);
2794 sprintf(temp, "0o%o", exp.val.i64);
2797 sprintf(temp, "0o%o", exp.val.p);
2802 long v = (long)exp.val.f;
2803 sprintf(temp, "0o%o", v);
2808 long v = (long)exp.val.d;
2809 sprintf(temp, "0o%o", v);
2814 wh.octVal = CopyString(temp);
2817 // WHATS THIS HERE ?
2818 if(exp.type == constantExp && exp.constant)
2819 wh.constant = CopyString(exp.constant);
2825 case symbolErrorExp:
2826 snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2828 case structMemberSymbolErrorExp:
2829 // todo get info as in next case (ExpClassMemberSymbolError)
2830 snprintf(watchmsg, sizeof(watchmsg), $"Error: Struct member not found for \"%s\"", wh.expression);
2832 case classMemberSymbolErrorExp:
2835 Expression memberExp = exp.member.exp;
2836 Identifier memberID = exp.member.member;
2837 Type type = memberExp.expType;
2840 _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2843 char string[256] = "";
2845 PrintTypeNoConst(type, string, false, true);
2846 classSym = FindClass(string);
2847 _class = classSym ? classSym.registered : null;
2850 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2852 snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in unregistered class? (Should never get this message)", memberID ? memberID.string : "");
2855 snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in no type? (Should never get this message)", memberID ? memberID.string : "");
2858 case memoryErrorExp:
2859 // Need to ensure when set to memoryErrorExp, constant is set
2860 snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
2862 case dereferenceErrorExp:
2863 snprintf(watchmsg, sizeof(watchmsg), $"Dereference failure for \"%s\"", wh.expression);
2865 case unknownErrorExp:
2866 snprintf(watchmsg, sizeof(watchmsg), $"Unknown error for \"%s\"", wh.expression);
2868 case noDebuggerErrorExp:
2869 snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
2871 case debugStateErrorExp:
2872 snprintf(watchmsg, sizeof(watchmsg), $"Incorrect debugger state for symbol evaluation in \"%s\"", wh.expression);
2875 snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
2879 // Temporary Code for displaying Strings
2880 if((exp.expType && ((exp.expType.kind == pointerType ||
2881 exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) ||
2882 (wh.type && wh.type.kind == classType && wh.type._class &&
2883 wh.type._class.registered && wh.type._class.registered.type == normalClass &&
2884 !strcmp(wh.type._class.registered.name, "String")))
2887 if(exp.expType.kind != arrayType || exp.hasAddress)
2893 //char temp[MAX_F_STRING * 32];
2895 ExpressionType evalError = dummyExp;
2896 /*if(exp.expType.kind == arrayType)
2897 sprintf(temp, "(char*)0x%x", exp.address);
2899 sprintf(temp, "(char*)%s", exp.constant);*/
2901 //evaluation = Debugger::EvaluateExpression(temp, &evalError);
2902 // address = strtoul(exp.constant, null, 0);
2903 address = _strtoui64(exp.constant, null, 0);
2904 //_dpl(0, "0x", address);
2905 // snprintf(value, sizeof(value), "0x%08x ", address);
2907 if(address > 0xFFFFFFFFLL)
2908 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
2910 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
2911 value[sizeof(value)-1] = 0;
2914 strcat(value, $"Null string");
2918 len = strlen(value);
2920 while(!string && size > 2)
2922 string = GdbReadMemory(address, size);
2925 if(string && string[0])
2928 if(UTF8Validate(string))
2933 for(c = 0; (ch = string[c]) && c<4096; c++)
2936 value[len++] = '\0';
2941 ISO8859_1toUTF8(string, value + len, 4096 - len - 30);
2942 strcat(value, ") (ISO8859-1)");
2949 strcat(value, $"Empty string");
2953 strcat(value, $"Couldn't read memory");
2955 wh.value = CopyString(value);
2958 else if(wh.type && wh.type.kind == classType && wh.type._class &&
2959 wh.type._class.registered && wh.type._class.registered.type == enumClass)
2961 uint64 value = strtoul(exp.constant, null, 0);
2962 Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
2963 EnumClassData enumeration = (EnumClassData)enumClass.data;
2965 for(item = enumeration.values.first; item; item = item.next)
2966 if((int)item.data == value)
2969 wh.value = CopyString(item.name);
2971 wh.value = CopyString($"Invalid Enum Value");
2974 else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class &&
2975 wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
2982 if(exp.constant[0] == '\'')
2984 if((int)((byte *)exp.constant)[1] > 127)
2987 value = UTF8GetChar(exp.constant + 1, &nb);
2988 if(nb < 2) value = exp.constant[1];
2989 signedValue = value;
2993 signedValue = exp.constant[1];
2995 // Precomp Syntax error with boot strap here:
2996 byte b = (byte)(char)signedValue;
2997 value = (unichar) b;
3003 if(wh.type.kind == charType && wh.type.isSigned)
3005 signedValue = (int)(char)strtol(exp.constant, null, 0);
3007 // Precomp Syntax error with boot strap here:
3008 byte b = (byte)(char)signedValue;
3009 value = (unichar) b;
3014 value = (uint)strtoul(exp.constant, null, 0);
3015 signedValue = (int)value;
3019 UTF32toUTF8Len(&value, 1, charString, 5);
3021 snprintf(string, sizeof(string), "\'\\0' (0)");
3022 else if(value == '\t')
3023 snprintf(string, sizeof(string), "\'\\t' (%d)", value);
3024 else if(value == '\n')
3025 snprintf(string, sizeof(string), "\'\\n' (%d)", value);
3026 else if(value == '\r')
3027 snprintf(string, sizeof(string), "\'\\r' (%d)", value);
3028 else if(wh.type.kind == charType && wh.type.isSigned)
3029 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
3030 else if(value > 256 || wh.type.kind != charType)
3032 if(value > 0x10FFFF || !GetCharCategory(value))
3033 snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3035 snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3038 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3039 string[sizeof(string)-1] = 0;
3041 wh.value = CopyString(string);
3046 wh.value = CopyString(exp.constant);
3053 wh.value = PrintHexUInt64(exp.address);
3058 char tempString[256];
3059 if(exp.member.memberType == propertyMember)
3060 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation support for \"%s\"", wh.expression);
3062 snprintf(watchmsg, sizeof(watchmsg), $"Evaluation failed for \"%s\" of type \"%s\"", wh.expression,
3063 exp.type.OnGetString(tempString, null, null));
3069 snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3070 if(exp) FreeExpression(exp);
3073 SetPrivateModule(backupPrivateModule);
3074 SetCurrentContext(backupContext);
3075 SetTopContext(backupContext);
3076 SetGlobalContext(backupContext);
3077 SetThisClass(backupThisClass);
3080 // wh.value = CopyString("No source file found for selected frame");
3082 watchmsg[sizeof(watchmsg)-1] = 0;
3084 wh.value = CopyString(watchmsg);
3086 ide.watchesView.UpdateWatch(wh);
3090 void EvaluateWatches()
3092 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3093 WatchesLinkCodeEditor();
3094 if(state == stopped)
3096 for(wh : ide.workspace.watches)
3101 char * ::GdbEvaluateExpression(char * expression)
3103 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3106 GdbCommand(false, "-data-evaluate-expression \"%s\"", expression);
3108 ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3112 // to be removed... use GdbReadMemory that returns a byte array instead
3113 char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3115 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3120 _dpl(0, "GdbReadMemoryString called with size = 0!");
3122 // GdbCommand(false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3123 if(GetRuntimePlatform() == win32)
3124 GdbCommand(false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3126 GdbCommand(false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3128 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3132 byte * ::GdbReadMemory(uint64 address, int bytes)
3134 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3137 //GdbCommand(false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3138 if(GetRuntimePlatform() == win32)
3139 GdbCommand(false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3141 GdbCommand(false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3144 _dpl(0, "GdbReadMemory called with bytes = 0!");
3147 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3148 else if(eval.result && strcmp(eval.result, "N/A"))
3150 byte * result = new byte[bytes];
3151 byte * string = eval.result;
3155 result[c++] = (byte)strtol(string, &string, 10);
3171 bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3174 char * s1 = null; char * s2 = null;
3175 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3176 "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3177 "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3178 "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3179 "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor, ")");
3180 delete s1; delete s2;
3184 bool conditionMet = true;
3185 if(bpUser.condition)
3187 if(WatchesLinkCodeEditor())
3188 conditionMet = ResolveWatch(bpUser.condition);
3190 conditionMet = false;
3205 if(stopItem.frame.line && bpUser.line != stopItem.frame.line)
3207 // updating user breakpoint on hit location difference
3208 // todo, print something?
3209 bpUser.line = stopItem.frame.line;
3210 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3211 ide.workspace.Save();
3214 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3219 if(bpInternal.type == internalModulesLoaded)
3221 if(userAction == stepOver)
3223 if((bpInternal.type == internalEntry && ((intBpMain && intBpMain.inserted) || (intBpWinMain && intBpWinMain.inserted))) ||
3224 (bpInternal.type == internalMain && intBpWinMain && intBpWinMain.inserted))
3227 if(!bpUser && !userAction.breaksOnInternalBreakpoint)
3229 if(userAction == stepOut)
3230 StepOut(ignoreBreakpoints);
3236 if(!bpUser && !bpInternal)
3242 void ValgrindTargetThreadExit()
3244 ide.outputView.debugBox.Logf($"ValgrindTargetThreadExit\n");
3247 vgTargetHandle.Wait();
3248 delete vgTargetHandle;
3250 HandleExit(null, null);
3253 void GdbThreadExit()
3255 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3256 if(state != terminated)
3258 _ChangeState(terminated);
3259 targetProcessId = 0;
3260 ClearBreakDisplay();
3266 serialSemaphore.Release();
3271 ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3272 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3274 HideDebuggerViews();
3276 //_ChangeState(terminated);
3280 void GdbThreadMain(char * output)
3284 Array<char *> outTokens { minAllocSize = 50 };
3285 Array<char *> subTokens { minAllocSize = 50 };
3286 DebugListItem item { };
3287 DebugListItem item2 { };
3288 bool setWaitingForPID = false;
3290 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3291 #ifdef GDB_DEBUG_CONSOLE
3292 // _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3295 #ifdef GDB_DEBUG_OUTPUT
3297 int len = strlen(output);
3305 for(c = 0; c < len / 1024; c++)
3307 strncpy(tmp, start, 1024);
3308 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3311 ide.outputView.gdbBox.Logf("out: %s\n", start);
3315 ide.outputView.gdbBox.Logf("out: %s\n", output);
3319 #ifdef GDB_DEBUG_CONSOLE
3320 strcpy(lastGdbOutput, output);
3322 #ifdef GDB_DEBUG_GUI
3323 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3330 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3333 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3336 if(!entryPoint && (t = strstr(output, "Entry point:")))
3338 char * addr = t + strlen("Entry point:");
3340 if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3343 while(isxdigit(*++t));
3345 for(bp : sysBPs; bp.type == internalEntry)
3348 bp.enabled = entryPoint = true;
3356 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3358 //if(outTokens.count == 1)
3363 _ChangeState(loaded);
3364 targetProcessId = 0;
3365 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3367 if(!strcmp(item.name, "reason"))
3369 char * reason = item.value;
3370 StripQuotes(reason, reason);
3371 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3374 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3376 StripQuotes(item2.value, item2.value);
3377 if(!strcmp(item2.name, "exit-code"))
3378 exitCode = item2.value;
3384 HandleExit(reason, exitCode);
3388 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3391 HandleExit(null, null);
3394 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3396 if(!strcmp(item.name, "bkpt"))
3398 sentBreakInsert = false;
3403 bpItem = ParseBreakpoint(item.value, outTokens);
3404 //breakType = bpValidation;
3406 else if(!strcmp(item.name, "depth"))
3408 StripQuotes(item.value, item.value);
3409 frameCount = atoi(item.value);
3411 stackFrames.Free(Frame::Free);
3413 else if(!strcmp(item.name, "stack"))
3416 if(stackFrames.count)
3417 ide.callStackView.Logf("...\n");
3420 item.value = StripBrackets(item.value);
3421 TokenizeList(item.value, ',', subTokens);
3422 for(i = 0; i < subTokens.count; i++)
3424 if(TokenizeListItem(subTokens[i], item))
3426 if(!strcmp(item.name, "frame"))
3429 stackFrames.Add(frame);
3430 item.value = StripCurlies(item.value);
3431 ParseFrame(frame, item.value);
3432 if(frame.file && frame.from)
3433 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3437 if(activeFrameLevel == -1)
3439 if(ide.projectView.IsModuleInProject(frame.file));
3441 if(frame.level != 0)
3443 //stopItem.frame = frame;
3444 breakType = selectFrame;
3447 activeFrame = frame;
3448 activeFrameLevel = frame.level;
3451 ide.callStackView.Logf("%3d ", frame.level);
3452 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3453 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3454 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3455 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3456 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3457 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3458 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3459 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3461 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3466 ide.callStackView.Logf("%3d ", frame.level);
3471 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3475 ide.callStackView.Logf("%s\n", frame.func);
3477 ide.callStackView.Logf($"unknown source\n");
3481 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3484 if(activeFrameLevel == -1)
3486 activeFrameLevel = 0;
3487 activeFrame = stackFrames.first;
3489 ide.callStackView.Home();
3491 subTokens.RemoveAll();
3493 /*else if(!strcmp(item.name, "frame"))
3496 item.value = StripCurlies(item.value);
3497 ParseFrame(&frame, item.value);
3499 else if(!strcmp(item.name, "thread-ids"))
3501 ide.threadsView.Clear();
3502 item.value = StripCurlies(item.value);
3503 TokenizeList(item.value, ',', subTokens);
3504 for(i = subTokens.count - 1; ; i--)
3506 if(TokenizeListItem(subTokens[i], item))
3508 if(!strcmp(item.name, "thread-id"))
3511 StripQuotes(item.value, item.value);
3512 value = atoi(item.value);
3513 ide.threadsView.Logf("%3d \n", value);
3516 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3521 ide.threadsView.Home();
3523 subTokens.RemoveAll();
3524 //if(!strcmp(outTokens[2], "number-of-threads"))
3526 else if(!strcmp(item.name, "new-thread-id"))
3528 StripQuotes(item.value, item.value);
3529 activeThread = atoi(item.value);
3531 else if(!strcmp(item.name, "value"))
3533 StripQuotes(item.value, item.value);
3534 eval.result = CopyString(item.value);
3535 eval.active = false;
3537 else if(!strcmp(item.name, "addr"))
3539 for(i = 2; i < outTokens.count; i++)
3541 if(TokenizeListItem(outTokens[i], item))
3543 if(!strcmp(item.name, "total-bytes"))
3545 StripQuotes(item.value, item.value);
3546 eval.bytes = atoi(item.value);
3548 else if(!strcmp(item.name, "next-row"))
3550 StripQuotes(item.value, item.value);
3551 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3553 else if(!strcmp(item.name, "memory"))
3557 //StripQuotes(item.value, item.value);
3558 item.value = StripBrackets(item.value);
3559 // this should be treated as a list...
3560 item.value = StripCurlies(item.value);
3561 TokenizeList(item.value, ',', subTokens);
3562 for(j = 0; j < subTokens.count; j++)
3564 if(TokenizeListItem(subTokens[j], item))
3566 if(!strcmp(item.name, "data"))
3568 item.value = StripBrackets(item.value);
3569 StripQuotes2(item.value, item.value);
3570 eval.result = CopyString(item.value);
3571 eval.active = false;
3575 subTokens.RemoveAll();
3580 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3581 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3583 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3586 else if(!strcmp(outTokens[0], "^running"))
3588 waitingForPID = true;
3589 setWaitingForPID = true;
3590 ClearBreakDisplay();
3592 else if(!strcmp(outTokens[0], "^exit"))
3594 _ChangeState(terminated);
3595 // ide.outputView.debugBox.Logf("Exit\n");
3596 // ide.Update(null);
3598 serialSemaphore.Release();
3600 else if(!strcmp(outTokens[0], "^error"))
3604 sentBreakInsert = false;
3605 breakpointError = true;
3608 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3610 if(!strcmp(item.name, "msg"))
3612 StripQuotes(item.value, item.value);
3615 eval.active = false;
3617 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3618 eval.error = symbolNotFound;
3619 else if(strstr(item.value, "Cannot access memory at address"))
3620 eval.error = memoryCantBeRead;
3622 eval.error = unknown;
3624 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3627 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3630 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3632 _ChangeState(stopped);
3633 gdbHandle.Printf("-exec-continue\n");
3635 else if(!strcmp(item.value, "ptrace: No such process."))
3637 _ChangeState(loaded);
3638 targetProcessId = 0;
3640 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3643 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3645 _ChangeState(loaded);
3646 targetProcessId = 0;
3648 else if(strstr(item.value, "No such file or directory."))
3650 _ChangeState(loaded);
3651 targetProcessId = 0;
3653 else if(strstr(item.value, "During startup program exited with code "))
3655 _ChangeState(loaded);
3656 targetProcessId = 0;
3661 if(strlen(item.value) < MAX_F_STRING)
3664 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3668 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3674 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3677 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3678 outTokens.RemoveAll();
3681 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3684 if(TokenizeList(output, ',', outTokens))
3686 if(!strcmp(outTokens[0], "=library-loaded"))
3687 FGODetectLoadedLibraryForAddedProjectIssues(outTokens);
3688 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3689 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3690 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3691 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3692 !strcmp(outTokens[0], "=breakpoint-modified"))
3693 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3694 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3695 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3696 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3697 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3699 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3701 outTokens.RemoveAll();
3705 if(TokenizeList(output, ',', outTokens))
3707 if(!strcmp(outTokens[0],"*running"))
3709 waitingForPID = true;
3710 setWaitingForPID = true;
3712 else if(!strcmp(outTokens[0], "*stopped"))
3715 _ChangeState(stopped);
3717 for(tk = 1; tk < outTokens.count; tk++)
3719 if(TokenizeListItem(outTokens[tk], item))
3721 if(!strcmp(item.name, "reason"))
3723 char * reason = item.value;
3724 StripQuotes(reason, reason);
3725 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3728 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3731 StripQuotes(item2.value, item2.value);
3732 if(!strcmp(item2.name, "exit-code"))
3733 exitCode = item2.value;
3739 HandleExit(reason, exitCode);
3742 else if(!strcmp(reason, "breakpoint-hit") ||
3743 !strcmp(reason, "function-finished") ||
3744 !strcmp(reason, "end-stepping-range") ||
3745 !strcmp(reason, "location-reached") ||
3746 !strcmp(reason, "signal-received"))
3750 if(stopItem) _dpl(0, "problem");
3752 stopItem = GdbDataStop { };
3753 stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3755 for(i = tk+1; i < outTokens.count; i++)
3757 TokenizeListItem(outTokens[i], item);
3758 StripQuotes(item.value, item.value);
3759 if(!strcmp(item.name, "thread-id"))
3760 stopItem.threadid = atoi(item.value);
3761 else if(!strcmp(item.name, "frame"))
3763 item.value = StripCurlies(item.value);
3764 ParseFrame(stopItem.frame, item.value);
3766 else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3767 stopItem.bkptno = atoi(item.value);
3768 else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3769 stopItem.gdbResultVar = CopyString(item.value);
3770 else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3771 stopItem.returnValue = CopyString(item.value);
3772 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3773 stopItem.name = CopyString(item.value);
3774 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3775 stopItem.meaning = CopyString(item.value);
3776 else if(!strcmp(item.name, "stopped-threads"))
3777 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3778 else if(!strcmp(item.name, "core"))
3779 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3780 else if(!strcmp(item.name, "disp"))
3781 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3783 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3786 if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
3802 event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
3806 else if(!strcmp(reason, "watchpoint-trigger"))
3807 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3808 else if(!strcmp(reason, "read-watchpoint-trigger"))
3809 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3810 else if(!strcmp(reason, "access-watchpoint-trigger"))
3811 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3812 else if(!strcmp(reason, "watchpoint-scope"))
3813 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3815 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3823 if(usingValgrind && event == none && !stopItem)
3824 event = valgrindStartPause;
3829 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3830 outTokens.RemoveAll();
3833 if(!strcmpi(output, "(gdb) "))
3837 char exeFile[MAX_LOCATION];
3838 int oldProcessID = targetProcessId;
3839 GetLastDirectory(targetFile, exeFile);
3841 while(!targetProcessId/*true*/)
3843 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3844 if(targetProcessId || gdbHandle.Peek()) break;
3849 _ChangeState(running);
3850 else if(!oldProcessID)
3852 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3853 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3854 gdbHandle.Printf("-gdb-exit\n");
3856 _ChangeState(terminated); //loaded;
3861 for(bp : ide.workspace.breakpoints)
3862 bp.inserted = false;
3865 bp.inserted = false;
3867 bpRunToCursor.inserted = false;
3869 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3870 ClearBreakDisplay();
3872 #if defined(__unix__)
3873 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
3875 progThread.terminate = true;
3878 fifoFile.CloseInput();
3885 DeleteFile(progFifoPath);
3886 progFifoPath[0] = '\0';
3893 serialSemaphore.Release();
3896 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
3900 if(!strncmp(output, "&\"warning:", 10))
3903 content = strstr(output, "\"");
3904 StripQuotes(content, content);
3905 content = strstr(content, ":");
3911 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
3918 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
3920 if(!setWaitingForPID)
3921 waitingForPID = false;
3922 setWaitingForPID = false;
3930 // From GDB Output functions
3931 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens)
3933 char path[MAX_LOCATION] = "";
3934 char file[MAX_FILENAME] = "";
3936 DebugListItem item { };
3937 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
3938 for(token : outTokens)
3940 if(TokenizeListItem(token, item))
3942 if(!strcmp(item.name, "target-name"))
3944 StripQuotes(item.value, path);
3945 MakeSystemPath(path);
3946 GetLastDirectory(path, file);
3948 else if(!strcmp(item.name, "symbols-loaded"))
3950 symbolsLoaded = (atoi(item.value) == 1);
3955 if(path[0] && file[0])
3957 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
3961 char prjTargetPath[MAX_LOCATION];
3962 char prjTargetFile[MAX_FILENAME];
3963 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
3964 strcpy(prjTargetPath, prj.topNode.path);
3965 PathCat(prjTargetPath, targetDirExp.dir);
3966 prjTargetFile[0] = '\0';
3967 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
3968 PathCat(prjTargetPath, prjTargetFile);
3969 MakeSystemPath(prjTargetPath);
3971 match = !fstrcmp(prjTargetFile, file);
3972 if(!match && (dot = strstr(prjTargetFile, ".so.")))
3974 char * dot3 = strstr(dot+4, ".");
3978 match = !fstrcmp(prjTargetFile, file);
3983 match = !fstrcmp(prjTargetFile, file);
3988 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
3989 /* -- 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)
3991 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
3993 match = !fstrcmp(prjTargetPath, path);
3994 if(!match && (dot = strstr(prjTargetPath, ".so.")))
3996 char * dot3 = strstr(dot+4, ".");
4000 match = !fstrcmp(prjTargetPath, path);
4005 match = !fstrcmp(prjTargetPath, path);
4009 projectsLibraryLoaded[prj.name] = true;
4011 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4018 void FGOBreakpointModified(Array<char *> outTokens)
4020 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4022 DebugListItem item { };
4023 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4025 if(!strcmp(item.name, "bkpt"))
4027 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4035 ExpressionType ::DebugEvalExpTypeError(char * result)
4037 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4042 case symbolNotFound:
4043 return symbolErrorExp;
4044 case memoryCantBeRead:
4045 return memoryErrorExp;
4047 return unknownErrorExp;
4050 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4053 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4054 if(ide.projectView && ide.debugger.state == stopped)
4056 result = GdbEvaluateExpression(expression);
4057 *error = DebugEvalExpTypeError(result);
4062 *error = noDebuggerErrorExp;
4067 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4070 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4071 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4072 if(!result || !strcmp(result, "N/A"))
4073 *error = memoryErrorExp;
4075 *error = DebugEvalExpTypeError(result);
4080 class ValgrindLogThread : Thread
4086 static char output[4096];
4087 Array<char> dynamicBuffer { minAllocSize = 4096 };
4088 File oldValgrindHandle = vgLogFile;
4089 incref oldValgrindHandle;
4092 while(debugger.state != terminated && vgLogFile)
4096 result = vgLogFile.Read(output, 1, sizeof(output));
4098 if(debugger.state == terminated || !vgLogFile/* || vgLogFile.Eof()*/)
4105 for(c = 0; c<result; c++)
4107 if(output[c] == '\n')
4109 int pos = dynamicBuffer.size;
4110 dynamicBuffer.size += c - start;
4111 memcpy(&dynamicBuffer[pos], output + start, c - start);
4112 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4113 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4114 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4115 dynamicBuffer.size++;
4116 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4118 // printf("%s\n", dynamicBuffer.array);
4120 if(strstr(&dynamicBuffer[0], "vgdb me"))
4121 debugger.serialSemaphore.Release();
4122 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4123 dynamicBuffer.size = 0;
4129 int pos = dynamicBuffer.size;
4130 dynamicBuffer.size += c - start;
4131 memcpy(&dynamicBuffer[pos], output + start, c - start);
4134 else if(debugger.state == stopped)
4137 printf("Got end of file from GDB!\n");
4144 delete dynamicBuffer;
4145 ide.outputView.debugBox.Logf($"ValgrindLogThreadExit\n");
4146 //if(oldValgrindHandle == vgLogFile)
4147 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4148 delete oldValgrindHandle;
4154 class ValgrindTargetThread : Thread
4160 static char output[4096];
4161 Array<char> dynamicBuffer { minAllocSize = 4096 };
4162 DualPipe oldValgrindHandle = vgTargetHandle;
4163 incref oldValgrindHandle;
4166 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4170 result = vgTargetHandle.Read(output, 1, sizeof(output));
4172 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4179 for(c = 0; c<result; c++)
4181 if(output[c] == '\n')
4183 int pos = dynamicBuffer.size;
4184 dynamicBuffer.size += c - start;
4185 memcpy(&dynamicBuffer[pos], output + start, c - start);
4186 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4187 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4188 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4189 dynamicBuffer.size++;
4190 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4192 // printf("%s\n", dynamicBuffer.array);
4194 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4196 dynamicBuffer.size = 0;
4202 int pos = dynamicBuffer.size;
4203 dynamicBuffer.size += c - start;
4204 memcpy(&dynamicBuffer[pos], output + start, c - start);
4210 printf("Got end of file from GDB!\n");
4214 delete dynamicBuffer;
4215 //if(oldValgrindHandle == vgTargetHandle)
4216 debugger.ValgrindTargetThreadExit();
4217 delete oldValgrindHandle;
4223 class GdbThread : Thread
4229 static char output[4096];
4230 Array<char> dynamicBuffer { minAllocSize = 4096 };
4231 DualPipe oldGdbHandle = gdbHandle;
4232 incref oldGdbHandle;
4235 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4239 result = gdbHandle.Read(output, 1, sizeof(output));
4241 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4248 for(c = 0; c<result; c++)
4250 if(output[c] == '\n')
4252 int pos = dynamicBuffer.size;
4253 dynamicBuffer.size += c - start;
4254 memcpy(&dynamicBuffer[pos], output + start, c - start);
4255 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4256 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4257 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4258 dynamicBuffer.size++;
4259 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4261 // _dpl(0, dynamicBuffer.array);
4263 debugger.GdbThreadMain(&dynamicBuffer[0]);
4264 dynamicBuffer.size = 0;
4270 int pos = dynamicBuffer.size;
4271 dynamicBuffer.size += c - start;
4272 memcpy(&dynamicBuffer[pos], output + start, c - start);
4278 _dpl(0, "Got end of file from GDB!");
4282 delete dynamicBuffer;
4283 //if(oldGdbHandle == gdbHandle)
4284 debugger.GdbThreadExit();
4285 delete oldGdbHandle;
4291 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4292 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4294 #if defined(__unix__)
4299 #include <sys/types.h>
4304 class ProgramThread : Thread
4310 bool fileCreated = false;
4312 static char output[1000];
4315 /*if(!mkfifo(progFifoPath, mask))
4322 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4326 if(FileExists(progFifoPath)) //fileCreated)
4328 fifoFile = FileOpen(progFifoPath, read);
4332 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4337 fd = fileno((FILE *)fifoFile.input);
4338 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4342 while(!terminate && fifoFile && !fifoFile.Eof())
4345 struct timeval time;
4353 selectResult = select(fd + 1, &rs, null, null, &time);
4354 if(FD_ISSET(fd, &rs))
4356 int result = (int)read(fd, output, sizeof(output)-1);
4357 if(!result || (result < 0 && errno != EAGAIN))
4361 output[result] = '\0';
4362 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4365 ide.outputView.debugBox.Log(output);
4374 //fifoFile.CloseInput();
4377 ide.outputView.debugBox.Log("\n");
4381 if(FileExists(progFifoPath)) //fileCreated)
4383 DeleteFile(progFifoPath);
4384 progFifoPath[0] = '\0';
4392 class Argument : struct
4394 Argument prev, next;
4396 property char * name { set { delete name; if(value) name = CopyString(value); } }
4398 property char * val { set { delete val; if(value) val = CopyString(value); } }
4412 class Frame : struct
4417 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4419 property char * func { set { delete func; if(value) func = CopyString(value); } }
4423 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4425 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4426 char * absoluteFile;
4427 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4436 delete absoluteFile;
4437 args.Free(Argument::Free);
4446 class GdbDataStop : struct
4448 DebuggerReason reason;
4463 char * gdbResultVar;
4473 if(reason == signalReceived)
4478 else if(reason == functionFinished)
4480 delete gdbResultVar;
4484 if(frame) frame.Free();
4493 class GdbDataBreakpoint : struct
4497 property char * number { set { delete number; if(value) number = CopyString(value); } }
4499 property char * type { set { delete type; if(value) type = CopyString(value); } }
4501 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4504 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4506 property char * func { set { delete func; if(value) func = CopyString(value); } }
4508 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4510 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4513 property char * at { set { delete at; if(value) at = CopyString(value); } }
4516 Array<GdbDataBreakpoint> multipleBPs;
4521 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4532 if(multipleBPs) multipleBPs.Free();
4536 ~GdbDataBreakpoint()
4542 class Breakpoint : struct
4547 property char * function { set { delete function; if(value) function = CopyString(value); } }
4548 char * relativeFilePath;
4549 property char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4550 char * absoluteFilePath;
4551 property char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4553 property char * location { set { delete location; if(value) location = CopyString(value); } }
4562 BreakpointType type;
4564 GdbDataBreakpoint bp;
4567 property char * address { set { delete address; if(value) address = CopyString(value); } }
4569 void ParseLocation()
4571 char * prjName = null;
4572 char * filePath = null;
4575 char fullPath[MAX_LOCATION];
4576 if(location[0] == '\(' && location[1] && (file = strchr(location+2, '\)')) && file[1])
4578 prjName = new char[file-location];
4579 strncpy(prjName, location+1, file-location-1);
4580 prjName[file-location-1] = '\0';
4585 if((line = strchr(file+1, ':')))
4587 filePath = new char[strlen(file)+1];
4588 strncpy(filePath, file, line-file);
4589 filePath[line-file] = '\0';
4593 filePath = CopyString(file);
4594 property::relativeFilePath = filePath;
4597 for(prj : ide.workspace.projects)
4599 if(!strcmp(prjName, prj.name))
4601 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4603 property::absoluteFilePath = fullPath;
4610 this.line = atoi(line);
4614 Project prj = ide.project;
4615 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4617 property::absoluteFilePath = fullPath;
4621 if(!absoluteFilePath)
4622 property::absoluteFilePath = "";
4627 char * CopyLocationString(bool removePath)
4630 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4631 bool removingPath = removePath && file;
4634 char * fileName = new char[MAX_FILENAME];
4635 GetLastDirectory(file, fileName);
4641 location = PrintString(file, ":", function);
4643 location = CopyString(function);
4646 location = PrintString(file, ":", line);
4652 char * CopyUserLocationString()
4655 char * loc = CopyLocationString(false);
4657 if(absoluteFilePath)
4659 for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4661 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4670 location = PrintString("(", prj.name, ")", loc);
4680 if(relativeFilePath && relativeFilePath[0])
4682 char * location = CopyUserLocationString();
4683 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4686 f.Printf(" ~ %s\n", condition.expression);
4694 delete relativeFilePath;
4695 delete absoluteFilePath;
4715 class Watch : struct
4726 f.Printf(" ~ %s\n", expression);
4750 class DebugListItem : struct
4756 struct DebugEvaluationData
4761 uint64 nextBlockAddress;
4763 DebuggerEvaluationError error;
4766 class CodeLocation : struct
4769 char * absoluteFile;
4772 CodeLocation ::ParseCodeLocation(char * location)
4776 char * colon = null;
4778 char loc[MAX_LOCATION];
4779 strcpy(loc, location);
4780 for(temp = loc; temp = strstr(temp, ":"); temp++)
4788 int line = atoi(colon);
4791 CodeLocation codloc { line = line };
4792 codloc.file = CopyString(loc);
4793 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
4805 delete absoluteFile;
4814 void GDBFallBack(Expression exp, String expString)
4817 ExpressionType evalError = dummyExp;
4818 result = Debugger::EvaluateExpression(expString, &evalError);
4821 exp.constant = result;
4822 exp.type = constantExp;
4826 static Project WorkspaceGetFileOwner(char * absolutePath)
4828 Project owner = null;
4829 for(prj : ide.workspace.projects)
4831 if(prj.topNode.FindByFullPath(absolutePath, false))
4838 WorkspaceGetObjectFileNode(absolutePath, &owner);
4842 static ProjectNode WorkspaceGetObjectFileNode(char * filePath, Project * project)
4844 ProjectNode node = null;
4845 char ext[MAX_EXTENSION];
4846 GetExtension(filePath, ext);
4849 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4852 char fileName[MAX_FILENAME];
4853 GetLastDirectory(filePath, fileName);
4856 DotMain dotMain = DotMain::FromFileName(fileName);
4857 for(prj : ide.workspace.projects)
4859 if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
4872 static ProjectNode ProjectGetObjectFileNode(Project project, char * filePath)
4874 ProjectNode node = null;
4875 char ext[MAX_EXTENSION];
4876 GetExtension(filePath, ext);
4879 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4882 char fileName[MAX_FILENAME];
4883 GetLastDirectory(filePath, fileName);
4886 DotMain dotMain = DotMain::FromFileName(fileName);
4887 node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
4894 static void WorkspaceGetRelativePath(char * absolutePath, char * relativePath, Project * owner)
4896 Project prj = WorkspaceGetFileOwner(absolutePath);
4900 prj = ide.workspace.projects.firstIterator.data;
4903 MakePathRelative(absolutePath, prj.topNode.path, relativePath);
4904 MakeSlashPath(relativePath);
4907 relativePath[0] = '\0';
4910 static bool ProjectGetAbsoluteFromRelativePath(Project project, char * relativePath, char * absolutePath)
4912 ProjectNode node = project.topNode.FindWithPath(relativePath, false);
4914 node = ProjectGetObjectFileNode(project, relativePath);
4917 strcpy(absolutePath, node.project.topNode.path);
4918 PathCat(absolutePath, relativePath);
4919 MakeSlashPath(absolutePath);
4921 return node != null;