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
32 #include <sys/time.h> // Required on Apple...
46 sprintf(s[0], "%04d", now.year);
47 sprintf(s[1], "%02d", now.month+1);
48 sprintf(s[2], "%02d", now.day);
49 sprintf(s[3], "%02d", now.hour);
50 sprintf(s[4], "%02d", now.minute);
51 sprintf(s[5], "%02d", now.second);
52 time = PrintString("*", s[0], s[1], s[2], "-", s[3], s[4], s[5], "*");
58 // use =0 to disable printing of specific channels
60 static enum dplchan { none, gdbProtoIgnored=0/*1*/, gdbProtoUnknown=2, gdbOutput=0/*3*/, gdbCommand=0/*4*/, debuggerCall=0/*5*/, debuggerProblem=6,
61 debuggerUserAction=7,debuggerState=8, debuggerBreakpoints=9, debuggerWatches=0/*10*/, debuggerTemp=0 };
63 static enum dplchan { none, gdbProtoIgnored=0, gdbProtoUnknown=0, gdbOutput=0, gdbCommand=0, debuggerCall=0, debuggerProblem=0,
64 debuggerUserAction=0,debuggerState=0, debuggerBreakpoints=0, debuggerWatches=0, debuggerTemp=0 };
66 static char * _dpct[] = {
68 "GDB Protocol Ignored",
69 "GDB Protocol ***Unknown***",
73 "Debugger ***Problem***",
74 "Debugger::ChangeUserAction",
75 "Debugger::ChangeState",
78 "-----> Temporary Message",
82 // TODO if(strlen(item.value) < MAX_F_STRING)
86 #define _dpl2(...) __dpl2(__FILE__, __LINE__, ##__VA_ARGS__)
90 static void __dpl2(char * file, int line, char ** channels, int channel, int indent, typed_object object, ...)
92 bool chan = channel && channels && channels[channel];
95 char string[MAX_F_STRING];
97 char * time = PrintNow();
99 //ide.outputView.debugBox.Logf();
100 Logf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
101 va_start(args, object);
102 len = PrintStdArgsToBuffer(string, sizeof(string), object, args);
110 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
111 static void __dpl(char * file, int line, int indent, char * format, ...)
114 char string[MAX_F_STRING];
116 char * time = PrintNow();
117 //static File f = null;
118 va_start(args, format);
119 vsnprintf(string, sizeof(string), format, args);
120 string[sizeof(string)-1] = 0;
123 char * time = PrintNow();
125 logName = PrintString(time, ".log");
127 f = FileOpen(logName, write);
130 /*f.Printf("%s %s:% 5d: ", time, file, line);
131 for(c = 0; c<indent; c++)
133 f.Printf("%s\n", string);*/
134 Logf("%s %s:% 5d: ", time, file, line);
135 for(c = 0; c<indent; c++)
137 Logf("%s\n", string);
142 public char * StripQuotes2(char * string, char * output)
146 bool quoted = false, escaped = false;
148 for(c = 0; ch = string[c]; c++)
152 if(escaped || ch != '\"')
155 escaped = !escaped && ch == '\\';
169 // String Escape Copy
170 static void strescpy(char * d, char * s)
223 static char * CopyUnescapedSystemPath(char * p)
225 char * d = new char[strlen(p) + 1];
227 #if defined(__WIN32__)
228 ChangeCh(d, '/', '\\');
233 static char * CopyUnescapedUnixPath(char * p)
235 char * d = new char[strlen(p) + 1];
237 #if defined(__WIN32__)
238 ChangeCh(d, '\\', '/');
243 static char * CopyUnescapedString(char * s)
245 char * d = new char[strlen(s) + 1];
250 // String Unescape Copy
252 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
253 // Seems very similar to ReadString in pass15.ec (which also misses numeric escape codes :) )
255 static void struscpy(char * d, char * s)
307 static char * StripBrackets(char * string)
309 int length = strlen(string);
310 if(length > 1 && *string == '[' && string[length - 1] == ']')
313 string[length - 1] = '\0';
320 static char * StripCurlies(char * string)
322 int length = strlen(string);
323 if(length > 1 && *string == '{' && string[length - 1] == '}')
326 string[length - 1] = '\0';
333 static int StringGetInt(char * string, int start)
336 int i, len = strlen(string);
338 for(i = start; i < len && i < start + 8; i++)
340 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')
341 strncat(number, &string[i], 1);
348 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
352 bool quoted = false, escaped = false;
353 char * start = string, ch;
355 for(; (ch = *string); string++)
362 if(escaped || ch != '\"')
363 escaped = !escaped && ch == '\\';
369 else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
371 else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
373 else if(ch == seperator && !level)
382 //tokens[count] = start;
383 //tokens[count++] = start;
390 static bool TokenizeListItem(char * string, DebugListItem item)
392 char * equal = strstr(string, "=");
406 static bool CheckCommandAvailable(const char * command)
408 bool available = false;
410 char * name = new char[MAX_FILENAME];
411 char * pathVar = new char[maxPathLen];
413 GetEnvironment("PATH", pathVar, maxPathLen);
414 count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
415 strcpy(name, command);
419 const char * extensions[] = { "exe", "com", "bat", null };
420 for(e=0; extensions[e]; e++)
422 ChangeExtension(name, extensions[e], name);
424 for(c=0; c<count; c++)
426 FileListing fl { paths[c] };
429 if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
447 // define GdbGetLineSize = 1638400;
448 define GdbGetLineSize = 5638400;
449 #if defined(__unix__)
450 char progFifoPath[MAX_LOCATION];
451 char progFifoDir[MAX_LOCATION];
454 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
457 none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause;
459 property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd); } };
461 enum DebuggerAction { none, internal, restart, stop, selectFrame }; //, bpValidation
464 unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit
465 //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope, locationReached,
466 //exited, exitedNormally, exitedSignalled;
470 none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad;
472 property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad); } };
473 property bool isUser { get { return (this == user || this == runToCursor); } };
475 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
476 enum DebuggerUserAction { none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepOut, runToCursor };
478 FileDialog debuggerFileDialog { type = selectDir };
480 static DualPipe vgTargetHandle;
481 static File vgLogFile;
482 static char vgLogPath[MAX_LOCATION];
483 static DualPipe gdbHandle;
484 static DebugEvaluationData eval { };
486 static int targetProcessId;
488 static bool gdbReady;
489 static bool breakpointError;
493 Semaphore serialSemaphore { };
499 bool sentBreakInsert;
500 bool ignoreBreakpoints;
501 bool userBreakOnInternalBreakpoint;
502 //bool runToCursorDebugStart;
511 int activeFrameLevel;
520 DebuggerUserAction userAction;
523 DebuggerAction breakType;
524 //DebuggerCommand lastCommand; // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
526 GdbDataStop stopItem;
527 GdbDataBreakpoint bpItem;
530 List<Breakpoint> sysBPs { };
531 Breakpoint bpRunToCursor;
535 CompilerConfig currentCompiler;
536 ProjectConfig prjConfig;
539 CodeEditor codeEditor;
541 ValgrindLogThread vgLogThread { debugger = this };
542 ValgrindTargetThread vgTargetThread { debugger = this };
543 GdbThread gdbThread { debugger = this };
546 delay = 0.0, userData = this;
550 bool monitor = false;
551 DebuggerEvent curEvent = event;
552 GdbDataStop stopItem = this.stopItem;
553 Breakpoint bpUser = null;
554 Breakpoint bpInternal = null;
562 this.stopItem = null;
566 DynamicString bpReport { };
568 for(bp : sysBPs; bp.inserted)
570 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
573 if(bpRunToCursor && bpRunToCursor.inserted)
575 Breakpoint bp = bpRunToCursor;
576 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
579 for(bp : ide.workspace.breakpoints; bp.inserted)
581 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
585 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
590 Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
592 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")"); delete s;
600 if(curEvent && curEvent != exit)
602 _dpl(0, "No stop item");
610 Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
619 GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
620 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
621 if(activeFrame.level == activeFrameLevel)
627 // GdbCommand(false, "-break-info %s", bpItem.number);
639 Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
640 if(bp && bp.inserted && bp.bp.addr)
642 if(bp.type.isInternal)
646 if(stopItem && stopItem.frame)
648 if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
649 bpUser = bpRunToCursor;
652 for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
654 if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
666 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
667 if(bpUser && strcmp(stopItem.frame.addr, bpUser.bp.addr))
671 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
672 if((bpUser && !ignoreBreakpoints) || (bpInternal && userBreakOnInternalBreakpoint))
674 hitThread = stopItem.threadid;
678 signalThread = stopItem.threadid;
683 ignoreBreakpoints = false;
685 case valgrindStartPause:
686 GdbExecContinue(true);
693 if(monitor || (bpUser && bpUser.type == runToCursor))
698 activeThread = stopItem.threadid;
699 GdbCommand(false, "-thread-list-ids");
700 if(activeFrameLevel > 0)
701 GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
703 WatchesCodeEditorLinkInit();
707 if(curEvent == signal)
711 ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
712 ide.outputView.debugBox.Logf(" %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
713 ide.outputView.Show();
714 ide.callStackView.Show();
717 else if(curEvent == breakEvent)
719 ide.threadsView.Show();
720 ide.callStackView.Show();
721 ide.callStackView.Activate();
724 if(monitor && curEvent.canBeMonitored)
726 InternalSelectFrame(activeFrameLevel);
727 GoToStackFrameLine(activeFrameLevel, true, false);
728 ide.ShowCodeEditor();
729 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
735 if(BreakpointHit(stopItem, bpInternal, bpUser))
737 if(bpUser && bpUser.type == runToCursor)
739 ignoreBreakpoints = false;
740 UnsetBreakpoint(bpUser);
741 delete bpRunToCursor;
745 GdbExecContinue(false);
753 if(userBreakOnInternalBreakpoint)
754 userBreakOnInternalBreakpoint = false;
759 #ifdef GDB_DEBUG_CONSOLE
760 char lastGdbOutput[GdbGetLineSize];
762 #if defined(__unix__)
763 ProgramThread progThread { };
767 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
768 void ChangeUserAction(char * file, int line, DebuggerUserAction value)
770 bool same = value == userAction;
771 __dpl2(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
775 #define _ChangeUserAction(value) userAction = value
779 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
780 void ChangeState(char * file, int line, DebuggerState value)
782 #define _ChangeState(value) ChangeState(value)
783 void ChangeState(DebuggerState value)
786 bool same = value == state;
788 __dpl2(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
791 if(!same && ide) ide.AdjustDebugMenus();
796 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
798 stackFrames.Free(Frame::Free);
808 waitingForPID = false;
813 sentBreakInsert = false;
814 ignoreBreakpoints = false;
815 userBreakOnInternalBreakpoint = false;
816 //runToCursorDebugStart = false;
819 activeFrameLevel = 0;
836 bpRunToCursor = null;
838 delete currentCompiler;
842 /*GdbThread gdbThread
848 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
849 ideProcessId = Process_GetCurrentProcessId();
851 sysBPs.Add(Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 });
852 #if defined(__WIN32__)
853 sysBPs.Add(Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 });
855 sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
856 sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
861 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
869 property bool isActive { get { return state == running || state == stopped; } }
870 property bool isPrepared { get { return state == loaded || state == running || state == stopped; } }
874 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
875 _ChangeUserAction(resume);
876 GdbExecContinue(true);
881 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
882 _ChangeUserAction(_break);
886 GdbDebugBreak(false);
892 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
893 _ChangeUserAction(stop);
900 GdbDebugBreak(false);
914 void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
916 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
917 _ChangeUserAction(restart);
918 if(StartSession(compiler, config, bitDepth, useValgrind, true, false, false/*, false*/) == loaded)
922 bool GoToCodeLine(char * location)
925 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
926 codloc = CodeLocation::ParseCodeLocation(location);
929 CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, normal, true, null, no, normal, false);
932 EditBox editBox = editor.editBox;
933 editBox.GoToLineNum(codloc.line - 1);
934 editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
941 bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
943 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
946 char filePath[MAX_LOCATION];
947 char sourceDir[MAX_LOCATION];
949 CodeEditor editor = null;
950 if(stackLevel == -1) // this (the two lines) is part of that fix that I would not put in for some time
952 for(frame = stackFrames.first; frame; frame = frame.next)
953 if(frame.level == stackLevel)
958 ide.callStackView.Show();
960 if(frame.absoluteFile)
961 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
962 if(!editor && frame.file)
963 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
964 if(!frame.absoluteFile && askForLocation && frame.file)
967 char title[MAX_LOCATION];
968 snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
969 title[sizeof(title)-1] = 0;
971 if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
973 AddSourceDir(sourceDir);
974 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
977 if(!editor && frame.absoluteFile)
978 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
980 ide.RepositionWindows(false);
982 if(editor && frame.line)
984 EditBox editBox = editor.editBox;
985 editBox.GoToLineNum(frame.line - 1);
986 editBox.GoToPosition(editBox.line, frame.line - 1, 0);
994 void SelectThread(int thread)
996 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
997 _ChangeUserAction(selectThread);
1000 if(thread != activeThread)
1002 activeFrameLevel = -1;
1003 ide.callStackView.Clear();
1004 GdbCommand(false, "-thread-select %d", thread);
1006 InternalSelectFrame(activeFrameLevel);
1007 GoToStackFrameLine(activeFrameLevel, true, false);
1008 WatchesCodeEditorLinkRelease();
1009 WatchesCodeEditorLinkInit();
1013 ide.callStackView.Show();
1017 void SelectFrame(int frame)
1019 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
1020 _ChangeUserAction(selectFrame);
1021 InternalSelectFrame(frame);
1024 void InternalSelectFrame(int frame)
1026 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
1027 if(state == stopped)
1029 if(frame != activeFrameLevel || !codeEditor || !codeEditor.visible)
1031 activeFrameLevel = frame; // there is no active frame number in the gdb reply
1032 GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
1033 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
1034 if(activeFrame.level == activeFrameLevel)
1037 WatchesCodeEditorLinkRelease();
1038 WatchesCodeEditorLinkInit();
1045 void HandleExit(char * reason, char * code)
1047 bool returnedExitCode = false;
1048 char verboseExitCode[128];
1050 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
1051 _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
1052 targetProcessId = 0;
1056 snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
1057 verboseExitCode[sizeof(verboseExitCode)-1] = 0;
1060 verboseExitCode[0] = '\0';
1064 // ClearBreakDisplay();
1068 for(wh : ide.workspace.watches)
1070 if(wh.type) FreeType(wh.type);
1073 ide.watchesView.UpdateWatch(wh);
1077 #if defined(__unix__)
1080 progThread.terminate = true;
1083 fifoFile.CloseInput();
1093 char program[MAX_LOCATION];
1094 GetSystemPathBuffer(program, targetFile);
1096 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1097 else if(!strcmp(reason, "exited-normally"))
1098 ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
1099 else if(!strcmp(reason, "exited"))
1100 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1101 else if(!strcmp(reason, "exited-signalled"))
1102 ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
1104 ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
1109 DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool userBreakOnInternalBreakpoint, bool ignoreBreakpoints/*, bool runToCursorDebugStart*/)
1111 DebuggerState result = none;
1112 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), userBreakOnInternalBreakpoint(", userBreakOnInternalBreakpoint, "), ignoreBreakpoints(", ignoreBreakpoints, ")"/*, runToCursorDebugStart(", runToCursorDebugStart, ")"*/);
1113 if(restart && state == running && targetProcessId)
1115 breakType = DebuggerAction::restart;
1116 GdbDebugBreak(false);
1120 if(restart && state == stopped)
1122 if(needReset && state == loaded)
1123 GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
1125 if(result == none || result == terminated)
1127 ide.outputView.ShowClearSelectTab(debug);
1128 ide.outputView.debugBox.Logf($"Starting debug mode\n");
1135 for(bp : ide.workspace.breakpoints)
1141 //this.runToCursorDebugStart = runToCursorDebugStart;
1143 if(GdbInit(compiler, config, bitDepth, useValgrind))
1148 this.ignoreBreakpoints = ignoreBreakpoints;
1149 this.userBreakOnInternalBreakpoint = userBreakOnInternalBreakpoint;
1150 if(result == loaded || result == stopped)
1151 GdbBreakpointsDelete(false, (userAction == stepOver || userAction == stepOut), ignoreBreakpoints);
1156 void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1158 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1159 _ChangeUserAction(start);
1160 if(StartSession(compiler, config, bitDepth, useValgrind, true, false, false/*, false*/) == loaded)
1164 void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1166 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1167 _ChangeUserAction(stepInto);
1168 switch(StartSession(compiler, config, bitDepth, useValgrind, false, true, false/*, false*/))
1170 case loaded: GdbExecRun(); break;
1171 case stopped: GdbExecStep(); break;
1175 void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1177 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1178 _ChangeUserAction(stepOver);
1179 switch(StartSession(compiler, config, bitDepth, useValgrind, false, true, ignoreBreakpoints/*, false*/))
1181 case loaded: GdbExecRun(); break;
1182 case stopped: GdbExecNext(); break;
1186 void StepOut(bool ignoreBreakpoints)
1188 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1189 _ChangeUserAction(stepOut);
1190 if(state == stopped)
1192 this.ignoreBreakpoints = ignoreBreakpoints;
1193 GdbBreakpointsDelete(true, true, ignoreBreakpoints);
1197 GdbExecContinue(true);
1201 void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel)
1203 char relativeFilePath[MAX_LOCATION];
1204 DebuggerState st = state;
1205 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1206 _ChangeUserAction(runToCursor);
1209 // ide.outputView.ShowClearSelectTab(debug);
1210 // ide.outputView.debugBox.Logf($"Starting debug mode\n");
1212 if(!ide.projectView.project.GetRelativePath(absoluteFilePath, relativeFilePath))
1213 strcpy(relativeFilePath, absoluteFilePath);
1215 if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1217 UnsetBreakpoint(bpRunToCursor);
1218 delete bpRunToCursor;
1221 bpRunToCursor = Breakpoint { };
1222 bpRunToCursor.absoluteFilePath = absoluteFilePath;
1223 bpRunToCursor.relativeFilePath = relativeFilePath;
1224 bpRunToCursor.line = lineNumber;
1225 bpRunToCursor.type = runToCursor;
1226 bpRunToCursor.enabled = true;
1227 bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1229 switch(StartSession(compiler, config, bitDepth, useValgrind, false, false, ignoreBreakpoints/*, true*/))
1235 GdbExecContinue(true);
1240 void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1242 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1243 if(activeFrameLevel == -1)
1251 *error = signalOn && activeThread == signalThread;
1252 *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1253 *lineTopFrame = activeFrameLevel ? 1 : 0;
1257 int GetMarginIconsLineNumbers(char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1259 char winFilePath[MAX_LOCATION];
1260 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1262 Iterator<Breakpoint> it { ide.workspace.breakpoints };
1263 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1264 while(it.Next() && count < max)
1266 Breakpoint bp = it.data;
1269 if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1271 lines[count] = bp.line;
1272 enabled[count] = bp.enabled;
1277 if(activeFrameLevel == -1)
1285 *error = signalOn && activeThread == signalThread;
1286 if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1287 *lineCursor = activeFrame.line;
1290 if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1292 else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1293 *lineTopFrame = stopItem.frame.line;
1297 if(*lineTopFrame == *lineCursor && *lineTopFrame)
1303 void ChangeWatch(DataRow row, char * expression)
1305 Watch wh = (Watch)row.tag;
1306 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1309 delete wh.expression;
1311 wh.expression = CopyString(expression);
1314 Iterator<Watch> it { ide.workspace.watches };
1316 ide.workspace.watches.Delete(it.pointer);
1322 row.tag = (int64)wh;
1323 ide.workspace.watches.Add(wh);
1325 wh.expression = CopyString(expression);
1327 ide.workspace.Save();
1328 //if(expression && state == stopped)
1333 void MoveIcons(char * fileName, int lineNumber, int move, bool start)
1335 char winFilePath[MAX_LOCATION];
1336 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1339 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1340 for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1342 Breakpoint bp = (Breakpoint)bpLink.data;
1345 if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1347 if(bp.line > lineNumber || (bp.line == lineNumber && start))
1349 if(move < 0 && (bp.line < lineNumber - move))
1350 ide.workspace.RemoveBreakpoint(bp);
1354 ide.breakpointsView.UpdateBreakpoint(bp.row);
1355 ide.workspace.Save();
1361 // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1364 bool SourceDirDialog(char * title, char * startDir, char * test, char * sourceDir)
1368 String srcDir = null;
1370 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1371 debuggerFileDialog.text = title;
1372 debuggerFileDialog.currentDirectory = startDir;
1373 debuggerFileDialog.master = ide;
1375 while(debuggerFileDialog.Modal())
1377 strcpy(sourceDir, debuggerFileDialog.filePath);
1378 if(!fstrcmp(ide.workspace.projectDir, sourceDir) &&
1379 MessageBox { type = yesNo, master = ide,
1380 contents = $"This is the project directory.\nWould you like to try again?",
1381 text = $"Invalid Source Directory" }.Modal() == no)
1385 for(dir : ide.workspace.sourceDirs)
1387 if(!fstrcmp(dir, sourceDir))
1395 MessageBox { type = yesNo, master = ide,
1396 contents = $"This source directory is already specified.\nWould you like to try again?",
1397 text = $"Invalid Source Directory" }.Modal() == no)
1403 char file[MAX_LOCATION];
1404 strcpy(file, sourceDir);
1405 PathCat(file, test);
1406 result = FileExists(file);
1408 MessageBox { type = yesNo, master = ide,
1409 contents = $"Unable to locate source file.\nWould you like to try again?",
1410 text = $"Invalid Source Directory" }.Modal() == no)
1424 void AddSourceDir(char * sourceDir)
1426 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1427 ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1428 ide.workspace.Save();
1432 DebuggerState oldState = state;
1437 GdbDebugBreak(true);
1440 GdbCommand(false, "-environment-directory \"%s\"", sourceDir);
1443 if(oldState == running)
1444 GdbExecContinue(false);
1448 void ToggleBreakpoint(char * fileName, int lineNumber, Project prj)
1450 char winFilePath[MAX_LOCATION];
1451 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1452 char absolutePath[MAX_LOCATION];
1453 char relativePath[MAX_LOCATION];
1454 char sourceDir[MAX_LOCATION];
1455 Breakpoint bp = null;
1457 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1458 strcpy(absolutePath, absoluteFilePath);
1459 for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1468 ide.workspace.RemoveBreakpoint(bp);
1476 // FIXED: This is how it should have been... Source locations are only for files not in project
1477 // if(IsPathInsideOf(absolutePath, ide.workspace.projectDir))
1478 // MakePathRelative(absolutePath, ide.workspace.projectDir, relativePath);
1479 bool result = false;
1481 result = prj.GetRelativePath(absolutePath, relativePath);
1483 result = ide.projectView.project.GetRelativePath(absolutePath, relativePath);
1484 //if(ide.projectView.project.GetRelativePath(absolutePath, relativePath));
1488 char title[MAX_LOCATION];
1489 char directory[MAX_LOCATION];
1490 StripLastDirectory(absolutePath, directory);
1491 snprintf(title, sizeof(title), $"Provide source files location directory for %s", absolutePath);
1492 title[sizeof(title)-1] = 0;
1495 String srcDir = null;
1496 for(dir : ide.workspace.sourceDirs)
1498 if(IsPathInsideOf(absolutePath, dir))
1500 MakePathRelative(absoluteFilePath, dir, relativePath);
1508 if(SourceDirDialog(title, directory, null, sourceDir))
1510 if(IsPathInsideOf(absolutePath, sourceDir))
1512 AddSourceDir(sourceDir);
1513 MakePathRelative(absoluteFilePath, sourceDir, relativePath);
1516 else if(MessageBox { type = yesNo, master = ide,
1517 contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1518 text = $"Invalid Source Directory" }.Modal() == no)
1521 else if(MessageBox { type = yesNo, master = ide,
1522 contents = $"You must provide a source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1523 text = $"No Source Directory Provided" }.Modal() == no)
1527 ide.workspace.bpCount++;
1528 bp = { line = lineNumber, type = user, enabled = true, level = -1 };
1529 ide.workspace.breakpoints.Add(bp);
1530 bp.absoluteFilePath = absolutePath;
1531 bp.relativeFilePath = relativePath;
1532 ide.breakpointsView.AddBreakpoint(bp);
1537 DebuggerState oldState = state;
1542 GdbDebugBreak(true);
1545 SetBreakpoint(bp, false);
1548 if(oldState == running)
1549 GdbExecContinue(false);
1552 ide.workspace.Save();
1555 void UpdateRemovedBreakpoint(Breakpoint bp)
1557 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1558 if(targeted && bp.inserted)
1560 DebuggerState oldState = state;
1565 GdbDebugBreak(true);
1568 UnsetBreakpoint(bp);
1571 if(oldState == running)
1572 GdbExecContinue(false);
1578 void ParseFrame(Frame frame, char * string)
1581 Array<char *> frameTokens { minAllocSize = 50 };
1582 Array<char *> argsTokens { minAllocSize = 50 };
1583 Array<char *> argumentTokens { minAllocSize = 50 };
1584 DebugListItem item { };
1587 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1588 TokenizeList(string, ',', frameTokens);
1589 for(i = 0; i < frameTokens.count; i++)
1591 if(TokenizeListItem(frameTokens[i], item))
1593 StripQuotes(item.value, item.value);
1594 if(!strcmp(item.name, "level"))
1595 frame.level = atoi(item.value);
1596 else if(!strcmp(item.name, "addr"))
1597 frame.addr = item.value;
1598 else if(!strcmp(item.name, "func"))
1599 frame.func = item.value;
1600 else if(!strcmp(item.name, "args"))
1602 if(!strcmp(item.value, "[]"))
1603 frame.argsCount = 0;
1606 item.value = StripBrackets(item.value);
1607 TokenizeList(item.value, ',', argsTokens);
1608 for(j = 0; j < argsTokens.count; j++)
1610 argsTokens[j] = StripCurlies(argsTokens[j]);
1611 TokenizeList(argsTokens[j], ',', argumentTokens);
1612 for(k = 0; k < argumentTokens.count; k++)
1615 frame.args.Add(arg);
1616 if(TokenizeListItem(argumentTokens[k], item))
1618 if(!strcmp(item.name, "name"))
1620 StripQuotes(item.value, item.value);
1621 arg.name = item.value;
1623 else if(!strcmp(item.name, "value"))
1625 StripQuotes(item.value, item.value);
1626 arg.val = item.value;
1629 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1632 _dpl(0, "Bad frame args item");
1634 argumentTokens.RemoveAll();
1636 frame.argsCount = argsTokens.count;
1637 argsTokens.RemoveAll();
1640 else if(!strcmp(item.name, "from"))
1641 frame.from = item.value;
1642 else if(!strcmp(item.name, "file"))
1643 frame.file = item.value;
1644 else if(!strcmp(item.name, "line"))
1645 frame.line = atoi(item.value);
1646 else if(!strcmp(item.name, "fullname"))
1647 frame.absoluteFile = item.value;
1649 // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1650 String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1651 if(strcmp(frame.file, path))
1654 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
1659 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1662 _dpl(0, "Bad frame");
1667 delete argumentTokens;
1671 Breakpoint GetBreakpointById(int id, bool * isInternal)
1673 Breakpoint bp = null;
1674 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1676 *isInternal = false;
1679 for(i : sysBPs; i.bp && i.bp.id == id)
1686 if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1690 for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1700 GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1703 GdbDataBreakpoint bp { };
1704 DebugListItem item { };
1705 Array<char *> bpTokens { minAllocSize = 16 };
1706 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1707 string = StripCurlies(string);
1708 TokenizeList(string, ',', bpTokens);
1709 for(i = 0; i < bpTokens.count; i++)
1711 if(TokenizeListItem(bpTokens[i], item))
1713 StripQuotes(item.value, item.value);
1714 if(!strcmp(item.name, "number"))
1716 if(!strchr(item.value, '.'))
1717 bp.id = atoi(item.value);
1718 bp.number = item.value;
1720 else if(!strcmp(item.name, "type"))
1721 bp.type = item.value;
1722 else if(!strcmp(item.name, "disp"))
1723 bp.disp = item.value;
1724 else if(!strcmp(item.name, "enabled"))
1725 bp.enabled = (!strcmpi(item.value, "y"));
1726 else if(!strcmp(item.name, "addr"))
1728 if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1731 Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1732 while(outTokens.count > ++c)
1734 GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1735 bpArray.Add(multBp);
1739 bp.addr = item.value;
1741 else if(!strcmp(item.name, "func"))
1742 bp.func = item.value;
1743 else if(!strcmp(item.name, "file"))
1744 bp.file = item.value;
1745 else if(!strcmp(item.name, "fullname"))
1746 bp.fullname = item.value;
1747 else if(!strcmp(item.name, "line"))
1748 bp.line = atoi(item.value);
1749 else if(!strcmp(item.name, "at"))
1751 else if(!strcmp(item.name, "times"))
1752 bp.times = atoi(item.value);
1753 else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1754 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1756 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1762 void ShowDebuggerViews()
1764 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1765 ide.outputView.Show();
1766 ide.outputView.SelectTab(debug);
1767 ide.threadsView.Show();
1768 ide.callStackView.Show();
1769 ide.watchesView.Show();
1773 void HideDebuggerViews()
1775 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1776 ide.RepositionWindows(true);
1779 void ::GdbCommand(bool focus, char * format, ...)
1783 // TODO: Improve this limit
1784 static char string[MAX_F_STRING*4];
1786 va_start(args, format);
1787 vsnprintf(string, sizeof(string), format, args);
1788 string[sizeof(string)-1] = 0;
1792 ide.debugger.serialSemaphore.TryWait();
1794 #ifdef GDB_DEBUG_CONSOLE
1795 _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1797 #ifdef GDB_DEBUG_OUTPUT
1798 ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1800 #ifdef GDB_DEBUG_GUI
1802 ide.gdbDialog.AddCommand(string);
1805 strcat(string,"\n");
1806 gdbHandle.Puts(string);
1809 Process_ShowWindows(targetProcessId);
1812 ide.debugger.serialSemaphore.Wait();
1817 bool ValidateBreakpoint(Breakpoint bp)
1819 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1820 if(modules && bp.line && bp.bp)
1822 if(bp.bp.line != bp.line)
1828 ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1830 //UnsetBreakpoint(bp);
1831 //bp.enabled = false;
1837 ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1838 bp.line = bp.bp.line;
1845 void GdbBreakpointsInsert()
1847 //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GdbBreakpointsInsert()");
1850 if(userAction != stepOut && (userAction != stepOver || state == loaded))
1852 DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1853 for(bp : sysBPs; !bp.inserted)
1855 bool insert = false;
1856 if(bp.type == internalModulesLoaded)
1858 char path[MAX_LOCATION];
1859 char name[MAX_LOCATION];
1860 char fixedModuleName[MAX_FILENAME];
1863 bool moduleLoadBlock = false;
1865 ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1866 snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1867 name[sizeof(name)-1] = 0;
1868 strcpy(path, ide.workspace.projectDir);
1869 PathCatSlash(path, objDir.dir);
1870 PathCatSlash(path, name);
1871 f = FileOpen(path, read);
1874 for(lineNumber = 1; !f.Eof(); lineNumber++)
1876 if(f.GetLine(line, sizeof(line) - 1))
1878 bool moduleLoadLine;
1879 TrimLSpaces(line, line);
1880 moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1881 if(!moduleLoadBlock && moduleLoadLine)
1882 moduleLoadBlock = true;
1883 else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1889 char relative[MAX_LOCATION];
1890 bp.absoluteFilePath = path;
1891 MakePathRelative(path, ide.workspace.projectDir, relative);
1892 bp.relativeFilePath = relative;
1893 bp.line = lineNumber;
1899 else if(bp.type == internalModuleLoad)
1903 for(prj : ide.workspace.projects)
1905 if(!strcmp(prj.moduleName, "ecere"))
1907 ProjectNode node = prj.topNode.Find("instance.c", false);
1910 char path[MAX_LOCATION];
1911 char relative[MAX_LOCATION];
1912 node.GetFullFilePath(path);
1913 bp.absoluteFilePath = path;
1914 MakePathRelative(path, prj.topNode.path, relative);
1915 bp.relativeFilePath = relative;
1926 SetBreakpoint(bp, false);
1931 if(bpRunToCursor && !bpRunToCursor.inserted)
1932 SetBreakpoint(bpRunToCursor, false);
1934 if(!ignoreBreakpoints)
1936 for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1940 if(!SetBreakpoint(bp, false))
1941 SetBreakpoint(bp, true);
1949 bp.bp = GdbDataBreakpoint { };
1956 void UnsetBreakpoint(Breakpoint bp)
1958 char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1959 if(symbols && bp.inserted)
1961 GdbCommand(false, "-break-delete %s", bp.bp.number);
1962 bp.inserted = false;
1967 bool SetBreakpoint(Breakpoint bp, bool removePath)
1969 char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
1970 breakpointError = false;
1973 char * location = bp.CopyLocationString(removePath);
1974 sentBreakInsert = true;
1975 GdbCommand(false, "-break-insert %s", location);
1977 if(!breakpointError)
1979 if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
1982 GdbDataBreakpoint first = null;
1983 for(n : bpItem.multipleBPs)
1985 if(!fstrcmp(n.fullname, bp.absoluteFilePath))
1995 GdbCommand(false, "-break-disable %s", n.number);
1999 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2004 bpItem.addr = first.addr;
2005 bpItem.func = first.func;
2006 bpItem.file = first.file;
2007 bpItem.fullname = first.fullname;
2008 bpItem.line = first.line;
2009 //bpItem.thread-groups = first.thread-groups;
2010 bpItem.multipleBPs.Free();
2011 delete bpItem.multipleBPs;
2014 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2016 _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2020 bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2022 ValidateBreakpoint(bp);
2023 /*if(bp == bpRunToCursor)
2024 runToCursorDebugStart = false;*/
2027 return !breakpointError;
2030 void GdbBreakpointsDelete(bool deleteRunToCursor, bool deleteInternalBreakpoints, bool deleteUserBreakpoints)
2032 _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GdbBreakpointsDelete(deleteRunToCursor(", deleteRunToCursor, "))");
2035 if(deleteInternalBreakpoints)
2037 for(bp : sysBPs; bp.inserted)
2038 UnsetBreakpoint(bp);
2040 if(deleteUserBreakpoints)
2042 for(bp : ide.workspace.breakpoints; bp.inserted)
2043 UnsetBreakpoint(bp);
2045 if(deleteRunToCursor && bpRunToCursor && bpRunToCursor.inserted)
2046 UnsetBreakpoint(bpRunToCursor);
2052 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2054 stackFrames.Free(Frame::Free);
2055 GdbCommand(false, "-stack-info-depth");
2057 GdbCommand(false, "-stack-info-depth 192");
2058 if(frameCount && frameCount <= 192)
2059 GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2062 GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2063 GdbCommand(false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2065 GdbCommand(false, "");
2070 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2073 char escaped[MAX_LOCATION];
2074 strescpy(escaped, targetFile);
2075 GdbCommand(false, "file \"%s\"", escaped); //GDB/MI Missing Implementation -symbol-file, -target-attach
2082 const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2083 //GdbCommand(false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2084 printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2085 GdbCommand(false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2088 /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2089 GdbCommand(false, "-environment-directory \"%s\"", prj.topNode.path);*/
2091 for(dir : ide.workspace.sourceDirs; dir && dir[0])
2093 bool interference = false;
2094 for(prj : ide.workspace.projects)
2096 if(!fstrcmp(prj.topNode.path, dir))
2098 interference = true;
2102 if(!interference && dir[0])
2103 GdbCommand(false, "-environment-directory \"%s\"", dir);
2111 /*void GdbTargetRelease()
2115 GdbBreakpointsDelete(true, true, true);
2116 GdbCommand(false, "file"); //GDB/MI Missing Implementation -target-detach
2122 void GdbDebugBreak(bool internal)
2124 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2128 breakType = DebuggerAction::internal;
2130 if(ide) ide.Update(null);
2132 if(Process_Break(targetProcessId)) //GdbCommand(false, "-exec-interrupt");
2133 serialSemaphore.Wait();
2136 _ChangeState(loaded);
2137 targetProcessId = 0;
2142 ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2147 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2150 ShowDebuggerViews();
2152 GdbCommand(true, "-exec-continue");
2154 GdbCommand(true, "-exec-run");
2157 void GdbExecContinue(bool focus)
2159 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2161 GdbCommand(focus, "-exec-continue");
2166 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2168 GdbCommand(true, "-exec-next");
2173 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2175 GdbCommand(true, "-exec-step");
2178 void GdbExecFinish()
2180 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2182 GdbCommand(true, "-exec-finish");
2185 void GdbExecCommon()
2187 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2188 GdbBreakpointsInsert();
2191 #ifdef GDB_DEBUG_GUI
2192 void SendGDBCommand(char * command)
2194 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2195 DebuggerState oldState = state;
2200 GdbDebugBreak(true);
2203 GdbCommand(false, command);
2206 if(oldState == running)
2207 GdbExecContinue(false);
2211 void ClearBreakDisplay()
2213 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2215 activeFrameLevel = -1;
2225 stackFrames.Free(Frame::Free);
2226 WatchesCodeEditorLinkRelease();
2227 ide.callStackView.Clear();
2228 ide.threadsView.Clear();
2234 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2236 GdbCommand(false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2240 bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2243 char oldDirectory[MAX_LOCATION];
2244 char tempPath[MAX_LOCATION];
2245 char command[MAX_F_STRING*4];
2246 Project project = ide.project;
2247 DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2248 PathBackup pathBackup { };
2250 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2251 if(currentCompiler != compiler)
2253 delete currentCompiler;
2254 currentCompiler = compiler;
2255 incref currentCompiler;
2258 this.bitDepth = bitDepth;
2259 usingValgrind = useValgrind;
2261 _ChangeState(loaded);
2263 sentBreakInsert = false;
2264 breakpointError = false;
2265 ignoreBreakpoints = false;
2271 ide.outputView.ShowClearSelectTab(debug);
2272 ide.outputView.debugBox.Logf($"Starting debug mode\n");
2274 #ifdef GDB_DEBUG_OUTPUT
2275 ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2278 strcpy(tempPath, ide.workspace.projectDir);
2279 PathCatSlash(tempPath, targetDirExp.dir);
2281 targetDir = CopyString(tempPath);
2282 project.CatTargetFileName(tempPath, compiler, config);
2284 targetFile = CopyString(tempPath);
2286 GetWorkingDir(oldDirectory, MAX_LOCATION);
2287 if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2289 char temp[MAX_LOCATION];
2290 strcpy(temp, ide.workspace.projectDir);
2291 PathCatSlash(temp, ide.workspace.debugDir);
2292 ChangeWorkingDir(temp);
2295 ChangeWorkingDir(ide.workspace.projectDir);
2297 ide.SetPath(true, compiler, config, bitDepth);
2299 // TODO: This pollutes the environment, but at least it works
2300 // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2301 // What is the proper solution for this? DualPipeOpenEnv?
2302 // gdb set environment commands don't seem to take effect
2303 for(e : ide.workspace.environmentVars)
2305 SetEnvironment(e.name, e.string);
2310 char * clArgs = ide.workspace.commandLineArgs;
2311 const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2312 ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2313 int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2314 bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2315 vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2319 vgLogThread.Create();
2323 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2326 if(result && !CheckCommandAvailable(valgrindCommand))
2328 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2333 char * vgRedzoneSizeFlag = vgRedzoneSize == -1 ? "" : PrintString(" --redzone-size=", vgRedzoneSize);
2334 sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2335 valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSizeFlag, vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2336 if(vgRedzoneSize != -1)
2337 delete vgRedzoneSizeFlag;
2338 vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2341 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2347 incref vgTargetHandle;
2348 vgTargetThread.Create();
2350 targetProcessId = vgTargetHandle.GetProcessID();
2351 waitingForPID = false;
2352 if(!targetProcessId)
2354 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2361 serialSemaphore.Wait();
2369 (compiler.targetPlatform == win32 && bitDepth == 64) ? "x86_64-w64-mingw32-gdb" :
2370 (compiler.targetPlatform == win32 && bitDepth == 32) ? "i686-w64-mingw32-gdb" :
2372 if(!CheckCommandAvailable(command))
2374 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2379 strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2381 gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2384 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2394 gdbProcessId = gdbHandle.GetProcessID();
2397 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2404 serialSemaphore.Wait();
2407 GdbCommand(false, "-gdb-set verbose off");
2408 //GdbCommand(false, "-gdb-set exec-done-display on");
2409 GdbCommand(false, "-gdb-set step-mode off");
2410 GdbCommand(false, "-gdb-set unwindonsignal on");
2411 //GdbCommand(false, "-gdb-set shell on");
2412 GdbCommand(false, "set print elements 992");
2413 GdbCommand(false, "-gdb-set backtrace limit 100000");
2417 //_ChangeState(terminated);
2423 #if defined(__unix__)
2425 CreateTemporaryDir(progFifoDir, "ecereide");
2426 strcpy(progFifoPath, progFifoDir);
2427 PathCat(progFifoPath, "ideprogfifo");
2428 if(!mkfifo(progFifoPath, 0600))
2430 //fileCreated = true;
2435 ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2442 progThread.terminate = false;
2443 progThread.Create();
2447 #if defined(__WIN32__)
2448 GdbCommand(false, "-gdb-set new-console on");
2451 #if defined(__unix__)
2453 GdbCommand(false, "-inferior-tty-set %s", progFifoPath);
2457 GdbCommand(false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2459 for(e : ide.workspace.environmentVars)
2461 GdbCommand(false, "set environment %s=%s", e.name, e.string);
2466 ChangeWorkingDir(oldDirectory);
2472 delete targetDirExp;
2478 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2479 if(gdbHandle && gdbProcessId)
2481 GdbCommand(false, "-gdb-exit");
2496 _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2502 for(bp : ide.workspace.breakpoints)
2504 bp.inserted = false;
2510 bp.inserted = false;
2515 bpRunToCursor.inserted = false;
2516 delete bpRunToCursor.bp;
2519 ide.outputView.debugBox.Logf($"Debugging stopped\n");
2520 ClearBreakDisplay();
2523 #if defined(__unix__)
2524 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2526 progThread.terminate = true;
2529 fifoFile.CloseInput();
2535 DeleteFile(progFifoPath);
2536 progFifoPath[0] = '\0';
2542 void WatchesCodeEditorLinkInit()
2544 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesCodeEditorLinkInit()");
2546 char tempPath[MAX_LOCATION];
2547 char path[MAX_LOCATION];
2549 //void MakeFilePathProjectRelative(char * path, char * relativePath)
2550 if(!ide.projectView.project.GetRelativePath(activeFrame.file, tempPath))
2551 strcpy(tempPath, activeFrame.file);
2553 strcpy(path, ide.workspace.projectDir);
2554 PathCat(path, tempPath);
2555 codeEditor = (CodeEditor)ide.OpenFile(path, Normal, false, null, no, normal, false);
2558 for(srcDir : ide.workspace.sourceDirs)
2560 strcpy(path, srcDir);
2561 PathCat(path, tempPath);
2562 codeEditor = (CodeEditor)ide.OpenFile(path, Normal, false, null, no, normal, false);
2563 if(codeEditor) break;
2568 /*if(activeFrame && !activeFrame.absoluteFile && activeFrame.file)
2569 activeFrame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(activeFrame.file);*/
2570 if(!activeFrame || !activeFrame.absoluteFile)
2573 codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, normal, false, null, no, normal, false);
2576 codeEditor.inUseDebug = true;
2579 //watchesInit = true;
2582 void WatchesCodeEditorLinkRelease()
2584 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesCodeEditorLinkRelease()");
2589 codeEditor.inUseDebug = false;
2590 if(!codeEditor.visible)
2591 codeEditor.Destroy(0);
2597 bool ResolveWatch(Watch wh)
2599 bool result = false;
2601 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2613 char watchmsg[MAX_F_STRING];
2614 if(state == stopped && !codeEditor)
2615 wh.value = CopyString($"No source file found for selected frame");
2616 //if(codeEditor && state == stopped || state != stopped)
2619 Module backupPrivateModule;
2620 Context backupContext;
2621 Class backupThisClass;
2625 backupPrivateModule = GetPrivateModule();
2626 backupContext = GetCurrentContext();
2627 backupThisClass = GetThisClass();
2630 SetPrivateModule(codeEditor.privateModule);
2631 SetCurrentContext(codeEditor.globalContext);
2632 SetTopContext(codeEditor.globalContext);
2633 SetGlobalContext(codeEditor.globalContext);
2634 SetGlobalData(&codeEditor.globalData);
2637 exp = ParseExpressionString(wh.expression);
2639 if(exp && !parseError)
2641 char expString[4096];
2643 PrintExpression(exp, expString);
2645 if(GetPrivateModule())
2648 DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2649 ProcessExpressionType(exp);
2651 wh.type = exp.expType;
2654 DebugComputeExpression(exp);
2655 if(ExpressionIsError(exp))
2657 GDBFallBack(exp, expString);
2660 /*if(exp.hasAddress)
2662 char temp[MAX_F_STRING];
2663 sprintf(temp, "0x%x", exp.address);
2664 wh.address = CopyString(temp);
2665 // wh.address = CopyStringf("0x%x", exp.address);
2670 Type dataType = exp.expType;
2673 char temp[MAX_F_STRING];
2674 switch(dataType.kind)
2677 sprintf(temp, "%i", exp.val.c);
2680 sprintf(temp, "%i", exp.val.s);
2685 sprintf(temp, "%i", exp.val.i);
2688 sprintf(temp, "%i", exp.val.i64);
2691 sprintf(temp, "%i", exp.val.p);
2696 long v = (long)exp.val.f;
2697 sprintf(temp, "%i", v);
2702 long v = (long)exp.val.d;
2703 sprintf(temp, "%i", v);
2708 wh.intVal = CopyString(temp);
2709 switch(dataType.kind)
2712 sprintf(temp, "0x%x", exp.val.c);
2715 sprintf(temp, "0x%x", exp.val.s);
2719 sprintf(temp, "0x%x", exp.val.i);
2722 sprintf(temp, "0x%x", exp.val.i64);
2725 sprintf(temp, "0x%x", exp.val.i64);
2728 sprintf(temp, "0x%x", exp.val.p);
2733 long v = (long)exp.val.f;
2734 sprintf(temp, "0x%x", v);
2739 long v = (long)exp.val.d;
2740 sprintf(temp, "0x%x", v);
2745 wh.hexVal = CopyString(temp);
2746 switch(dataType.kind)
2749 sprintf(temp, "0o%o", exp.val.c);
2752 sprintf(temp, "0o%o", exp.val.s);
2756 sprintf(temp, "0o%o", exp.val.i);
2759 sprintf(temp, "0o%o", exp.val.i64);
2762 sprintf(temp, "0o%o", exp.val.i64);
2765 sprintf(temp, "0o%o", exp.val.p);
2770 long v = (long)exp.val.f;
2771 sprintf(temp, "0o%o", v);
2776 long v = (long)exp.val.d;
2777 sprintf(temp, "0o%o", v);
2782 wh.octVal = CopyString(temp);
2785 // WHATS THIS HERE ?
2786 if(exp.type == constantExp && exp.constant)
2787 wh.constant = CopyString(exp.constant);
2793 case symbolErrorExp:
2794 snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2796 case structMemberSymbolErrorExp:
2797 // todo get info as in next case (ExpClassMemberSymbolError)
2798 snprintf(watchmsg, sizeof(watchmsg), $"Error: Struct member not found for \"%s\"", wh.expression);
2800 case classMemberSymbolErrorExp:
2803 Expression memberExp = exp.member.exp;
2804 Identifier memberID = exp.member.member;
2805 Type type = memberExp.expType;
2808 _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2811 char string[256] = "";
2813 PrintTypeNoConst(type, string, false, true);
2814 classSym = FindClass(string);
2815 _class = classSym ? classSym.registered : null;
2818 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2820 snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in unregistered class? (Should never get this message)", memberID ? memberID.string : "");
2823 snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in no type? (Should never get this message)", memberID ? memberID.string : "");
2826 case memoryErrorExp:
2827 // Need to ensure when set to memoryErrorExp, constant is set
2828 snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
2830 case dereferenceErrorExp:
2831 snprintf(watchmsg, sizeof(watchmsg), $"Dereference failure for \"%s\"", wh.expression);
2833 case unknownErrorExp:
2834 snprintf(watchmsg, sizeof(watchmsg), $"Unknown error for \"%s\"", wh.expression);
2836 case noDebuggerErrorExp:
2837 snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
2839 case debugStateErrorExp:
2840 snprintf(watchmsg, sizeof(watchmsg), $"Incorrect debugger state for symbol evaluation in \"%s\"", wh.expression);
2843 snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
2847 // Temporary Code for displaying Strings
2848 if((exp.expType && ((exp.expType.kind == pointerType ||
2849 exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) ||
2850 (wh.type && wh.type.kind == classType && wh.type._class &&
2851 wh.type._class.registered && wh.type._class.registered.type == normalClass &&
2852 !strcmp(wh.type._class.registered.name, "String")))
2855 if(exp.expType.kind != arrayType || exp.hasAddress)
2861 //char temp[MAX_F_STRING * 32];
2863 ExpressionType evalError = dummyExp;
2864 /*if(exp.expType.kind == arrayType)
2865 sprintf(temp, "(char*)0x%x", exp.address);
2867 sprintf(temp, "(char*)%s", exp.constant);*/
2869 //evaluation = Debugger::EvaluateExpression(temp, &evalError);
2870 // address = strtoul(exp.constant, null, 0);
2871 address = _strtoui64(exp.constant, null, 0);
2872 //_dpl(0, "0x", address);
2873 // snprintf(value, sizeof(value), "0x%08x ", address);
2875 if(address > 0xFFFFFFFFLL)
2876 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
2878 snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
2879 value[sizeof(value)-1] = 0;
2882 strcat(value, $"Null string");
2886 len = strlen(value);
2888 while(!string && size > 2)
2890 string = GdbReadMemory(address, size);
2893 if(string && string[0])
2896 if(UTF8Validate(string))
2901 for(c = 0; (ch = string[c]) && c<4096; c++)
2904 value[len++] = '\0';
2909 ISO8859_1toUTF8(string, value + len, 4096 - len - 30);
2910 strcat(value, ") (ISO8859-1)");
2917 strcat(value, $"Empty string");
2921 strcat(value, $"Couldn't read memory");
2923 wh.value = CopyString(value);
2926 else if(wh.type && wh.type.kind == classType && wh.type._class &&
2927 wh.type._class.registered && wh.type._class.registered.type == enumClass)
2929 uint64 value = strtoul(exp.constant, null, 0);
2930 Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
2931 EnumClassData enumeration = (EnumClassData)enumClass.data;
2933 for(item = enumeration.values.first; item; item = item.next)
2934 if((int)item.data == value)
2937 wh.value = CopyString(item.name);
2939 wh.value = CopyString($"Invalid Enum Value");
2942 else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class &&
2943 wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
2950 if(exp.constant[0] == '\'')
2952 if((int)((byte *)exp.constant)[1] > 127)
2955 value = UTF8GetChar(exp.constant + 1, &nb);
2956 if(nb < 2) value = exp.constant[1];
2957 signedValue = value;
2961 signedValue = exp.constant[1];
2963 // Precomp Syntax error with boot strap here:
2964 byte b = (byte)(char)signedValue;
2965 value = (unichar) b;
2971 if(wh.type.kind == charType && wh.type.isSigned)
2973 signedValue = (int)(char)strtol(exp.constant, null, 0);
2975 // Precomp Syntax error with boot strap here:
2976 byte b = (byte)(char)signedValue;
2977 value = (unichar) b;
2982 value = (uint)strtoul(exp.constant, null, 0);
2983 signedValue = (int)value;
2987 UTF32toUTF8Len(&value, 1, charString, 5);
2989 snprintf(string, sizeof(string), "\'\\0' (0)");
2990 else if(value == '\t')
2991 snprintf(string, sizeof(string), "\'\\t' (%d)", value);
2992 else if(value == '\n')
2993 snprintf(string, sizeof(string), "\'\\n' (%d)", value);
2994 else if(value == '\r')
2995 snprintf(string, sizeof(string), "\'\\r' (%d)", value);
2996 else if(wh.type.kind == charType && wh.type.isSigned)
2997 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
2998 else if(value > 256 || wh.type.kind != charType)
3000 if(value > 0x10FFFF || !GetCharCategory(value))
3001 snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3003 snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3006 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3007 string[sizeof(string)-1] = 0;
3009 wh.value = CopyString(string);
3014 wh.value = CopyString(exp.constant);
3021 wh.value = PrintHexUInt64(exp.address);
3026 char tempString[256];
3027 if(exp.member.memberType == propertyMember)
3028 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation support for \"%s\"", wh.expression);
3030 snprintf(watchmsg, sizeof(watchmsg), $"Evaluation failed for \"%s\" of type \"%s\"", wh.expression,
3031 exp.type.OnGetString(tempString, null, null));
3037 snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3038 if(exp) FreeExpression(exp);
3041 SetPrivateModule(backupPrivateModule);
3042 SetCurrentContext(backupContext);
3043 SetTopContext(backupContext);
3044 SetGlobalContext(backupContext);
3045 SetThisClass(backupThisClass);
3048 // wh.value = CopyString("No source file found for selected frame");
3050 watchmsg[sizeof(watchmsg)-1] = 0;
3052 wh.value = CopyString(watchmsg);
3054 ide.watchesView.UpdateWatch(wh);
3058 void EvaluateWatches()
3060 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3061 for(wh : ide.workspace.watches)
3065 char * ::GdbEvaluateExpression(char * expression)
3067 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3070 GdbCommand(false, "-data-evaluate-expression \"%s\"", expression);
3072 ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3076 // to be removed... use GdbReadMemory that returns a byte array instead
3077 char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3079 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3084 _dpl(0, "GdbReadMemoryString called with size = 0!");
3086 // GdbCommand(false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3087 if(GetRuntimePlatform() == win32)
3088 GdbCommand(false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3090 GdbCommand(false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3092 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3096 byte * ::GdbReadMemory(uint64 address, int bytes)
3098 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3101 //GdbCommand(false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3102 if(GetRuntimePlatform() == win32)
3103 GdbCommand(false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3105 GdbCommand(false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3108 _dpl(0, "GdbReadMemory called with bytes = 0!");
3111 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3112 else if(eval.result && strcmp(eval.result, "N/A"))
3114 byte * result = new byte[bytes];
3115 byte * string = eval.result;
3119 result[c++] = (byte)strtol(string, &string, 10);
3135 bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3138 char * s1 = null; char * s2 = null;
3139 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3140 "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3141 "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3142 "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3143 "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor, ")");
3144 delete s1; delete s2;
3146 if(bpUser && stopItem.frame.line && bpUser.line != stopItem.frame.line)
3148 // updating user breakpoint on hit location difference
3149 // todo, print something?
3150 bpUser.line = stopItem.frame.line;
3151 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3152 ide.workspace.Save();
3157 if(bpInternal.type == internalModulesLoaded)
3159 if(!bpUser && !userBreakOnInternalBreakpoint)
3161 if(userAction == stepOut)//if(prevStopItem.reason == functionFinished)
3162 StepOut(ignoreBreakpoints);
3169 bool conditionMet = true;
3170 bool levelMatch = (bpUser.level == -1 || bpUser.level == frameCount-1);
3171 if(bpUser.condition)
3172 conditionMet = ResolveWatch(bpUser.condition);
3174 if(levelMatch && conditionMet)
3186 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3189 if(!bpUser && !bpInternal)
3195 void ValgrindTargetThreadExit()
3197 ide.outputView.debugBox.Logf($"ValgrindTargetThreadExit\n");
3200 vgTargetHandle.Wait();
3201 delete vgTargetHandle;
3203 HandleExit(null, null);
3206 void GdbThreadExit()
3208 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3209 if(state != terminated)
3211 _ChangeState(terminated);
3212 targetProcessId = 0;
3213 ClearBreakDisplay();
3219 serialSemaphore.Release();
3224 ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3225 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3227 HideDebuggerViews();
3229 //_ChangeState(terminated);
3233 void GdbThreadMain(char * output)
3236 Array<char *> outTokens { minAllocSize = 50 };
3237 Array<char *> subTokens { minAllocSize = 50 };
3238 DebugListItem item { };
3239 DebugListItem item2 { };
3240 bool setWaitingForPID = false;
3242 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3243 #ifdef GDB_DEBUG_CONSOLE
3244 _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3246 #ifdef GDB_DEBUG_OUTPUT
3248 int len = strlen(output);
3256 for(c = 0; c < len / 1024; c++)
3258 strncpy(tmp, start, 1024);
3259 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3262 ide.outputView.gdbBox.Logf("out: %s\n", start);
3266 ide.outputView.gdbBox.Logf("out: %s\n", output);
3270 #ifdef GDB_DEBUG_CONSOLE
3271 strcpy(lastGdbOutput, output);
3273 #ifdef GDB_DEBUG_GUI
3274 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3281 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3284 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3290 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3292 //if(outTokens.count == 1)
3297 _ChangeState(loaded);
3298 targetProcessId = 0;
3299 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3301 if(!strcmp(item.name, "reason"))
3303 char * reason = item.value;
3304 StripQuotes(reason, reason);
3305 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3308 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3310 StripQuotes(item2.value, item2.value);
3311 if(!strcmp(item2.name, "exit-code"))
3312 exitCode = item2.value;
3318 HandleExit(reason, exitCode);
3322 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3325 HandleExit(null, null);
3328 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3330 if(!strcmp(item.name, "bkpt"))
3332 sentBreakInsert = false;
3337 bpItem = ParseBreakpoint(item.value, outTokens);
3338 //breakType = bpValidation;
3340 else if(!strcmp(item.name, "depth"))
3342 StripQuotes(item.value, item.value);
3343 frameCount = atoi(item.value);
3345 stackFrames.Free(Frame::Free);
3347 else if(!strcmp(item.name, "stack"))
3350 if(stackFrames.count)
3351 ide.callStackView.Logf("...\n");
3354 item.value = StripBrackets(item.value);
3355 TokenizeList(item.value, ',', subTokens);
3356 for(i = 0; i < subTokens.count; i++)
3358 if(TokenizeListItem(subTokens[i], item))
3360 if(!strcmp(item.name, "frame"))
3363 stackFrames.Add(frame);
3364 item.value = StripCurlies(item.value);
3365 ParseFrame(frame, item.value);
3366 if(frame.file && frame.from)
3367 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3371 if(activeFrameLevel == -1)
3373 if(ide.projectView.IsModuleInProject(frame.file));
3375 if(frame.level != 0)
3377 //stopItem.frame = frame;
3378 breakType = selectFrame;
3381 activeFrame = frame;
3382 activeFrameLevel = frame.level;
3385 ide.callStackView.Logf("%3d ", frame.level);
3386 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3387 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3388 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3389 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3390 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3391 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3392 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3393 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3395 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3400 ide.callStackView.Logf("%3d ", frame.level);
3405 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3409 ide.callStackView.Logf("%s\n", frame.func);
3411 ide.callStackView.Logf($"unknown source\n");
3415 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3418 if(activeFrameLevel == -1)
3420 activeFrameLevel = 0;
3421 activeFrame = stackFrames.first;
3423 ide.callStackView.Home();
3425 subTokens.RemoveAll();
3427 /*else if(!strcmp(item.name, "frame"))
3430 item.value = StripCurlies(item.value);
3431 ParseFrame(&frame, item.value);
3433 else if(!strcmp(item.name, "thread-ids"))
3435 ide.threadsView.Clear();
3436 item.value = StripCurlies(item.value);
3437 TokenizeList(item.value, ',', subTokens);
3438 for(i = subTokens.count - 1; ; i--)
3440 if(TokenizeListItem(subTokens[i], item))
3442 if(!strcmp(item.name, "thread-id"))
3445 StripQuotes(item.value, item.value);
3446 value = atoi(item.value);
3447 ide.threadsView.Logf("%3d \n", value);
3450 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3455 ide.threadsView.Home();
3457 subTokens.RemoveAll();
3458 //if(!strcmp(outTokens[2], "number-of-threads"))
3460 else if(!strcmp(item.name, "new-thread-id"))
3462 StripQuotes(item.value, item.value);
3463 activeThread = atoi(item.value);
3465 else if(!strcmp(item.name, "value"))
3467 StripQuotes(item.value, item.value);
3468 eval.result = CopyString(item.value);
3469 eval.active = false;
3471 else if(!strcmp(item.name, "addr"))
3473 for(i = 2; i < outTokens.count; i++)
3475 if(TokenizeListItem(outTokens[i], item))
3477 if(!strcmp(item.name, "total-bytes"))
3479 StripQuotes(item.value, item.value);
3480 eval.bytes = atoi(item.value);
3482 else if(!strcmp(item.name, "next-row"))
3484 StripQuotes(item.value, item.value);
3485 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3487 else if(!strcmp(item.name, "memory"))
3491 //StripQuotes(item.value, item.value);
3492 item.value = StripBrackets(item.value);
3493 // this should be treated as a list...
3494 item.value = StripCurlies(item.value);
3495 TokenizeList(item.value, ',', subTokens);
3496 for(j = 0; j < subTokens.count; j++)
3498 if(TokenizeListItem(subTokens[j], item))
3500 if(!strcmp(item.name, "data"))
3502 item.value = StripBrackets(item.value);
3503 StripQuotes2(item.value, item.value);
3504 eval.result = CopyString(item.value);
3505 eval.active = false;
3509 subTokens.RemoveAll();
3514 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3515 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3517 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3520 else if(!strcmp(outTokens[0], "^running"))
3522 waitingForPID = true;
3523 setWaitingForPID = true;
3524 ClearBreakDisplay();
3526 else if(!strcmp(outTokens[0], "^exit"))
3528 _ChangeState(terminated);
3529 // ide.outputView.debugBox.Logf("Exit\n");
3530 // ide.Update(null);
3532 serialSemaphore.Release();
3534 else if(!strcmp(outTokens[0], "^error"))
3538 sentBreakInsert = false;
3539 breakpointError = true;
3542 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3544 if(!strcmp(item.name, "msg"))
3546 StripQuotes(item.value, item.value);
3549 eval.active = false;
3551 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3552 eval.error = symbolNotFound;
3553 else if(strstr(item.value, "Cannot access memory at address"))
3554 eval.error = memoryCantBeRead;
3556 eval.error = unknown;
3558 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3561 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3564 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3566 _ChangeState(stopped);
3567 gdbHandle.Printf("-exec-continue\n");
3569 else if(!strcmp(item.value, "ptrace: No such process."))
3571 _ChangeState(loaded);
3572 targetProcessId = 0;
3574 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3577 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3579 _ChangeState(loaded);
3580 targetProcessId = 0;
3582 else if(strstr(item.value, "No such file or directory."))
3584 _ChangeState(loaded);
3585 targetProcessId = 0;
3587 else if(strstr(item.value, "During startup program exited with code "))
3589 _ChangeState(loaded);
3590 targetProcessId = 0;
3595 if(strlen(item.value) < MAX_F_STRING)
3598 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3602 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3608 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3611 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3612 outTokens.RemoveAll();
3615 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3618 if(TokenizeList(output, ',', outTokens))
3620 if(!strcmp(outTokens[0], "=library-loaded"))
3621 FGODetectLoadedLibraryForAddedProjectIssues(outTokens);
3622 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3623 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3624 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3625 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3626 !strcmp(outTokens[0], "=breakpoint-modified"))
3627 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3628 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3629 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3630 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3631 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3633 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3635 outTokens.RemoveAll();
3639 if(TokenizeList(output, ',', outTokens))
3641 if(!strcmp(outTokens[0],"*running"))
3643 waitingForPID = true;
3644 setWaitingForPID = true;
3646 else if(!strcmp(outTokens[0], "*stopped"))
3649 _ChangeState(stopped);
3651 for(tk = 1; tk < outTokens.count; tk++)
3653 if(TokenizeListItem(outTokens[tk], item))
3655 if(!strcmp(item.name, "reason"))
3657 char * reason = item.value;
3658 StripQuotes(reason, reason);
3659 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3662 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3665 StripQuotes(item2.value, item2.value);
3666 if(!strcmp(item2.name, "exit-code"))
3667 exitCode = item2.value;
3673 HandleExit(reason, exitCode);
3676 else if(!strcmp(reason, "breakpoint-hit"))
3682 stopItem = GdbDataStop { };
3683 stopItem.reason = breakpointHit;
3685 for(i = tk+1; i < outTokens.count; i++)
3687 TokenizeListItem(outTokens[i], item);
3688 StripQuotes(item.value, item.value);
3689 if(!strcmp(item.name, "bkptno"))
3690 stopItem.bkptno = atoi(item.value);
3691 else if(!strcmp(item.name, "thread-id"))
3692 stopItem.threadid = atoi(item.value);
3693 else if(!strcmp(item.name, "frame"))
3695 item.value = StripCurlies(item.value);
3696 ParseFrame(stopItem.frame, item.value);
3698 else if(!strcmp(item.name, "disp") || !strcmp(item.name, "stopped-threads") || !strcmp(item.name, "core"))
3699 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "(", item.name, "=", item.value, ")");
3701 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown breakpoint hit item name (", item.name, "=", item.value, ")");
3706 else if(!strcmp(reason, "end-stepping-range"))
3712 stopItem = GdbDataStop { };
3713 stopItem.reason = endSteppingRange;
3715 for(i = tk+1; i < outTokens.count; i++)
3717 TokenizeListItem(outTokens[i], item);
3718 StripQuotes(item.value, item.value);
3719 if(!strcmp(item.name, "thread-id"))
3720 stopItem.threadid = atoi(item.value);
3721 else if(!strcmp(item.name, "frame"))
3723 item.value = StripCurlies(item.value);
3724 ParseFrame(stopItem.frame, item.value);
3726 else if(!strcmp(item.name, "reason") || !strcmp(item.name, "bkptno"))
3727 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "(", item.name, "=", item.value, ")");
3729 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown end of stepping range item name (", item.name, "=", item.value, ")");
3735 else if(!strcmp(reason, "function-finished"))
3741 stopItem = GdbDataStop { };
3742 stopItem.reason = functionFinished;
3744 for(i = tk+1; i < outTokens.count; i++)
3746 TokenizeListItem(outTokens[i], item);
3747 StripQuotes(item.value, item.value);
3748 if(!strcmp(item.name, "thread-id"))
3749 stopItem.threadid = atoi(item.value);
3750 else if(!strcmp(item.name, "frame"))
3752 item.value = StripCurlies(item.value);
3753 ParseFrame(stopItem.frame, item.value);
3755 else if(!strcmp(item.name, "gdb-result-var"))
3756 stopItem.gdbResultVar = CopyString(item.value);
3757 else if(!strcmp(item.name, "return-value"))
3758 stopItem.returnValue = CopyString(item.value);
3759 else if(!strcmp(item.name, "stopped-threads"))
3760 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Advanced thread debugging not handled");
3761 else if(!strcmp(item.name, "core"))
3762 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Information (core) not used");
3764 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown function finished item name (", item.name, "=", item.value, ")");
3767 event = functionEnd;
3770 else if(!strcmp(reason, "signal-received"))
3776 stopItem = GdbDataStop { };
3777 stopItem.reason = signalReceived;
3779 for(i = tk+1; i < outTokens.count; i++)
3781 TokenizeListItem(outTokens[i], item);
3782 StripQuotes(item.value, item.value);
3783 if(!strcmp(item.name, "signal-name"))
3784 stopItem.name = CopyString(item.value);
3785 else if(!strcmp(item.name, "signal-meaning"))
3786 stopItem.meaning = CopyString(item.value);
3787 else if(!strcmp(item.name, "thread-id"))
3788 stopItem.threadid = atoi(item.value);
3789 else if(!strcmp(item.name, "frame"))
3791 item.value = StripCurlies(item.value);
3792 ParseFrame(stopItem.frame, item.value);
3795 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown signal reveived item name (", item.name, "=", item.value, ")");
3797 if(!strcmp(stopItem.name, "SIGTRAP"))
3816 else if(!strcmp(reason, "watchpoint-trigger"))
3817 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3818 else if(!strcmp(reason, "read-watchpoint-trigger"))
3819 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3820 else if(!strcmp(reason, "access-watchpoint-trigger"))
3821 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3822 else if(!strcmp(reason, "watchpoint-scope"))
3823 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3824 else if(!strcmp(reason, "location-reached"))
3825 _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason location reached not handled");
3827 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3835 if(usingValgrind && event == none && !stopItem)
3836 event = valgrindStartPause;
3841 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3842 outTokens.RemoveAll();
3845 if(!strcmpi(output, "(gdb) "))
3849 char exeFile[MAX_LOCATION];
3850 int oldProcessID = targetProcessId;
3851 GetLastDirectory(targetFile, exeFile);
3853 while(!targetProcessId/*true*/)
3855 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3856 if(targetProcessId || gdbHandle.Peek()) break;
3861 _ChangeState(running);
3862 else if(!oldProcessID)
3864 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3865 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3866 gdbHandle.Printf("-gdb-exit\n");
3868 _ChangeState(terminated); //loaded;
3873 for(bp : ide.workspace.breakpoints)
3874 bp.inserted = false;
3877 bp.inserted = false;
3879 bpRunToCursor.inserted = false;
3881 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3882 ClearBreakDisplay();
3884 #if defined(__unix__)
3885 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
3887 progThread.terminate = true;
3890 fifoFile.CloseInput();
3897 DeleteFile(progFifoPath);
3898 progFifoPath[0] = '\0';
3905 serialSemaphore.Release();
3908 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
3912 if(!strncmp(output, "&\"warning:", 10))
3915 content = strstr(output, "\"");
3916 StripQuotes(content, content);
3917 content = strstr(content, ":");
3923 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
3930 _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
3932 if(!setWaitingForPID)
3933 waitingForPID = false;
3934 setWaitingForPID = false;
3942 // From GDB Output functions
3943 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens)
3945 char path[MAX_LOCATION] = "";
3946 char file[MAX_FILENAME] = "";
3948 DebugListItem item { };
3949 //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
3950 for(token : outTokens)
3952 if(TokenizeListItem(token, item))
3954 if(!strcmp(item.name, "target-name"))
3956 StripQuotes(item.value, path);
3957 MakeSystemPath(path);
3958 GetLastDirectory(path, file);
3960 else if(!strcmp(item.name, "symbols-loaded"))
3962 symbolsLoaded = (atoi(item.value) == 1);
3967 if(path[0] && file[0])
3969 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
3973 char prjTargetPath[MAX_LOCATION];
3974 char prjTargetFile[MAX_FILENAME];
3975 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
3976 strcpy(prjTargetPath, prj.topNode.path);
3977 PathCat(prjTargetPath, targetDirExp.dir);
3978 prjTargetFile[0] = '\0';
3979 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
3980 PathCat(prjTargetPath, prjTargetFile);
3981 MakeSystemPath(prjTargetPath);
3983 match = !fstrcmp(prjTargetFile, file);
3984 if(!match && (dot = strstr(prjTargetFile, ".so.")))
3986 char * dot3 = strstr(dot+4, ".");
3990 match = !fstrcmp(prjTargetFile, file);
3995 match = !fstrcmp(prjTargetFile, file);
4000 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
4001 /* -- 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)
4003 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
4005 match = !fstrcmp(prjTargetPath, path);
4006 if(!match && (dot = strstr(prjTargetPath, ".so.")))
4008 char * dot3 = strstr(dot+4, ".");
4012 match = !fstrcmp(prjTargetPath, path);
4017 match = !fstrcmp(prjTargetPath, path);
4021 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4028 void FGOBreakpointModified(Array<char *> outTokens)
4030 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4032 DebugListItem item { };
4033 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4035 if(!strcmp(item.name, "bkpt"))
4037 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4045 ExpressionType ::DebugEvalExpTypeError(char * result)
4047 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4052 case symbolNotFound:
4053 return symbolErrorExp;
4054 case memoryCantBeRead:
4055 return memoryErrorExp;
4057 return unknownErrorExp;
4060 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4063 _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4064 if(ide.projectView && ide.debugger.state == stopped)
4066 result = GdbEvaluateExpression(expression);
4067 *error = DebugEvalExpTypeError(result);
4072 *error = noDebuggerErrorExp;
4077 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4080 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4081 _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4082 if(!result || !strcmp(result, "N/A"))
4083 *error = memoryErrorExp;
4085 *error = DebugEvalExpTypeError(result);
4090 class ValgrindLogThread : Thread
4096 static char output[4096];
4097 Array<char> dynamicBuffer { minAllocSize = 4096 };
4098 File oldValgrindHandle = vgLogFile;
4099 incref oldValgrindHandle;
4102 while(debugger.state != terminated && vgLogFile)
4106 result = vgLogFile.Read(output, 1, sizeof(output));
4108 if(debugger.state == terminated || !vgLogFile/* || vgLogFile.Eof()*/)
4115 for(c = 0; c<result; c++)
4117 if(output[c] == '\n')
4119 int pos = dynamicBuffer.size;
4120 dynamicBuffer.size += c - start;
4121 memcpy(&dynamicBuffer[pos], output + start, c - start);
4122 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4123 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4124 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4125 dynamicBuffer.size++;
4126 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4128 // printf("%s\n", dynamicBuffer.array);
4130 if(strstr(&dynamicBuffer[0], "vgdb me"))
4131 debugger.serialSemaphore.Release();
4132 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4133 dynamicBuffer.size = 0;
4139 int pos = dynamicBuffer.size;
4140 dynamicBuffer.size += c - start;
4141 memcpy(&dynamicBuffer[pos], output + start, c - start);
4144 else if(debugger.state == stopped)
4147 printf("Got end of file from GDB!\n");
4154 delete dynamicBuffer;
4155 ide.outputView.debugBox.Logf($"ValgrindLogThreadExit\n");
4156 //if(oldValgrindHandle == vgLogFile)
4157 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4158 delete oldValgrindHandle;
4164 class ValgrindTargetThread : Thread
4170 static char output[4096];
4171 Array<char> dynamicBuffer { minAllocSize = 4096 };
4172 DualPipe oldValgrindHandle = vgTargetHandle;
4173 incref oldValgrindHandle;
4176 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4180 result = vgTargetHandle.Read(output, 1, sizeof(output));
4182 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4189 for(c = 0; c<result; c++)
4191 if(output[c] == '\n')
4193 int pos = dynamicBuffer.size;
4194 dynamicBuffer.size += c - start;
4195 memcpy(&dynamicBuffer[pos], output + start, c - start);
4196 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4197 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4198 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4199 dynamicBuffer.size++;
4200 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4202 // printf("%s\n", dynamicBuffer.array);
4204 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4206 dynamicBuffer.size = 0;
4212 int pos = dynamicBuffer.size;
4213 dynamicBuffer.size += c - start;
4214 memcpy(&dynamicBuffer[pos], output + start, c - start);
4220 printf("Got end of file from GDB!\n");
4224 delete dynamicBuffer;
4225 //if(oldValgrindHandle == vgTargetHandle)
4226 debugger.ValgrindTargetThreadExit();
4227 delete oldValgrindHandle;
4233 class GdbThread : Thread
4239 static char output[4096];
4240 Array<char> dynamicBuffer { minAllocSize = 4096 };
4241 DualPipe oldGdbHandle = gdbHandle;
4242 incref oldGdbHandle;
4245 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4249 result = gdbHandle.Read(output, 1, sizeof(output));
4251 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4258 for(c = 0; c<result; c++)
4260 if(output[c] == '\n')
4262 int pos = dynamicBuffer.size;
4263 dynamicBuffer.size += c - start;
4264 memcpy(&dynamicBuffer[pos], output + start, c - start);
4265 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4266 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4267 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4268 dynamicBuffer.size++;
4269 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4271 // _dpl(0, dynamicBuffer.array);
4273 debugger.GdbThreadMain(&dynamicBuffer[0]);
4274 dynamicBuffer.size = 0;
4280 int pos = dynamicBuffer.size;
4281 dynamicBuffer.size += c - start;
4282 memcpy(&dynamicBuffer[pos], output + start, c - start);
4288 _dpl(0, "Got end of file from GDB!");
4292 delete dynamicBuffer;
4293 //if(oldGdbHandle == gdbHandle)
4294 debugger.GdbThreadExit();
4295 delete oldGdbHandle;
4301 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4302 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4304 #if defined(__unix__)
4309 #include <sys/types.h>
4314 class ProgramThread : Thread
4320 bool fileCreated = false;
4322 static char output[1000];
4325 /*if(!mkfifo(progFifoPath, mask))
4332 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4336 if(FileExists(progFifoPath)) //fileCreated)
4338 fifoFile = FileOpen(progFifoPath, read);
4342 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4347 fd = fileno((FILE *)fifoFile.input);
4348 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4352 while(!terminate && fifoFile && !fifoFile.Eof())
4355 struct timeval time;
4363 selectResult = select(fd + 1, &rs, null, null, &time);
4364 if(FD_ISSET(fd, &rs))
4366 int result = (int)read(fd, output, sizeof(output)-1);
4367 if(!result || (result < 0 && errno != EAGAIN))
4371 output[result] = '\0';
4372 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4375 ide.outputView.debugBox.Log(output);
4384 //fifoFile.CloseInput();
4387 ide.outputView.debugBox.Log("\n");
4391 if(FileExists(progFifoPath)) //fileCreated)
4393 DeleteFile(progFifoPath);
4394 progFifoPath[0] = '\0';
4402 class Argument : struct
4404 Argument prev, next;
4406 property char * name { set { delete name; if(value) name = CopyString(value); } }
4408 property char * val { set { delete val; if(value) val = CopyString(value); } }
4422 class Frame : struct
4427 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4429 property char * func { set { delete func; if(value) func = CopyString(value); } }
4433 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4435 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4436 char * absoluteFile;
4437 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4446 delete absoluteFile;
4447 args.Free(Argument::Free);
4456 class GdbDataStop : struct
4458 DebuggerReason reason;
4473 char * gdbResultVar;
4483 if(reason == signalReceived)
4488 else if(reason == functionFinished)
4490 delete gdbResultVar;
4494 if(frame) frame.Free();
4503 class GdbDataBreakpoint : struct
4507 property char * number { set { delete number; if(value) number = CopyString(value); } }
4509 property char * type { set { delete type; if(value) type = CopyString(value); } }
4511 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4514 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4516 property char * func { set { delete func; if(value) func = CopyString(value); } }
4518 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4520 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4523 property char * at { set { delete at; if(value) at = CopyString(value); } }
4526 Array<GdbDataBreakpoint> multipleBPs;
4531 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4542 if(multipleBPs) multipleBPs.Free();
4546 ~GdbDataBreakpoint()
4552 class Breakpoint : struct
4557 property char * function { set { delete function; if(value) function = CopyString(value); } }
4558 char * relativeFilePath;
4559 property char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4560 char * absoluteFilePath;
4561 property char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4570 BreakpointType type;
4572 GdbDataBreakpoint bp;
4574 char * CopyLocationString(bool removePath)
4577 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4578 bool removingPath = removePath && file;
4581 char * fileName = new char[MAX_FILENAME];
4582 GetLastDirectory(file, fileName);
4588 location = PrintString(file, ":", function);
4590 location = CopyString(function);
4593 location = PrintString(file, ":", line);
4599 char * CopyUserLocationString()
4602 char * loc = CopyLocationString(false);
4604 for(p : ide.workspace.projects)
4606 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4614 location = PrintString("(", prj.name, ")", loc);
4624 if(relativeFilePath && relativeFilePath[0])
4626 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, relativeFilePath);
4628 f.Printf(" ~ %s\n", condition.expression);
4638 delete relativeFilePath;
4639 delete absoluteFilePath;
4649 class Watch : struct
4660 f.Printf(" ~ %s\n", expression);
4684 class DebugListItem : struct
4690 struct DebugEvaluationData
4695 uint64 nextBlockAddress;
4697 DebuggerEvaluationError error;
4700 class CodeLocation : struct
4703 char * absoluteFile;
4706 CodeLocation ::ParseCodeLocation(char * location)
4710 char * colon = null;
4712 char loc[MAX_LOCATION];
4713 strcpy(loc, location);
4714 for(temp = loc; temp = strstr(temp, ":"); temp++)
4722 int line = atoi(colon);
4725 CodeLocation codloc { line = line };
4726 codloc.file = CopyString(loc);
4727 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
4739 delete absoluteFile;
4748 void GDBFallBack(Expression exp, String expString)
4751 ExpressionType evalError = dummyExp;
4752 result = Debugger::EvaluateExpression(expString, &evalError);
4755 exp.constant = result;
4756 exp.type = constantExp;