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);
3294 #ifdef GDB_DEBUG_OUTPUT
3296 int len = strlen(output);
3304 for(c = 0; c < len / 1024; c++)
3306 strncpy(tmp, start, 1024);
3307 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3310 ide.outputView.gdbBox.Logf("out: %s\n", start);
3314 ide.outputView.gdbBox.Logf("out: %s\n", output);
3318 #ifdef GDB_DEBUG_CONSOLE
3319 strcpy(lastGdbOutput, output);
3321 #ifdef GDB_DEBUG_GUI
3322 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3329 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3332 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3335 if(!entryPoint && (t = strstr(output, "Entry point:")))
3337 char * addr = t + strlen("Entry point:");
3339 if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3342 while(isxdigit(*++t));
3344 for(bp : sysBPs; bp.type == internalEntry)
3347 bp.enabled = entryPoint = true;
3355 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3357 //if(outTokens.count == 1)
3362 _ChangeState(loaded);
3363 targetProcessId = 0;
3364 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3366 if(!strcmp(item.name, "reason"))
3368 char * reason = item.value;
3369 StripQuotes(reason, reason);
3370 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3373 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3375 StripQuotes(item2.value, item2.value);
3376 if(!strcmp(item2.name, "exit-code"))
3377 exitCode = item2.value;
3383 HandleExit(reason, exitCode);
3387 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3390 HandleExit(null, null);
3393 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3395 if(!strcmp(item.name, "bkpt"))
3397 sentBreakInsert = false;
3402 bpItem = ParseBreakpoint(item.value, outTokens);
3403 //breakType = bpValidation;
3405 else if(!strcmp(item.name, "depth"))
3407 StripQuotes(item.value, item.value);
3408 frameCount = atoi(item.value);
3410 stackFrames.Free(Frame::Free);
3412 else if(!strcmp(item.name, "stack"))
3415 if(stackFrames.count)
3416 ide.callStackView.Logf("...\n");
3419 item.value = StripBrackets(item.value);
3420 TokenizeList(item.value, ',', subTokens);
3421 for(i = 0; i < subTokens.count; i++)
3423 if(TokenizeListItem(subTokens[i], item))
3425 if(!strcmp(item.name, "frame"))
3428 stackFrames.Add(frame);
3429 item.value = StripCurlies(item.value);
3430 ParseFrame(frame, item.value);
3431 if(frame.file && frame.from)
3432 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3436 if(activeFrameLevel == -1)
3438 if(ide.projectView.IsModuleInProject(frame.file));
3440 if(frame.level != 0)
3442 //stopItem.frame = frame;
3443 breakType = selectFrame;
3446 activeFrame = frame;
3447 activeFrameLevel = frame.level;
3450 ide.callStackView.Logf("%3d ", frame.level);
3451 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3452 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3453 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3454 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3455 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3456 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3457 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3458 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3460 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3465 ide.callStackView.Logf("%3d ", frame.level);
3470 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3474 ide.callStackView.Logf("%s\n", frame.func);
3476 ide.callStackView.Logf($"unknown source\n");
3480 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3483 if(activeFrameLevel == -1)
3485 activeFrameLevel = 0;
3486 activeFrame = stackFrames.first;
3488 ide.callStackView.Home();
3490 subTokens.RemoveAll();
3492 /*else if(!strcmp(item.name, "frame"))
3495 item.value = StripCurlies(item.value);
3496 ParseFrame(&frame, item.value);
3498 else if(!strcmp(item.name, "thread-ids"))
3500 ide.threadsView.Clear();
3501 item.value = StripCurlies(item.value);
3502 TokenizeList(item.value, ',', subTokens);
3503 for(i = subTokens.count - 1; ; i--)
3505 if(TokenizeListItem(subTokens[i], item))
3507 if(!strcmp(item.name, "thread-id"))
3510 StripQuotes(item.value, item.value);
3511 value = atoi(item.value);
3512 ide.threadsView.Logf("%3d \n", value);
3515 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3520 ide.threadsView.Home();
3522 subTokens.RemoveAll();
3523 //if(!strcmp(outTokens[2], "number-of-threads"))
3525 else if(!strcmp(item.name, "new-thread-id"))
3527 StripQuotes(item.value, item.value);
3528 activeThread = atoi(item.value);
3530 else if(!strcmp(item.name, "value"))
3532 StripQuotes(item.value, item.value);
3533 eval.result = CopyString(item.value);
3534 eval.active = false;
3536 else if(!strcmp(item.name, "addr"))
3538 for(i = 2; i < outTokens.count; i++)
3540 if(TokenizeListItem(outTokens[i], item))
3542 if(!strcmp(item.name, "total-bytes"))
3544 StripQuotes(item.value, item.value);
3545 eval.bytes = atoi(item.value);
3547 else if(!strcmp(item.name, "next-row"))
3549 StripQuotes(item.value, item.value);
3550 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3552 else if(!strcmp(item.name, "memory"))
3556 //StripQuotes(item.value, item.value);
3557 item.value = StripBrackets(item.value);
3558 // this should be treated as a list...
3559 item.value = StripCurlies(item.value);
3560 TokenizeList(item.value, ',', subTokens);
3561 for(j = 0; j < subTokens.count; j++)
3563 if(TokenizeListItem(subTokens[j], item))
3565 if(!strcmp(item.name, "data"))
3567 item.value = StripBrackets(item.value);
3568 StripQuotes2(item.value, item.value);
3569 eval.result = CopyString(item.value);
3570 eval.active = false;
3574 subTokens.RemoveAll();
3579 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3580 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3582 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3585 else if(!strcmp(outTokens[0], "^running"))
3587 waitingForPID = true;
3588 setWaitingForPID = true;
3589 ClearBreakDisplay();
3591 else if(!strcmp(outTokens[0], "^exit"))
3593 _ChangeState(terminated);
3594 // ide.outputView.debugBox.Logf("Exit\n");
3595 // ide.Update(null);
3597 serialSemaphore.Release();
3599 else if(!strcmp(outTokens[0], "^error"))
3603 sentBreakInsert = false;
3604 breakpointError = true;
3607 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3609 if(!strcmp(item.name, "msg"))
3611 StripQuotes(item.value, item.value);
3614 eval.active = false;
3616 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3617 eval.error = symbolNotFound;
3618 else if(strstr(item.value, "Cannot access memory at address"))
3619 eval.error = memoryCantBeRead;
3621 eval.error = unknown;
3623 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3626 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3629 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3631 _ChangeState(stopped);
3632 gdbHandle.Printf("-exec-continue\n");
3634 else if(!strcmp(item.value, "ptrace: No such process."))
3636 _ChangeState(loaded);
3637 targetProcessId = 0;
3639 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3642 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3644 _ChangeState(loaded);
3645 targetProcessId = 0;
3647 else if(strstr(item.value, "No such file or directory."))
3649 _ChangeState(loaded);
3650 targetProcessId = 0;
3652 else if(strstr(item.value, "During startup program exited with code "))
3654 _ChangeState(loaded);
3655 targetProcessId = 0;
3660 if(strlen(item.value) < MAX_F_STRING)
3663 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3667 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3673 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3676 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3677 outTokens.RemoveAll();
3680 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3683 if(TokenizeList(output, ',', outTokens))
3685 if(!strcmp(outTokens[0], "=library-loaded"))
3686 FGODetectLoadedLibraryForAddedProjectIssues(outTokens);
3687 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3688 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3689 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3690 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3691 !strcmp(outTokens[0], "=breakpoint-modified"))
3692 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3693 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3694 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3695 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3696 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3698 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3700 outTokens.RemoveAll();
3704 if(TokenizeList(output, ',', outTokens))
3706 if(!strcmp(outTokens[0],"*running"))
3708 waitingForPID = true;
3709 setWaitingForPID = true;
3711 else if(!strcmp(outTokens[0], "*stopped"))
3714 _ChangeState(stopped);
3716 for(tk = 1; tk < outTokens.count; tk++)
3718 if(TokenizeListItem(outTokens[tk], item))
3720 if(!strcmp(item.name, "reason"))
3722 char * reason = item.value;
3723 StripQuotes(reason, reason);
3724 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3727 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3730 StripQuotes(item2.value, item2.value);
3731 if(!strcmp(item2.name, "exit-code"))
3732 exitCode = item2.value;
3738 HandleExit(reason, exitCode);
3741 else if(!strcmp(reason, "breakpoint-hit") ||
3742 !strcmp(reason, "function-finished") ||
3743 !strcmp(reason, "end-stepping-range") ||
3744 !strcmp(reason, "location-reached") ||
3745 !strcmp(reason, "signal-received"))
3749 if(stopItem) _dpl(0, "problem");
3751 stopItem = GdbDataStop { };
3752 stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3754 for(i = tk+1; i < outTokens.count; i++)
3756 TokenizeListItem(outTokens[i], item);
3757 StripQuotes(item.value, item.value);
3758 if(!strcmp(item.name, "thread-id"))
3759 stopItem.threadid = atoi(item.value);
3760 else if(!strcmp(item.name, "frame"))
3762 item.value = StripCurlies(item.value);
3763 ParseFrame(stopItem.frame, item.value);
3765 else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3766 stopItem.bkptno = atoi(item.value);
3767 else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3768 stopItem.gdbResultVar = CopyString(item.value);
3769 else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3770 stopItem.returnValue = CopyString(item.value);
3771 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3772 stopItem.name = CopyString(item.value);
3773 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3774 stopItem.meaning = CopyString(item.value);
3775 else if(!strcmp(item.name, "stopped-threads"))
3776 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3777 else if(!strcmp(item.name, "core"))
3778 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3779 else if(!strcmp(item.name, "disp"))
3780 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3782 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3785 if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
3801 event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
3805 else if(!strcmp(reason, "watchpoint-trigger"))
3806 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3807 else if(!strcmp(reason, "read-watchpoint-trigger"))
3808 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3809 else if(!strcmp(reason, "access-watchpoint-trigger"))
3810 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3811 else if(!strcmp(reason, "watchpoint-scope"))
3812 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3814 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3822 if(usingValgrind && event == none && !stopItem)
3823 event = valgrindStartPause;
3828 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3829 outTokens.RemoveAll();
3832 if(!strcmpi(output, "(gdb) "))
3836 char exeFile[MAX_LOCATION];
3837 int oldProcessID = targetProcessId;
3838 GetLastDirectory(targetFile, exeFile);
3840 while(!targetProcessId/*true*/)
3842 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3843 if(targetProcessId || gdbHandle.Peek()) break;
3848 _ChangeState(running);
3849 else if(!oldProcessID)
3851 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3852 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3853 gdbHandle.Printf("-gdb-exit\n");
3855 _ChangeState(terminated); //loaded;
3860 for(bp : ide.workspace.breakpoints)
3861 bp.inserted = false;
3864 bp.inserted = false;
3866 bpRunToCursor.inserted = false;
3868 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3869 ClearBreakDisplay();
3871 #if defined(__unix__)
3872 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
3874 progThread.terminate = true;
3877 fifoFile.CloseInput();
3884 DeleteFile(progFifoPath);
3885 progFifoPath[0] = '\0';
3892 serialSemaphore.Release();
3895 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
3899 if(!strncmp(output, "&\"warning:", 10))
3902 content = strstr(output, "\"");
3903 StripQuotes(content, content);
3904 content = strstr(content, ":");
3910 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
3917 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
3919 if(!setWaitingForPID)
3920 waitingForPID = false;
3921 setWaitingForPID = false;
3929 // From GDB Output functions
3930 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens)
3932 char path[MAX_LOCATION] = "";
3933 char file[MAX_FILENAME] = "";
3935 DebugListItem item { };
3936 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
3937 for(token : outTokens)
3939 if(TokenizeListItem(token, item))
3941 if(!strcmp(item.name, "target-name"))
3943 StripQuotes(item.value, path);
3944 MakeSystemPath(path);
3945 GetLastDirectory(path, file);
3947 else if(!strcmp(item.name, "symbols-loaded"))
3949 symbolsLoaded = (atoi(item.value) == 1);
3954 if(path[0] && file[0])
3956 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
3960 char prjTargetPath[MAX_LOCATION];
3961 char prjTargetFile[MAX_FILENAME];
3962 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
3963 strcpy(prjTargetPath, prj.topNode.path);
3964 PathCat(prjTargetPath, targetDirExp.dir);
3965 prjTargetFile[0] = '\0';
3966 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
3967 PathCat(prjTargetPath, prjTargetFile);
3968 MakeSystemPath(prjTargetPath);
3970 match = !fstrcmp(prjTargetFile, file);
3971 if(!match && (dot = strstr(prjTargetFile, ".so.")))
3973 char * dot3 = strstr(dot+4, ".");
3977 match = !fstrcmp(prjTargetFile, file);
3982 match = !fstrcmp(prjTargetFile, file);
3987 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
3988 /* -- 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)
3990 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
3992 match = !fstrcmp(prjTargetPath, path);
3993 if(!match && (dot = strstr(prjTargetPath, ".so.")))
3995 char * dot3 = strstr(dot+4, ".");
3999 match = !fstrcmp(prjTargetPath, path);
4004 match = !fstrcmp(prjTargetPath, path);
4008 projectsLibraryLoaded[prj.name] = true;
4010 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4017 void FGOBreakpointModified(Array<char *> outTokens)
4019 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4021 DebugListItem item { };
4022 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4024 if(!strcmp(item.name, "bkpt"))
4026 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4034 ExpressionType ::DebugEvalExpTypeError(char * result)
4036 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4041 case symbolNotFound:
4042 return symbolErrorExp;
4043 case memoryCantBeRead:
4044 return memoryErrorExp;
4046 return unknownErrorExp;
4049 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4052 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4053 if(ide.projectView && ide.debugger.state == stopped)
4055 result = GdbEvaluateExpression(expression);
4056 *error = DebugEvalExpTypeError(result);
4061 *error = noDebuggerErrorExp;
4066 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4069 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4070 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4071 if(!result || !strcmp(result, "N/A"))
4072 *error = memoryErrorExp;
4074 *error = DebugEvalExpTypeError(result);
4079 class ValgrindLogThread : Thread
4085 static char output[4096];
4086 Array<char> dynamicBuffer { minAllocSize = 4096 };
4087 File oldValgrindHandle = vgLogFile;
4088 incref oldValgrindHandle;
4091 while(debugger.state != terminated && vgLogFile)
4095 result = vgLogFile.Read(output, 1, sizeof(output));
4097 if(debugger.state == terminated || !vgLogFile/* || vgLogFile.Eof()*/)
4104 for(c = 0; c<result; c++)
4106 if(output[c] == '\n')
4108 int pos = dynamicBuffer.size;
4109 dynamicBuffer.size += c - start;
4110 memcpy(&dynamicBuffer[pos], output + start, c - start);
4111 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4112 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4113 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4114 dynamicBuffer.size++;
4115 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4117 // printf("%s\n", dynamicBuffer.array);
4119 if(strstr(&dynamicBuffer[0], "vgdb me"))
4120 debugger.serialSemaphore.Release();
4121 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4122 dynamicBuffer.size = 0;
4128 int pos = dynamicBuffer.size;
4129 dynamicBuffer.size += c - start;
4130 memcpy(&dynamicBuffer[pos], output + start, c - start);
4133 else if(debugger.state == stopped)
4136 printf("Got end of file from GDB!\n");
4143 delete dynamicBuffer;
4144 ide.outputView.debugBox.Logf($"ValgrindLogThreadExit\n");
4145 //if(oldValgrindHandle == vgLogFile)
4146 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4147 delete oldValgrindHandle;
4153 class ValgrindTargetThread : Thread
4159 static char output[4096];
4160 Array<char> dynamicBuffer { minAllocSize = 4096 };
4161 DualPipe oldValgrindHandle = vgTargetHandle;
4162 incref oldValgrindHandle;
4165 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4169 result = vgTargetHandle.Read(output, 1, sizeof(output));
4171 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4178 for(c = 0; c<result; c++)
4180 if(output[c] == '\n')
4182 int pos = dynamicBuffer.size;
4183 dynamicBuffer.size += c - start;
4184 memcpy(&dynamicBuffer[pos], output + start, c - start);
4185 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4186 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4187 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4188 dynamicBuffer.size++;
4189 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4191 // printf("%s\n", dynamicBuffer.array);
4193 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4195 dynamicBuffer.size = 0;
4201 int pos = dynamicBuffer.size;
4202 dynamicBuffer.size += c - start;
4203 memcpy(&dynamicBuffer[pos], output + start, c - start);
4209 printf("Got end of file from GDB!\n");
4213 delete dynamicBuffer;
4214 //if(oldValgrindHandle == vgTargetHandle)
4215 debugger.ValgrindTargetThreadExit();
4216 delete oldValgrindHandle;
4222 class GdbThread : Thread
4228 static char output[4096];
4229 Array<char> dynamicBuffer { minAllocSize = 4096 };
4230 DualPipe oldGdbHandle = gdbHandle;
4231 incref oldGdbHandle;
4234 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4238 result = gdbHandle.Read(output, 1, sizeof(output));
4240 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4247 for(c = 0; c<result; c++)
4249 if(output[c] == '\n')
4251 int pos = dynamicBuffer.size;
4252 dynamicBuffer.size += c - start;
4253 memcpy(&dynamicBuffer[pos], output + start, c - start);
4254 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4255 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4256 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4257 dynamicBuffer.size++;
4258 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4260 // _dpl(0, dynamicBuffer.array);
4262 debugger.GdbThreadMain(&dynamicBuffer[0]);
4263 dynamicBuffer.size = 0;
4269 int pos = dynamicBuffer.size;
4270 dynamicBuffer.size += c - start;
4271 memcpy(&dynamicBuffer[pos], output + start, c - start);
4277 _dpl(0, "Got end of file from GDB!");
4281 delete dynamicBuffer;
4282 //if(oldGdbHandle == gdbHandle)
4283 debugger.GdbThreadExit();
4284 delete oldGdbHandle;
4290 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4291 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4293 #if defined(__unix__)
4298 #include <sys/types.h>
4303 class ProgramThread : Thread
4309 bool fileCreated = false;
4311 static char output[1000];
4314 /*if(!mkfifo(progFifoPath, mask))
4321 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4325 if(FileExists(progFifoPath)) //fileCreated)
4327 fifoFile = FileOpen(progFifoPath, read);
4331 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4336 fd = fileno((FILE *)fifoFile.input);
4337 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4341 while(!terminate && fifoFile && !fifoFile.Eof())
4344 struct timeval time;
4352 selectResult = select(fd + 1, &rs, null, null, &time);
4353 if(FD_ISSET(fd, &rs))
4355 int result = (int)read(fd, output, sizeof(output)-1);
4356 if(!result || (result < 0 && errno != EAGAIN))
4360 output[result] = '\0';
4361 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4364 ide.outputView.debugBox.Log(output);
4373 //fifoFile.CloseInput();
4376 ide.outputView.debugBox.Log("\n");
4380 if(FileExists(progFifoPath)) //fileCreated)
4382 DeleteFile(progFifoPath);
4383 progFifoPath[0] = '\0';
4391 class Argument : struct
4393 Argument prev, next;
4395 property char * name { set { delete name; if(value) name = CopyString(value); } }
4397 property char * val { set { delete val; if(value) val = CopyString(value); } }
4411 class Frame : struct
4416 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4418 property char * func { set { delete func; if(value) func = CopyString(value); } }
4422 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4424 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4425 char * absoluteFile;
4426 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4435 delete absoluteFile;
4436 args.Free(Argument::Free);
4445 class GdbDataStop : struct
4447 DebuggerReason reason;
4462 char * gdbResultVar;
4472 if(reason == signalReceived)
4477 else if(reason == functionFinished)
4479 delete gdbResultVar;
4483 if(frame) frame.Free();
4492 class GdbDataBreakpoint : struct
4496 property char * number { set { delete number; if(value) number = CopyString(value); } }
4498 property char * type { set { delete type; if(value) type = CopyString(value); } }
4500 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4503 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4505 property char * func { set { delete func; if(value) func = CopyString(value); } }
4507 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4509 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4512 property char * at { set { delete at; if(value) at = CopyString(value); } }
4515 Array<GdbDataBreakpoint> multipleBPs;
4520 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4531 if(multipleBPs) multipleBPs.Free();
4535 ~GdbDataBreakpoint()
4541 class Breakpoint : struct
4546 property char * function { set { delete function; if(value) function = CopyString(value); } }
4547 char * relativeFilePath;
4548 property char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4549 char * absoluteFilePath;
4550 property char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4552 property char * location { set { delete location; if(value) location = CopyString(value); } }
4561 BreakpointType type;
4563 GdbDataBreakpoint bp;
4566 property char * address { set { delete address; if(value) address = CopyString(value); } }
4568 void ParseLocation()
4570 char * prjName = null;
4571 char * filePath = null;
4574 char fullPath[MAX_LOCATION];
4575 if(location[0] == '\(' && location[1] && (file = strchr(location+2, '\)')) && file[1])
4577 prjName = new char[file-location];
4578 strncpy(prjName, location+1, file-location-1);
4579 prjName[file-location-1] = '\0';
4584 if((line = strchr(file+1, ':')))
4586 filePath = new char[strlen(file)+1];
4587 strncpy(filePath, file, line-file);
4588 filePath[line-file] = '\0';
4592 filePath = CopyString(file);
4593 property::relativeFilePath = filePath;
4596 for(prj : ide.workspace.projects)
4598 if(!strcmp(prjName, prj.name))
4600 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4602 property::absoluteFilePath = fullPath;
4609 this.line = atoi(line);
4613 Project prj = ide.project;
4614 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4616 property::absoluteFilePath = fullPath;
4620 if(!absoluteFilePath)
4621 property::absoluteFilePath = "";
4626 char * CopyLocationString(bool removePath)
4629 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4630 bool removingPath = removePath && file;
4633 char * fileName = new char[MAX_FILENAME];
4634 GetLastDirectory(file, fileName);
4640 location = PrintString(file, ":", function);
4642 location = CopyString(function);
4645 location = PrintString(file, ":", line);
4651 char * CopyUserLocationString()
4654 char * loc = CopyLocationString(false);
4656 if(absoluteFilePath)
4658 for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4660 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4669 location = PrintString("(", prj.name, ")", loc);
4679 if(relativeFilePath && relativeFilePath[0])
4681 char * location = CopyUserLocationString();
4682 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4685 f.Printf(" ~ %s\n", condition.expression);
4693 delete relativeFilePath;
4694 delete absoluteFilePath;
4714 class Watch : struct
4725 f.Printf(" ~ %s\n", expression);
4749 class DebugListItem : struct
4755 struct DebugEvaluationData
4760 uint64 nextBlockAddress;
4762 DebuggerEvaluationError error;
4765 class CodeLocation : struct
4768 char * absoluteFile;
4771 CodeLocation ::ParseCodeLocation(char * location)
4775 char * colon = null;
4777 char loc[MAX_LOCATION];
4778 strcpy(loc, location);
4779 for(temp = loc; temp = strstr(temp, ":"); temp++)
4787 int line = atoi(colon);
4790 CodeLocation codloc { line = line };
4791 codloc.file = CopyString(loc);
4792 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
4804 delete absoluteFile;
4813 void GDBFallBack(Expression exp, String expString)
4816 ExpressionType evalError = dummyExp;
4817 result = Debugger::EvaluateExpression(expString, &evalError);
4820 exp.constant = result;
4821 exp.type = constantExp;
4825 static Project WorkspaceGetFileOwner(char * absolutePath)
4827 Project owner = null;
4828 for(prj : ide.workspace.projects)
4830 if(prj.topNode.FindByFullPath(absolutePath, false))
4837 WorkspaceGetObjectFileNode(absolutePath, &owner);
4841 static ProjectNode WorkspaceGetObjectFileNode(char * filePath, Project * project)
4843 ProjectNode node = null;
4844 char ext[MAX_EXTENSION];
4845 GetExtension(filePath, ext);
4848 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4851 char fileName[MAX_FILENAME];
4852 GetLastDirectory(filePath, fileName);
4855 DotMain dotMain = DotMain::FromFileName(fileName);
4856 for(prj : ide.workspace.projects)
4858 if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
4871 static ProjectNode ProjectGetObjectFileNode(Project project, char * filePath)
4873 ProjectNode node = null;
4874 char ext[MAX_EXTENSION];
4875 GetExtension(filePath, ext);
4878 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4881 char fileName[MAX_FILENAME];
4882 GetLastDirectory(filePath, fileName);
4885 DotMain dotMain = DotMain::FromFileName(fileName);
4886 node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
4893 static void WorkspaceGetRelativePath(char * absolutePath, char * relativePath, Project * owner)
4895 Project prj = WorkspaceGetFileOwner(absolutePath);
4899 prj = ide.workspace.projects.firstIterator.data;
4902 MakePathRelative(absolutePath, prj.topNode.path, relativePath);
4903 MakeSlashPath(relativePath);
4906 relativePath[0] = '\0';
4909 static bool ProjectGetAbsoluteFromRelativePath(Project project, char * relativePath, char * absolutePath)
4911 ProjectNode node = project.topNode.FindWithPath(relativePath, false);
4913 node = ProjectGetObjectFileNode(project, relativePath);
4916 strcpy(absolutePath, node.project.topNode.path);
4917 PathCat(absolutePath, relativePath);
4918 MakeSlashPath(absolutePath);
4920 return node != null;