2 public import static "ecere"
3 public import static "ec"
16 #define GDB_DEBUG_CONSOLE
20 extern char * strrchr(const char * s, int c);
23 #define strlen _strlen
27 #include <string.h> // For memchr
35 #include <sys/time.h> // Required on Apple...
42 // use =0 to disable printing of specific channels
43 static enum dplchan { none, gdbProtoIgnored=0/*1*/, gdbProtoUnknown=2, gdbOutput=3/*3*/, gdbCommand=4/*4*/, debuggerCall=0/*5*/, debuggerProblem=6,
44 debuggerUserAction=7,debuggerState=8, debuggerBreakpoints=9, debuggerWatches=0/*10*/, debuggerTemp=0 };
45 static const char * _dpct[] = {
47 "GDB Protocol Ignored",
48 "GDB Protocol ***Unknown***",
52 "Debugger ***Problem***",
53 "Debugger::ChangeUserAction",
54 "Debugger::ChangeState",
57 "-----> Temporary Message",
62 public char * StripQuotes2(char * string, char * output)
66 bool quoted = false, escaped = false;
68 for(c = 0; (ch = string[c]); c++)
72 if(escaped || ch != '\"')
75 escaped = !escaped && ch == '\\';
90 static void strescpy(char * d, char * s)
98 case '\n': d[k] = '\\'; d[++k] = 'n'; break;
99 case '\t': d[k] = '\\'; d[++k] = 't'; break;
100 case '\a': d[k] = '\\'; d[++k] = 'a'; break;
101 case '\b': d[k] = '\\'; d[++k] = 'b'; break;
102 case '\f': d[k] = '\\'; d[++k] = 'f'; break;
103 case '\r': d[k] = '\\'; d[++k] = 'r'; break;
104 case '\v': d[k] = '\\'; d[++k] = 'v'; break;
105 case '\\': d[k] = '\\'; d[++k] = '\\'; break;
106 case '\"': d[k] = '\\'; d[++k] = '\"'; break;
107 default: d[k] = s[j];
114 /*static char * CopyUnescapedSystemPath(char * p)
117 char * d = new char[len + 1];
118 UnescapeString(d, p, len);
119 #if defined(__WIN32__)
120 ChangeCh(d, '/', '\\');
125 static char * CopyUnescapedUnixPath(char * p)
128 char * d = new char[len + 1];
129 UnescapeString(d, p, len);
130 #if defined(__WIN32__)
131 ChangeCh(d, '\\', '/');
136 static char * CopyUnescapedString(char * s)
139 char * d = new char[len + 1];
140 UnescapeString(d, s, len);
144 static char * StripBrackets(char * string)
146 int length = strlen(string);
147 if(length > 1 && *string == '[' && string[length - 1] == ']')
150 string[length - 1] = '\0';
157 static char * StripCurlies(char * string)
159 int length = strlen(string);
160 if(length > 1 && *string == '{' && string[length - 1] == '}')
163 string[length - 1] = '\0';
170 /*static int StringGetInt(char * string, int start)
173 int i, len = strlen(string);
175 for(i = start; i < len && i < start + 8; i++)
177 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')
178 strncat(number, &string[i], 1);
185 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
189 bool quoted = false, escaped = false;
190 char * start = string, ch;
192 for(; (ch = *string); string++)
199 if(escaped || ch != '\"')
200 escaped = !escaped && ch == '\\';
206 else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
208 else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
210 else if(ch == seperator && !level)
219 //tokens[count] = start;
220 //tokens[count++] = start;
227 static bool TokenizeListItem(char * string, DebugListItem item)
229 char * equal = strstr(string, "=");
241 static bool CheckCommandAvailable(const char * command)
243 bool available = false;
245 char * name = new char[MAX_FILENAME];
246 char * pathVar = new char[maxPathLen];
248 GetEnvironment("PATH", pathVar, maxPathLen);
249 count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
250 strcpy(name, command);
254 const char * extensions[] = { "exe", "com", "bat", null };
255 for(e=0; extensions[e]; e++)
257 ChangeExtension(name, extensions[e], name);
259 for(c=0; c<count; c++)
261 FileListing fl { paths[c] };
264 if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
283 // define GdbGetLineSize = 1638400;
284 define GdbGetLineSize = 5638400;
285 #if defined(__unix__)
286 char progFifoPath[MAX_LOCATION];
287 char progFifoDir[MAX_LOCATION];
290 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
293 none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause, locationReached;
295 property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd || this == locationReached); } };
297 enum DebuggerAction { none, internal, restart, stop, selectFrame, advance }; //, bpValidation
300 unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit, locationReached
301 //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope,
302 //exited, exitedNormally, exitedSignalled;
306 none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad, internalEntry;
308 property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad || this == internalEntry); } };
309 property bool isUser { get { return (this == user || this == runToCursor); } };
311 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
312 enum DebuggerUserAction
314 none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepUntil, stepOut, runToCursor;
315 property bool breaksOnInternalBreakpoint { get { return (this == stepInto || this == stepOver || this == stepUntil); } };
319 none, run, _continue, next, until, advance, step, finish;
320 property bool suspendInternalBreakpoints { get { return (this == until || this == advance || this == step || this == finish); } };
323 FileDialog debuggerFileDialog { type = selectDir };
325 static DualPipe vgTargetHandle;
326 static File vgLogFile;
327 static char vgLogPath[MAX_LOCATION];
328 static DualPipe gdbHandle;
329 static DebugEvaluationData eval { };
331 static int targetProcessId;
333 static bool gdbReady;
334 static bool breakpointError;
338 Semaphore serialSemaphore { };
344 bool sentBreakInsert;
345 bool ignoreBreakpoints;
353 int activeFrameLevel;
362 GdbExecution gdbExecution;
363 DebuggerUserAction userAction;
366 DebuggerAction breakType;
368 //DebuggerCommand lastCommand; // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
370 GdbDataStop stopItem;
371 GdbDataBreakpoint bpItem;
374 List<Breakpoint> sysBPs { };
375 Breakpoint bpRunToCursor;
376 Breakpoint intBpEntry;
377 Breakpoint intBpMain;
378 Breakpoint intBpWinMain;
382 CompilerConfig currentCompiler;
383 ProjectConfig prjConfig;
386 CodeEditor codeEditor;
388 ValgrindLogThread vgLogThread { debugger = this };
389 ValgrindTargetThread vgTargetThread { debugger = this };
390 GdbThread gdbThread { debugger = this };
393 Map<String, bool> projectsLibraryLoaded { };
397 delay = 0.0, userData = this;
401 bool monitor = false;
402 DebuggerEvent curEvent = event;
403 GdbDataStop stopItem = this.stopItem;
404 Breakpoint bpUser = null;
405 Breakpoint bpInternal = null;
413 this.stopItem = null;
417 DynamicString bpReport { };
419 for(bp : sysBPs; bp.inserted)
421 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
424 if(bpRunToCursor && bpRunToCursor.inserted)
426 Breakpoint bp = bpRunToCursor;
427 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
430 for(bp : ide.workspace.breakpoints; bp.inserted)
432 bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
436 _dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
441 Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
444 _dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")");
455 if(curEvent && curEvent != exit)
457 _dplf(0, "No stop item");
465 Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
474 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
475 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
476 if(activeFrame.level == activeFrameLevel)
482 // GdbCommand(0, false, "-break-info %s", bpItem.number);
494 Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
495 if(bp && bp.inserted && bp.bp.addr)
497 if(bp.type.isInternal)
501 if(stopItem && stopItem.frame)
503 if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
504 bpUser = bpRunToCursor;
507 for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
509 if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
521 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
522 if(bpUser && stopItem.frame.addr && strcmp(stopItem.frame.addr, bpUser.bp.addr))
523 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") address missmatch!");
526 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
527 if((bpUser && !ignoreBreakpoints) || (bpInternal && userAction.breaksOnInternalBreakpoint))
529 hitThread = stopItem.threadid;
533 signalThread = stopItem.threadid;
537 case locationReached:
539 ignoreBreakpoints = false;
541 case valgrindStartPause:
542 GdbExecContinue(true);
550 if(curEvent == signal)
554 ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
555 ide.outputView.debugBox.Logf(" %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
556 ide.outputView.Show();
557 ide.callStackView.Show();
560 else if(curEvent == breakEvent)
562 ide.threadsView.Show();
563 ide.callStackView.Show();
564 ide.callStackView.Activate();
566 else if(curEvent == hit)
568 if(BreakpointHit(stopItem, bpInternal, bpUser))
570 ide.AdjustDebugMenus();
571 if(bpUser && bpUser.type == runToCursor)
573 ignoreBreakpoints = false;
574 UnsetBreakpoint(bpUser);
575 delete bpRunToCursor;
580 if(breakType == advance && bpInternal && (bpInternal.type == internalMain || bpInternal.type == internalEntry))
583 GdbExecAdvance(breakString, 0);
588 GdbExecContinue(false);
594 if(monitor && curEvent.canBeMonitored)
597 activeThread = stopItem.threadid;
598 GdbCommand(0, false, "-thread-list-ids");
599 InternalSelectFrame(activeFrameLevel);
600 GoToStackFrameLine(activeFrameLevel, true, false);
602 ide.ShowCodeEditor();
603 ide.AdjustDebugMenus();
604 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
617 #ifdef GDB_DEBUG_CONSOLE
618 char lastGdbOutput[GdbGetLineSize];
620 #if defined(__unix__)
621 ProgramThread progThread { };
625 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
626 void ChangeUserAction(const char * file, int line, DebuggerUserAction value)
629 bool same = value == userAction;
630 __dpl(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
635 #define _ChangeUserAction(value) userAction = value
639 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
640 void ChangeState(const char * file, int line, DebuggerState value)
642 #define _ChangeState(value) ChangeState(value)
643 void ChangeState(DebuggerState value)
646 bool same = value == state;
648 __dpl(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
651 if(!same) ide.AdjustDebugMenus();
656 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
658 stackFrames.Free(Frame::Free);
668 waitingForPID = false;
673 sentBreakInsert = false;
674 ignoreBreakpoints = false;
677 activeFrameLevel = 0;
694 bpRunToCursor = null;
696 delete currentCompiler;
699 WatchesReleaseCodeEditor();
702 projectsLibraryLoaded.Free();
704 /*GdbThread gdbThread
710 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
711 ideProcessId = Process_GetCurrentProcessId();
713 sysBPs.Add((intBpEntry = Breakpoint { type = internalEntry, enabled = false, level = -1 }));
714 sysBPs.Add((intBpMain = Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 }));
715 #if defined(__WIN32__)
716 sysBPs.Add((intBpWinMain = Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 }));
718 sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
719 sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
724 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
732 property bool isActive { get { return state == running || state == stopped; } }
733 property bool isPrepared { get { return state == loaded || state == running || state == stopped; } }
737 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
738 _ChangeUserAction(resume);
739 GdbExecContinue(true);
744 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
745 _ChangeUserAction(_break);
749 GdbDebugBreak(false);
755 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
756 _ChangeUserAction(stop);
763 GdbDebugBreak(false);
777 void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
779 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
780 _ChangeUserAction(restart);
781 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
785 bool GoToCodeLine(char * location)
788 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
789 codloc = CodeLocation::ParseCodeLocation(location);
792 CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, false, true, null, no, normal, false);
795 EditBox editBox = editor.editBox;
796 if(editBox.GoToLineNum(codloc.line - 1))
797 editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
804 bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
806 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
809 char sourceDir[MAX_LOCATION];
811 CodeEditor editor = null;
812 if(stackLevel == -1) // this (the two lines) is part of that fix that I would not put in for some time
814 for(frame = stackFrames.first; frame; frame = frame.next)
815 if(frame.level == stackLevel)
820 ide.callStackView.Show();
822 if(frame.absoluteFile)
823 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
824 if(!editor && frame.file)
825 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
826 if(!frame.absoluteFile && askForLocation && frame.file)
829 char title[MAX_LOCATION];
830 snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
831 title[sizeof(title)-1] = 0;
833 if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
835 AddSourceDir(sourceDir);
836 frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
839 if(!editor && frame.absoluteFile)
840 editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, false, true, null, no, normal, false);
842 ide.RepositionWindows(false);
844 if(editor && frame.line)
846 EditBox editBox = editor.editBox;
847 editBox.GoToLineNum(frame.line - 1);
848 editBox.GoToPosition(editBox.line, frame.line - 1, 0);
856 void SelectThread(int thread)
858 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
859 _ChangeUserAction(selectThread);
862 if(thread != activeThread)
864 activeFrameLevel = -1;
865 ide.callStackView.Clear();
866 GdbCommand(0, false, "-thread-select %d", thread);
868 InternalSelectFrame(activeFrameLevel);
869 GoToStackFrameLine(activeFrameLevel, true, false);
873 ide.callStackView.Show();
877 void SelectFrame(int frame)
879 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
880 _ChangeUserAction(selectFrame);
883 if(frame != activeFrameLevel)
885 InternalSelectFrame(frame);
892 void InternalSelectFrame(int frame)
894 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
895 activeFrameLevel = frame; // there is no active frame number in the gdb reply
896 GdbCommand(0, false, "-stack-select-frame %d", activeFrameLevel);
897 for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
898 if(activeFrame.level == activeFrameLevel)
902 void HandleExit(char * reason, char * code)
904 char verboseExitCode[128];
906 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
907 _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
912 snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
913 verboseExitCode[sizeof(verboseExitCode)-1] = 0;
916 verboseExitCode[0] = '\0';
920 // ClearBreakDisplay();
924 for(wh : ide.workspace.watches)
926 if(wh.type) FreeType(wh.type);
929 ide.watchesView.UpdateWatch(wh);
933 #if defined(__unix__)
936 progThread.terminate = true;
939 fifoFile.CloseInput();
949 char program[MAX_LOCATION];
950 GetSystemPathBuffer(program, targetFile);
952 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
953 else if(!strcmp(reason, "exited-normally"))
954 ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
955 else if(!strcmp(reason, "exited"))
956 ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
957 else if(!strcmp(reason, "exited-signalled"))
958 ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
960 ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
965 DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool ignoreBreakpoints)
967 DebuggerState result = none;
968 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), ignoreBreakpoints(", ignoreBreakpoints, ")");
969 if(restart && state == running && targetProcessId)
971 breakType = DebuggerAction::restart;
972 GdbDebugBreak(false);
976 if(restart && state == stopped)
978 if(needReset && state == loaded)
979 GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
981 if(result == none || result == terminated)
983 ide.outputView.ShowClearSelectTab(debug);
984 ide.outputView.debugBox.Logf($"Starting debug mode\n");
991 for(bp : ide.workspace.breakpoints)
997 if(GdbInit(compiler, config, bitDepth, useValgrind))
1002 this.ignoreBreakpoints = ignoreBreakpoints;
1007 void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1009 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1010 _ChangeUserAction(start);
1011 if(StartSession(compiler, config, bitDepth, useValgrind, true, false) == loaded)
1015 void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1017 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1018 _ChangeUserAction(stepInto);
1019 switch(StartSession(compiler, config, bitDepth, useValgrind, false, false))
1021 case loaded: GdbExecRun(); break;
1022 case stopped: GdbExecStep(); break;
1026 void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1028 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1029 _ChangeUserAction(stepOver);
1030 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1032 case loaded: GdbExecRun(); break;
1033 case stopped: GdbExecNext(); break;
1037 void StepUntil(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1039 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::StepUntil()");
1040 _ChangeUserAction(stepUntil);
1041 switch(StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints))
1043 case loaded: GdbExecRun(); break;
1044 case stopped: GdbExecUntil(null, 0); break;
1048 void StepOut(bool ignoreBreakpoints)
1050 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1051 _ChangeUserAction(stepOut);
1052 if(state == stopped)
1054 this.ignoreBreakpoints = ignoreBreakpoints;
1058 GdbExecContinue(true);
1062 void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, const char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel, bool oldImplementation)
1064 char relativeFilePath[MAX_LOCATION];
1065 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1066 _ChangeUserAction(runToCursor);
1067 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
1069 if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1071 UnsetBreakpoint(bpRunToCursor);
1072 delete bpRunToCursor;
1075 StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints);
1078 if(oldImplementation)
1080 bpRunToCursor = Breakpoint { };
1081 bpRunToCursor.absoluteFilePath = absoluteFilePath;
1082 bpRunToCursor.relativeFilePath = relativeFilePath;
1083 bpRunToCursor.line = lineNumber;
1084 bpRunToCursor.type = runToCursor;
1085 bpRunToCursor.enabled = true;
1086 bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1091 breakType = advance;
1092 breakString = PrintString(relativeFilePath, ":", lineNumber);
1095 else if(state == stopped)
1097 if(oldImplementation)
1098 GdbExecContinue(true);
1102 GdbExecUntil(absoluteFilePath, lineNumber);
1104 GdbExecAdvance(absoluteFilePath, lineNumber);
1109 void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1111 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1112 if(activeFrameLevel == -1)
1120 *error = signalOn && activeThread == signalThread;
1121 *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1122 *lineTopFrame = activeFrameLevel ? 1 : 0;
1126 int GetMarginIconsLineNumbers(const char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1128 char winFilePath[MAX_LOCATION];
1129 char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1131 Iterator<Breakpoint> it { ide.workspace.breakpoints };
1132 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1133 while(it.Next() && count < max)
1135 Breakpoint bp = it.data;
1138 if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1140 lines[count] = bp.line;
1141 enabled[count] = bp.enabled;
1146 if(activeFrameLevel == -1)
1154 *error = signalOn && activeThread == signalThread;
1155 if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1156 *lineCursor = activeFrame.line;
1159 if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1161 else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1162 *lineTopFrame = stopItem.frame.line;
1166 if(*lineTopFrame == *lineCursor && *lineTopFrame)
1172 void ChangeWatch(DataRow row, char * expression)
1174 Watch wh = (Watch)(intptr)row.tag;
1175 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1178 delete wh.expression;
1180 wh.expression = CopyString(expression);
1183 Iterator<Watch> it { ide.workspace.watches };
1185 ide.workspace.watches.Delete(it.pointer);
1191 row.tag = (int64)(intptr)wh;
1192 ide.workspace.watches.Add(wh);
1194 wh.expression = CopyString(expression);
1196 ide.workspace.Save();
1197 //if(expression && state == stopped)
1202 void MoveIcons(const char * fileName, int lineNumber, int move, bool start)
1204 char winFilePath[MAX_LOCATION];
1205 const char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1208 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1209 for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1211 Breakpoint bp = (Breakpoint)(intptr)bpLink.data;
1214 if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1216 if(bp.line > lineNumber || (bp.line == lineNumber && start))
1218 if(move < 0 && (bp.line < lineNumber - move))
1219 ide.workspace.RemoveBreakpoint(bp);
1223 ide.breakpointsView.UpdateBreakpoint(bp.row);
1224 ide.workspace.Save();
1230 // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1233 bool SourceDirDialog(const char * title, const char * startDir, const char * test, char * sourceDir)
1236 String srcDir = null;
1238 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1239 debuggerFileDialog.text = title;
1240 debuggerFileDialog.currentDirectory = startDir;
1241 debuggerFileDialog.master = ide;
1243 while(debuggerFileDialog.Modal())
1245 strcpy(sourceDir, debuggerFileDialog.filePath);
1246 if(!fstrcmp(ide.workspace.projectDir, sourceDir) &&
1247 MessageBox { type = yesNo, master = ide,
1248 contents = $"This is the project directory.\nWould you like to try again?",
1249 text = $"Invalid Source Directory" }.Modal() == no)
1253 for(dir : ide.workspace.sourceDirs)
1255 if(!fstrcmp(dir, sourceDir))
1263 MessageBox { type = yesNo, master = ide,
1264 contents = $"This source directory is already specified.\nWould you like to try again?",
1265 text = $"Invalid Source Directory" }.Modal() == no)
1271 char file[MAX_LOCATION];
1272 strcpy(file, sourceDir);
1273 PathCat(file, test);
1274 result = FileExists(file);
1276 MessageBox { type = yesNo, master = ide,
1277 contents = $"Unable to locate source file.\nWould you like to try again?",
1278 text = $"Invalid Source Directory" }.Modal() == no)
1292 void AddSourceDir(const char * sourceDir)
1294 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1295 ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1296 ide.workspace.Save();
1300 DebuggerState oldState = state;
1305 GdbDebugBreak(true);
1308 GdbCommand(0, false, "-environment-directory \"%s\"", sourceDir);
1311 if(oldState == running)
1312 GdbExecContinue(false);
1316 void ToggleBreakpoint(const char * fileName, int lineNumber)
1318 char absolutePath[MAX_LOCATION];
1319 Breakpoint bp = null;
1321 _dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1323 GetSlashPathBuffer(absolutePath, fileName);
1324 for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1333 ide.workspace.RemoveBreakpoint(bp);
1342 char relativePath[MAX_LOCATION];
1344 WorkspaceGetRelativePath(absolutePath, relativePath, &owner);
1346 if(!owner && !FileExists(absolutePath))
1348 char title[MAX_LOCATION];
1349 char directory[MAX_LOCATION];
1350 char sourceDir[MAX_LOCATION];
1351 StripLastDirectory(absolutePath, directory);
1352 snprintf(title, sizeof(title), $"Provide source files location directory for %s", relativePath);
1353 title[sizeof(title)-1] = 0;
1356 String srcDir = null;
1357 for(dir : ide.workspace.sourceDirs)
1359 if(IsPathInsideOf(absolutePath, dir))
1361 MakePathRelative(absolutePath, dir, relativePath);
1369 if(SourceDirDialog(title, directory, null, sourceDir))
1371 if(IsPathInsideOf(absolutePath, sourceDir))
1373 AddSourceDir(sourceDir);
1374 MakePathRelative(absolutePath, sourceDir, relativePath);
1377 else if(MessageBox { type = yesNo, master = ide,
1378 contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?",
1379 text = $"Invalid Source Directory" }.Modal() == no)
1386 ide.workspace.bpCount++;
1387 bp = { line = lineNumber, type = user, enabled = true, level = -1, project = owner };
1388 ide.workspace.breakpoints.Add(bp);
1389 bp.absoluteFilePath = absolutePath;
1390 bp.relativeFilePath = relativePath;
1391 ide.breakpointsView.AddBreakpoint(bp);
1396 DebuggerState oldState = state;
1401 GdbDebugBreak(true);
1404 if(!SetBreakpoint(bp, false))
1405 SetBreakpoint(bp, true);
1408 if(oldState == running)
1409 GdbExecContinue(false);
1412 ide.workspace.Save();
1415 void UpdateRemovedBreakpoint(Breakpoint bp)
1417 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1418 if(targeted && bp.inserted)
1420 DebuggerState oldState = state;
1425 GdbDebugBreak(true);
1428 UnsetBreakpoint(bp);
1431 if(oldState == running)
1432 GdbExecContinue(false);
1438 void ParseFrame(Frame frame, char * string)
1441 Array<char *> frameTokens { minAllocSize = 50 };
1442 Array<char *> argsTokens { minAllocSize = 50 };
1443 Array<char *> argumentTokens { minAllocSize = 50 };
1444 DebugListItem item { };
1447 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1448 TokenizeList(string, ',', frameTokens);
1449 for(i = 0; i < frameTokens.count; i++)
1451 if(TokenizeListItem(frameTokens[i], item))
1453 StripQuotes(item.value, item.value);
1454 if(!strcmp(item.name, "level"))
1455 frame.level = atoi(item.value);
1456 else if(!strcmp(item.name, "addr"))
1457 frame.addr = item.value;
1458 else if(!strcmp(item.name, "func"))
1459 frame.func = item.value;
1460 else if(!strcmp(item.name, "args"))
1462 if(!strcmp(item.value, "[]"))
1463 frame.argsCount = 0;
1466 item.value = StripBrackets(item.value);
1467 TokenizeList(item.value, ',', argsTokens);
1468 for(j = 0; j < argsTokens.count; j++)
1470 argsTokens[j] = StripCurlies(argsTokens[j]);
1471 TokenizeList(argsTokens[j], ',', argumentTokens);
1472 for(k = 0; k < argumentTokens.count; k++)
1475 frame.args.Add(arg);
1476 if(TokenizeListItem(argumentTokens[k], item))
1478 if(!strcmp(item.name, "name"))
1480 StripQuotes(item.value, item.value);
1481 arg.name = item.value;
1483 else if(!strcmp(item.name, "value"))
1485 StripQuotes(item.value, item.value);
1486 arg.val = item.value;
1489 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1492 _dplf(0, "Bad frame args item");
1494 argumentTokens.RemoveAll();
1496 frame.argsCount = argsTokens.count;
1497 argsTokens.RemoveAll();
1500 else if(!strcmp(item.name, "from"))
1501 frame.from = item.value;
1502 else if(!strcmp(item.name, "file"))
1503 frame.file = item.value;
1504 else if(!strcmp(item.name, "line"))
1505 frame.line = atoi(item.value);
1506 else if(!strcmp(item.name, "fullname"))
1508 // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1509 Workspace ws = ide.workspace;
1512 String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1513 if(strcmp(frame.file, path))
1517 frame.absoluteFile = item.value;
1520 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1523 _dplf(0, "Bad frame");
1528 delete argumentTokens;
1532 Breakpoint GetBreakpointById(int id, bool * isInternal)
1534 Breakpoint bp = null;
1535 //_dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1537 *isInternal = false;
1540 for(i : sysBPs; i.bp && i.bp.id == id)
1547 if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1551 for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1561 GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1564 GdbDataBreakpoint bp { };
1565 DebugListItem item { };
1566 Array<char *> bpTokens { minAllocSize = 16 };
1567 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1568 string = StripCurlies(string);
1569 TokenizeList(string, ',', bpTokens);
1570 for(i = 0; i < bpTokens.count; i++)
1572 if(TokenizeListItem(bpTokens[i], item))
1574 StripQuotes(item.value, item.value);
1575 if(!strcmp(item.name, "number"))
1577 if(!strchr(item.value, '.'))
1578 bp.id = atoi(item.value);
1579 bp.number = item.value;
1581 else if(!strcmp(item.name, "type"))
1582 bp.type = item.value;
1583 else if(!strcmp(item.name, "disp"))
1584 bp.disp = item.value;
1585 else if(!strcmp(item.name, "enabled"))
1586 bp.enabled = (!strcmpi(item.value, "y"));
1587 else if(!strcmp(item.name, "addr"))
1589 if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1592 Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1593 while(outTokens.count > ++c)
1595 GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1596 bpArray.Add(multBp);
1600 bp.addr = item.value;
1602 else if(!strcmp(item.name, "func"))
1603 bp.func = item.value;
1604 else if(!strcmp(item.name, "file"))
1605 bp.file = item.value;
1606 else if(!strcmp(item.name, "fullname"))
1607 bp.fullname = item.value;
1608 else if(!strcmp(item.name, "line"))
1609 bp.line = atoi(item.value);
1610 else if(!strcmp(item.name, "at"))
1612 else if(!strcmp(item.name, "times"))
1613 bp.times = atoi(item.value);
1614 else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1615 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1617 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1625 void ShowDebuggerViews()
1627 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1628 ide.outputView.Show();
1629 ide.outputView.SelectTab(debug);
1630 ide.threadsView.Show();
1631 ide.callStackView.Show();
1632 ide.watchesView.Show();
1636 void HideDebuggerViews()
1638 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1639 ide.RepositionWindows(true);
1642 bool ::GdbCommand(Time timeOut, bool focus, const char * format, ...)
1644 bool result = false;
1648 // TODO: Improve this limit
1649 static char string[MAX_F_STRING*4];
1651 va_start(args, format);
1652 vsnprintf(string, sizeof(string), format, args);
1653 string[sizeof(string)-1] = 0;
1657 ide.debugger.serialSemaphore.TryWait();
1659 #ifdef GDB_DEBUG_CONSOLE
1660 _dpcl(_dpct, dplchan::gdbCommand, 0, string);
1662 #ifdef GDB_DEBUG_OUTPUT
1663 ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1665 #ifdef GDB_DEBUG_GUI
1667 ide.gdbDialog.AddCommand(string);
1670 strcat(string,"\n");
1671 gdbHandle.Puts(string);
1674 Process_ShowWindows(targetProcessId);
1680 startTime = GetTime();
1683 if(ide.debugger.serialSemaphore.TryWait())
1690 if(GetTime() - startTime > timeOut)
1698 ide.debugger.serialSemaphore.Wait();
1707 bool ValidateBreakpoint(Breakpoint bp)
1709 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1710 if(modules && bp.line && bp.bp)
1712 if(bp.bp.line != bp.line)
1718 ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1720 //UnsetBreakpoint(bp);
1721 //bp.enabled = false;
1727 ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1728 bp.line = bp.bp.line;
1735 void BreakpointsMaintenance()
1737 //_dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::BreakpointsMaintenance()");
1740 if(gdbExecution.suspendInternalBreakpoints)
1742 for(bp : sysBPs; bp.inserted)
1743 UnsetBreakpoint(bp);
1747 DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1748 for(bp : sysBPs; !bp.inserted)
1750 bool insert = false;
1751 if(bp.type == internalModulesLoaded)
1753 char path[MAX_LOCATION];
1754 char name[MAX_LOCATION];
1755 char fixedModuleName[MAX_FILENAME];
1758 bool moduleLoadBlock = false;
1760 ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1761 snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1762 name[sizeof(name)-1] = 0;
1763 strcpy(path, ide.workspace.projectDir);
1764 PathCatSlash(path, objDir.dir);
1765 PathCatSlash(path, name);
1766 f = FileOpen(path, read);
1769 for(lineNumber = 1; !f.Eof(); lineNumber++)
1771 if(f.GetLine(line, sizeof(line) - 1))
1773 bool moduleLoadLine;
1774 TrimLSpaces(line, line);
1775 moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1776 if(!moduleLoadBlock && moduleLoadLine)
1777 moduleLoadBlock = true;
1778 else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1784 char relative[MAX_LOCATION];
1785 bp.absoluteFilePath = path;
1786 MakePathRelative(path, ide.workspace.projectDir, relative);
1787 bp.relativeFilePath = relative;
1788 bp.line = lineNumber;
1794 else if(bp.type == internalModuleLoad)
1798 for(prj : ide.workspace.projects)
1800 if(!strcmp(prj.moduleName, "ecere"))
1802 ProjectNode node = prj.topNode.Find("instance.c", false);
1805 char path[MAX_LOCATION];
1806 char relative[MAX_LOCATION];
1807 node.GetFullFilePath(path);
1808 bp.absoluteFilePath = path;
1809 MakePathRelative(path, prj.topNode.path, relative);
1810 bp.relativeFilePath = relative;
1822 if(!SetBreakpoint(bp, false))
1823 SetBreakpoint(bp, true);
1829 if(userAction != runToCursor && bpRunToCursor && bpRunToCursor.inserted)
1830 UnsetBreakpoint(bpRunToCursor);
1831 if(bpRunToCursor && !bpRunToCursor.inserted)
1833 if(!SetBreakpoint(bpRunToCursor, false))
1834 SetBreakpoint(bpRunToCursor, true);
1837 if(ignoreBreakpoints)
1839 for(bp : ide.workspace.breakpoints; bp.inserted)
1840 UnsetBreakpoint(bp);
1844 for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1848 if(!SetBreakpoint(bp, false))
1849 SetBreakpoint(bp, true);
1855 _dplf(0, "problem");
1858 bp.bp = GdbDataBreakpoint { };
1865 void UnsetBreakpoint(Breakpoint bp)
1867 char * s = null; _dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1868 if(symbols && bp.inserted)
1870 GdbCommand(0, false, "-break-delete %s", bp.bp.number);
1871 bp.inserted = false;
1877 bool SetBreakpoint(Breakpoint bp, bool removePath)
1879 char * s = null; _dpcl(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
1880 breakpointError = false;
1881 if(symbols && bp.enabled && (!bp.project || bp.project.GetTargetType(bp.project.config) == staticLibrary || bp.project == ide.project || projectsLibraryLoaded[bp.project.name]))
1883 sentBreakInsert = true;
1885 GdbCommand(0, false, "-break-insert *%s", bp.address);
1888 char * location = bp.CopyLocationString(removePath);
1889 GdbCommand(0, false, "-break-insert %s", location);
1892 if(!breakpointError)
1894 char * address = null;
1895 if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
1898 GdbDataBreakpoint first = null;
1899 for(n : bpItem.multipleBPs)
1901 if(!fstrcmp(n.fullname, bp.absoluteFilePath) && !first)
1911 GdbCommand(0, false, "-break-disable %s", n.number);
1915 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
1920 address = CopyString(first.addr);
1921 bpItem.addr = first.addr;
1922 bpItem.func = first.func;
1923 bpItem.file = first.file;
1924 bpItem.fullname = first.fullname;
1925 bpItem.line = first.line;
1926 //bpItem.thread-groups = first.thread-groups;*/
1929 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
1931 _dpcl(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
1932 bpItem.multipleBPs.Free();
1933 delete bpItem.multipleBPs;
1938 bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
1940 ValidateBreakpoint(bp);
1944 UnsetBreakpoint(bp);
1945 bp.address = address;
1947 SetBreakpoint(bp, removePath);
1950 return !breakpointError;
1957 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
1959 stackFrames.Free(Frame::Free);
1960 GdbCommand(0, false, "-stack-info-depth");
1962 GdbCommand(0, false, "-stack-info-depth 192");
1963 if(frameCount && frameCount <= 192)
1964 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
1967 GdbCommand(0, false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
1968 GdbCommand(0, false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
1970 GdbCommand(0, false, "");
1975 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
1978 char escaped[MAX_LOCATION];
1979 strescpy(escaped, targetFile);
1980 GdbCommand(0, false, "file \"%s\"", escaped); //GDB/MI Missing Implementation in 5.1.1 but we now have -file-exec-and-symbols / -file-exec-file / -file-symbol-file
1987 const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
1988 //GdbCommand(0, false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
1989 printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
1990 GdbCommand(0, false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
1993 GdbCommand(0, false, "info target"); //GDB/MI Missing Implementation -file-list-symbol-files and -file-list-exec-sections
1995 /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
1996 GdbCommand(0, false, "-environment-directory \"%s\"", prj.topNode.path);*/
1998 for(dir : ide.workspace.sourceDirs; dir && dir[0])
2000 bool interference = false;
2001 for(prj : ide.workspace.projects)
2003 if(!fstrcmp(prj.topNode.path, dir))
2005 interference = true;
2009 if(!interference && dir[0])
2010 GdbCommand(0, false, "-environment-directory \"%s\"", dir);
2018 /*void GdbTargetRelease()
2022 BreakpointsDeleteAll();
2023 GdbCommand(0, false, "file"); //GDB/MI Missing Implementation -target-detach
2029 void GdbDebugBreak(bool internal)
2031 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2035 breakType = DebuggerAction::internal;
2037 if(ide) ide.Update(null);
2039 if(Process_Break(targetProcessId)) //GdbCommand(0, false, "-exec-interrupt");
2040 serialSemaphore.Wait();
2043 _ChangeState(loaded);
2044 targetProcessId = 0;
2049 ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2054 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2059 ShowDebuggerViews();
2061 GdbExecContinue(true);
2062 else if(!GdbCommand(3, true, "-exec-run"))
2063 gdbExecution = none;
2066 void GdbExecContinue(bool focus)
2068 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2071 GdbCommand(0, focus, "-exec-continue");
2076 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2077 gdbExecution = next;
2079 GdbCommand(0, true, "-exec-next");
2082 void ForceUpdateCurrentFrame()
2085 GdbCommand(0, false, "-thread-list-ids");
2086 InternalSelectFrame(activeFrameLevel);
2087 GoToStackFrameLine(activeFrameLevel, true, false);
2089 ide.ShowCodeEditor();
2090 ide.AdjustDebugMenus();
2091 ideMainFrame.Activate(); // TOFIX: ide.Activate() is not reliable (app inactive)
2095 void GdbExecUntil(const char * absoluteFilePath, int lineNumber)
2097 bool forceUpdate = false;
2098 char relativeFilePath[MAX_LOCATION];
2099 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecUntil()");
2100 gdbExecution = until;
2102 if(absoluteFilePath)
2104 WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
2105 if(!GdbCommand(0.1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2107 GetLastDirectory(relativeFilePath, relativeFilePath);
2108 if(GdbCommand(1, true, "-exec-until %s:%d", relativeFilePath, lineNumber))
2113 GdbCommand(0, true, "-exec-until");
2115 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2116 // (It may not be ideal, we may need to wait?)
2118 ForceUpdateCurrentFrame();
2121 void GdbExecAdvance(const char * absoluteFilePathOrLocation, int lineNumber)
2123 bool forceUpdate = false;
2124 char relativeFilePath[MAX_LOCATION];
2125 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecAdvance()");
2126 gdbExecution = advance;
2130 WorkspaceGetRelativePath(absoluteFilePathOrLocation, relativeFilePath, null);
2131 if(!GdbCommand(0.1, true, "advance %s:%d", relativeFilePath, lineNumber)) // should use -exec-advance -- GDB/MI implementation missing
2133 GetLastDirectory(relativeFilePath, relativeFilePath);
2134 if(GdbCommand(1, true, "advance %s:%d", relativeFilePath, lineNumber))
2140 if(!GdbCommand(0.1, true, "advance %s", absoluteFilePathOrLocation)) // should use -exec-advance -- GDB/MI implementation missing
2142 GetLastDirectory(absoluteFilePathOrLocation, relativeFilePath);
2143 if(GdbCommand(1, true, "advance %s", relativeFilePath))
2148 // This is to handle GDB 6.3 on OS X not giving us *running then *stopped:
2149 // (It may not be ideal, we may need to wait?)
2151 ForceUpdateCurrentFrame();
2156 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2157 gdbExecution = step;
2159 GdbCommand(0, true, "-exec-step");
2162 void GdbExecFinish()
2164 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2165 gdbExecution = finish;
2167 GdbCommand(0, true, "-exec-finish");
2170 void GdbExecCommon()
2172 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2173 BreakpointsMaintenance();
2176 #ifdef GDB_DEBUG_GUI
2177 void SendGDBCommand(const char * command)
2179 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2180 DebuggerState oldState = state;
2185 GdbDebugBreak(true);
2188 GdbCommand(0, false, command);
2191 if(oldState == running)
2192 GdbExecContinue(false);
2196 void ClearBreakDisplay()
2198 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2200 activeFrameLevel = -1;
2210 stackFrames.Free(Frame::Free);
2211 ide.callStackView.Clear();
2212 ide.threadsView.Clear();
2218 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2220 GdbCommand(0, false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2224 bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2227 char oldDirectory[MAX_LOCATION];
2228 char tempPath[MAX_LOCATION];
2229 char command[MAX_F_STRING*4];
2230 Project project = ide.project;
2231 DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2232 PathBackup pathBackup { };
2233 Map<String, String> envBackup { };
2235 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2236 if(currentCompiler != compiler)
2238 delete currentCompiler;
2239 currentCompiler = compiler;
2240 incref currentCompiler;
2243 this.bitDepth = bitDepth;
2244 usingValgrind = useValgrind;
2246 _ChangeState(loaded);
2248 sentBreakInsert = false;
2249 breakpointError = false;
2250 ignoreBreakpoints = false;
2256 projectsLibraryLoaded.Free();
2258 ide.outputView.ShowClearSelectTab(debug);
2259 ide.outputView.debugBox.Logf($"Starting debug mode\n");
2261 #ifdef GDB_DEBUG_OUTPUT
2262 ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2265 strcpy(tempPath, ide.workspace.projectDir);
2266 PathCatSlash(tempPath, targetDirExp.dir);
2268 targetDir = CopyString(tempPath);
2269 project.CatTargetFileName(tempPath, compiler, config);
2271 targetFile = CopyString(tempPath);
2273 GetWorkingDir(oldDirectory, MAX_LOCATION);
2274 if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2276 char temp[MAX_LOCATION];
2277 strcpy(temp, ide.workspace.projectDir);
2278 PathCatSlash(temp, ide.workspace.debugDir);
2279 ChangeWorkingDir(temp);
2282 ChangeWorkingDir(ide.workspace.projectDir);
2284 ide.SetPath(true, compiler, config, bitDepth);
2286 // TODO: This pollutes the environment, but at least it works
2287 // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2288 // What is the proper solution for this? DualPipeOpenEnv?
2289 // gdb set environment commands don't seem to take effect
2290 for(e : ide.workspace.environmentVars)
2292 // Backing up the environment variables until we use DualPipeOpenEnv()
2293 envBackup[e.name] = CopyString(getenv(e.name));
2294 SetEnvironment(e.name, e.string);
2299 char * clArgs = ide.workspace.commandLineArgs;
2300 const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2301 ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2302 int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2303 bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2304 vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2308 vgLogThread.Create();
2312 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2315 if(result && !CheckCommandAvailable(valgrindCommand))
2317 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2322 char * vgRedzoneSizeFlag = PrintString(" --redzone-size=", vgRedzoneSize);
2323 sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2324 valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSize > -1 ? vgRedzoneSizeFlag : "", vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2325 delete vgRedzoneSizeFlag;
2326 vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2329 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2335 incref vgTargetHandle;
2336 vgTargetThread.Create();
2338 targetProcessId = vgTargetHandle.GetProcessID();
2339 waitingForPID = false;
2340 if(!targetProcessId)
2342 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2349 serialSemaphore.Wait();
2356 if(compiler.targetPlatform == win32)
2359 #if !((defined(__WORDSIZE) && __WORDSIZE == 8) || defined(__x86_64__))
2362 bitDepth == 32 ? "i686-w64-mingw32-gdb" : "gdb"); // x86_64-w64-mingw32-gdb
2365 // We really should have a box to select GDB in the compiler/toolchain options
2366 strcpy(command, "gdb");
2367 if(!CheckCommandAvailable(command))
2369 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2374 strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2376 gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2379 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2389 gdbProcessId = gdbHandle.GetProcessID();
2392 ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2399 serialSemaphore.Wait();
2402 GdbCommand(0, false, "-gdb-set verbose off");
2403 //GdbCommand(0, false, "-gdb-set exec-done-display on");
2404 GdbCommand(0, false, "-gdb-set step-mode off");
2405 GdbCommand(0, false, "-gdb-set unwindonsignal on");
2406 //GdbCommand(0, false, "-gdb-set shell on");
2407 GdbCommand(0, false, "set print elements 992");
2408 GdbCommand(0, false, "-gdb-set backtrace limit 100000");
2412 //_ChangeState(terminated);
2418 #if defined(__unix__)
2420 CreateTemporaryDir(progFifoDir, "ecereide");
2421 strcpy(progFifoPath, progFifoDir);
2422 PathCat(progFifoPath, "ideprogfifo");
2423 if(!mkfifo(progFifoPath, 0600))
2425 //fileCreated = true;
2430 ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2437 progThread.terminate = false;
2438 progThread.Create();
2442 #if defined(__WIN32__)
2443 GdbCommand(0, false, "-gdb-set new-console on");
2446 #if defined(__unix__)
2448 GdbCommand(0, false, "-inferior-tty-set %s", progFifoPath);
2452 GdbCommand(0, false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2454 for(e : ide.workspace.environmentVars)
2456 GdbCommand(0, false, "set environment %s=%s", e.name, e.string);
2461 ChangeWorkingDir(oldDirectory);
2465 SetEnvironment(&e, e);
2474 delete targetDirExp;
2480 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2481 if(gdbHandle && gdbProcessId)
2484 GdbCommand(0, false, "-gdb-exit");
2493 vgLogFile.CloseInput();
2494 if(vgLogThread.created)
2504 vgTargetThread.Wait();
2514 _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2519 for(bp : ide.workspace.breakpoints)
2525 bpRunToCursor.Reset();
2527 ide.outputView.debugBox.Logf($"Debugging stopped\n");
2529 #if defined(__unix__)
2530 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2532 progThread.terminate = true;
2535 fifoFile.CloseInput();
2541 DeleteFile(progFifoPath);
2542 progFifoPath[0] = '\0';
2551 bool WatchesLinkCodeEditor()
2553 bool goodFrame = activeFrame && activeFrame.absoluteFile;
2554 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesLinkCodeEditor()");
2555 if(codeEditor && (!goodFrame || fstrcmp(codeEditor.fileName, activeFrame.absoluteFile)))
2556 WatchesReleaseCodeEditor();
2558 if(!codeEditor && goodFrame)
2560 codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, false, false, null, no, normal, false);
2563 codeEditor.inUseDebug = true;
2567 return codeEditor != null;
2570 void WatchesReleaseCodeEditor()
2572 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesReleaseCodeEditor()");
2575 codeEditor.inUseDebug = false;
2576 if(!codeEditor.visible)
2577 codeEditor.Destroy(0);
2582 bool ResolveWatch(Watch wh)
2584 bool result = false;
2586 _dpcl(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2598 char watchmsg[MAX_F_STRING];
2600 if(state == stopped && !codeEditor)
2601 wh.value = CopyString($"No source file found for selected frame");
2602 //if(codeEditor && state == stopped || state != stopped)
2605 Module backupPrivateModule;
2606 Context backupContext;
2607 Class backupThisClass;
2610 backupPrivateModule = GetPrivateModule();
2611 backupContext = GetCurrentContext();
2612 backupThisClass = GetThisClass();
2615 SetPrivateModule(codeEditor.privateModule);
2616 SetCurrentContext(codeEditor.globalContext);
2617 SetTopContext(codeEditor.globalContext);
2618 SetGlobalContext(codeEditor.globalContext);
2619 SetGlobalData(&codeEditor.globalData);
2622 exp = ParseExpressionString(wh.expression);
2624 if(exp && !GetParseError())
2626 char expString[4096];
2628 PrintExpression(exp, expString);
2630 SetInDebugger(true);
2633 // NOTE: DebugFindCtxTree() should be called only once for evaluating all watches in the watch window
2634 if(codeEditor && activeFrame)
2635 DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2636 ProcessExpressionType(exp);
2638 wh.type = exp.expType;
2641 DebugComputeExpression(exp);
2642 // e.g. Meters * Degrees has no type set yet for some reason
2643 if(!wh.type && exp.expType)
2645 wh.type = exp.expType;
2646 exp.expType.refCount++;
2649 // This makes Degrees { 45 } work
2650 if(exp.type == constantExp && exp.isConstant && exp.expType && exp.expType.kind == classType &&
2651 exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == unitClass && exp.expType._class.registered.base.type == unitClass)
2653 ApplyUnitConverters(exp);
2655 else if(exp.type == instanceExp && exp.instance.data)
2657 Symbol s = exp.instance._class ? exp.instance._class.symbol : null;
2658 Class c = s ? s.registered : null;
2662 bool needClass = false;
2663 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, exp.instance.data, tmp, null, &needClass);
2666 FreeExpContents(exp);
2667 exp.type = constantExp;
2668 exp.isConstant = true;
2669 exp.constant = CopyString(s);
2673 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == bitClass)
2675 Class c = exp.expType._class.registered;
2677 bool needClass = false;
2678 Operand op = GetOperand(exp);
2680 char * (* onGetString)(void *, void *, void *, void *, void *) = (void *)c._vTbl[__ecereVMethodID_class_OnGetString];
2683 if(op.type) op.type.refCount++;
2686 case charType: s = onGetString(c, &op.c, tmp, null, &needClass); break;
2687 case shortType: s = onGetString(c, &op.s, tmp, null, &needClass); break;
2688 case intType: s = onGetString(c, &op.i, tmp, null, &needClass); break;
2689 case int64Type: s = onGetString(c, &op.i64, tmp, null, &needClass); break;
2694 FreeExpContents(exp);
2695 exp.type = constantExp;
2696 exp.isConstant = true;
2697 exp.constant = CopyString(s);
2701 else if(exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass && exp.hasAddress)
2703 Class c = exp.expType._class.registered;
2704 char structString[1024];
2705 strcpy(structString, "*(struct ");
2706 FullClassNameCat(structString, c.fullName, false);
2707 strcat(structString, " *)");
2708 strcatf(structString, "0x%p", exp.address);
2709 GDBFallBack(exp, structString);
2711 byte * data = GdbReadMemory(exp.address, c.structSize);
2715 bool needClass = false;
2716 char * s = ((char * (*)(void *, void *, void *, void *, void *))(void *)c._vTbl[__ecereVMethodID_class_OnGetString])(c, data, tmp, null, &needClass);
2719 FreeExpContents(exp);
2720 exp.type = constantExp;
2721 exp.isConstant = true;
2722 exp.constant = CopyString(s);
2729 if(ExpressionIsError(exp) && exp.type != functionCallErrorExp)
2731 GDBFallBack(exp, expString);
2734 /*if(exp.hasAddress)
2736 char temp[MAX_F_STRING];
2737 sprintf(temp, "0x%x", exp.address);
2738 wh.address = CopyString(temp);
2739 // wh.address = CopyStringf("0x%x", exp.address);
2744 Type dataType = exp.expType;
2747 char temp[MAX_F_STRING];
2748 switch(dataType.kind)
2751 sprintf(temp, "%i", exp.val.c);
2754 sprintf(temp, "%i", exp.val.s);
2759 sprintf(temp, "%i", exp.val.i);
2762 sprintf(temp, "%i", exp.val.i64);
2765 sprintf(temp, "%i", exp.val.p);
2770 long v = (long)exp.val.f;
2771 sprintf(temp, "%i", v);
2776 long v = (long)exp.val.d;
2777 sprintf(temp, "%i", v);
2782 wh.intVal = CopyString(temp);
2783 switch(dataType.kind)
2786 sprintf(temp, "0x%x", exp.val.c);
2789 sprintf(temp, "0x%x", exp.val.s);
2793 sprintf(temp, "0x%x", exp.val.i);
2796 sprintf(temp, "0x%x", exp.val.i64);
2799 sprintf(temp, "0x%x", exp.val.i64);
2802 sprintf(temp, "0x%x", exp.val.p);
2807 long v = (long)exp.val.f;
2808 sprintf(temp, "0x%x", v);
2813 long v = (long)exp.val.d;
2814 sprintf(temp, "0x%x", v);
2819 wh.hexVal = CopyString(temp);
2820 switch(dataType.kind)
2823 sprintf(temp, "0o%o", exp.val.c);
2826 sprintf(temp, "0o%o", exp.val.s);
2830 sprintf(temp, "0o%o", exp.val.i);
2833 sprintf(temp, "0o%o", exp.val.i64);
2836 sprintf(temp, "0o%o", exp.val.i64);
2839 sprintf(temp, "0o%o", exp.val.p);
2844 long v = (long)exp.val.f;
2845 sprintf(temp, "0o%o", v);
2850 long v = (long)exp.val.d;
2851 sprintf(temp, "0o%o", v);
2856 wh.octVal = CopyString(temp);
2859 // WHATS THIS HERE ?
2860 if(exp.type == constantExp && exp.constant)
2861 wh.constant = CopyString(exp.constant);
2867 case symbolErrorExp:
2868 snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2870 case memberSymbolErrorExp:
2872 Expression memberExp = exp.member.exp;
2873 Identifier memberID = exp.member.member;
2874 Type type = memberExp.expType;
2877 if(type.kind == structType || type.kind == unionType)
2879 char string[1024] = "";
2881 PrintTypeNoConst(type, string, false, true);
2882 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in %s \"%s\"",
2883 memberID ? memberID.string : "", type.kind == unionType ? "union" : "struct", type.name ? type.name : string);
2887 Class _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2888 char string[1024] = "";
2892 PrintTypeNoConst(type, string, false, true);
2893 classSym = FindClass(string);
2894 _class = classSym ? classSym.registered : null;
2897 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2899 // NOTE: This should probably never happen, only classes and struct can be dereferenced, a dereferenceErrorExp should be displayed instead
2900 snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in type \"%s\"", memberID ? memberID.string : "", string);
2904 // NOTE: This should probably never happen, the error causing the unresolved expression should be displayed instead
2905 snprintf(watchmsg, sizeof(watchmsg), $"Accessing member \"%s\" from unresolved expression", memberID ? memberID.string : "");
2908 case memberPropertyErrorExp:
2910 Expression memberExp = exp.member.exp;
2911 Identifier memberID = exp.member.member;
2912 Type type = memberExp.expType;
2913 Class _class = (type && memberID) ? (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null) : null;
2914 if(_class && memberID && memberID.string)
2915 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\" in class \"%s\"", memberID.string, _class.name);
2917 snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation for \"%s\"", wh.expression);
2920 case functionCallErrorExp:
2921 if(exp.call.exp && exp.call.exp.type == identifierExp && exp.call.exp.identifier.string)
2922 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for call to \"%s\"", exp.call.exp.identifier.string);
2924 snprintf(watchmsg, sizeof(watchmsg), $"Missing function evaluation for \"%s\"", wh.expression);
2926 case memoryErrorExp:
2927 // Need to ensure when set to memoryErrorExp, constant is set
2928 snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
2930 case dereferenceErrorExp:
2931 snprintf(watchmsg, sizeof(watchmsg), $"Dereferencing error evaluating \"%s\"", wh.expression);
2933 case divideBy0ErrorExp:
2934 snprintf(watchmsg, sizeof(watchmsg), "%s", $"Integer division by 0");
2936 case noDebuggerErrorExp:
2937 snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
2939 case unknownErrorExp:
2940 // NOTE: This should never happen
2941 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
2944 // NOTE: This should never happen
2945 snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
2948 wh.value = CopyString(exp.string);
2951 // Temporary Code for displaying Strings
2952 if((exp.expType && ((exp.expType.kind == pointerType ||
2953 exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) ||
2954 (wh.type && wh.type.kind == classType && wh.type._class &&
2955 wh.type._class.registered && wh.type._class.registered.type == normalClass &&
2956 !strcmp(wh.type._class.registered.name, "String")))
2959 if(exp.expType.kind != arrayType || exp.hasAddress)
2963 //char temp[MAX_F_STRING * 32];
2965 //ExpressionType evalError = dummyExp;
2966 /*if(exp.expType.kind == arrayType)
2967 sprintf(temp, "(char*)0x%x", exp.address);
2969 sprintf(temp, "(char*)%s", exp.constant);*/
2971 //evaluation = Debugger::EvaluateExpression(temp, &evalError);
2972 // address = strtoul(exp.constant, null, 0);
2973 address = _strtoui64(exp.constant, null, 0);
2974 //_dplf(0, "0x", address);
2975 // snprintf(value, sizeof(value), "0x%08x ", address);
2977 if(address > 0xFFFFFFFFLL)
2978 snprintf(value, sizeof(value), (__runtimePlatform == win32) ? "0x%016I64x " : "0x%016llx ", address);
2980 snprintf(value, sizeof(value), (__runtimePlatform == win32) ? "0x%08I64x " : "0x%08llx ", address);
2981 value[sizeof(value)-1] = 0;
2984 strcat(value, $"Null string");
2987 String string = new char[4097];
2989 bool success = false;
2993 for(start = 0; !done && start + size <= 4096; start += size)
2998 // Try to read 256 bytes at a time, then half if that fails
2999 s = GdbReadMemory(address + start, size);
3003 memcpy(string + start, s, size);
3004 string[start + size] = 0;
3005 if(size == 1 || memchr(s, 0, size))
3019 int len = strlen(value);
3021 if(UTF8Validate(string))
3026 for(c = 0; (ch = string[c]); c++)
3029 value[len++] = '\0';
3034 ISO8859_1toUTF8(string, value + len, strlen(value) - len - 30);
3035 strcat(value, ") (ISO8859-1)");
3039 strcat(value, $"Empty string");
3042 strcat(value, $"Couldn't read memory");
3045 wh.value = CopyString(value);
3048 else if(wh.type && wh.type.kind == classType && wh.type._class &&
3049 wh.type._class.registered && wh.type._class.registered.type == enumClass)
3051 Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
3052 EnumClassData enumeration = (EnumClassData)enumClass.data;
3056 if(!strcmp(enumClass.dataTypeString, "uint64"))
3058 uint64 v = strtoull(exp.constant, null, 0);
3059 value = *(int64*)&v;
3062 value = strtoll(exp.constant, null, 0);
3064 for(item = enumeration.values.first; item; item = item.next)
3065 if(item.data == value)
3068 wh.value = CopyString(item.name);
3070 wh.value = PrintString($"Invalid Enum Value (", value, ")");
3073 else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class &&
3074 wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "unichar"))) )
3081 if(exp.constant[0] == '\'')
3083 if((int)((byte *)exp.constant)[1] > 127)
3086 value = UTF8GetChar(exp.constant + 1, &nb);
3087 if(nb < 2) value = exp.constant[1];
3088 signedValue = value;
3092 signedValue = exp.constant[1];
3094 // Precomp Syntax error with boot strap here:
3095 byte b = (byte)(char)signedValue;
3096 value = (unichar) b;
3102 if(wh.type.kind == charType && wh.type.isSigned)
3104 signedValue = (int)(char)strtol(exp.constant, null, 0);
3106 // Precomp Syntax error with boot strap here:
3107 byte b = (byte)(char)signedValue;
3108 value = (unichar) b;
3113 value = (uint)strtoul(exp.constant, null, 0);
3114 signedValue = (int)value;
3118 UTF32toUTF8Len(&value, 1, charString, 5);
3120 snprintf(string, sizeof(string), "\'\\0' (0)");
3121 else if(value == '\t')
3122 snprintf(string, sizeof(string), "\'\\t' (%d)", value);
3123 else if(value == '\n')
3124 snprintf(string, sizeof(string), "\'\\n' (%d)", value);
3125 else if(value == '\r')
3126 snprintf(string, sizeof(string), "\'\\r' (%d)", value);
3127 else if(wh.type.kind == charType && wh.type.isSigned)
3128 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
3129 else if(value > 256 || wh.type.kind != charType)
3131 if(value > 0x10FFFF || !GetCharCategory(value))
3132 snprintf(string, sizeof(string), $"Invalid Unicode Codepoint (0x%08X)", value);
3134 snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3137 snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3138 string[sizeof(string)-1] = 0;
3140 wh.value = CopyString(string);
3145 wh.value = CopyString(exp.constant);
3151 bool genericError = true;
3152 if(exp.expType && exp.hasAddress)
3154 bool showAddress = false;
3155 switch(exp.expType.kind)
3167 Symbol s = exp.expType._class;
3170 Class c = s.registered;
3171 if(c.type == noHeadClass || c.type == normalClass)
3179 wh.value = PrintHexUInt64(exp.address);
3181 genericError = false;
3185 // NOTE: This should ideally be handled with a specific error message
3186 snprintf(watchmsg, sizeof(watchmsg), $"Error evaluating \"%s\"", wh.expression);
3191 SetInDebugger(false);
3194 snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3195 if(exp) FreeExpression(exp);
3198 SetPrivateModule(backupPrivateModule);
3199 SetCurrentContext(backupContext);
3200 SetTopContext(backupContext);
3201 SetGlobalContext(backupContext);
3202 SetThisClass(backupThisClass);
3205 // wh.value = CopyString("No source file found for selected frame");
3207 watchmsg[sizeof(watchmsg)-1] = 0;
3209 wh.value = CopyString(watchmsg);
3211 ide.watchesView.UpdateWatch(wh);
3215 void EvaluateWatches()
3217 _dpcl(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3218 WatchesLinkCodeEditor();
3219 if(state == stopped)
3221 for(wh : ide.workspace.watches)
3226 char * ::GdbEvaluateExpression(char * expression)
3228 _dpcl(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3231 GdbCommand(0, false, "-data-evaluate-expression \"%s\"", expression);
3233 ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3237 // to be removed... use GdbReadMemory that returns a byte array instead
3238 char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3240 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3245 _dplf(0, "GdbReadMemoryString called with size = 0!");
3247 GdbCommand(0, false,
3248 (__runtimePlatform == win32) ? "-data-read-memory 0x%016I64x %c, %d, %d, %d" : "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3250 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3254 byte * ::GdbReadMemory(uint64 address, int bytes)
3256 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3259 GdbCommand(0, false,
3260 (__runtimePlatform == win32) ? "-data-read-memory 0x%016I64x %c, 1, 1, %d" : "-data-read-memory 0x%016llx %c, 1, 1, %d",
3261 address, 'u', bytes);
3264 _dplf(0, "GdbReadMemory called with bytes = 0!");
3267 ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3268 else if(eval.result && strcmp(eval.result, "N/A"))
3270 byte * result = new byte[bytes];
3271 char * string = eval.result;
3275 result[c++] = (byte)strtol(string, &string, 10);
3292 bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3295 char * s1 = null; char * s2 = null;
3296 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3297 "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3298 "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3299 "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3300 "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor, ")");
3301 delete s1; delete s2;
3305 bool conditionMet = true;
3306 if(bpUser.condition)
3308 if(WatchesLinkCodeEditor())
3309 conditionMet = ResolveWatch(bpUser.condition);
3311 conditionMet = false;
3326 if(stopItem.frame.line && bpUser.line != stopItem.frame.line)
3328 // updating user breakpoint on hit location difference
3329 // todo, print something?
3330 bpUser.line = stopItem.frame.line;
3331 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3332 ide.workspace.Save();
3335 ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3340 if(bpInternal.type == internalModulesLoaded)
3342 if(userAction == stepOver)
3344 if((bpInternal.type == internalEntry && ((intBpMain && intBpMain.inserted) || (intBpWinMain && intBpWinMain.inserted))) ||
3345 (bpInternal.type == internalMain && intBpWinMain && intBpWinMain.inserted))
3348 if(!bpUser && !userAction.breaksOnInternalBreakpoint)
3350 if(userAction == stepOut)
3351 StepOut(ignoreBreakpoints);
3357 if(!bpUser && !bpInternal)
3363 void ValgrindTargetThreadExit()
3365 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ValgrindTargetThreadExit()");
3368 vgTargetHandle.Wait();
3369 delete vgTargetHandle;
3371 HandleExit(null, null);
3374 void GdbThreadExit()
3376 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3377 if(state != terminated)
3379 _ChangeState(terminated);
3380 targetProcessId = 0;
3381 ClearBreakDisplay();
3386 serialSemaphore.Release();
3391 ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3392 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3394 HideDebuggerViews();
3396 //_ChangeState(terminated);
3400 void GdbThreadMain(char * output)
3404 Array<char *> outTokens { minAllocSize = 50 };
3405 Array<char *> subTokens { minAllocSize = 50 };
3406 DebugListItem item { };
3407 DebugListItem item2 { };
3408 bool setWaitingForPID = false;
3410 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3411 #ifdef GDB_DEBUG_CONSOLE
3412 // _dpcl(_dpct, dplchan::gdbOutput, 0, output);
3415 #ifdef GDB_DEBUG_OUTPUT
3417 int len = strlen(output);
3425 for(c = 0; c < len / 1024; c++)
3427 strncpy(tmp, start, 1024);
3428 ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3431 ide.outputView.gdbBox.Logf("out: %s\n", start);
3435 ide.outputView.gdbBox.Logf("out: %s\n", output);
3439 #ifdef GDB_DEBUG_CONSOLE
3440 strcpy(lastGdbOutput, output);
3442 #ifdef GDB_DEBUG_GUI
3443 if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3450 if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3453 ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3456 if(!entryPoint && (t = strstr(output, "Entry point:")))
3458 char * addr = t + strlen("Entry point:");
3460 if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3463 while(isxdigit(*++t));
3465 for(bp : sysBPs; bp.type == internalEntry)
3468 bp.enabled = entryPoint = true;
3476 if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3478 //if(outTokens.count == 1)
3483 _ChangeState(loaded);
3484 targetProcessId = 0;
3485 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3487 if(!strcmp(item.name, "reason"))
3489 char * reason = item.value;
3490 StripQuotes(reason, reason);
3491 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3494 if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3496 StripQuotes(item2.value, item2.value);
3497 if(!strcmp(item2.name, "exit-code"))
3498 exitCode = item2.value;
3504 HandleExit(reason, exitCode);
3508 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3511 HandleExit(null, null);
3514 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3516 if(!strcmp(item.name, "bkpt"))
3518 sentBreakInsert = false;
3521 _dplf(0, "problem");
3524 bpItem = ParseBreakpoint(item.value, outTokens);
3525 //breakType = bpValidation;
3527 else if(!strcmp(item.name, "depth"))
3529 StripQuotes(item.value, item.value);
3530 frameCount = atoi(item.value);
3532 stackFrames.Free(Frame::Free);
3534 else if(!strcmp(item.name, "stack"))
3537 if(stackFrames.count)
3538 ide.callStackView.Logf("...\n");
3541 item.value = StripBrackets(item.value);
3542 TokenizeList(item.value, ',', subTokens);
3543 for(i = 0; i < subTokens.count; i++)
3545 if(TokenizeListItem(subTokens[i], item))
3547 if(!strcmp(item.name, "frame"))
3550 stackFrames.Add(frame);
3551 item.value = StripCurlies(item.value);
3552 ParseFrame(frame, item.value);
3553 if(frame.file && frame.from)
3554 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3558 if(activeFrameLevel == -1)
3560 if(ide.projectView.IsModuleInProject(frame.file));
3562 if(frame.level != 0)
3564 //stopItem.frame = frame;
3565 breakType = selectFrame;
3568 activeFrame = frame;
3569 activeFrameLevel = frame.level;
3572 ide.callStackView.Logf("%3d ", frame.level);
3573 if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3574 ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3575 else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3576 ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3577 else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3578 ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3579 else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3580 ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3582 ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3587 ide.callStackView.Logf("%3d ", frame.level);
3592 ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3596 ide.callStackView.Logf("%s\n", frame.func);
3598 ide.callStackView.Logf($"unknown source\n");
3602 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3605 if(activeFrameLevel == -1)
3607 activeFrameLevel = 0;
3608 activeFrame = stackFrames.first;
3610 ide.callStackView.Home();
3612 subTokens.RemoveAll();
3614 /*else if(!strcmp(item.name, "frame"))
3617 item.value = StripCurlies(item.value);
3618 ParseFrame(&frame, item.value);
3620 else if(!strcmp(item.name, "thread-ids"))
3622 ide.threadsView.Clear();
3623 item.value = StripCurlies(item.value);
3624 TokenizeList(item.value, ',', subTokens);
3625 for(i = subTokens.count - 1; ; i--)
3627 if(TokenizeListItem(subTokens[i], item))
3629 if(!strcmp(item.name, "thread-id"))
3632 StripQuotes(item.value, item.value);
3633 value = atoi(item.value);
3634 ide.threadsView.Logf("%3d \n", value);
3637 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3642 ide.threadsView.Home();
3644 subTokens.RemoveAll();
3645 //if(!strcmp(outTokens[2], "number-of-threads"))
3647 else if(!strcmp(item.name, "new-thread-id"))
3649 StripQuotes(item.value, item.value);
3650 activeThread = atoi(item.value);
3652 else if(!strcmp(item.name, "value"))
3654 StripQuotes(item.value, item.value);
3655 eval.result = CopyString(item.value);
3656 eval.active = false;
3658 else if(!strcmp(item.name, "addr"))
3660 for(i = 2; i < outTokens.count; i++)
3662 if(TokenizeListItem(outTokens[i], item))
3664 if(!strcmp(item.name, "total-bytes"))
3666 StripQuotes(item.value, item.value);
3667 eval.bytes = atoi(item.value);
3669 else if(!strcmp(item.name, "next-row"))
3671 StripQuotes(item.value, item.value);
3672 eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3674 else if(!strcmp(item.name, "memory"))
3678 //StripQuotes(item.value, item.value);
3679 item.value = StripBrackets(item.value);
3680 // this should be treated as a list...
3681 item.value = StripCurlies(item.value);
3682 TokenizeList(item.value, ',', subTokens);
3683 for(j = 0; j < subTokens.count; j++)
3685 if(TokenizeListItem(subTokens[j], item))
3687 if(!strcmp(item.name, "data"))
3689 item.value = StripBrackets(item.value);
3690 StripQuotes2(item.value, item.value);
3691 eval.result = CopyString(item.value);
3692 eval.active = false;
3696 subTokens.RemoveAll();
3701 else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3702 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3704 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3707 else if(!strcmp(outTokens[0], "^running"))
3709 waitingForPID = true;
3710 setWaitingForPID = true;
3711 ClearBreakDisplay();
3713 else if(!strcmp(outTokens[0], "^exit"))
3715 _ChangeState(terminated);
3716 // ide.outputView.debugBox.Logf("Exit\n");
3717 // ide.Update(null);
3719 serialSemaphore.Release();
3721 else if(!strcmp(outTokens[0], "^error"))
3725 sentBreakInsert = false;
3726 breakpointError = true;
3729 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3731 if(!strcmp(item.name, "msg"))
3733 StripQuotes(item.value, item.value);
3736 eval.active = false;
3738 if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3739 eval.error = symbolNotFound;
3740 else if(strstr(item.value, "Cannot access memory at address"))
3741 eval.error = memoryCantBeRead;
3743 eval.error = unknown;
3745 else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3748 else if(!strncmp(item.value, "Cannot access memory at address", 31))
3751 else if(!strcmp(item.value, "Cannot find bounds of current function"))
3753 _ChangeState(stopped);
3754 gdbHandle.Printf("-exec-continue\n");
3756 else if(!strcmp(item.value, "ptrace: No such process."))
3758 _ChangeState(loaded);
3759 targetProcessId = 0;
3761 else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3764 else if(!strcmp(item.value, "You can't do that without a process to debug."))
3766 _ChangeState(loaded);
3767 targetProcessId = 0;
3769 else if(strstr(item.value, "No such file or directory."))
3771 _ChangeState(loaded);
3772 targetProcessId = 0;
3774 else if(strstr(item.value, "During startup program exited with code "))
3776 _ChangeState(loaded);
3777 targetProcessId = 0;
3782 if(strlen(item.value) < MAX_F_STRING)
3785 ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3789 ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3795 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3798 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3799 outTokens.RemoveAll();
3802 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3805 if(TokenizeList(output, ',', outTokens))
3807 if(!strcmp(outTokens[0], "=library-loaded"))
3808 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, false);
3809 else if(!strcmp(outTokens[0], "=shlibs-added"))
3810 FGODetectLoadedLibraryForAddedProjectIssues(outTokens, true);
3811 else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3812 !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3813 !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3814 !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3815 !strcmp(outTokens[0], "=breakpoint-modified"))
3816 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3817 outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3818 outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3819 outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3820 outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3822 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3824 outTokens.RemoveAll();
3828 if(TokenizeList(output, ',', outTokens))
3830 if(!strcmp(outTokens[0],"*running"))
3832 waitingForPID = true;
3833 setWaitingForPID = true;
3835 else if(!strcmp(outTokens[0], "*stopped"))
3838 _ChangeState(stopped);
3840 for(tk = 1; tk < outTokens.count; tk++)
3842 if(TokenizeListItem(outTokens[tk], item))
3844 if(!strcmp(item.name, "reason"))
3846 char * reason = item.value;
3847 StripQuotes(reason, reason);
3848 if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3851 if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3854 StripQuotes(item2.value, item2.value);
3855 if(!strcmp(item2.name, "exit-code"))
3856 exitCode = item2.value;
3862 HandleExit(reason, exitCode);
3865 else if(!strcmp(reason, "breakpoint-hit") ||
3866 !strcmp(reason, "function-finished") ||
3867 !strcmp(reason, "end-stepping-range") ||
3868 !strcmp(reason, "location-reached") ||
3869 !strcmp(reason, "signal-received"))
3873 if(stopItem) _dplf(0, "problem");
3875 stopItem = GdbDataStop { };
3876 stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3878 for(i = tk+1; i < outTokens.count; i++)
3880 TokenizeListItem(outTokens[i], item);
3881 StripQuotes(item.value, item.value);
3882 if(!strcmp(item.name, "thread-id"))
3883 stopItem.threadid = atoi(item.value);
3884 else if(!strcmp(item.name, "frame"))
3886 item.value = StripCurlies(item.value);
3887 ParseFrame(stopItem.frame, item.value);
3889 else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3890 stopItem.bkptno = atoi(item.value);
3891 else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3892 stopItem.gdbResultVar = CopyString(item.value);
3893 else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3894 stopItem.returnValue = CopyString(item.value);
3895 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3896 stopItem.name = CopyString(item.value);
3897 else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3898 stopItem.meaning = CopyString(item.value);
3899 else if(!strcmp(item.name, "stopped-threads"))
3900 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3901 else if(!strcmp(item.name, "core"))
3902 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3903 else if(!strcmp(item.name, "disp"))
3904 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3906 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3909 if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
3925 event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
3929 else if(!strcmp(reason, "watchpoint-trigger"))
3930 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3931 else if(!strcmp(reason, "read-watchpoint-trigger"))
3932 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3933 else if(!strcmp(reason, "access-watchpoint-trigger"))
3934 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3935 else if(!strcmp(reason, "watchpoint-scope"))
3936 _dpcl(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3938 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3946 if(usingValgrind && event == none && !stopItem)
3947 event = valgrindStartPause;
3952 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3953 outTokens.RemoveAll();
3956 if(!strcmpi(output, "(gdb) "))
3960 Time startTime = GetTime();
3961 char exeFile[MAX_LOCATION];
3962 int oldProcessID = targetProcessId;
3963 GetLastDirectory(targetFile, exeFile);
3965 while(!targetProcessId/*true*/)
3967 targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3968 if(targetProcessId) break;
3969 // Can't break on Peek(), GDB is giving us =library and other info before the process is listed in /proc
3970 // if(gdbHandle.Peek()) break;
3972 if(gdbHandle.Peek() && GetTime() - startTime > 2.5) // Give the process 2.5 seconds to show up in /proc
3977 _ChangeState(running);
3978 else if(!oldProcessID)
3980 ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3981 // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3982 gdbHandle.Printf("-gdb-exit\n");
3984 _ChangeState(terminated); //loaded;
3989 for(bp : ide.workspace.breakpoints)
3990 bp.inserted = false;
3993 bp.inserted = false;
3995 bpRunToCursor.inserted = false;
3997 ide.outputView.debugBox.Logf($"Debugging stopped\n");
3998 ClearBreakDisplay();
4000 #if defined(__unix__)
4001 if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
4003 progThread.terminate = true;
4006 fifoFile.CloseInput();
4013 DeleteFile(progFifoPath);
4014 progFifoPath[0] = '\0';
4021 serialSemaphore.Release();
4024 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
4028 if(!strncmp(output, "&\"warning:", 10))
4031 content = strstr(output, "\"");
4032 StripQuotes(content, content);
4033 content = strstr(content, ":");
4039 ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
4046 _dpcl(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
4048 if(!setWaitingForPID)
4049 waitingForPID = false;
4050 setWaitingForPID = false;
4058 // From GDB Output functions
4059 void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens, bool shlibs)
4061 char path[MAX_LOCATION] = "";
4062 char file[MAX_FILENAME] = "";
4063 //bool symbolsLoaded = false;
4064 DebugListItem item { };
4065 //_dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
4066 for(token : outTokens)
4068 if(TokenizeListItem(token, item))
4070 if(!strcmp(item.name, "target-name"))
4072 StripQuotes(item.value, path);
4073 MakeSystemPath(path);
4074 GetLastDirectory(path, file);
4076 else if(!strcmp(item.name, "symbols-loaded"))
4078 //symbolsLoaded = (atoi(item.value) == 1);
4080 else if(!strcmp(item.name, "shlib-info"))
4082 DebugListItem subItem { };
4083 Array<char *> tokens { minAllocSize = 50 };
4084 item.value = StripBrackets(item.value);
4085 TokenizeList(item.value, ',', tokens);
4088 if(TokenizeListItem(t, subItem))
4090 if(!strcmp(subItem.name, "path"))
4092 StripQuotes(subItem.value, path);
4093 MakeSystemPath(path);
4094 GetLastDirectory(path, file);
4095 //symbolsLoaded = true;
4106 if(path[0] && file[0])
4108 for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
4112 char prjTargetPath[MAX_LOCATION];
4113 char prjTargetFile[MAX_FILENAME];
4114 DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
4115 strcpy(prjTargetPath, prj.topNode.path);
4116 PathCat(prjTargetPath, targetDirExp.dir);
4117 delete targetDirExp;
4118 prjTargetFile[0] = '\0';
4119 prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
4120 PathCat(prjTargetPath, prjTargetFile);
4121 MakeSystemPath(prjTargetPath);
4123 match = !fstrcmp(prjTargetFile, file);
4124 if(!match && (dot = strstr(prjTargetFile, ".so.")))
4126 char * dot3 = strstr(dot+4, ".");
4130 match = !fstrcmp(prjTargetFile, file);
4135 match = !fstrcmp(prjTargetFile, file);
4140 // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
4141 /* -- 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)
4143 ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
4145 match = !fstrcmp(prjTargetPath, path);
4146 if(!match && (dot = strstr(prjTargetPath, ".so.")))
4148 char * dot3 = strstr(dot+4, ".");
4152 match = !fstrcmp(prjTargetPath, path);
4157 match = !fstrcmp(prjTargetPath, path);
4161 projectsLibraryLoaded[prj.name] = true;
4163 ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4170 void FGOBreakpointModified(Array<char *> outTokens)
4172 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4174 DebugListItem item { };
4175 if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4177 if(!strcmp(item.name, "bkpt"))
4179 GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4187 ExpressionType ::DebugEvalExpTypeError(char * result)
4189 _dpcl(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4194 case symbolNotFound:
4195 return symbolErrorExp;
4196 case memoryCantBeRead:
4197 return memoryErrorExp;
4199 return unknownErrorExp;
4202 char * ::EvaluateExpression(char * expression, ExpressionType * error)
4205 _dpcl(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4206 if(ide.projectView && ide.debugger.state == stopped)
4208 result = GdbEvaluateExpression(expression);
4209 *error = DebugEvalExpTypeError(result);
4214 *error = noDebuggerErrorExp;
4219 char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4222 char * result = GdbReadMemoryString(address, size, format, 1, 1);
4223 _dpcl(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4224 if(!result || !strcmp(result, "N/A"))
4225 *error = memoryErrorExp;
4227 *error = DebugEvalExpTypeError(result);
4232 class ValgrindLogThread : Thread
4238 static char output[4096];
4239 bool lastLineEmpty = true;
4240 Array<char> dynamicBuffer { minAllocSize = 4096 };
4241 File oldValgrindHandle = vgLogFile;
4242 incref oldValgrindHandle;
4245 while(debugger.state != terminated && vgLogFile && vgLogFile.input)
4250 result = vgLogFile.Read(output, 1, sizeof(output));
4252 if(debugger.state == terminated || !vgLogFile) // || vgLogFile.Eof()
4259 for(c = 0; c<result; c++)
4261 if(output[c] == '\n')
4263 int pos = dynamicBuffer.size;
4264 dynamicBuffer.size += c - start;
4265 memcpy(&dynamicBuffer[pos], output + start, c - start);
4266 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4267 dynamicBuffer.size++;
4268 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4270 // printf("%s\n", dynamicBuffer.array);
4272 if(strstr(&dynamicBuffer[0], "vgdb me ..."))
4273 debugger.serialSemaphore.Release();
4275 char * s = strstr(&dynamicBuffer[0], "==");
4277 s = strstr(s+2, "== ");
4281 if(s[0] == '\0' && !lastLineEmpty)
4284 lastLineEmpty = true;
4285 dynamicBuffer[0] = '\0';
4294 if(strstr(s, "vgdb me ..."))
4296 if(strstr(s, "(action on error) vgdb me ..."))
4297 ide.outputView.debugBox.Logf($"...breaked on Valgrind error (F5 to resume)\n");
4304 if(strstr(s, "TO DEBUG THIS PROCESS USING GDB: start GDB like this"))
4310 if(strstr(s, "and then give GDB the following command"))
4316 if(strstr(s, "/path/to/gdb") || strstr(s, "target remote | /usr/lib/valgrind/../../bin/vgdb --pid="))
4322 if(strstr(s, "--pid is optional if only one valgrind process is running"))
4328 if((s = strstr(s, "; rerun with -h for copyright info")))
4340 if(lastLineEmpty && t[0] != '\0')
4341 lastLineEmpty = false;
4344 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4346 dynamicBuffer.size = 0;
4352 int pos = dynamicBuffer.size;
4353 dynamicBuffer.size += c - start;
4354 memcpy(&dynamicBuffer[pos], output + start, c - start);
4357 else if(debugger.state == stopped)
4360 printf("Got end of file from GDB!\n");
4367 delete dynamicBuffer;
4368 _dpcl(_dpct, dplchan::debuggerCall, 0, "ValgrindLogThreadExit");
4369 //if(oldValgrindHandle == vgLogFile)
4370 debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4371 delete oldValgrindHandle;
4377 class ValgrindTargetThread : Thread
4383 static char output[4096];
4384 Array<char> dynamicBuffer { minAllocSize = 4096 };
4385 DualPipe oldValgrindHandle = vgTargetHandle;
4386 incref oldValgrindHandle;
4389 while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4393 result = vgTargetHandle.Read(output, 1, sizeof(output));
4395 if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4402 for(c = 0; c<result; c++)
4404 if(output[c] == '\n')
4406 int pos = dynamicBuffer.size;
4407 dynamicBuffer.size += c - start;
4408 memcpy(&dynamicBuffer[pos], output + start, c - start);
4409 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4410 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4411 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4412 dynamicBuffer.size++;
4413 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4415 // printf("%s\n", dynamicBuffer.array);
4417 ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4419 dynamicBuffer.size = 0;
4425 int pos = dynamicBuffer.size;
4426 dynamicBuffer.size += c - start;
4427 memcpy(&dynamicBuffer[pos], output + start, c - start);
4433 printf("Got end of file from GDB!\n");
4437 delete dynamicBuffer;
4438 //if(oldValgrindHandle == vgTargetHandle)
4439 debugger.ValgrindTargetThreadExit();
4440 delete oldValgrindHandle;
4446 class GdbThread : Thread
4452 static char output[4096];
4453 Array<char> dynamicBuffer { minAllocSize = 4096 };
4454 DualPipe oldGdbHandle = gdbHandle;
4455 incref oldGdbHandle;
4458 while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4462 result = gdbHandle.Read(output, 1, sizeof(output));
4464 if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4471 for(c = 0; c<result; c++)
4473 if(output[c] == '\n')
4475 int pos = dynamicBuffer.size;
4476 dynamicBuffer.size += c - start;
4477 memcpy(&dynamicBuffer[pos], output + start, c - start);
4478 if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4479 // COMMENTED OUT DUE TO ISSUE #135, FIXED
4480 //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4481 dynamicBuffer.size++;
4482 dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4484 // _dplf(0, dynamicBuffer.array);
4486 debugger.GdbThreadMain(&dynamicBuffer[0]);
4487 dynamicBuffer.size = 0;
4493 int pos = dynamicBuffer.size;
4494 dynamicBuffer.size += c - start;
4495 memcpy(&dynamicBuffer[pos], output + start, c - start);
4501 _dplf(0, "Got end of file from GDB!");
4505 delete dynamicBuffer;
4506 //if(oldGdbHandle == gdbHandle)
4507 debugger.GdbThreadExit();
4508 delete oldGdbHandle;
4514 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4515 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4517 #if defined(__unix__)
4522 #include <sys/types.h>
4527 class ProgramThread : Thread
4532 //bool result = true;
4533 //bool fileCreated = false;
4534 //mode_t mask = 0600;
4535 static char output[1000];
4538 /*if(!mkfifo(progFifoPath, mask))
4545 ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4549 if(FileExists(progFifoPath)) //fileCreated)
4551 fifoFile = FileOpen(progFifoPath, read);
4555 ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4560 fd = fileno((FILE *)fifoFile.input);
4561 //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4565 while(!terminate && fifoFile && !fifoFile.Eof())
4568 struct timeval time;
4576 /*selectResult = */select(fd + 1, &rs, null, null, &time);
4577 if(FD_ISSET(fd, &rs))
4579 int result = (int)read(fd, output, sizeof(output)-1);
4580 if(!result || (result < 0 && errno != EAGAIN))
4584 output[result] = '\0';
4585 if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4588 ide.outputView.debugBox.Log(output);
4597 //fifoFile.CloseInput();
4600 ide.outputView.debugBox.Log("\n");
4604 if(FileExists(progFifoPath)) //fileCreated)
4606 DeleteFile(progFifoPath);
4607 progFifoPath[0] = '\0';
4615 class Argument : struct
4617 Argument prev, next;
4619 property char * name { set { delete name; if(value) name = CopyString(value); } }
4621 property char * val { set { delete val; if(value) val = CopyString(value); } }
4635 class Frame : struct
4640 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4642 property char * func { set { delete func; if(value) func = CopyString(value); } }
4646 property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4648 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4649 char * absoluteFile;
4650 property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4659 delete absoluteFile;
4660 args.Free(Argument::Free);
4669 class GdbDataStop : struct
4671 DebuggerReason reason;
4686 char * gdbResultVar;
4696 if(reason == signalReceived)
4701 else if(reason == functionFinished)
4703 delete gdbResultVar;
4707 if(frame) frame.Free();
4716 class GdbDataBreakpoint : struct
4720 property char * number { set { delete number; if(value) number = CopyString(value); } }
4722 property char * type { set { delete type; if(value) type = CopyString(value); } }
4724 property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4727 property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4729 property char * func { set { delete func; if(value) func = CopyString(value); } }
4731 property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4733 property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4736 property char * at { set { delete at; if(value) at = CopyString(value); } }
4739 Array<GdbDataBreakpoint> multipleBPs;
4744 PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4755 if(multipleBPs) multipleBPs.Free();
4761 ~GdbDataBreakpoint()
4767 class Breakpoint : struct
4772 property const char * function { set { delete function; if(value) function = CopyString(value); } }
4773 char * relativeFilePath;
4774 property const char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4775 char * absoluteFilePath;
4776 property const char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4778 property const char * location { set { delete location; if(value) location = CopyString(value); } }
4787 BreakpointType type;
4789 GdbDataBreakpoint bp;
4792 property const char * address { set { delete address; if(value) address = CopyString(value); } }
4794 void ParseLocation()
4796 char * prjName = null;
4797 char * filePath = null;
4800 char fullPath[MAX_LOCATION];
4801 if(location[0] == '(' && location[1] && (file = strchr(location+2, ')')) && file[1])
4803 prjName = new char[file-location];
4804 strncpy(prjName, location+1, file-location-1);
4805 prjName[file-location-1] = '\0';
4810 if((line = strchr(file+1, ':')))
4812 filePath = new char[strlen(file)+1];
4813 strncpy(filePath, file, line-file);
4814 filePath[line-file] = '\0';
4818 filePath = CopyString(file);
4819 property::relativeFilePath = filePath;
4822 for(prj : ide.workspace.projects)
4824 if(!strcmp(prjName, prj.name))
4826 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4828 property::absoluteFilePath = fullPath;
4835 this.line = atoi(line);
4839 Project prj = ide.project;
4840 if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4842 property::absoluteFilePath = fullPath;
4846 if(!absoluteFilePath)
4847 property::absoluteFilePath = "";
4852 char * CopyLocationString(bool removePath)
4855 char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4856 bool removingPath = removePath && file;
4859 char * fileName = new char[MAX_FILENAME];
4860 GetLastDirectory(file, fileName);
4866 location = PrintString(file, ":", function);
4868 location = CopyString(function);
4871 location = PrintString(file, ":", line);
4877 char * CopyUserLocationString()
4880 char * loc = CopyLocationString(false);
4882 if(absoluteFilePath)
4884 for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4886 if(p.topNode.FindByFullPath(absoluteFilePath, false))
4895 location = PrintString("(", prj.name, ")", loc);
4905 if(relativeFilePath && relativeFilePath[0])
4907 char * location = CopyUserLocationString();
4908 f.Printf(" * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4911 f.Printf(" ~ %s\n", condition.expression);
4919 delete relativeFilePath;
4920 delete absoluteFilePath;
4940 class Watch : struct
4951 f.Printf(" ~ %s\n", expression);
4975 class DebugListItem : struct
4981 struct DebugEvaluationData
4986 uint64 nextBlockAddress;
4988 DebuggerEvaluationError error;
4991 class CodeLocation : struct
4994 char * absoluteFile;
4997 CodeLocation ::ParseCodeLocation(char * location)
5001 char * colon = null;
5003 char loc[MAX_LOCATION];
5004 strcpy(loc, location);
5005 for(temp = loc; (temp = strstr(temp, ":")); temp++)
5013 int line = atoi(colon);
5016 CodeLocation codloc { line = line };
5017 codloc.file = CopyString(loc);
5018 codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
5030 delete absoluteFile;
5039 void GDBFallBack(Expression exp, String expString)
5042 ExpressionType evalError = dummyExp;
5043 result = Debugger::EvaluateExpression(expString, &evalError);
5046 FreeExpContents(exp);
5047 exp.constant = result;
5048 exp.type = constantExp;
5052 static Project WorkspaceGetFileOwner(const char * absolutePath)
5054 Project owner = null;
5055 for(prj : ide.workspace.projects)
5057 if(prj.topNode.FindByFullPath(absolutePath, false))
5064 WorkspaceGetObjectFileNode(absolutePath, &owner);
5068 static ProjectNode WorkspaceGetObjectFileNode(const char * filePath, Project * project)
5070 ProjectNode node = null;
5071 char ext[MAX_EXTENSION];
5072 GetExtension(filePath, ext);
5075 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5078 char fileName[MAX_FILENAME];
5079 GetLastDirectory(filePath, fileName);
5082 DotMain dotMain = DotMain::FromFileName(fileName);
5083 for(prj : ide.workspace.projects)
5085 if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
5098 static ProjectNode ProjectGetObjectFileNode(Project project, const char * filePath)
5100 ProjectNode node = null;
5101 char ext[MAX_EXTENSION];
5102 GetExtension(filePath, ext);
5105 IntermediateFileType type = IntermediateFileType::FromExtension(ext);
5108 char fileName[MAX_FILENAME];
5109 GetLastDirectory(filePath, fileName);
5112 DotMain dotMain = DotMain::FromFileName(fileName);
5113 node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
5120 static void WorkspaceGetRelativePath(const char * absolutePath, char * relativePath, Project * owner)
5122 Project prj = WorkspaceGetFileOwner(absolutePath);
5126 prj = ide.workspace.projects.firstIterator.data;
5129 MakePathRelative(absolutePath, prj.topNode.path, relativePath);
5130 MakeSlashPath(relativePath);
5133 relativePath[0] = '\0';
5136 static bool ProjectGetAbsoluteFromRelativePath(Project project, const char * relativePath, char * absolutePath)
5138 ProjectNode node = project.topNode.FindWithPath(relativePath, false);
5140 node = ProjectGetObjectFileNode(project, relativePath);
5143 strcpy(absolutePath, node.project.topNode.path);
5144 PathCat(absolutePath, relativePath);
5145 MakeSlashPath(absolutePath);
5147 return node != null;