ide;debugger; (#995) fixed run to cursor at same level skipping breakpoints.
[sdk] / ide / src / debugger / Debugger.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 public import static "ec"
4 #else
5 public import "ecere"
6 public import "ec"
7 #endif
8
9 import "ide"
10 import "process"
11 import "debugFindCtx"
12 import "debugTools"
13
14 #ifdef _DEBUG
15 #define GDB_DEBUG_CONSOLE
16 #define _DEBUG_INST
17 #endif
18
19 extern char * strrchr(const char * s, int c);
20
21 #define uint _uint
22 #define strlen _strlen
23 #include <stdarg.h>
24 #include <unistd.h>
25
26 #ifdef __APPLE__
27 #define __unix__
28 #endif
29
30 #if defined(__unix__)
31 #include <sys/stat.h>
32 #include <sys/time.h> // Required on Apple...
33 #endif
34 #undef uint
35 #undef strlen
36
37 char * PrintNow()
38 {
39    int c;
40    char * s[6];
41    char * time;
42    DateTime now;
43    now.GetLocalTime();
44    for(c=0; c<6; c++)
45       s[c] = new char[8];
46    sprintf(s[0], "%04d", now.year);
47    sprintf(s[1], "%02d", now.month+1);
48    sprintf(s[2], "%02d", now.day);
49    sprintf(s[3], "%02d", now.hour);
50    sprintf(s[4], "%02d", now.minute);
51    sprintf(s[5], "%02d", now.second);
52    time = PrintString("*", s[0], s[1], s[2], "-", s[3], s[4], s[5], "*");
53    for(c=0; c<6; c++)
54       delete s[c];
55    return time;
56 }
57
58 // use =0 to disable printing of specific channels
59 #ifdef _DEBUG_INST
60 static enum dplchan { none, gdbProtoIgnored=0/*1*/, gdbProtoUnknown=2, gdbOutput=0/*3*/, gdbCommand=0/*4*/, debuggerCall=0/*5*/, debuggerProblem=6,
61                         debuggerUserAction=7,debuggerState=8, debuggerBreakpoints=9, debuggerWatches=0/*10*/, debuggerTemp=0 };
62 #else
63 static enum dplchan { none, gdbProtoIgnored=0, gdbProtoUnknown=0, gdbOutput=0, gdbCommand=0, debuggerCall=0, debuggerProblem=0,
64                         debuggerUserAction=0,debuggerState=0, debuggerBreakpoints=0, debuggerWatches=0, debuggerTemp=0 };
65 #endif
66 static char * _dpct[] = {
67    null,
68    "GDB Protocol Ignored",
69    "GDB Protocol ***Unknown***",
70    "GDB Output",
71    "GDB Command",
72    ""/*Debugger Call*/,
73    "Debugger ***Problem***",
74    "Debugger::ChangeUserAction",
75    "Debugger::ChangeState",
76    "Breakpoints",
77    "Watches",
78    "-----> Temporary Message",
79    null
80 };
81
82 // TODO if(strlen(item.value) < MAX_F_STRING)
83
84 // Debug Print Line
85 #ifdef _DEBUG_INST
86 #define _dpl2(...) __dpl2(__FILE__, __LINE__, ##__VA_ARGS__)
87 #else
88 #define _dpl2(...)
89 #endif
90 static void __dpl2(char * file, int line, char ** channels, int channel, int indent, typed_object object, ...)
91 {
92    bool chan = channel && channels && channels[channel];
93    if(chan || !channels)
94    {
95       char string[MAX_F_STRING];
96       int len;
97       char * time = PrintNow();
98       va_list args;
99       //ide.outputView.debugBox.Logf();
100       Logf("%s %s:% 5d: %s%s", time, file, line, chan ? channels[channel] : "", chan && channels[channel][0] ? ": " : "");
101       va_start(args, object);
102       len = PrintStdArgsToBuffer(string, sizeof(string), object, args);
103       Log(string);
104       va_end(args);
105       Log("\n");
106       delete time;
107    }
108 }
109
110 #define _dpl(...) __dpl(__FILE__, __LINE__, ##__VA_ARGS__)
111 static void __dpl(char * file, int line, int indent, char * format, ...)
112 {
113    va_list args;
114    char string[MAX_F_STRING];
115    int c;
116    char * time = PrintNow();
117    //static File f = null;
118    va_start(args, format);
119    vsnprintf(string, sizeof(string), format, args);
120    string[sizeof(string)-1] = 0;
121    /*if(!f)
122    {
123       char * time = PrintNow();
124       char * logName;
125       logName = PrintString(time, ".log");
126       delete time;
127       f = FileOpen(logName, write);
128       delete logName;
129    }*/
130    /*f.Printf("%s %s:% 5d: ", time, file, line);
131    for(c = 0; c<indent; c++)
132       f.Putc(' ');
133    f.Printf("%s\n", string);*/
134    Logf("%s %s:% 5d: ", time, file, line);
135    for(c = 0; c<indent; c++)
136       Log(" ");
137    Logf("%s\n", string);
138    va_end(args);
139    delete time;
140 }
141
142 public char * StripQuotes2(char * string, char * output)
143 {
144    int c;
145    int d = 0;
146    bool quoted = false, escaped = false;
147    char ch;
148    for(c = 0; ch = string[c]; c++)
149    {
150       if(quoted)
151       {
152          if(escaped || ch != '\"')
153          {
154             output[d++] = ch;
155             escaped = !escaped && ch == '\\';
156          }
157          else
158             quoted = false;
159       }
160       else if(ch == '\"')
161          quoted = true;
162       else
163          output[d++] = ch;
164    }
165    output[d] = '\0';
166    return output;
167 }
168
169 // String Escape Copy
170 static void strescpy(char * d, char * s)
171 {
172    int j, k;
173    j = k = 0;
174    while(s[j])
175    {
176       switch(s[j])
177       {
178          case '\n':
179             d[k] = '\\';
180             d[++k] = 'n';
181             break;
182          case '\t':
183             d[k] = '\\';
184             d[++k] = 't';
185             break;
186          case '\a':
187             d[k] = '\\';
188             d[++k] = 'a';
189             break;
190          case '\b':
191             d[k] = '\\';
192             d[++k] = 'b';
193             break;
194          case '\f':
195             d[k] = '\\';
196             d[++k] = 'f';
197             break;
198          case '\r':
199             d[k] = '\\';
200             d[++k] = 'r';
201             break;
202          case '\v':
203             d[k] = '\\';
204             d[++k] = 'v';
205             break;
206          case '\\':
207             d[k] = '\\';
208             d[++k] = '\\';
209             break;
210          case '\"':
211             d[k] = '\\';
212             d[++k] = '\"';
213             break;
214          default:
215             d[k] = s[j];
216       }
217       ++j;
218       ++k;
219    }
220    d[k] = s[j];
221 }
222
223 static char * CopyUnescapedSystemPath(char * p)
224 {
225    char * d = new char[strlen(p) + 1];
226    struscpy(d, p);
227 #if defined(__WIN32__)
228    ChangeCh(d, '/', '\\');
229 #endif
230    return d;
231 }
232
233 static char * CopyUnescapedUnixPath(char * p)
234 {
235    char * d = new char[strlen(p) + 1];
236    struscpy(d, p);
237 #if defined(__WIN32__)
238    ChangeCh(d, '\\', '/');
239 #endif
240    return d;
241 }
242
243 static char * CopyUnescapedString(char * s)
244 {
245    char * d = new char[strlen(s) + 1];
246    struscpy(d, s);
247    return d;
248 }
249
250 // String Unescape Copy
251
252 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
253 // Seems very similar to ReadString in pass15.ec (which also misses numeric escape codes :) )
254
255 static void struscpy(char * d, char * s)
256 {
257    int j, k;
258    j = k = 0;
259    while(s[j])
260    {
261       switch(s[j])
262       {
263          case '\\':
264             switch(s[++j])
265             {
266                case 'n':
267                   d[k] = '\n';
268                   break;
269                case 't':
270                   d[k] = '\t';
271                   break;
272                case 'a':
273                   d[k] = '\a';
274                   break;
275                case 'b':
276                   d[k] = '\b';
277                   break;
278                case 'f':
279                   d[k] = '\f';
280                   break;
281                case 'r':
282                   d[k] = '\r';
283                   break;
284                case 'v':
285                   d[k] = '\v';
286                   break;
287                case '\\':
288                   d[k] = '\\';
289                   break;
290                case '\"':
291                   d[k] = '\"';
292                   break;
293                default:
294                   d[k] = '\\';
295                   d[++k] = s[j];
296             }
297             break;
298          default:
299             d[k] = s[j];
300       }
301       ++j;
302       ++k;
303    }
304    d[k] = s[j];
305 }
306
307 static char * StripBrackets(char * string)
308 {
309    int length = strlen(string);
310    if(length > 1 && *string == '[' && string[length - 1] == ']')
311    {
312       *string = '\0';
313       string[length - 1] = '\0';
314       return ++string;
315    }
316    else
317       return string;
318 }
319
320 static char * StripCurlies(char * string)
321 {
322    int length = strlen(string);
323    if(length > 1 && *string == '{' && string[length - 1] == '}')
324    {
325       *string = '\0';
326       string[length - 1] = '\0';
327       return ++string;
328    }
329    else
330       return string;
331 }
332
333 static int StringGetInt(char * string, int start)
334 {
335    char number[8];
336    int i, len = strlen(string);
337    number[0] = '\0';
338    for(i = start; i < len && i < start + 8; i++)
339    {
340       if(string[i] == '0' || string[i] == '1' || string[i] == '2' || string[i] == '3' || string[i] == '4' || string[i] == '5' || string[i] == '6' || string[i] == '7' || string[i] == '8' || string[i] == '9')
341          strncat(number, &string[i], 1);
342       else
343          break;
344    }
345    return atoi(number);
346 }
347
348 static int TokenizeList(char * string, const char seperator, Array<char *> tokens)
349 {
350    uint level = 0;
351    
352    bool quoted = false, escaped = false;
353    char * start = string, ch;
354    
355    for(; (ch = *string); string++)
356    {
357       if(!start)
358          start = string;
359
360       if(quoted)
361       {
362          if(escaped || ch != '\"')
363             escaped = !escaped && ch == '\\';
364          else
365             quoted = false;
366       }
367       else if(ch == '\"')
368          quoted = true;
369       else if(ch == '{' || ch == '[' || ch == '(' || ch == '<')
370          level++;
371       else if(ch == '}' || ch == ']' || ch == ')' || ch == '>')
372          level--;
373       else if(ch == seperator && !level)
374       {
375          tokens.Add(start);
376          *string = '\0';
377          start = null;
378       }
379    }
380    if(start)
381    {
382       //tokens[count] = start;
383       //tokens[count++] = start;
384       tokens.Add(start);
385       *string = '\0';
386    }
387    return tokens.count;
388 }
389
390 static bool TokenizeListItem(char * string, DebugListItem item)
391 {
392    char * equal = strstr(string, "=");
393    if(equal)
394    {
395       item.name = string;
396       *equal = '\0';
397       equal++;
398       item.value = equal;
399       equal = null;
400       return true;
401    }
402    else
403       return false;
404 }
405
406 static bool CheckCommandAvailable(const char * command)
407 {
408    bool available = false;
409    int c, count;
410    char * name = new char[MAX_FILENAME];
411    char * pathVar = new char[maxPathLen];
412    char * paths[128];
413    GetEnvironment("PATH", pathVar, maxPathLen);
414    count = TokenizeWith(pathVar, sizeof(paths) / sizeof(char *), paths, pathListSep, false);
415    strcpy(name, command);
416 #ifdef __WIN32__
417    {
418       int e;
419       const char * extensions[] = { "exe", "com", "bat", null };
420       for(e=0; extensions[e]; e++)
421       {
422          ChangeExtension(name, extensions[e], name);
423 #endif
424          for(c=0; c<count; c++)
425          {
426             FileListing fl { paths[c] };
427             while(fl.Find())
428             {
429                if(fl.stats.attribs.isFile && !fstrcmp(fl.name, name))
430                {
431                   available = true;
432                   break;
433                }
434             }
435             if(available) break;
436          }
437 #ifdef __WIN32__
438          if(available) break;
439       }
440    }
441 #endif
442    delete name;
443    delete pathVar;
444    return available;
445 }
446
447 // define GdbGetLineSize = 1638400;
448 define GdbGetLineSize = 5638400;
449 #if defined(__unix__)
450 char progFifoPath[MAX_LOCATION];
451 char progFifoDir[MAX_LOCATION];
452 #endif
453
454 enum DebuggerState { none, prompt, loaded, running, stopped, terminated, error };
455 enum DebuggerEvent
456 {
457    none, hit, breakEvent, signal, stepEnd, functionEnd, exit, valgrindStartPause;
458
459    property bool canBeMonitored { get { return (this == hit || this == breakEvent || this == signal || this == stepEnd || this == functionEnd); } };
460 };
461 enum DebuggerAction { none, internal, restart, stop, selectFrame }; //, bpValidation
462 enum DebuggerReason
463 {
464    unknown, endSteppingRange, functionFinished, signalReceived, breakpointHit
465    //watchpointTrigger, readWatchpointTrigger, accessWatchpointTrigger, watchpointScope, locationReached,
466    //exited, exitedNormally, exitedSignalled;
467 };
468 enum BreakpointType
469 {
470    none, internalMain, internalWinMain, internalModulesLoaded, user, runToCursor, internalModuleLoad;
471
472    property bool isInternal { get { return (this == internalMain || this == internalWinMain || this == internalModulesLoaded || this == internalModuleLoad); } };
473    property bool isUser { get { return (this == user || this == runToCursor); } };
474 };
475 enum DebuggerEvaluationError { none, symbolNotFound, memoryCantBeRead, unknown };
476 enum DebuggerUserAction { none, start, resume, _break, stop, restart, selectThread, selectFrame, stepInto, stepOver, stepOut, runToCursor };
477
478 FileDialog debuggerFileDialog { type = selectDir };
479
480 static DualPipe vgTargetHandle;
481 static File vgLogFile;
482 static char vgLogPath[MAX_LOCATION];
483 static DualPipe gdbHandle;
484 static DebugEvaluationData eval { };
485
486 static int targetProcessId;
487
488 static bool gdbReady;
489 static bool breakpointError;
490
491 class Debugger
492 {
493    Semaphore serialSemaphore { };
494    bool waitingForPID;
495    bool targeted;
496    bool symbols;
497    bool modules;
498    bool sentKill;
499    bool sentBreakInsert;
500    bool ignoreBreakpoints;
501    bool userBreakOnInternalBreakpoint;
502    //bool runToCursorDebugStart;
503    bool signalOn;
504    bool needReset;
505    //bool watchesInit;
506    bool usingValgrind;
507
508    int ideProcessId;
509    int gdbProcessId;
510
511    int activeFrameLevel;
512    int activeThread;
513    int hitThread;
514    int signalThread;
515    int frameCount;
516
517    char * targetDir;
518    char * targetFile;
519    
520    DebuggerUserAction userAction;
521    DebuggerState state;
522    DebuggerEvent event;
523    DebuggerAction breakType;
524    //DebuggerCommand lastCommand;    // THE COMPILER COMPILES STUFF THAT DOES NOT EXIST???
525
526    GdbDataStop stopItem;
527    GdbDataBreakpoint bpItem;
528    Frame activeFrame;
529    
530    List<Breakpoint> sysBPs { };
531    Breakpoint bpRunToCursor;
532
533    OldList stackFrames;
534
535    CompilerConfig currentCompiler;
536    ProjectConfig prjConfig;
537    int bitDepth;
538
539    CodeEditor codeEditor;
540
541    ValgrindLogThread vgLogThread { debugger = this };
542    ValgrindTargetThread vgTargetThread { debugger = this };
543    GdbThread gdbThread { debugger = this };
544    Timer gdbTimer
545    {
546       delay = 0.0, userData = this;
547
548       bool DelayExpired()
549       {
550          bool monitor = false;
551          DebuggerEvent curEvent = event;
552          GdbDataStop stopItem = this.stopItem;
553          Breakpoint bpUser = null;
554          Breakpoint bpInternal = null;
555
556          if(!gdbReady)
557             return false;
558
559          event = none;
560          if(this.stopItem)
561          {
562             this.stopItem = null;
563 #ifdef _DEBUG_INST
564             {
565                char * s;
566                DynamicString bpReport { };
567
568                for(bp : sysBPs; bp.inserted)
569                {
570                   bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
571                   delete s;
572                }
573                if(bpRunToCursor && bpRunToCursor.inserted)
574                {
575                   Breakpoint bp = bpRunToCursor;
576                   bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
577                   delete s;
578                }
579                for(bp : ide.workspace.breakpoints; bp.inserted)
580                {
581                   bpReport.concatx(",", bp.type, "(", s=bp.CopyLocationString(false), ")");
582                   delete s;
583                }
584                s = bpReport;
585                _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdbTimer::DelayExpired: ", s+1);
586
587                if(stopItem.bkptno)
588                {
589                   bool isInternal;
590                   Breakpoint bp = GetBreakpointById(stopItem.bkptno, &isInternal);
591                   if(bp)
592                      _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "gdb stopped by a breakpoint: ", bp.type, "(", s=bp.CopyLocationString(false), ")"); delete s;
593                }
594             }
595 #endif
596          }
597 #ifdef _DEBUG_INST
598          else
599          {
600             if(curEvent && curEvent != exit)
601             {
602                _dpl(0, "No stop item");
603             }
604          }
605 #endif
606          switch(breakType)
607          {
608             case restart:
609                breakType = none;
610                Restart(currentCompiler, prjConfig, bitDepth, usingValgrind);
611                break;
612             case stop:
613                breakType = none;
614                Stop();
615                break;
616             case selectFrame:
617             {
618                breakType = none;
619                GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
620                for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
621                   if(activeFrame.level == activeFrameLevel)
622                      break;
623                break;
624             }
625             //case bpValidation:
626             //   breakType = none;
627             //   GdbCommand(false, "-break-info %s", bpItem.number);
628             //   break;
629          }
630
631          if(curEvent == none)
632             return false;
633
634          switch(curEvent)
635          {
636             case hit:
637                {
638                   bool isInternal;
639                   Breakpoint bp = stopItem ? GetBreakpointById(stopItem.bkptno, &isInternal) : null;
640                   if(bp && bp.inserted && bp.bp.addr)
641                   {
642                      if(bp.type.isInternal)
643                         bpInternal = bp;
644                      else
645                         bpUser = bp;
646                      if(stopItem && stopItem.frame)
647                      {
648                         if(bpInternal && bpRunToCursor && bpRunToCursor.inserted && !strcmp(bpRunToCursor.bp.addr, bp.bp.addr))
649                            bpUser = bpRunToCursor;
650                         else
651                         {
652                            for(item : (bpInternal ? ide.workspace.breakpoints : sysBPs); item.inserted)
653                            {
654                               if(item.bp && item.bp.addr && !strcmp(item.bp.addr, bp.bp.addr))
655                               {
656                                  if(bpInternal)
657                                     bpUser = item;
658                                  else
659                                     bpInternal = item;
660                                  break;
661                               }
662                            }
663                         }
664                      }
665                      else
666                         _dpl2(_dpct, dplchan::debuggerProblem, 0, "Invalid stopItem!");
667                      if(bpUser && strcmp(stopItem.frame.addr, bpUser.bp.addr))
668                         bpUser = null;
669                   }
670                   else
671                      _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") invalid or not found!");
672                   if((bpUser && !ignoreBreakpoints) || (bpInternal && userBreakOnInternalBreakpoint))
673                      monitor = true;
674                   hitThread = stopItem.threadid;
675                }
676                break;
677             case signal:
678                signalThread = stopItem.threadid;
679             case breakEvent:
680             case stepEnd:
681             case functionEnd:
682                monitor = true;
683                ignoreBreakpoints = false;
684                break;
685             case valgrindStartPause:
686                GdbExecContinue(true);
687                break;
688             case exit:
689                HideDebuggerViews();
690                break;
691          }
692
693          if(monitor || (bpUser && bpUser.type == runToCursor))
694             GdbGetStack();
695
696          if(monitor)
697          {
698             activeThread = stopItem.threadid;
699             GdbCommand(false, "-thread-list-ids");
700             if(activeFrameLevel > 0)
701                GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
702
703             WatchesCodeEditorLinkInit();
704             EvaluateWatches();
705          }
706
707          if(curEvent == signal)
708          {
709             char * s;
710             signalOn = true;
711             ide.outputView.debugBox.Logf($"Signal received: %s - %s\n", stopItem.name, stopItem.meaning);
712             ide.outputView.debugBox.Logf("    %s:%d\n", (s = CopySystemPath(stopItem.frame.file)), stopItem.frame.line);
713             ide.outputView.Show();
714             ide.callStackView.Show();
715             delete s;
716          }
717          else if(curEvent == breakEvent)
718          {
719             ide.threadsView.Show();
720             ide.callStackView.Show();
721             ide.callStackView.Activate();
722          }
723
724          if(monitor && curEvent.canBeMonitored)
725          {
726             InternalSelectFrame(activeFrameLevel);
727             GoToStackFrameLine(activeFrameLevel, true, false);
728             ide.ShowCodeEditor();
729             ideMainFrame.Activate();   // TOFIX: ide.Activate() is not reliable (app inactive)
730             ide.Update(null);
731          }
732
733          if(curEvent == hit)
734          {
735             if(BreakpointHit(stopItem, bpInternal, bpUser))
736             {
737                if(bpUser && bpUser.type == runToCursor)
738                {
739                   ignoreBreakpoints = false;
740                   UnsetBreakpoint(bpUser);
741                   delete bpRunToCursor;
742                }
743             }
744             else
745                GdbExecContinue(false);
746          }
747
748          if(stopItem)
749          {
750             stopItem.Free();
751             delete stopItem;
752          }
753          if(userBreakOnInternalBreakpoint)
754             userBreakOnInternalBreakpoint = false;
755          return false;
756       }
757    };
758
759 #ifdef GDB_DEBUG_CONSOLE
760    char lastGdbOutput[GdbGetLineSize];
761 #endif
762 #if defined(__unix__)
763    ProgramThread progThread { };
764 #endif
765
766 #ifdef _DEBUG_INST
767 #define _ChangeUserAction(value) ChangeUserAction(__FILE__, __LINE__, value)
768    void ChangeUserAction(char * file, int line, DebuggerUserAction value)
769    {
770       bool same = value == userAction;
771       __dpl2(file, line, _dpct, dplchan::debuggerUserAction, 0, userAction, /*same ? " *** == *** " : */" -> ", value);
772       userAction = value;
773    }
774 #else
775 #define _ChangeUserAction(value) userAction = value
776 #endif
777
778 #ifdef _DEBUG_INST
779 #define _ChangeState(value) ChangeState(__FILE__, __LINE__, value)
780    void ChangeState(char * file, int line, DebuggerState value)
781 #else
782 #define _ChangeState(value) ChangeState(value)
783    void ChangeState(DebuggerState value)
784 #endif
785    {
786       bool same = value == state;
787 #ifdef _DEBUG_INST
788       __dpl2(file, line, _dpct, dplchan::debuggerState, 0, state, same ? " *** == *** " : " -> ", value);
789 #endif
790       state = value;
791       if(!same && ide) ide.AdjustDebugMenus();
792    }
793
794    void CleanUp()
795    {
796       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::CleanUp");
797
798       stackFrames.Free(Frame::Free);
799
800       delete targetDir;
801       delete targetFile;
802
803       ClearBreakDisplay();
804
805       // Clear Stuff up
806       gdbProcessId = 0;
807
808       waitingForPID = false;
809       targeted = false;
810       symbols = false;
811       modules = false;
812       sentKill = false;
813       sentBreakInsert = false;
814       ignoreBreakpoints = false;
815       userBreakOnInternalBreakpoint = false;
816       //runToCursorDebugStart = false;
817       signalOn = false;
818
819       activeFrameLevel = 0;
820       activeThread = 0;
821       hitThread = 0;
822       signalThread = 0;
823       frameCount = 0;
824
825       targetDir = null;
826       targetFile = null;
827       
828       _ChangeState(none);
829       event = none;
830       breakType = none;
831
832       stopItem = null;
833       bpItem = null;
834       activeFrame = 0;
835       
836       bpRunToCursor = null;
837
838       delete currentCompiler;
839       prjConfig = null;
840       codeEditor = null;
841
842       /*GdbThread gdbThread
843       Timer gdbTimer*/
844    }
845    
846    Debugger()
847    {
848       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::constructor");
849       ideProcessId = Process_GetCurrentProcessId();
850
851       sysBPs.Add(Breakpoint { type = internalMain, function = "main", enabled = true, level = -1 });
852 #if defined(__WIN32__)
853       sysBPs.Add(Breakpoint { type = internalWinMain, function = "WinMain", enabled = true, level = -1 });
854 #endif
855       sysBPs.Add(Breakpoint { type = internalModulesLoaded, enabled = true, level = -1 });
856       sysBPs.Add(Breakpoint { type = internalModuleLoad, function = "InternalModuleLoadBreakpoint", enabled = true, level = -1 });
857    }
858
859    ~Debugger()
860    {
861       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::destructor");
862       sysBPs.Free();
863       Stop();
864       CleanUp();
865    }
866
867    // PUBLIC MEMBERS
868
869    property bool isActive { get { return state == running || state == stopped; } }
870    property bool isPrepared  { get { return state == loaded || state == running || state == stopped; } }
871
872    void Resume()
873    {
874       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Resume");
875       _ChangeUserAction(resume);
876       GdbExecContinue(true);
877    }
878
879    void Break()
880    {
881       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Break");
882       _ChangeUserAction(_break);
883       if(state == running)
884       {
885          if(targetProcessId)
886             GdbDebugBreak(false);
887       }
888    }
889
890    void Stop()
891    {
892       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Stop");
893       _ChangeUserAction(stop);
894       switch(state)
895       {
896          case running:
897             if(targetProcessId)
898             {
899                breakType = stop;
900                GdbDebugBreak(false);
901             }
902             break;
903          case stopped:
904             GdbAbortExec();
905             HideDebuggerViews();
906             GdbExit();
907             break;
908          case loaded:
909             GdbExit();
910             break;
911       }
912    }
913
914    void Restart(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
915    {
916       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Restart");
917       _ChangeUserAction(restart);
918       if(StartSession(compiler, config, bitDepth, useValgrind, true, false, false/*, false*/) == loaded)
919          GdbExecRun();
920    }
921
922    bool GoToCodeLine(char * location)
923    {
924       CodeLocation codloc;
925       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToCodeLine(", location, ")");
926       codloc = CodeLocation::ParseCodeLocation(location);
927       if(codloc)
928       {
929          CodeEditor editor = (CodeEditor)ide.OpenFile(codloc.absoluteFile, normal, true, null, no, normal, false);
930          if(editor)
931          {
932             EditBox editBox = editor.editBox;
933             editBox.GoToLineNum(codloc.line - 1);
934             editBox.GoToPosition(editBox.line, codloc.line - 1, 0);
935             return true;
936          }
937       }
938       return false;
939    }
940
941    bool GoToStackFrameLine(int stackLevel, bool askForLocation, bool fromCallStack)
942    {
943       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GoToStackFrameLine(", stackLevel, ", ", askForLocation, ")");
944       if(ide)
945       {
946          char filePath[MAX_LOCATION];
947          char sourceDir[MAX_LOCATION];
948          Frame frame;
949          CodeEditor editor = null;
950          if(stackLevel == -1)  // this (the two lines) is part of that fix that I would not put in for some time
951             return false;
952          for(frame = stackFrames.first; frame; frame = frame.next)
953             if(frame.level == stackLevel)
954                break;
955          if(frame)
956          {
957             if(!fromCallStack)
958                ide.callStackView.Show();
959
960             if(frame.absoluteFile)
961                editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
962             if(!editor && frame.file)
963                frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
964             if(!frame.absoluteFile && askForLocation && frame.file)
965             {
966                char * s;
967                char title[MAX_LOCATION];
968                snprintf(title, sizeof(title), $"Provide source file location for %s", (s = CopySystemPath(frame.file)));
969                title[sizeof(title)-1] = 0;
970                delete s;
971                if(SourceDirDialog(title, ide.workspace.projectDir, frame.file, sourceDir))
972                {
973                   AddSourceDir(sourceDir);
974                   frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
975                }
976             }
977             if(!editor && frame.absoluteFile)
978                editor = (CodeEditor)ide.OpenFile(frame.absoluteFile, normal, true, null, no, normal, false);
979             if(editor)
980                ide.RepositionWindows(false);
981             ide.Update(null);
982             if(editor && frame.line)
983             {
984                EditBox editBox = editor.editBox;
985                editBox.GoToLineNum(frame.line - 1);
986                editBox.GoToPosition(editBox.line, frame.line - 1, 0);
987                return true;
988             }
989          }
990       }
991       return false;
992    }
993
994    void SelectThread(int thread)
995    {
996       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectThread(", thread, ")");
997       _ChangeUserAction(selectThread);
998       if(state == stopped)
999       {
1000          if(thread != activeThread)
1001          {
1002             activeFrameLevel = -1;
1003             ide.callStackView.Clear();
1004             GdbCommand(false, "-thread-select %d", thread);
1005             GdbGetStack();
1006             InternalSelectFrame(activeFrameLevel);
1007             GoToStackFrameLine(activeFrameLevel, true, false);
1008             WatchesCodeEditorLinkRelease();
1009             WatchesCodeEditorLinkInit();
1010             EvaluateWatches();
1011             ide.Update(null);
1012          }
1013          ide.callStackView.Show();
1014       }
1015    }
1016
1017    void SelectFrame(int frame)
1018    {
1019       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SelectFrame(", frame, ")");
1020       _ChangeUserAction(selectFrame);
1021       InternalSelectFrame(frame);
1022    }
1023
1024    void InternalSelectFrame(int frame)
1025    {
1026       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::InternalSelectFrame(", frame, ")");
1027       if(state == stopped)
1028       {
1029          if(frame != activeFrameLevel || !codeEditor || !codeEditor.visible)
1030          {
1031             activeFrameLevel = frame;  // there is no active frame number in the gdb reply
1032             GdbCommand(false, "-stack-select-frame %d", activeFrameLevel);
1033             for(activeFrame = stackFrames.first; activeFrame; activeFrame = activeFrame.next)
1034                if(activeFrame.level == activeFrameLevel)
1035                   break;
1036
1037             WatchesCodeEditorLinkRelease();
1038             WatchesCodeEditorLinkInit();
1039             EvaluateWatches();
1040             ide.Update(null);
1041          }
1042       }
1043    }
1044
1045    void HandleExit(char * reason, char * code)
1046    {
1047       bool returnedExitCode = false;
1048       char verboseExitCode[128];
1049       
1050       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HandleExit(", reason, ", ", code, ")");
1051       _ChangeState(loaded); // this state change seems to be superfluous, might be in case of gdb crash
1052       targetProcessId = 0;
1053
1054       if(code)
1055       {
1056          snprintf(verboseExitCode, sizeof(verboseExitCode), $" with exit code %s", code);
1057          verboseExitCode[sizeof(verboseExitCode)-1] = 0;
1058       }
1059       else
1060          verboseExitCode[0] = '\0';
1061       
1062       event = exit;
1063
1064       // ClearBreakDisplay();
1065
1066       if(ide.workspace)
1067       {
1068          for(wh : ide.workspace.watches)
1069          {
1070             if(wh.type) FreeType(wh.type);
1071             wh.type = null;
1072             delete wh.value;
1073             ide.watchesView.UpdateWatch(wh);
1074          }
1075       }
1076
1077 #if defined(__unix__)
1078       if(!usingValgrind)
1079       {
1080          progThread.terminate = true;
1081          if(fifoFile)
1082          {
1083             fifoFile.CloseInput();
1084             app.Unlock();
1085             progThread.Wait();
1086             app.Lock();
1087             delete fifoFile;
1088          }
1089       }
1090 #endif
1091
1092       {
1093          char program[MAX_LOCATION];
1094          GetSystemPathBuffer(program, targetFile);
1095          if(!reason)
1096             ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1097          else if(!strcmp(reason, "exited-normally"))
1098             ide.outputView.debugBox.Logf($"The program %s has exited normally%s.\n", program, verboseExitCode);
1099          else if(!strcmp(reason, "exited"))
1100             ide.outputView.debugBox.Logf($"The program %s has exited%s.\n", program, verboseExitCode);
1101          else if(!strcmp(reason, "exited-signalled"))
1102             ide.outputView.debugBox.Logf($"The program %s has exited with a signal%s.\n", program, verboseExitCode);
1103          else
1104             ide.outputView.debugBox.Logf($"The program %s has exited (gdb provided an unknown reason)%s.\n", program, verboseExitCode);
1105       }
1106       ide.Update(null);
1107    }
1108       
1109    DebuggerState StartSession(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool restart, bool userBreakOnInternalBreakpoint, bool ignoreBreakpoints/*, bool runToCursorDebugStart*/)
1110    {
1111       DebuggerState result = none;
1112       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StartSession(restart(", restart, "), userBreakOnInternalBreakpoint(", userBreakOnInternalBreakpoint, "), ignoreBreakpoints(", ignoreBreakpoints, ")"/*, runToCursorDebugStart(", runToCursorDebugStart, ")"*/);
1113       if(restart && state == running && targetProcessId)
1114       {
1115          breakType = DebuggerAction::restart;
1116          GdbDebugBreak(false);
1117       }
1118       else
1119       {
1120          if(restart && state == stopped)
1121             GdbAbortExec();
1122          if(needReset && state == loaded)
1123             GdbExit(); // this reset is to get a clean state with all the breakpoints until a better state can be maintained on program exit
1124          result = state;
1125          if(result == none || result == terminated)
1126          {
1127             ide.outputView.ShowClearSelectTab(debug);
1128             ide.outputView.debugBox.Logf($"Starting debug mode\n");
1129
1130             for(bp : sysBPs)
1131             {
1132                bp.hits = 0;
1133                bp.breaks = 0;
1134             }
1135             for(bp : ide.workspace.breakpoints)
1136             {
1137                bp.hits = 0;
1138                bp.breaks = 0;
1139             }
1140
1141             //this.runToCursorDebugStart = runToCursorDebugStart;
1142
1143             if(GdbInit(compiler, config, bitDepth, useValgrind))
1144                result = state;
1145             else
1146                result = error;
1147          }
1148          this.ignoreBreakpoints = ignoreBreakpoints;
1149          this.userBreakOnInternalBreakpoint = userBreakOnInternalBreakpoint;
1150          if(result == loaded || result == stopped)
1151             GdbBreakpointsDelete(false, (userAction == stepOver || userAction == stepOut), ignoreBreakpoints);
1152       }
1153       return result;
1154    }
1155
1156    void Start(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1157    {
1158       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::Start()");
1159       _ChangeUserAction(start);
1160       if(StartSession(compiler, config, bitDepth, useValgrind, true, false, false/*, false*/) == loaded)
1161          GdbExecRun();
1162    }
1163
1164    void StepInto(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
1165    {
1166       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepInto()");
1167       _ChangeUserAction(stepInto);
1168       switch(StartSession(compiler, config, bitDepth, useValgrind, false, true, false/*, false*/))
1169       {
1170          case loaded:  GdbExecRun();  break;
1171          case stopped: GdbExecStep(); break;
1172       }
1173    }
1174
1175    void StepOver(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, bool ignoreBreakpoints)
1176    {
1177       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOver()");
1178       _ChangeUserAction(stepOver);
1179       switch(StartSession(compiler, config, bitDepth, useValgrind, false, true, ignoreBreakpoints/*, false*/))
1180       {
1181          case loaded:  GdbExecRun();  break;
1182          case stopped: GdbExecNext(); break;
1183       }
1184    }
1185
1186    void StepOut(bool ignoreBreakpoints)
1187    {
1188       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::StepOut()");
1189       _ChangeUserAction(stepOut);
1190       if(state == stopped)
1191       {
1192          this.ignoreBreakpoints = ignoreBreakpoints;
1193          GdbBreakpointsDelete(true, true, ignoreBreakpoints);
1194          if(frameCount > 1)
1195             GdbExecFinish();
1196          else
1197             GdbExecContinue(true);
1198       }
1199    }
1200
1201    void RunToCursor(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind, char * absoluteFilePath, int lineNumber, bool ignoreBreakpoints, bool atSameLevel)
1202    {
1203       char relativeFilePath[MAX_LOCATION];
1204       DebuggerState st = state;
1205       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::RunToCursor()");
1206       _ChangeUserAction(runToCursor);
1207       //if(st == loaded)
1208       //{
1209       //   ide.outputView.ShowClearSelectTab(debug);
1210       //   ide.outputView.debugBox.Logf($"Starting debug mode\n");
1211       //}
1212       if(!ide.projectView.project.GetRelativePath(absoluteFilePath, relativeFilePath))
1213          strcpy(relativeFilePath, absoluteFilePath);
1214
1215       if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1216       {
1217          UnsetBreakpoint(bpRunToCursor);
1218          delete bpRunToCursor;
1219       }
1220
1221       bpRunToCursor = Breakpoint { };
1222       bpRunToCursor.absoluteFilePath = absoluteFilePath;
1223       bpRunToCursor.relativeFilePath = relativeFilePath;
1224       bpRunToCursor.line = lineNumber;
1225       bpRunToCursor.type = runToCursor;
1226       bpRunToCursor.enabled = true;
1227       bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1228
1229       switch(StartSession(compiler, config, bitDepth, useValgrind, false, false, ignoreBreakpoints/*, true*/))
1230       {
1231          case loaded:
1232             GdbExecRun();
1233             break;
1234          case stopped:
1235             GdbExecContinue(true);
1236             break;
1237       }
1238    }
1239
1240    void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1241    {
1242       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1243       if(activeFrameLevel == -1)
1244       {
1245          *error = false;
1246          *lineCursor = 0;
1247          *lineTopFrame = 0;
1248       }
1249       else
1250       {
1251          *error = signalOn && activeThread == signalThread;
1252          *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1253          *lineTopFrame = activeFrameLevel ? 1 : 0;
1254       }
1255    }
1256
1257    int GetMarginIconsLineNumbers(char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1258    {
1259       char winFilePath[MAX_LOCATION];
1260       char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1261       int count = 0;
1262       Iterator<Breakpoint> it { ide.workspace.breakpoints };
1263       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1264       while(it.Next() && count < max)
1265       {
1266          Breakpoint bp = it.data;
1267          if(bp.type == user)
1268          {
1269             if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1270             {
1271                lines[count] = bp.line;
1272                enabled[count] = bp.enabled;
1273                count++;
1274             }
1275          }
1276       }
1277       if(activeFrameLevel == -1)
1278       {
1279          *error = false;
1280          *lineCursor = 0;
1281          *lineTopFrame = 0;
1282       }
1283       else
1284       {
1285          *error = signalOn && activeThread == signalThread;
1286          if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1287             *lineCursor = activeFrame.line;
1288          else
1289             *lineCursor = 0;
1290          if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1291             *lineTopFrame = 0;
1292          else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1293             *lineTopFrame = stopItem.frame.line;
1294          else
1295             *lineTopFrame = 0;
1296          
1297          if(*lineTopFrame == *lineCursor && *lineTopFrame)
1298             *lineTopFrame = 0;
1299       }
1300       return count;
1301    }
1302
1303    void ChangeWatch(DataRow row, char * expression)
1304    {
1305       Watch wh = (Watch)row.tag;
1306       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1307       if(wh)
1308       {
1309          delete wh.expression;
1310          if(expression)
1311             wh.expression = CopyString(expression);
1312          else
1313          {
1314             Iterator<Watch> it { ide.workspace.watches };
1315             if(it.Find(wh))
1316                ide.workspace.watches.Delete(it.pointer);
1317          }
1318       }
1319       else if(expression)
1320       {
1321          wh = Watch { };
1322          row.tag = (int64)wh;
1323          ide.workspace.watches.Add(wh);
1324          wh.row = row;
1325          wh.expression = CopyString(expression);
1326       }
1327       ide.workspace.Save();
1328       //if(expression && state == stopped)
1329       if(expression)
1330          ResolveWatch(wh);
1331    }
1332
1333    void MoveIcons(char * fileName, int lineNumber, int move, bool start)
1334    {
1335       char winFilePath[MAX_LOCATION];
1336       char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1337
1338       Link bpLink, next;
1339       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1340       for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1341       {
1342          Breakpoint bp = (Breakpoint)bpLink.data;
1343          next = bpLink.next;
1344
1345          if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1346          {
1347             if(bp.line > lineNumber || (bp.line == lineNumber && start))
1348             {
1349                if(move < 0 && (bp.line < lineNumber - move))
1350                   ide.workspace.RemoveBreakpoint(bp);
1351                else
1352                {
1353                   bp.line += move;
1354                   ide.breakpointsView.UpdateBreakpoint(bp.row);
1355                   ide.workspace.Save();
1356                }
1357             }
1358          }
1359       }
1360       
1361       // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1362    }
1363
1364    bool SourceDirDialog(char * title, char * startDir, char * test, char * sourceDir)
1365    {
1366       bool result;
1367       bool retry;
1368       String srcDir = null;
1369
1370       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1371       debuggerFileDialog.text = title;
1372       debuggerFileDialog.currentDirectory = startDir;
1373       debuggerFileDialog.master = ide;
1374
1375       while(debuggerFileDialog.Modal())
1376       {
1377          strcpy(sourceDir, debuggerFileDialog.filePath);
1378          if(!fstrcmp(ide.workspace.projectDir, sourceDir) && 
1379                   MessageBox { type = yesNo, master = ide, 
1380                               contents = $"This is the project directory.\nWould you like to try again?", 
1381                               text = $"Invalid Source Directory" }.Modal() == no)
1382             return false;
1383          else
1384          {
1385             for(dir : ide.workspace.sourceDirs)
1386             {
1387                if(!fstrcmp(dir, sourceDir))
1388                {
1389                   srcDir = dir;
1390                   break;
1391                }
1392             }
1393             
1394             if(srcDir && 
1395                   MessageBox { type = yesNo, master = ide, 
1396                               contents = $"This source directory is already specified.\nWould you like to try again?", 
1397                               text = $"Invalid Source Directory" }.Modal() == no)
1398                return false;
1399             else
1400             {
1401                if(test)
1402                {
1403                   char file[MAX_LOCATION];
1404                   strcpy(file, sourceDir);
1405                   PathCat(file, test);
1406                   result = FileExists(file);
1407                   if(!result && 
1408                         MessageBox { type = yesNo, master = ide, 
1409                                     contents = $"Unable to locate source file.\nWould you like to try again?", 
1410                                     text = $"Invalid Source Directory" }.Modal() == no)
1411                         return false;
1412                }
1413                else
1414                   result = true;
1415                
1416                if(result)
1417                   return true;
1418             }
1419          }
1420       }
1421       return false;
1422    }
1423
1424    void AddSourceDir(char * sourceDir)
1425    {
1426       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1427       ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1428       ide.workspace.Save();
1429       
1430       if(targeted)
1431       {
1432          DebuggerState oldState = state;
1433          switch(state)
1434          {
1435             case running:
1436                if(targetProcessId)
1437                   GdbDebugBreak(true);
1438             case stopped:
1439             case loaded:
1440                GdbCommand(false, "-environment-directory \"%s\"", sourceDir);
1441                break;
1442          }
1443          if(oldState == running)
1444             GdbExecContinue(false);
1445       }
1446    }
1447
1448    void ToggleBreakpoint(char * fileName, int lineNumber, Project prj)
1449    {
1450       char winFilePath[MAX_LOCATION];
1451       char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1452       char absolutePath[MAX_LOCATION];
1453       char relativePath[MAX_LOCATION];
1454       char sourceDir[MAX_LOCATION];
1455       Breakpoint bp = null;
1456
1457       _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1458       strcpy(absolutePath, absoluteFilePath);
1459       for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1460       {
1461          bp = i;
1462          break;
1463       }
1464       if(bp)
1465       {
1466          if(bp.enabled)
1467          {
1468             ide.workspace.RemoveBreakpoint(bp);
1469             bp = null;
1470          }
1471          else
1472             bp.enabled = true;
1473       }
1474       else
1475       {
1476          // FIXED: This is how it should have been... Source locations are only for files not in project
1477          // if(IsPathInsideOf(absolutePath, ide.workspace.projectDir))
1478          //   MakePathRelative(absolutePath, ide.workspace.projectDir, relativePath);
1479          bool result = false;
1480          if(prj)
1481             result = prj.GetRelativePath(absolutePath, relativePath);
1482          else
1483             result = ide.projectView.project.GetRelativePath(absolutePath, relativePath);
1484          //if(ide.projectView.project.GetRelativePath(absolutePath, relativePath));
1485          //else
1486          if(!result)
1487          {
1488             char title[MAX_LOCATION];
1489             char directory[MAX_LOCATION];
1490             StripLastDirectory(absolutePath, directory);
1491             snprintf(title, sizeof(title), $"Provide source files location directory for %s", absolutePath);
1492             title[sizeof(title)-1] = 0;
1493             while(true)
1494             {
1495                String srcDir = null;
1496                for(dir : ide.workspace.sourceDirs)
1497                {
1498                   if(IsPathInsideOf(absolutePath, dir))
1499                   {
1500                      MakePathRelative(absoluteFilePath, dir, relativePath);
1501                      srcDir = dir;
1502                      break;
1503                   }
1504                }
1505                if(srcDir)
1506                   break;
1507                
1508                if(SourceDirDialog(title, directory, null, sourceDir))
1509                {
1510                   if(IsPathInsideOf(absolutePath, sourceDir))
1511                   {
1512                      AddSourceDir(sourceDir);
1513                      MakePathRelative(absoluteFilePath, sourceDir, relativePath);
1514                      break;
1515                   }
1516                   else if(MessageBox { type = yesNo, master = ide, 
1517                                  contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?", 
1518                                  text = $"Invalid Source Directory" }.Modal() == no)
1519                      return;
1520                }
1521                else if(MessageBox { type = yesNo, master = ide, 
1522                                  contents = $"You must provide a source directory in order to place a breakpoint in this file.\nWould you like to try again?", 
1523                                  text = $"No Source Directory Provided" }.Modal() == no)
1524                   return;
1525             }
1526          }
1527          ide.workspace.bpCount++;
1528          bp = { line = lineNumber, type = user, enabled = true, level = -1 };
1529          ide.workspace.breakpoints.Add(bp);
1530          bp.absoluteFilePath = absolutePath;
1531          bp.relativeFilePath = relativePath;
1532          ide.breakpointsView.AddBreakpoint(bp);
1533       }
1534
1535       if(bp && targeted)
1536       {
1537          DebuggerState oldState = state;
1538          switch(state)
1539          {
1540             case running:
1541                if(targetProcessId)
1542                   GdbDebugBreak(true);
1543             case stopped:
1544             case loaded:
1545                SetBreakpoint(bp, false);
1546                break;
1547          }
1548          if(oldState == running)
1549             GdbExecContinue(false);
1550       }
1551
1552       ide.workspace.Save();
1553    }
1554
1555    void UpdateRemovedBreakpoint(Breakpoint bp)
1556    {
1557       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1558       if(targeted && bp.inserted)
1559       {
1560          DebuggerState oldState = state;
1561          switch(state)
1562          {
1563             case running:
1564                if(targetProcessId)
1565                   GdbDebugBreak(true);
1566             case stopped:
1567             case loaded:
1568                UnsetBreakpoint(bp);
1569                break;
1570          }
1571          if(oldState == running)
1572             GdbExecContinue(false);
1573       }
1574    }
1575
1576    // PRIVATE MEMBERS
1577
1578    void ParseFrame(Frame frame, char * string)
1579    {
1580       int i, j, k;
1581       Array<char *> frameTokens { minAllocSize = 50 };
1582       Array<char *> argsTokens { minAllocSize = 50 };
1583       Array<char *> argumentTokens { minAllocSize = 50 };
1584       DebugListItem item { };
1585       Argument arg;
1586       
1587       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1588       TokenizeList(string, ',', frameTokens);
1589       for(i = 0; i < frameTokens.count; i++)
1590       {
1591          if(TokenizeListItem(frameTokens[i], item))
1592          {
1593             StripQuotes(item.value, item.value);
1594             if(!strcmp(item.name, "level"))
1595                frame.level = atoi(item.value);
1596             else if(!strcmp(item.name, "addr"))
1597                frame.addr = item.value;
1598             else if(!strcmp(item.name, "func"))
1599                frame.func = item.value;
1600             else if(!strcmp(item.name, "args"))
1601             {
1602                if(!strcmp(item.value, "[]"))
1603                   frame.argsCount = 0;
1604                else
1605                {
1606                   item.value = StripBrackets(item.value);
1607                   TokenizeList(item.value, ',', argsTokens);
1608                   for(j = 0; j < argsTokens.count; j++)
1609                   {
1610                      argsTokens[j] = StripCurlies(argsTokens[j]);
1611                      TokenizeList(argsTokens[j], ',', argumentTokens);
1612                      for(k = 0; k < argumentTokens.count; k++)
1613                      {
1614                         arg = Argument { };
1615                         frame.args.Add(arg);
1616                         if(TokenizeListItem(argumentTokens[k], item))
1617                         {
1618                            if(!strcmp(item.name, "name"))
1619                            {
1620                               StripQuotes(item.value, item.value);
1621                               arg.name = item.value;
1622                            }
1623                            else if(!strcmp(item.name, "value"))
1624                            {
1625                               StripQuotes(item.value, item.value);
1626                               arg.val = item.value;
1627                            }
1628                            else
1629                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1630                         }
1631                         else
1632                            _dpl(0, "Bad frame args item");
1633                      }
1634                      argumentTokens.RemoveAll();
1635                   }
1636                   frame.argsCount = argsTokens.count;
1637                   argsTokens.RemoveAll();
1638                }
1639             }
1640             else if(!strcmp(item.name, "from"))
1641                frame.from = item.value;
1642             else if(!strcmp(item.name, "file"))
1643                frame.file = item.value;
1644             else if(!strcmp(item.name, "line"))
1645                frame.line = atoi(item.value);
1646             else if(!strcmp(item.name, "fullname"))
1647                frame.absoluteFile = item.value;
1648             /*{
1649                // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1650                String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1651                if(strcmp(frame.file, path))
1652                {
1653                   frame.file = path;
1654                   frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
1655                }
1656                delete path;
1657             }*/
1658             else
1659                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1660          }
1661          else
1662             _dpl(0, "Bad frame");
1663       }
1664       
1665       delete frameTokens;
1666       delete argsTokens;
1667       delete argumentTokens;
1668       delete item;
1669    }
1670
1671    Breakpoint GetBreakpointById(int id, bool * isInternal)
1672    {
1673       Breakpoint bp = null;
1674       //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1675       if(isInternal)
1676          *isInternal = false;
1677       if(id)
1678       {
1679          for(i : sysBPs; i.bp && i.bp.id == id)
1680          {
1681             if(isInternal)
1682                *isInternal = true;
1683             bp = i;
1684             break;
1685          }
1686          if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1687             bp = bpRunToCursor;
1688          if(!bp)
1689          {
1690             for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1691             {
1692                bp = i;
1693                break;
1694             }
1695          }
1696       }
1697       return bp;
1698    }
1699
1700    GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1701    {
1702       int i;
1703       GdbDataBreakpoint bp { };
1704       DebugListItem item { };
1705       Array<char *> bpTokens { minAllocSize = 16 };
1706       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1707       string = StripCurlies(string);
1708       TokenizeList(string, ',', bpTokens);
1709       for(i = 0; i < bpTokens.count; i++)
1710       {
1711          if(TokenizeListItem(bpTokens[i], item))
1712          {
1713             StripQuotes(item.value, item.value);
1714             if(!strcmp(item.name, "number"))
1715             {
1716                if(!strchr(item.value, '.'))
1717                   bp.id = atoi(item.value);
1718                bp.number = item.value;
1719             }
1720             else if(!strcmp(item.name, "type"))
1721                bp.type = item.value;
1722             else if(!strcmp(item.name, "disp"))
1723                bp.disp = item.value;
1724             else if(!strcmp(item.name, "enabled"))
1725                bp.enabled = (!strcmpi(item.value, "y"));
1726             else if(!strcmp(item.name, "addr"))
1727             {
1728                if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1729                {
1730                   int c = 1;
1731                   Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1732                   while(outTokens.count > ++c)
1733                   {
1734                      GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1735                      bpArray.Add(multBp);
1736                   }
1737                }
1738                else
1739                   bp.addr = item.value;
1740             }
1741             else if(!strcmp(item.name, "func"))
1742                bp.func = item.value;
1743             else if(!strcmp(item.name, "file"))
1744                bp.file = item.value;
1745             else if(!strcmp(item.name, "fullname"))
1746                bp.fullname = item.value;
1747             else if(!strcmp(item.name, "line"))
1748                bp.line = atoi(item.value);
1749             else if(!strcmp(item.name, "at"))
1750                bp.at = item.value;
1751             else if(!strcmp(item.name, "times"))
1752                bp.times = atoi(item.value);
1753             else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1754                _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1755             else
1756                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1757          }
1758       }
1759       return bp;
1760    }
1761
1762    void ShowDebuggerViews()
1763    {
1764       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1765       ide.outputView.Show();
1766       ide.outputView.SelectTab(debug);
1767       ide.threadsView.Show();
1768       ide.callStackView.Show();
1769       ide.watchesView.Show();
1770       ide.Update(null);
1771    }
1772
1773    void HideDebuggerViews()
1774    {
1775       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1776       ide.RepositionWindows(true);
1777    }
1778
1779    void ::GdbCommand(bool focus, char * format, ...)
1780    {
1781       if(gdbHandle)
1782       {
1783          // TODO: Improve this limit
1784          static char string[MAX_F_STRING*4];
1785          va_list args;
1786          va_start(args, format);
1787          vsnprintf(string, sizeof(string), format, args);
1788          string[sizeof(string)-1] = 0;
1789          va_end(args);
1790          
1791          gdbReady = false;
1792          ide.debugger.serialSemaphore.TryWait();
1793
1794 #ifdef GDB_DEBUG_CONSOLE
1795          _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1796 #endif
1797 #ifdef GDB_DEBUG_OUTPUT
1798          ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1799 #endif
1800 #ifdef GDB_DEBUG_GUI
1801          if(ide.gdbDialog)
1802             ide.gdbDialog.AddCommand(string);
1803 #endif
1804
1805          strcat(string,"\n");
1806          gdbHandle.Puts(string);
1807
1808          if(focus)
1809             Process_ShowWindows(targetProcessId);
1810
1811          app.Unlock();
1812          ide.debugger.serialSemaphore.Wait();
1813          app.Lock();
1814       } 
1815    }
1816
1817    bool ValidateBreakpoint(Breakpoint bp)
1818    {
1819       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1820       if(modules && bp.line && bp.bp)
1821       {
1822          if(bp.bp.line != bp.line)
1823          {
1824             if(!bp.bp.line)
1825             {
1826 #ifdef _DEBUG
1827                //here
1828                ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1829 #endif
1830                //UnsetBreakpoint(bp);
1831                //bp.enabled = false;
1832                return false;
1833             }
1834             else
1835             {
1836                //here
1837                ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1838                bp.line = bp.bp.line;
1839             }
1840          }
1841       }
1842       return true;
1843    }
1844
1845    void GdbBreakpointsInsert()
1846    {
1847       //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GdbBreakpointsInsert()");
1848       if(symbols)
1849       {
1850          if(userAction != stepOut && (userAction != stepOver || state == loaded))
1851          {
1852             DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1853             for(bp : sysBPs; !bp.inserted)
1854             {
1855                bool insert = false;
1856                if(bp.type == internalModulesLoaded)
1857                {
1858                   char path[MAX_LOCATION];
1859                   char name[MAX_LOCATION];
1860                   char fixedModuleName[MAX_FILENAME];
1861                   char line[16384];
1862                   int lineNumber;
1863                   bool moduleLoadBlock = false;
1864                   File f;
1865                   ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1866                   snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1867                   name[sizeof(name)-1] = 0;
1868                   strcpy(path, ide.workspace.projectDir);
1869                   PathCatSlash(path, objDir.dir);
1870                   PathCatSlash(path, name);
1871                   f = FileOpen(path, read);
1872                   if(f)
1873                   {
1874                      for(lineNumber = 1; !f.Eof(); lineNumber++)
1875                      {
1876                         if(f.GetLine(line, sizeof(line) - 1))
1877                         {
1878                            bool moduleLoadLine;
1879                            TrimLSpaces(line, line);
1880                            moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1881                            if(!moduleLoadBlock && moduleLoadLine)
1882                               moduleLoadBlock = true;
1883                            else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1884                               break;
1885                         }
1886                      }
1887                      if(!f.Eof())
1888                      {
1889                         char relative[MAX_LOCATION];
1890                         bp.absoluteFilePath = path;
1891                         MakePathRelative(path, ide.workspace.projectDir, relative);
1892                         bp.relativeFilePath = relative;
1893                         bp.line = lineNumber;
1894                         insert = true;
1895                      }
1896                      delete f;
1897                   }
1898                }
1899                else if(bp.type == internalModuleLoad)
1900                {
1901                   if(modules)
1902                   {
1903                      for(prj : ide.workspace.projects)
1904                      {
1905                         if(!strcmp(prj.moduleName, "ecere"))
1906                         {
1907                            ProjectNode node = prj.topNode.Find("instance.c", false);
1908                            if(node)
1909                            {
1910                               char path[MAX_LOCATION];
1911                               char relative[MAX_LOCATION];
1912                               node.GetFullFilePath(path);
1913                               bp.absoluteFilePath = path;
1914                               MakePathRelative(path, prj.topNode.path, relative);
1915                               bp.relativeFilePath = relative;
1916                               insert = true;
1917                               break;
1918                            }
1919                         }
1920                      }
1921                   }
1922                }
1923                else
1924                   insert = true;
1925                if(insert)
1926                   SetBreakpoint(bp, false);
1927             }
1928             delete objDir;
1929          }
1930
1931          if(bpRunToCursor && !bpRunToCursor.inserted)
1932             SetBreakpoint(bpRunToCursor, false);
1933
1934          if(!ignoreBreakpoints)
1935          {
1936             for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1937             {
1938                if(bp.enabled)
1939                {
1940                   if(!SetBreakpoint(bp, false))
1941                      SetBreakpoint(bp, true);
1942                }
1943                else
1944                {
1945 #ifdef _DEBUG
1946                   if(bp.bp)
1947                      _dpl(0, "problem");
1948 #endif
1949                   bp.bp = GdbDataBreakpoint { };
1950                }
1951             }
1952          }
1953       }
1954    }
1955
1956    void UnsetBreakpoint(Breakpoint bp)
1957    {
1958       char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1959       if(symbols && bp.inserted)
1960       {
1961          GdbCommand(false, "-break-delete %s", bp.bp.number);
1962          bp.inserted = false;
1963          bp.bp = { };
1964       }
1965    }
1966
1967    bool SetBreakpoint(Breakpoint bp, bool removePath)
1968    {
1969       char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
1970       breakpointError = false;
1971       if(symbols)
1972       {
1973          char * location = bp.CopyLocationString(removePath);
1974          sentBreakInsert = true;
1975          GdbCommand(false, "-break-insert %s", location);
1976          delete location;
1977          if(!breakpointError)
1978          {
1979             if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
1980             {
1981                int count = 0;
1982                GdbDataBreakpoint first = null;
1983                for(n : bpItem.multipleBPs)
1984                {
1985                   if(!fstrcmp(n.fullname, bp.absoluteFilePath))
1986                   {
1987                      count++;
1988                      if(!first)
1989                         first = n;
1990                   }
1991                   else
1992                   {
1993                      if(n.enabled)
1994                      {
1995                         GdbCommand(false, "-break-disable %s", n.number);
1996                         n.enabled = false;
1997                      }
1998                      else
1999                         _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2000                   }
2001                }
2002                if(first)
2003                {
2004                   bpItem.addr = first.addr;
2005                   bpItem.func = first.func;
2006                   bpItem.file = first.file;
2007                   bpItem.fullname = first.fullname;
2008                   bpItem.line = first.line;
2009                   //bpItem.thread-groups = first.thread-groups;
2010                   bpItem.multipleBPs.Free();
2011                   delete bpItem.multipleBPs;
2012                }
2013                else if(count == 0)
2014                   _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2015                else
2016                   _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2017             }
2018             bp.bp = bpItem;
2019             bpItem = null;
2020             bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2021             if(bp.inserted)
2022                ValidateBreakpoint(bp);
2023             /*if(bp == bpRunToCursor)
2024                runToCursorDebugStart = false;*/
2025          }
2026       }
2027       return !breakpointError;
2028    }
2029
2030    void GdbBreakpointsDelete(bool deleteRunToCursor, bool deleteInternalBreakpoints, bool deleteUserBreakpoints)
2031    {
2032       _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GdbBreakpointsDelete(deleteRunToCursor(", deleteRunToCursor, "))");
2033       if(symbols)
2034       {
2035          if(deleteInternalBreakpoints)
2036          {
2037             for(bp : sysBPs; bp.inserted)
2038                UnsetBreakpoint(bp);
2039          }
2040          if(deleteUserBreakpoints)
2041          {
2042             for(bp : ide.workspace.breakpoints; bp.inserted)
2043                UnsetBreakpoint(bp);
2044          }
2045          if(deleteRunToCursor && bpRunToCursor && bpRunToCursor.inserted)
2046             UnsetBreakpoint(bpRunToCursor);
2047       }
2048    }
2049
2050    void GdbGetStack()
2051    {
2052       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2053       activeFrame = null;
2054       stackFrames.Free(Frame::Free);
2055       GdbCommand(false, "-stack-info-depth");
2056       if(!frameCount)
2057          GdbCommand(false, "-stack-info-depth 192");
2058       if(frameCount && frameCount <= 192)
2059          GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2060       else
2061       {
2062          GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2063          GdbCommand(false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2064       }
2065       GdbCommand(false, "");
2066    }
2067
2068    bool GdbTargetSet()
2069    {
2070       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2071       if(!targeted)
2072       {
2073          char escaped[MAX_LOCATION];
2074          strescpy(escaped, targetFile);
2075          GdbCommand(false, "file \"%s\"", escaped);  //GDB/MI Missing Implementation -symbol-file, -target-attach
2076
2077          if(!symbols)
2078             return true;
2079
2080          if(usingValgrind)
2081          {
2082             const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2083             //GdbCommand(false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2084             printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2085             GdbCommand(false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2086          }
2087
2088          /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2089             GdbCommand(false, "-environment-directory \"%s\"", prj.topNode.path);*/
2090
2091          for(dir : ide.workspace.sourceDirs; dir && dir[0])
2092          {
2093            bool interference = false;
2094            for(prj : ide.workspace.projects)
2095            {
2096               if(!fstrcmp(prj.topNode.path, dir))
2097               {
2098                  interference = true;
2099                  break;
2100               }
2101            }
2102            if(!interference && dir[0])
2103               GdbCommand(false, "-environment-directory \"%s\"", dir);
2104          }
2105
2106          targeted = true;
2107       }
2108       return true;
2109    }
2110
2111    /*void GdbTargetRelease()
2112    {
2113       if(targeted)
2114       {
2115          GdbBreakpointsDelete(true, true, true);
2116          GdbCommand(false, "file");  //GDB/MI Missing Implementation -target-detach
2117          targeted = false;
2118          symbols = true;
2119       }
2120    }*/
2121
2122    void GdbDebugBreak(bool internal)
2123    {
2124       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2125       if(targetProcessId)
2126       {
2127          if(internal)
2128             breakType = DebuggerAction::internal;
2129
2130          if(ide) ide.Update(null);
2131          app.Unlock();
2132          if(Process_Break(targetProcessId))  //GdbCommand(false, "-exec-interrupt");
2133             serialSemaphore.Wait();
2134          else
2135          {
2136             _ChangeState(loaded);
2137             targetProcessId = 0;
2138          }
2139          app.Lock();
2140       }
2141       else
2142          ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2143    }
2144
2145    void GdbExecRun()
2146    {
2147       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2148       GdbTargetSet();
2149       GdbExecCommon();
2150       ShowDebuggerViews();
2151       if(usingValgrind)
2152          GdbCommand(true, "-exec-continue");
2153       else
2154          GdbCommand(true, "-exec-run");
2155    }
2156
2157    void GdbExecContinue(bool focus)
2158    {
2159       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2160       GdbExecCommon();
2161       GdbCommand(focus, "-exec-continue");
2162    }
2163
2164    void GdbExecNext()
2165    {
2166       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2167       GdbExecCommon();
2168       GdbCommand(true, "-exec-next");
2169    }
2170
2171    void GdbExecStep()
2172    {
2173       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2174       GdbExecCommon();
2175       GdbCommand(true, "-exec-step");
2176    }
2177
2178    void GdbExecFinish()
2179    {
2180       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2181       GdbExecCommon();
2182       GdbCommand(true, "-exec-finish");
2183    }
2184
2185    void GdbExecCommon()
2186    {
2187       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2188       GdbBreakpointsInsert();
2189    }
2190
2191 #ifdef GDB_DEBUG_GUI
2192    void SendGDBCommand(char * command)
2193    {
2194       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2195       DebuggerState oldState = state;
2196       switch(state)
2197       {
2198          case running:
2199             if(targetProcessId)
2200                GdbDebugBreak(true);
2201          case stopped:
2202          case loaded:
2203             GdbCommand(false, command);
2204             break;
2205       }
2206       if(oldState == running)
2207          GdbExecContinue(false);
2208    }
2209 #endif
2210
2211    void ClearBreakDisplay()
2212    {
2213       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2214       activeThread = 0;
2215       activeFrameLevel = -1;
2216       hitThread = 0;
2217       signalThread = 0;
2218       signalOn = false;
2219       frameCount = 0;
2220       if(stopItem)
2221          stopItem.Free();
2222       delete stopItem;
2223       event = none;
2224       activeFrame = null;
2225       stackFrames.Free(Frame::Free);
2226       WatchesCodeEditorLinkRelease();
2227       ide.callStackView.Clear();
2228       ide.threadsView.Clear();
2229       ide.Update(null);
2230    }
2231
2232    bool GdbAbortExec()
2233    {
2234       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2235       sentKill = true;
2236       GdbCommand(false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2237       return true;
2238    }
2239
2240    bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2241    {
2242       bool result = true;
2243       char oldDirectory[MAX_LOCATION];
2244       char tempPath[MAX_LOCATION];
2245       char command[MAX_F_STRING*4];
2246       Project project = ide.project;
2247       DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2248       PathBackup pathBackup { };
2249
2250       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2251       if(currentCompiler != compiler)
2252       {
2253          delete currentCompiler;
2254          currentCompiler = compiler;
2255          incref currentCompiler;
2256       }
2257       prjConfig = config;
2258       this.bitDepth = bitDepth;
2259       usingValgrind = useValgrind;
2260
2261       _ChangeState(loaded);
2262       sentKill = false;
2263       sentBreakInsert = false;
2264       breakpointError = false;
2265       ignoreBreakpoints = false;
2266       symbols = true;
2267       targeted = false;
2268       modules = false;
2269       needReset = false;
2270       
2271       ide.outputView.ShowClearSelectTab(debug);
2272       ide.outputView.debugBox.Logf($"Starting debug mode\n");
2273
2274 #ifdef GDB_DEBUG_OUTPUT
2275       ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2276 #endif
2277
2278       strcpy(tempPath, ide.workspace.projectDir);
2279       PathCatSlash(tempPath, targetDirExp.dir);
2280       delete targetDir;
2281       targetDir = CopyString(tempPath);
2282       project.CatTargetFileName(tempPath, compiler, config);
2283       delete targetFile;
2284       targetFile = CopyString(tempPath);
2285
2286       GetWorkingDir(oldDirectory, MAX_LOCATION);
2287       if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2288       {
2289          char temp[MAX_LOCATION];
2290          strcpy(temp, ide.workspace.projectDir);
2291          PathCatSlash(temp, ide.workspace.debugDir);
2292          ChangeWorkingDir(temp);
2293       }
2294       else
2295          ChangeWorkingDir(ide.workspace.projectDir);
2296       
2297       ide.SetPath(true, compiler, config, bitDepth);
2298
2299       // TODO: This pollutes the environment, but at least it works
2300       // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2301       // What is the proper solution for this? DualPipeOpenEnv?
2302       // gdb set environment commands don't seem to take effect
2303       for(e : ide.workspace.environmentVars)
2304       {
2305          SetEnvironment(e.name, e.string);
2306       }
2307
2308       if(usingValgrind)
2309       {
2310          char * clArgs = ide.workspace.commandLineArgs;
2311          const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2312          ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2313          int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2314          bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2315          vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2316          if(vgLogFile)
2317          {
2318             incref vgLogFile;
2319             vgLogThread.Create();
2320          }
2321          else
2322          {
2323             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2324             result = false;
2325          }
2326          if(result && !CheckCommandAvailable(valgrindCommand))
2327          {
2328             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2329             result = false;
2330          }
2331          if(result)
2332          {
2333             char * vgRedzoneSizeFlag = vgRedzoneSize == -1 ? "" : PrintString(" --redzone-size=", vgRedzoneSize);
2334             sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2335                   valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSizeFlag, vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2336             if(vgRedzoneSize != -1)
2337                delete vgRedzoneSizeFlag;
2338             vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2339             if(!vgTargetHandle)
2340             {
2341                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2342                result = false;
2343             }
2344          }
2345          if(result)
2346          {
2347             incref vgTargetHandle;
2348             vgTargetThread.Create();
2349
2350             targetProcessId = vgTargetHandle.GetProcessID();
2351             waitingForPID = false;
2352             if(!targetProcessId)
2353             {
2354                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2355                result = false;
2356             }
2357          }
2358          if(result)
2359          {
2360             app.Unlock();
2361             serialSemaphore.Wait();
2362             app.Lock();
2363          }
2364       }
2365
2366       if(result)
2367       {
2368          strcpy(command,
2369             (compiler.targetPlatform == win32 && bitDepth == 64) ? "x86_64-w64-mingw32-gdb" :
2370             (compiler.targetPlatform == win32 && bitDepth == 32) ? "i686-w64-mingw32-gdb" :
2371             "gdb");
2372          if(!CheckCommandAvailable(command))
2373          {
2374             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2375             result = false;
2376          }
2377          else
2378          {
2379             strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2380             gdbTimer.Start();
2381             gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2382             if(!gdbHandle)
2383             {
2384                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2385                result = false;
2386             }
2387          }
2388       }
2389       if(result)
2390       {
2391          incref gdbHandle;
2392          gdbThread.Create();
2393
2394          gdbProcessId = gdbHandle.GetProcessID();
2395          if(!gdbProcessId)
2396          {
2397             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2398             result = false;
2399          }
2400       }
2401       if(result)
2402       {
2403          app.Unlock();
2404          serialSemaphore.Wait();
2405          app.Lock();
2406
2407          GdbCommand(false, "-gdb-set verbose off");
2408          //GdbCommand(false, "-gdb-set exec-done-display on");
2409          GdbCommand(false, "-gdb-set step-mode off");
2410          GdbCommand(false, "-gdb-set unwindonsignal on");
2411          //GdbCommand(false, "-gdb-set shell on");
2412          GdbCommand(false, "set print elements 992");
2413          GdbCommand(false, "-gdb-set backtrace limit 100000");
2414
2415          if(!GdbTargetSet())
2416          {
2417             //_ChangeState(terminated);
2418             result = false;
2419          }
2420       }
2421       if(result)
2422       {
2423 #if defined(__unix__)
2424          {
2425             CreateTemporaryDir(progFifoDir, "ecereide");
2426             strcpy(progFifoPath, progFifoDir);
2427             PathCat(progFifoPath, "ideprogfifo");
2428             if(!mkfifo(progFifoPath, 0600))
2429             {
2430                //fileCreated = true;
2431             }
2432             else
2433             {
2434                //app.Lock();
2435                ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2436                //app.Unlock();
2437             }
2438          }
2439
2440          if(!usingValgrind)
2441          {
2442             progThread.terminate = false;
2443             progThread.Create();
2444          }
2445 #endif
2446
2447 #if defined(__WIN32__)
2448          GdbCommand(false, "-gdb-set new-console on");
2449 #endif
2450
2451 #if defined(__unix__)
2452          if(!usingValgrind)
2453             GdbCommand(false, "-inferior-tty-set %s", progFifoPath);
2454 #endif
2455
2456          if(!usingValgrind)
2457             GdbCommand(false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2458          /*
2459          for(e : ide.workspace.environmentVars)
2460          {
2461             GdbCommand(false, "set environment %s=%s", e.name, e.string);
2462          }
2463          */
2464       }
2465
2466       ChangeWorkingDir(oldDirectory);
2467
2468       delete pathBackup;
2469
2470       if(!result)
2471          GdbExit();
2472       delete targetDirExp;
2473       return result;
2474    }
2475
2476    void GdbExit()
2477    {
2478       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2479       if(gdbHandle && gdbProcessId)
2480       {
2481          GdbCommand(false, "-gdb-exit");
2482
2483          if(gdbThread)
2484          {
2485             app.Unlock();
2486             gdbThread.Wait();
2487             app.Lock();
2488          }
2489          if(gdbHandle)
2490          {
2491             gdbHandle.Wait();
2492             delete gdbHandle;
2493          }
2494       }
2495       gdbTimer.Stop();
2496       _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2497       prjConfig = null;
2498       needReset = false;
2499
2500       if(ide.workspace)
2501       {
2502          for(bp : ide.workspace.breakpoints)
2503          {
2504             bp.inserted = false;
2505             delete bp.bp;
2506          }
2507       }
2508       for(bp : sysBPs)
2509       {
2510          bp.inserted = false;
2511          delete bp.bp;
2512       }
2513       if(bpRunToCursor)
2514       {
2515          bpRunToCursor.inserted = false;
2516          delete bpRunToCursor.bp;
2517       }
2518       
2519       ide.outputView.debugBox.Logf($"Debugging stopped\n");
2520       ClearBreakDisplay();
2521       ide.Update(null);
2522
2523 #if defined(__unix__)
2524       if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2525       {
2526          progThread.terminate = true;
2527          if(fifoFile)
2528          {
2529             fifoFile.CloseInput();
2530             app.Unlock();
2531             progThread.Wait();
2532             app.Lock();
2533             delete fifoFile;
2534          }         
2535          DeleteFile(progFifoPath);
2536          progFifoPath[0] = '\0';
2537          rmdir(progFifoDir);
2538       }
2539 #endif
2540    }
2541
2542    void WatchesCodeEditorLinkInit()
2543    {
2544       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesCodeEditorLinkInit()");
2545       /*
2546       char tempPath[MAX_LOCATION];
2547       char path[MAX_LOCATION];
2548       
2549       //void MakeFilePathProjectRelative(char * path, char * relativePath)
2550       if(!ide.projectView.project.GetRelativePath(activeFrame.file, tempPath))
2551          strcpy(tempPath, activeFrame.file);
2552       
2553       strcpy(path, ide.workspace.projectDir);
2554       PathCat(path, tempPath);
2555       codeEditor = (CodeEditor)ide.OpenFile(path, Normal, false, null, no, normal, false);
2556       if(!codeEditor)
2557       {
2558          for(srcDir : ide.workspace.sourceDirs)
2559          {
2560             strcpy(path, srcDir);
2561             PathCat(path, tempPath);
2562             codeEditor = (CodeEditor)ide.OpenFile(path, Normal, false, null, no, normal, false);
2563             if(codeEditor) break;
2564          }
2565       }
2566       */
2567
2568       /*if(activeFrame && !activeFrame.absoluteFile && activeFrame.file)
2569          activeFrame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(activeFrame.file);*/
2570       if(!activeFrame || !activeFrame.absoluteFile)
2571          codeEditor = null;
2572       else
2573          codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, normal, false, null, no, normal, false);
2574       if(codeEditor)
2575       {
2576          codeEditor.inUseDebug = true;
2577          incref codeEditor;
2578       }
2579       //watchesInit = true;
2580    }
2581
2582    void WatchesCodeEditorLinkRelease()
2583    {
2584       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesCodeEditorLinkRelease()");
2585       //if(watchesInit)
2586       {
2587          if(codeEditor)
2588          {
2589             codeEditor.inUseDebug = false;
2590             if(!codeEditor.visible)
2591                codeEditor.Destroy(0);
2592             delete codeEditor;
2593          }
2594       }
2595    }
2596
2597    bool ResolveWatch(Watch wh)
2598    {
2599       bool result = false;
2600       
2601       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2602       wh.Reset();
2603
2604       /*delete wh.value;
2605       if(wh.type) 
2606       {
2607          FreeType(wh.type);
2608          wh.type = null;
2609       }*/
2610
2611       if(wh.expression)
2612       {
2613          char watchmsg[MAX_F_STRING];
2614          if(state == stopped && !codeEditor)
2615             wh.value = CopyString($"No source file found for selected frame");
2616          //if(codeEditor && state == stopped || state != stopped)
2617          else
2618          {
2619             Module backupPrivateModule;
2620             Context backupContext;
2621             Class backupThisClass;
2622             Expression exp;
2623             parseError = false;
2624
2625             backupPrivateModule = GetPrivateModule();
2626             backupContext = GetCurrentContext();
2627             backupThisClass = GetThisClass();
2628             if(codeEditor)
2629             {
2630                SetPrivateModule(codeEditor.privateModule);
2631                SetCurrentContext(codeEditor.globalContext);
2632                SetTopContext(codeEditor.globalContext);
2633                SetGlobalContext(codeEditor.globalContext);
2634                SetGlobalData(&codeEditor.globalData);
2635             }
2636          
2637             exp = ParseExpressionString(wh.expression);
2638             
2639             if(exp && !parseError)
2640             {
2641                char expString[4096];
2642                expString[0] = 0;
2643                PrintExpression(exp, expString);
2644
2645                if(GetPrivateModule())
2646                {
2647                   if(codeEditor)
2648                      DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2649                   ProcessExpressionType(exp);
2650                }
2651                wh.type = exp.expType;
2652                if(wh.type)
2653                   wh.type.refCount++;
2654                DebugComputeExpression(exp);
2655                if(ExpressionIsError(exp))
2656                {
2657                   GDBFallBack(exp, expString);
2658                }
2659
2660                /*if(exp.hasAddress)
2661                {
2662                   char temp[MAX_F_STRING];
2663                   sprintf(temp, "0x%x", exp.address);
2664                   wh.address = CopyString(temp);
2665                   // wh.address = CopyStringf("0x%x", exp.address);
2666                }*/
2667 /*
2668 //#ifdef _DEBUG
2669                {
2670                   Type dataType = exp.expType;
2671                   if(dataType)
2672                   {
2673                      char temp[MAX_F_STRING];
2674                      switch(dataType.kind)
2675                      {
2676                         case charType:
2677                            sprintf(temp, "%i", exp.val.c);
2678                            break;
2679                         case shortType:
2680                            sprintf(temp, "%i", exp.val.s);
2681                            break;
2682                         case intType:
2683                         case longType:
2684                         case enumType:
2685                            sprintf(temp, "%i", exp.val.i);
2686                            break;
2687                         case int64Type:
2688                            sprintf(temp, "%i", exp.val.i64);
2689                            break;
2690                         case pointerType:
2691                            sprintf(temp, "%i", exp.val.p);
2692                            break;
2693
2694                         case floatType:
2695                         {
2696                            long v = (long)exp.val.f;
2697                            sprintf(temp, "%i", v);
2698                            break;
2699                         } 
2700                         case doubleType:
2701                         {
2702                            long v = (long)exp.val.d;
2703                            sprintf(temp, "%i", v);
2704                            break;
2705                         }
2706                      }
2707                      if(temp)
2708                         wh.intVal = CopyString(temp);
2709                      switch(dataType.kind)
2710                      {
2711                         case charType:
2712                            sprintf(temp, "0x%x", exp.val.c);
2713                            break;
2714                         case shortType:
2715                            sprintf(temp, "0x%x", exp.val.s);
2716                            break;
2717                         case enumType:
2718                         case intType:
2719                            sprintf(temp, "0x%x", exp.val.i);
2720                            break;
2721                         case int64Type:
2722                            sprintf(temp, "0x%x", exp.val.i64);
2723                            break;
2724                         case longType:
2725                            sprintf(temp, "0x%x", exp.val.i64);
2726                            break;
2727                         case pointerType:
2728                            sprintf(temp, "0x%x", exp.val.p);
2729                            break;
2730
2731                         case floatType:
2732                         {
2733                            long v = (long)exp.val.f;
2734                            sprintf(temp, "0x%x", v);
2735                            break;
2736                         } 
2737                         case doubleType:
2738                         {
2739                            long v = (long)exp.val.d;
2740                            sprintf(temp, "0x%x", v);
2741                            break;
2742                         }
2743                      }
2744                      if(temp)
2745                         wh.hexVal = CopyString(temp);
2746                      switch(dataType.kind)
2747                      {
2748                         case charType:
2749                            sprintf(temp, "0o%o", exp.val.c);
2750                            break;
2751                         case shortType:
2752                            sprintf(temp, "0o%o", exp.val.s);
2753                            break;
2754                         case enumType:
2755                         case intType:
2756                            sprintf(temp, "0o%o", exp.val.i);
2757                            break;
2758                         case int64Type:
2759                            sprintf(temp, "0o%o", exp.val.i64);
2760                            break;
2761                         case longType:
2762                            sprintf(temp, "0o%o", exp.val.i64);
2763                            break;
2764                         case pointerType:
2765                            sprintf(temp, "0o%o", exp.val.p);
2766                            break;
2767
2768                         case floatType:
2769                         {
2770                            long v = (long)exp.val.f;
2771                            sprintf(temp, "0o%o", v);
2772                            break;
2773                         } 
2774                         case doubleType:
2775                         {
2776                            long v = (long)exp.val.d;
2777                            sprintf(temp, "0o%o", v);
2778                            break;
2779                         }
2780                      }
2781                      if(temp)
2782                         wh.octVal = CopyString(temp);
2783                   }
2784                }
2785                // WHATS THIS HERE ?
2786                if(exp.type == constantExp && exp.constant)
2787                   wh.constant = CopyString(exp.constant);
2788 //#endif
2789 */
2790
2791                switch(exp.type)
2792                {
2793                   case symbolErrorExp:
2794                      snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2795                      break;
2796                   case structMemberSymbolErrorExp:
2797                      // todo get info as in next case (ExpClassMemberSymbolError)
2798                      snprintf(watchmsg, sizeof(watchmsg), $"Error: Struct member not found for \"%s\"", wh.expression);
2799                      break;
2800                   case classMemberSymbolErrorExp:
2801                      {
2802                         Class _class;
2803                         Expression memberExp = exp.member.exp;
2804                         Identifier memberID = exp.member.member;
2805                         Type type = memberExp.expType;
2806                         if(type)
2807                         {
2808                            _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2809                            if(!_class)
2810                            {
2811                               char string[256] = "";
2812                               Symbol classSym;
2813                               PrintTypeNoConst(type, string, false, true);
2814                               classSym = FindClass(string);
2815                               _class = classSym ? classSym.registered : null;
2816                            }
2817                            if(_class)
2818                               snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2819                            else
2820                               snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in unregistered class? (Should never get this message)", memberID ? memberID.string : "");
2821                         }
2822                         else
2823                            snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in no type? (Should never get this message)", memberID ? memberID.string : "");
2824                      }
2825                      break;
2826                   case memoryErrorExp:
2827                      // Need to ensure when set to memoryErrorExp, constant is set
2828                      snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
2829                      break;
2830                   case dereferenceErrorExp:
2831                      snprintf(watchmsg, sizeof(watchmsg), $"Dereference failure for \"%s\"", wh.expression);
2832                      break;
2833                   case unknownErrorExp:
2834                      snprintf(watchmsg, sizeof(watchmsg), $"Unknown error for \"%s\"", wh.expression);
2835                      break;
2836                   case noDebuggerErrorExp:
2837                      snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
2838                      break;
2839                   case debugStateErrorExp:
2840                      snprintf(watchmsg, sizeof(watchmsg), $"Incorrect debugger state for symbol evaluation in \"%s\"", wh.expression);
2841                      break;
2842                   case 0:
2843                      snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
2844                      break;
2845                   case constantExp:
2846                   case stringExp:
2847                      // Temporary Code for displaying Strings
2848                      if((exp.expType && ((exp.expType.kind == pointerType || 
2849                               exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) || 
2850                            (wh.type && wh.type.kind == classType && wh.type._class && 
2851                               wh.type._class.registered && wh.type._class.registered.type == normalClass &&
2852                               !strcmp(wh.type._class.registered.name, "String")))
2853                      {
2854
2855                         if(exp.expType.kind != arrayType || exp.hasAddress)
2856                         {
2857                            uint64 address;
2858                            char * string;
2859                            char value[4196];
2860                            int len;
2861                            //char temp[MAX_F_STRING * 32];
2862
2863                            ExpressionType evalError = dummyExp;
2864                            /*if(exp.expType.kind == arrayType)
2865                               sprintf(temp, "(char*)0x%x", exp.address);
2866                            else
2867                               sprintf(temp, "(char*)%s", exp.constant);*/
2868
2869                            //evaluation = Debugger::EvaluateExpression(temp, &evalError);
2870                            // address = strtoul(exp.constant, null, 0);
2871                            address = _strtoui64(exp.constant, null, 0);
2872                            //_dpl(0, "0x", address);
2873                            // snprintf(value, sizeof(value), "0x%08x ", address);
2874
2875                            if(address > 0xFFFFFFFFLL)
2876                               snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
2877                            else
2878                               snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
2879                            value[sizeof(value)-1] = 0;
2880                            
2881                            if(!address)
2882                               strcat(value, $"Null string");
2883                            else
2884                            {
2885                               int size = 4096;
2886                               len = strlen(value);
2887                               string = null;
2888                               while(!string && size > 2)
2889                               {
2890                                  string = GdbReadMemory(address, size);
2891                                  size /= 2;
2892                               }
2893                               if(string && string[0])
2894                               {
2895                                  value[len++] = '(';
2896                                  if(UTF8Validate(string))
2897                                  {
2898                                     int c;
2899                                     char ch;
2900                                     
2901                                     for(c = 0; (ch = string[c]) && c<4096; c++)
2902                                        value[len++] = ch;                                 
2903                                     value[len++] = ')';
2904                                     value[len++] = '\0';
2905                                     
2906                                  }
2907                                  else
2908                                  {
2909                                     ISO8859_1toUTF8(string, value + len, 4096 - len - 30);
2910                                     strcat(value, ") (ISO8859-1)");
2911                                  }
2912
2913                                  delete string;
2914                               }
2915                               else if(string)
2916                               {
2917                                  strcat(value, $"Empty string");
2918                                  delete string;
2919                               }
2920                               else
2921                                  strcat(value, $"Couldn't read memory");
2922                            }
2923                            wh.value = CopyString(value);
2924                         }
2925                      }
2926                      else if(wh.type && wh.type.kind == classType && wh.type._class && 
2927                               wh.type._class.registered && wh.type._class.registered.type == enumClass)
2928                      {
2929                         uint64 value = strtoul(exp.constant, null, 0);
2930                         Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
2931                         EnumClassData enumeration = (EnumClassData)enumClass.data;
2932                         NamedLink item;
2933                         for(item = enumeration.values.first; item; item = item.next)
2934                            if((int)item.data == value)
2935                               break;
2936                         if(item)
2937                            wh.value = CopyString(item.name);
2938                         else
2939                            wh.value = CopyString($"Invalid Enum Value");
2940                         result = true;
2941                      }
2942                      else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class && 
2943                               wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
2944                      {
2945                         unichar value;
2946                         int signedValue;
2947                         char charString[5];
2948                         char string[256];
2949
2950                         if(exp.constant[0] == '\'')
2951                         {
2952                            if((int)((byte *)exp.constant)[1] > 127)
2953                            {
2954                               int nb;
2955                               value = UTF8GetChar(exp.constant + 1, &nb);
2956                               if(nb < 2) value = exp.constant[1];
2957                               signedValue = value;
2958                            }
2959                            else
2960                            {
2961                               signedValue = exp.constant[1];
2962                               {
2963                                  // Precomp Syntax error with boot strap here:
2964                                  byte b = (byte)(char)signedValue;
2965                                  value = (unichar) b;
2966                               }
2967                            }
2968                         }
2969                         else
2970                         {
2971                            if(wh.type.kind == charType && wh.type.isSigned)
2972                            {
2973                               signedValue = (int)(char)strtol(exp.constant, null, 0);
2974                               {
2975                                  // Precomp Syntax error with boot strap here:
2976                                  byte b = (byte)(char)signedValue;
2977                                  value = (unichar) b;
2978                               }
2979                            }
2980                            else
2981                            {
2982                               value = (uint)strtoul(exp.constant, null, 0);
2983                               signedValue = (int)value;
2984                            }
2985                         }
2986                         charString[0] = 0;
2987                         UTF32toUTF8Len(&value, 1, charString, 5);
2988                         if(value == '\0')
2989                            snprintf(string, sizeof(string), "\'\\0' (0)");
2990                         else if(value == '\t')
2991                            snprintf(string, sizeof(string), "\'\\t' (%d)", value);
2992                         else if(value == '\n')
2993                            snprintf(string, sizeof(string), "\'\\n' (%d)", value);
2994                         else if(value == '\r')
2995                            snprintf(string, sizeof(string), "\'\\r' (%d)", value);
2996                         else if(wh.type.kind == charType && wh.type.isSigned)
2997                            snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
2998                         else if(value > 256 || wh.type.kind != charType)
2999                         {
3000                            if(value > 0x10FFFF || !GetCharCategory(value))
3001                               snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3002                            else
3003                               snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3004                         }
3005                         else
3006                            snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3007                         string[sizeof(string)-1] = 0;
3008                         
3009                         wh.value = CopyString(string);
3010                         result = true;
3011                      }
3012                      else
3013                      {
3014                         wh.value = CopyString(exp.constant);
3015                         result = true;
3016                      }
3017                      break;
3018                   default:
3019                      if(exp.hasAddress)
3020                      {
3021                         wh.value = PrintHexUInt64(exp.address);
3022                         result = true;
3023                      }
3024                      else
3025                      {
3026                         char tempString[256];
3027                         if(exp.member.memberType == propertyMember)
3028                            snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation support for \"%s\"", wh.expression);
3029                         else
3030                            snprintf(watchmsg, sizeof(watchmsg), $"Evaluation failed for \"%s\" of type \"%s\"", wh.expression, 
3031                                  exp.type.OnGetString(tempString, null, null));
3032                      }
3033                      break;
3034                }
3035             }
3036             else
3037                snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3038             if(exp) FreeExpression(exp);
3039
3040             
3041             SetPrivateModule(backupPrivateModule);
3042             SetCurrentContext(backupContext);
3043             SetTopContext(backupContext);
3044             SetGlobalContext(backupContext);
3045             SetThisClass(backupThisClass);
3046          }
3047          //else 
3048          //   wh.value = CopyString("No source file found for selected frame");
3049          
3050          watchmsg[sizeof(watchmsg)-1] = 0;
3051          if(!wh.value)
3052             wh.value = CopyString(watchmsg);
3053       }
3054       ide.watchesView.UpdateWatch(wh);
3055       return result;
3056    }
3057
3058    void EvaluateWatches()
3059    {
3060       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3061       for(wh : ide.workspace.watches)
3062          ResolveWatch(wh);
3063    }
3064
3065    char * ::GdbEvaluateExpression(char * expression)
3066    {
3067       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3068       eval.active = true;
3069       eval.error = none;
3070       GdbCommand(false, "-data-evaluate-expression \"%s\"", expression);
3071       if(eval.active)
3072          ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3073       return eval.result;
3074    }
3075
3076    // to be removed... use GdbReadMemory that returns a byte array instead
3077    char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3078    {
3079       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3080       eval.active = true;
3081       eval.error = none;
3082 #ifdef _DEBUG
3083       if(!size)
3084          _dpl(0, "GdbReadMemoryString called with size = 0!");
3085 #endif
3086       // GdbCommand(false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3087       if(GetRuntimePlatform() == win32)
3088          GdbCommand(false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3089       else
3090          GdbCommand(false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3091       if(eval.active)
3092          ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3093       return eval.result;
3094    }
3095
3096    byte * ::GdbReadMemory(uint64 address, int bytes)
3097    {
3098       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3099       eval.active = true;
3100       eval.error = none;
3101       //GdbCommand(false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3102       if(GetRuntimePlatform() == win32)
3103          GdbCommand(false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3104       else
3105          GdbCommand(false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3106 #ifdef _DEBUG
3107       if(!bytes)
3108          _dpl(0, "GdbReadMemory called with bytes = 0!");
3109 #endif
3110       if(eval.active)
3111          ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3112       else if(eval.result && strcmp(eval.result, "N/A"))
3113       {
3114          byte * result = new byte[bytes];
3115          byte * string = eval.result;
3116          int c = 0;
3117          while(true)
3118          {
3119             result[c++] = (byte)strtol(string, &string, 10);
3120             if(string)
3121             {
3122                if(*string == ',')
3123                   string++;
3124                 else
3125                   break;
3126             }
3127             else
3128                break;
3129          }
3130          return result;
3131       }
3132       return null;
3133    }
3134
3135    bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3136    {
3137       bool result = true;
3138       char * s1 = null; char * s2 = null;
3139       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3140             "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3141             "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3142             "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3143             "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor,  ")");
3144       delete s1; delete s2;
3145
3146       if(bpUser && stopItem.frame.line && bpUser.line != stopItem.frame.line)
3147       {
3148          // updating user breakpoint on hit location difference
3149          // todo, print something?
3150          bpUser.line = stopItem.frame.line;
3151          ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3152          ide.workspace.Save();
3153       }
3154       if(bpInternal)
3155       {
3156          bpInternal.hits++;
3157          if(bpInternal.type == internalModulesLoaded)
3158             modules = true;
3159          if(!bpUser && !userBreakOnInternalBreakpoint)
3160          {
3161             if(userAction == stepOut)//if(prevStopItem.reason == functionFinished)
3162                StepOut(ignoreBreakpoints);
3163             else
3164                result = false;
3165          }
3166       }
3167       if(bpUser)
3168       {
3169          bool conditionMet = true;
3170          bool levelMatch = (bpUser.level == -1 || bpUser.level == frameCount-1);
3171          if(bpUser.condition)
3172             conditionMet = ResolveWatch(bpUser.condition);
3173          bpUser.hits++;
3174          if(levelMatch && conditionMet)
3175          {
3176             if(!bpUser.ignore)
3177                bpUser.breaks++;
3178             else
3179             {
3180                bpUser.ignore--;
3181                result = false;
3182             }
3183          }
3184          else
3185             result = false;
3186          ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3187       }
3188
3189       if(!bpUser && !bpInternal)
3190          result = false;
3191
3192       return result;
3193    }
3194
3195    void ValgrindTargetThreadExit()
3196    {
3197       ide.outputView.debugBox.Logf($"ValgrindTargetThreadExit\n");
3198       if(vgTargetHandle)
3199       {
3200          vgTargetHandle.Wait();
3201          delete vgTargetHandle;
3202       }
3203       HandleExit(null, null);
3204    }
3205
3206    void GdbThreadExit()
3207    {
3208       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3209       if(state != terminated)
3210       {
3211          _ChangeState(terminated);
3212          targetProcessId = 0;
3213          ClearBreakDisplay();
3214
3215          if(vgLogFile)
3216             delete vgLogFile;
3217          if(gdbHandle)
3218          {
3219             serialSemaphore.Release();
3220             gdbTimer.Stop();
3221             gdbHandle.Wait();
3222             delete gdbHandle;
3223             
3224             ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3225             ide.outputView.debugBox.Logf($"Debugging stopped\n");
3226             ide.Update(null);
3227             HideDebuggerViews();
3228          }
3229          //_ChangeState(terminated);
3230       }
3231    }
3232
3233    void GdbThreadMain(char * output)
3234    {
3235       int i;
3236       Array<char *> outTokens { minAllocSize = 50 };
3237       Array<char *> subTokens { minAllocSize = 50 };
3238       DebugListItem item { };
3239       DebugListItem item2 { };
3240       bool setWaitingForPID = false;
3241       
3242 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3243 #ifdef GDB_DEBUG_CONSOLE
3244       _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3245 #endif
3246 #ifdef GDB_DEBUG_OUTPUT
3247       {
3248          int len = strlen(output);
3249          if(len > 1024)
3250          {
3251             int c;
3252             char * start;
3253             char tmp[1025];
3254             tmp[1024] = '\0';
3255             start = output;
3256             for(c = 0; c < len / 1024; c++)
3257             {
3258                strncpy(tmp, start, 1024);
3259                ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3260                start += 1024;
3261             }
3262             ide.outputView.gdbBox.Logf("out: %s\n", start);
3263          }
3264          else
3265          {
3266             ide.outputView.gdbBox.Logf("out: %s\n", output);
3267          }
3268       }
3269 #endif
3270 #ifdef GDB_DEBUG_CONSOLE
3271          strcpy(lastGdbOutput, output);
3272 #endif
3273 #ifdef GDB_DEBUG_GUI
3274          if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3275 #endif
3276 #endif
3277       
3278       switch(output[0])
3279       {
3280          case '~':
3281             if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3282             {
3283                symbols = false;
3284                ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3285                ide.Update(null);
3286             }
3287             break;
3288          case '^':
3289             gdbReady = false;
3290             if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3291             {
3292                //if(outTokens.count == 1)
3293                {
3294                   if(sentKill)
3295                   {
3296                      sentKill = false;
3297                      _ChangeState(loaded);
3298                      targetProcessId = 0;
3299                      if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3300                      {
3301                         if(!strcmp(item.name, "reason"))
3302                         {
3303                            char * reason = item.value;
3304                            StripQuotes(reason, reason);
3305                            if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3306                            {
3307                               char * exitCode;
3308                               if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3309                               {
3310                                  StripQuotes(item2.value, item2.value);
3311                                  if(!strcmp(item2.name, "exit-code"))
3312                                     exitCode = item2.value;
3313                                  else
3314                                     exitCode = null;
3315                               }
3316                               else
3317                                  exitCode = null;
3318                               HandleExit(reason, exitCode);
3319                            }
3320                         }
3321                         else
3322                            _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3323                      }
3324                      else
3325                         HandleExit(null, null);
3326                   }
3327                }
3328                if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3329                {
3330                   if(!strcmp(item.name, "bkpt"))
3331                   {
3332                      sentBreakInsert = false;
3333 #ifdef _DEBUG
3334                      if(bpItem)
3335                         _dpl(0, "problem");
3336 #endif
3337                      bpItem = ParseBreakpoint(item.value, outTokens);
3338                      //breakType = bpValidation;
3339                   }
3340                   else if(!strcmp(item.name, "depth"))
3341                   {
3342                      StripQuotes(item.value, item.value);
3343                      frameCount = atoi(item.value);
3344                      activeFrame = null;
3345                      stackFrames.Free(Frame::Free);
3346                   }
3347                   else if(!strcmp(item.name, "stack"))
3348                   {
3349                      Frame frame;
3350                      if(stackFrames.count)
3351                         ide.callStackView.Logf("...\n");
3352                      else
3353                         activeFrame = null;
3354                      item.value = StripBrackets(item.value);
3355                      TokenizeList(item.value, ',', subTokens);
3356                      for(i = 0; i < subTokens.count; i++)
3357                      {
3358                         if(TokenizeListItem(subTokens[i], item))
3359                         {
3360                            if(!strcmp(item.name, "frame"))
3361                            {
3362                               frame = Frame { };
3363                               stackFrames.Add(frame);
3364                               item.value = StripCurlies(item.value);
3365                               ParseFrame(frame, item.value);
3366                               if(frame.file && frame.from)
3367                                  _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3368                               if(frame.file)
3369                               {
3370                                  char * s;
3371                                  if(activeFrameLevel == -1)
3372                                  {
3373                                     if(ide.projectView.IsModuleInProject(frame.file));
3374                                     {
3375                                        if(frame.level != 0)
3376                                        {
3377                                           //stopItem.frame = frame;
3378                                           breakType = selectFrame;
3379                                        }
3380                                        else
3381                                           activeFrame = frame;
3382                                        activeFrameLevel = frame.level;
3383                                     }
3384                                  }
3385                                  ide.callStackView.Logf("%3d ", frame.level);
3386                                  if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3387                                     ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3388                                  else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3389                                     ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3390                                  else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3391                                     ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3392                                  else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3393                                     ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3394                                  else
3395                                     ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3396                                  delete s;
3397                               }
3398                               else
3399                               {
3400                                  ide.callStackView.Logf("%3d ", frame.level);
3401
3402                                  if(frame.from)
3403                                  {
3404                                     char * s;
3405                                     ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3406                                     delete s;
3407                                  }
3408                                  else if(frame.func)
3409                                     ide.callStackView.Logf("%s\n", frame.func);
3410                                  else
3411                                     ide.callStackView.Logf($"unknown source\n");
3412                               }
3413                            }
3414                            else
3415                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3416                         }
3417                      }
3418                      if(activeFrameLevel == -1)
3419                      {
3420                         activeFrameLevel = 0;
3421                         activeFrame = stackFrames.first;
3422                      }
3423                      ide.callStackView.Home();
3424                      ide.Update(null);
3425                      subTokens.RemoveAll();
3426                   }
3427                   /*else if(!strcmp(item.name, "frame"))
3428                   {
3429                      Frame frame { };
3430                      item.value = StripCurlies(item.value);
3431                      ParseFrame(&frame, item.value);
3432                   }*/
3433                   else if(!strcmp(item.name, "thread-ids"))
3434                   {
3435                      ide.threadsView.Clear();
3436                      item.value = StripCurlies(item.value);
3437                      TokenizeList(item.value, ',', subTokens);
3438                      for(i = subTokens.count - 1; ; i--)
3439                      {
3440                         if(TokenizeListItem(subTokens[i], item))
3441                         {
3442                            if(!strcmp(item.name, "thread-id"))
3443                            {
3444                               int value;
3445                               StripQuotes(item.value, item.value);
3446                               value = atoi(item.value);
3447                               ide.threadsView.Logf("%3d \n", value);
3448                            }
3449                            else
3450                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3451                         }
3452                         if(!i)
3453                            break;
3454                      }
3455                      ide.threadsView.Home();
3456                      ide.Update(null);
3457                      subTokens.RemoveAll();
3458                      //if(!strcmp(outTokens[2], "number-of-threads"))
3459                   }
3460                   else if(!strcmp(item.name, "new-thread-id"))
3461                   {
3462                      StripQuotes(item.value, item.value);
3463                      activeThread = atoi(item.value);
3464                   }
3465                   else if(!strcmp(item.name, "value"))
3466                   {
3467                      StripQuotes(item.value, item.value);
3468                      eval.result = CopyString(item.value);
3469                      eval.active = false;
3470                   }
3471                   else if(!strcmp(item.name, "addr"))
3472                   {
3473                      for(i = 2; i < outTokens.count; i++)
3474                      {
3475                         if(TokenizeListItem(outTokens[i], item))
3476                         {
3477                            if(!strcmp(item.name, "total-bytes"))
3478                            {
3479                               StripQuotes(item.value, item.value);
3480                               eval.bytes = atoi(item.value);
3481                            }
3482                            else if(!strcmp(item.name, "next-row"))
3483                            {
3484                               StripQuotes(item.value, item.value);
3485                               eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3486                            }
3487                            else if(!strcmp(item.name, "memory"))
3488                            {
3489                               int j;
3490                               //int value;
3491                               //StripQuotes(item.value, item.value);
3492                               item.value = StripBrackets(item.value);
3493                               // this should be treated as a list...
3494                               item.value = StripCurlies(item.value);
3495                               TokenizeList(item.value, ',', subTokens);
3496                               for(j = 0; j < subTokens.count; j++)
3497                               {
3498                                  if(TokenizeListItem(subTokens[j], item))
3499                                  {
3500                                     if(!strcmp(item.name, "data"))
3501                                     {
3502                                        item.value = StripBrackets(item.value);
3503                                        StripQuotes2(item.value, item.value);
3504                                        eval.result = CopyString(item.value);
3505                                        eval.active = false;
3506                                     }
3507                                  }
3508                               }
3509                               subTokens.RemoveAll();
3510                            }
3511                         }
3512                      }
3513                   }
3514                   else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3515                      _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3516                   else
3517                      _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3518                }
3519             }
3520             else if(!strcmp(outTokens[0], "^running"))
3521             {
3522                waitingForPID = true;
3523                setWaitingForPID = true;
3524                ClearBreakDisplay();
3525             }
3526             else if(!strcmp(outTokens[0], "^exit"))
3527             {
3528                _ChangeState(terminated);
3529                // ide.outputView.debugBox.Logf("Exit\n");
3530                // ide.Update(null);
3531                gdbReady = true;
3532                serialSemaphore.Release();
3533             }
3534             else if(!strcmp(outTokens[0], "^error"))
3535             {
3536                if(sentBreakInsert)
3537                {
3538                   sentBreakInsert = false;
3539                   breakpointError = true;
3540                }
3541
3542                if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3543                {
3544                   if(!strcmp(item.name, "msg"))
3545                   {
3546                      StripQuotes(item.value, item.value);
3547                      if(eval.active)
3548                      {
3549                         eval.active = false;
3550                         eval.result = null;
3551                         if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3552                            eval.error = symbolNotFound;
3553                         else if(strstr(item.value, "Cannot access memory at address"))
3554                            eval.error = memoryCantBeRead;
3555                         else
3556                            eval.error = unknown;
3557                      }
3558                      else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3559                      {
3560                      }
3561                      else if(!strncmp(item.value, "Cannot access memory at address", 31))
3562                      {
3563                      }
3564                      else if(!strcmp(item.value, "Cannot find bounds of current function"))
3565                      {
3566                         _ChangeState(stopped);
3567                         gdbHandle.Printf("-exec-continue\n");
3568                      }
3569                      else if(!strcmp(item.value, "ptrace: No such process."))
3570                      {
3571                         _ChangeState(loaded);
3572                         targetProcessId = 0;
3573                      }
3574                      else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3575                      {
3576                      }
3577                      else if(!strcmp(item.value, "You can't do that without a process to debug."))
3578                      {
3579                         _ChangeState(loaded);
3580                         targetProcessId = 0;
3581                      }
3582                      else if(strstr(item.value, "No such file or directory."))
3583                      {
3584                         _ChangeState(loaded);
3585                         targetProcessId = 0;
3586                      }
3587                      else if(strstr(item.value, "During startup program exited with code "))
3588                      {
3589                         _ChangeState(loaded);
3590                         targetProcessId = 0;
3591                      }
3592                      else
3593                      {
3594 #ifdef _DEBUG
3595                         if(strlen(item.value) < MAX_F_STRING)
3596                         {
3597                            char * s;
3598                            ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3599                            delete s;
3600                         }
3601                         else
3602                            ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3603 #endif
3604                      }
3605                   }
3606                }
3607                else
3608                   _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3609             }
3610             else
3611                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3612             outTokens.RemoveAll();
3613             break;
3614          case '+':
3615             _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3616             break;
3617          case '=':
3618             if(TokenizeList(output, ',', outTokens))
3619             {
3620                if(!strcmp(outTokens[0], "=library-loaded"))
3621                   FGODetectLoadedLibraryForAddedProjectIssues(outTokens);
3622                else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3623                         !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3624                         !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3625                         !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3626                         !strcmp(outTokens[0], "=breakpoint-modified"))
3627                   _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3628                            outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3629                            outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3630                            outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3631                            outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3632                else
3633                   _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3634             }
3635             outTokens.RemoveAll();
3636             break;
3637          case '*':
3638             gdbReady = false;
3639             if(TokenizeList(output, ',', outTokens))
3640             {
3641                if(!strcmp(outTokens[0],"*running"))
3642                {
3643                   waitingForPID = true;
3644                   setWaitingForPID = true;
3645                }
3646                else if(!strcmp(outTokens[0], "*stopped"))
3647                {
3648                   int tk;
3649                   _ChangeState(stopped);
3650
3651                   for(tk = 1; tk < outTokens.count; tk++)
3652                   {
3653                      if(TokenizeListItem(outTokens[tk], item))
3654                      {
3655                         if(!strcmp(item.name, "reason"))
3656                         {
3657                            char * reason = item.value;
3658                            StripQuotes(reason, reason);
3659                            if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3660                            {
3661                               char * exitCode;
3662                               if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3663                               {
3664                                  tk++;
3665                                  StripQuotes(item2.value, item2.value);
3666                                  if(!strcmp(item2.name, "exit-code"))
3667                                     exitCode = item2.value;
3668                                  else
3669                                     exitCode = null;
3670                               }
3671                               else
3672                                  exitCode = null;
3673                               HandleExit(reason, exitCode);
3674                               needReset = true;
3675                            }
3676                            else if(!strcmp(reason, "breakpoint-hit"))
3677                            {
3678       #ifdef _DEBUG
3679                               if(stopItem)
3680                                  _dpl(0, "problem");
3681       #endif
3682                               stopItem = GdbDataStop { };
3683                               stopItem.reason = breakpointHit;
3684
3685                               for(i = tk+1; i < outTokens.count; i++)
3686                               {
3687                                  TokenizeListItem(outTokens[i], item);
3688                                  StripQuotes(item.value, item.value);
3689                                  if(!strcmp(item.name, "bkptno"))
3690                                     stopItem.bkptno = atoi(item.value);
3691                                  else if(!strcmp(item.name, "thread-id"))
3692                                     stopItem.threadid = atoi(item.value);
3693                                  else if(!strcmp(item.name, "frame"))
3694                                  {
3695                                     item.value = StripCurlies(item.value);
3696                                     ParseFrame(stopItem.frame, item.value);
3697                                  }
3698                                  else if(!strcmp(item.name, "disp") || !strcmp(item.name, "stopped-threads") || !strcmp(item.name, "core"))
3699                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "(", item.name, "=", item.value, ")");
3700                                  else
3701                                     _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown breakpoint hit item name (", item.name, "=", item.value, ")");
3702                               }
3703
3704                               event = hit;
3705                            }
3706                            else if(!strcmp(reason, "end-stepping-range"))
3707                            {
3708       #ifdef _DEBUG
3709                               if(stopItem)
3710                                  _dpl(0, "problem");
3711       #endif
3712                               stopItem = GdbDataStop { };
3713                               stopItem.reason = endSteppingRange;
3714
3715                               for(i = tk+1; i < outTokens.count; i++)
3716                               {
3717                                  TokenizeListItem(outTokens[i], item);
3718                                  StripQuotes(item.value, item.value);
3719                                  if(!strcmp(item.name, "thread-id"))
3720                                     stopItem.threadid = atoi(item.value);
3721                                  else if(!strcmp(item.name, "frame"))
3722                                  {
3723                                     item.value = StripCurlies(item.value);
3724                                     ParseFrame(stopItem.frame, item.value);
3725                                  }
3726                                  else if(!strcmp(item.name, "reason") || !strcmp(item.name, "bkptno"))
3727                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "(", item.name, "=", item.value, ")");
3728                                  else
3729                                     _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown end of stepping range item name (", item.name, "=", item.value, ")");
3730                               }
3731
3732                               event = stepEnd;
3733                               ide.Update(null);
3734                            }
3735                            else if(!strcmp(reason, "function-finished"))
3736                            {
3737       #ifdef _DEBUG
3738                               if(stopItem)
3739                                  _dpl(0, "problem");
3740       #endif
3741                               stopItem = GdbDataStop { };
3742                               stopItem.reason = functionFinished;
3743
3744                               for(i = tk+1; i < outTokens.count; i++)
3745                               {
3746                                  TokenizeListItem(outTokens[i], item);
3747                                  StripQuotes(item.value, item.value);
3748                                  if(!strcmp(item.name, "thread-id"))
3749                                     stopItem.threadid = atoi(item.value);
3750                                  else if(!strcmp(item.name, "frame"))
3751                                  {
3752                                     item.value = StripCurlies(item.value);
3753                                     ParseFrame(stopItem.frame, item.value);
3754                                  }
3755                                  else if(!strcmp(item.name, "gdb-result-var"))
3756                                     stopItem.gdbResultVar = CopyString(item.value);
3757                                  else if(!strcmp(item.name, "return-value"))
3758                                     stopItem.returnValue = CopyString(item.value);
3759                                  else if(!strcmp(item.name, "stopped-threads"))
3760                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Advanced thread debugging not handled");
3761                                  else if(!strcmp(item.name, "core"))
3762                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Information (core) not used");
3763                                  else
3764                                     _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown function finished item name (", item.name, "=", item.value, ")");
3765                               }
3766
3767                               event = functionEnd;
3768                               ide.Update(null);
3769                            }
3770                            else if(!strcmp(reason, "signal-received"))
3771                            {
3772       #ifdef _DEBUG
3773                               if(stopItem)
3774                                  _dpl(0, "problem");
3775       #endif
3776                               stopItem = GdbDataStop { };
3777                               stopItem.reason = signalReceived;
3778
3779                               for(i = tk+1; i < outTokens.count; i++)
3780                               {
3781                                  TokenizeListItem(outTokens[i], item);
3782                                  StripQuotes(item.value, item.value);
3783                                  if(!strcmp(item.name, "signal-name"))
3784                                     stopItem.name = CopyString(item.value);
3785                                  else if(!strcmp(item.name, "signal-meaning"))
3786                                     stopItem.meaning = CopyString(item.value);
3787                                  else if(!strcmp(item.name, "thread-id"))
3788                                     stopItem.threadid = atoi(item.value);
3789                                  else if(!strcmp(item.name, "frame"))
3790                                  {
3791                                     item.value = StripCurlies(item.value);
3792                                     ParseFrame(stopItem.frame, item.value);
3793                                  }
3794                                  else
3795                                     _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown signal reveived item name (", item.name, "=", item.value, ")");
3796                               }
3797                               if(!strcmp(stopItem.name, "SIGTRAP"))
3798                               {
3799                                  switch(breakType)
3800                                  {
3801                                     case internal:
3802                                        breakType = none;
3803                                        break;
3804                                     case restart:
3805                                     case stop:
3806                                        break;
3807                                     default:
3808                                        event = breakEvent;
3809                                  }
3810                               }
3811                               else
3812                               {
3813                                  event = signal;
3814                               }
3815                            }
3816                            else if(!strcmp(reason, "watchpoint-trigger"))
3817                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3818                            else if(!strcmp(reason, "read-watchpoint-trigger"))
3819                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3820                            else if(!strcmp(reason, "access-watchpoint-trigger"))
3821                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3822                            else if(!strcmp(reason, "watchpoint-scope"))
3823                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3824                            else if(!strcmp(reason, "location-reached"))
3825                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason location reached not handled");
3826                            else
3827                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3828                         }
3829                         else
3830                         {
3831                            PrintLn(output);
3832                         }
3833                      }
3834                   }
3835                   if(usingValgrind && event == none && !stopItem)
3836                      event = valgrindStartPause;
3837                   app.SignalEvent();
3838                }
3839             }
3840             else
3841                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3842             outTokens.RemoveAll();
3843             break;
3844          case '(':
3845             if(!strcmpi(output, "(gdb) "))
3846             {
3847                if(waitingForPID)
3848                {
3849                   char exeFile[MAX_LOCATION];
3850                   int oldProcessID = targetProcessId;
3851                   GetLastDirectory(targetFile, exeFile);
3852
3853                   while(!targetProcessId/*true*/)
3854                   {
3855                      targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3856                      if(targetProcessId || gdbHandle.Peek()) break;
3857                      Sleep(0.01);
3858                   }
3859
3860                   if(targetProcessId)
3861                      _ChangeState(running);
3862                   else if(!oldProcessID)
3863                   {
3864                      ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3865                      // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3866                      gdbHandle.Printf("-gdb-exit\n");
3867                      gdbTimer.Stop();
3868                      _ChangeState(terminated); //loaded;
3869                      prjConfig = null;
3870
3871                      if(ide.workspace)
3872                      {
3873                         for(bp : ide.workspace.breakpoints)
3874                            bp.inserted = false;
3875                      }
3876                      for(bp : sysBPs)
3877                         bp.inserted = false;
3878                      if(bpRunToCursor)
3879                         bpRunToCursor.inserted = false;
3880
3881                      ide.outputView.debugBox.Logf($"Debugging stopped\n");
3882                      ClearBreakDisplay();
3883
3884                #if defined(__unix__)
3885                      if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
3886                      {
3887                         progThread.terminate = true;
3888                         if(fifoFile)
3889                         {
3890                            fifoFile.CloseInput();
3891                            app.Unlock();
3892                            progThread.Wait();
3893                            app.Lock();
3894                            delete fifoFile;
3895                         }
3896
3897                         DeleteFile(progFifoPath);
3898                         progFifoPath[0] = '\0';
3899                         rmdir(progFifoDir);
3900                      }
3901                #endif
3902                   }
3903                }
3904                gdbReady = true;
3905                serialSemaphore.Release();
3906             }
3907             else
3908                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
3909
3910             break;
3911          case '&':
3912             if(!strncmp(output, "&\"warning:", 10))
3913             {
3914                char * content;
3915                content = strstr(output, "\"");
3916                StripQuotes(content, content);
3917                content = strstr(content, ":");
3918                if(content)
3919                   content++;
3920                if(content)
3921                {
3922                   char * s;
3923                   ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
3924                   delete s;
3925                   ide.Update(null);
3926                }
3927             }
3928             break;
3929          default:
3930             _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
3931       }
3932       if(!setWaitingForPID)
3933          waitingForPID = false;
3934       setWaitingForPID = false;
3935
3936       delete outTokens;
3937       delete subTokens;
3938       delete item;
3939       delete item2;
3940    }
3941
3942    // From GDB Output functions
3943    void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens)
3944    {
3945       char path[MAX_LOCATION] = "";
3946       char file[MAX_FILENAME] = "";
3947       bool symbolsLoaded;
3948       DebugListItem item { };
3949       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
3950       for(token : outTokens)
3951       {
3952          if(TokenizeListItem(token, item))
3953          {
3954             if(!strcmp(item.name, "target-name"))
3955             {
3956                StripQuotes(item.value, path);
3957                MakeSystemPath(path);
3958                GetLastDirectory(path, file);
3959             }
3960             else if(!strcmp(item.name, "symbols-loaded"))
3961             {
3962                symbolsLoaded = (atoi(item.value) == 1);
3963             }
3964          }
3965       }
3966       delete item;
3967       if(path[0] && file[0])
3968       {
3969          for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
3970          {
3971             bool match;
3972             char * dot;
3973             char prjTargetPath[MAX_LOCATION];
3974             char prjTargetFile[MAX_FILENAME];
3975             DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
3976             strcpy(prjTargetPath, prj.topNode.path);
3977             PathCat(prjTargetPath, targetDirExp.dir);
3978             prjTargetFile[0] = '\0';
3979             prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
3980             PathCat(prjTargetPath, prjTargetFile);
3981             MakeSystemPath(prjTargetPath);
3982
3983             match = !fstrcmp(prjTargetFile, file);
3984             if(!match && (dot = strstr(prjTargetFile, ".so.")))
3985             {
3986                char * dot3 = strstr(dot+4, ".");
3987                if(dot3)
3988                {
3989                   dot3[0] = '\0';
3990                   match = !fstrcmp(prjTargetFile, file);
3991                }
3992                if(!match)
3993                {
3994                   dot[3] = '\0';
3995                   match = !fstrcmp(prjTargetFile, file);
3996                }
3997             }
3998             if(match)
3999             {
4000                // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
4001                /* -- this is disabled because we can't trust gdb's symbols-loaded="0" field for =library-loaded (http://sourceware.org/bugzilla/show_bug.cgi?id=10693)
4002                if(!symbolsLoaded)
4003                   ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
4004                */
4005                match = !fstrcmp(prjTargetPath, path);
4006                if(!match && (dot = strstr(prjTargetPath, ".so.")))
4007                {
4008                   char * dot3 = strstr(dot+4, ".");
4009                   if(dot3)
4010                   {
4011                      dot3[0] = '\0';
4012                      match = !fstrcmp(prjTargetPath, path);
4013                   }
4014                   if(!match)
4015                   {
4016                      dot[3] = '\0';
4017                      match = !fstrcmp(prjTargetPath, path);
4018                   }
4019                }
4020                if(!match)
4021                   ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4022                break;
4023             }
4024          }
4025       }
4026    }
4027
4028    void FGOBreakpointModified(Array<char *> outTokens)
4029    {
4030       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4031 #if 0
4032       DebugListItem item { };
4033       if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4034       {
4035          if(!strcmp(item.name, "bkpt"))
4036          {
4037             GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4038             delete modBp;
4039          }
4040       }
4041 #endif
4042    }
4043
4044
4045    ExpressionType ::DebugEvalExpTypeError(char * result)
4046    {
4047       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4048       if(result)
4049          return dummyExp;
4050       switch(eval.error)
4051       {
4052          case symbolNotFound:
4053             return symbolErrorExp;
4054          case memoryCantBeRead:
4055             return memoryErrorExp;
4056       }
4057       return unknownErrorExp;
4058    }
4059
4060    char * ::EvaluateExpression(char * expression, ExpressionType * error)
4061    {
4062       char * result;
4063       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4064       if(ide.projectView && ide.debugger.state == stopped)
4065       {
4066          result = GdbEvaluateExpression(expression);
4067          *error = DebugEvalExpTypeError(result);
4068       }
4069       else
4070       {
4071          result = null;
4072          *error = noDebuggerErrorExp;
4073       }
4074       return result;
4075    }
4076
4077    char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4078    {
4079       // check for state
4080       char * result = GdbReadMemoryString(address, size, format, 1, 1);
4081       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4082       if(!result || !strcmp(result, "N/A"))
4083          *error = memoryErrorExp;
4084       else
4085          *error = DebugEvalExpTypeError(result);
4086       return result;
4087    }
4088 }
4089
4090 class ValgrindLogThread : Thread
4091 {
4092    Debugger debugger;
4093
4094    unsigned int Main()
4095    {
4096       static char output[4096];
4097       Array<char> dynamicBuffer { minAllocSize = 4096 };
4098       File oldValgrindHandle = vgLogFile;
4099       incref oldValgrindHandle;
4100
4101       app.Lock();
4102       while(debugger.state != terminated && vgLogFile)
4103       {
4104          int result;
4105          app.Unlock();
4106          result = vgLogFile.Read(output, 1, sizeof(output));
4107          app.Lock();
4108          if(debugger.state == terminated || !vgLogFile/* || vgLogFile.Eof()*/)
4109             break;
4110          if(result)
4111          {
4112             int c;
4113             int start = 0;
4114
4115             for(c = 0; c<result; c++)
4116             {
4117                if(output[c] == '\n')
4118                {
4119                   int pos = dynamicBuffer.size;
4120                   dynamicBuffer.size += c - start;
4121                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4122                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4123                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4124                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4125                      dynamicBuffer.size++;
4126                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4127 #ifdef _DEBUG
4128                   // printf("%s\n", dynamicBuffer.array);
4129 #endif
4130                   if(strstr(&dynamicBuffer[0], "vgdb me"))
4131                      debugger.serialSemaphore.Release();
4132                   ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4133                   dynamicBuffer.size = 0;
4134                   start = c + 1;
4135                }
4136             }
4137             if(c == result)
4138             {
4139                int pos = dynamicBuffer.size;
4140                dynamicBuffer.size += c - start;
4141                memcpy(&dynamicBuffer[pos], output + start, c - start);
4142             }
4143          }
4144          else if(debugger.state == stopped)
4145          {
4146 /*#ifdef _DEBUG
4147             printf("Got end of file from GDB!\n");
4148 #endif*/
4149             app.Unlock();
4150             Sleep(0.2);
4151             app.Lock();
4152          }
4153       }
4154       delete dynamicBuffer;
4155       ide.outputView.debugBox.Logf($"ValgrindLogThreadExit\n");
4156       //if(oldValgrindHandle == vgLogFile)
4157          debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4158       delete oldValgrindHandle;
4159       app.Unlock();
4160       return 0;
4161    }
4162 }
4163
4164 class ValgrindTargetThread : Thread
4165 {
4166    Debugger debugger;
4167
4168    unsigned int Main()
4169    {
4170       static char output[4096];
4171       Array<char> dynamicBuffer { minAllocSize = 4096 };
4172       DualPipe oldValgrindHandle = vgTargetHandle;
4173       incref oldValgrindHandle;
4174
4175       app.Lock();
4176       while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4177       {
4178          int result;
4179          app.Unlock();
4180          result = vgTargetHandle.Read(output, 1, sizeof(output));
4181          app.Lock();
4182          if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4183             break;
4184          if(result)
4185          {
4186             int c;
4187             int start = 0;
4188
4189             for(c = 0; c<result; c++)
4190             {
4191                if(output[c] == '\n')
4192                {
4193                   int pos = dynamicBuffer.size;
4194                   dynamicBuffer.size += c - start;
4195                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4196                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4197                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4198                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4199                      dynamicBuffer.size++;
4200                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4201 #ifdef _DEBUG
4202                   // printf("%s\n", dynamicBuffer.array);
4203 #endif
4204                   ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4205
4206                   dynamicBuffer.size = 0;
4207                   start = c + 1;
4208                }
4209             }
4210             if(c == result)
4211             {
4212                int pos = dynamicBuffer.size;
4213                dynamicBuffer.size += c - start;
4214                memcpy(&dynamicBuffer[pos], output + start, c - start);
4215             }
4216          }
4217          else
4218          {
4219 #ifdef _DEBUG
4220             printf("Got end of file from GDB!\n");
4221 #endif
4222          }
4223       }
4224       delete dynamicBuffer;
4225       //if(oldValgrindHandle == vgTargetHandle)
4226          debugger.ValgrindTargetThreadExit();
4227       delete oldValgrindHandle;
4228       app.Unlock();
4229       return 0;
4230    }
4231 }
4232
4233 class GdbThread : Thread
4234 {
4235    Debugger debugger;
4236
4237    unsigned int Main()
4238    {
4239       static char output[4096];
4240       Array<char> dynamicBuffer { minAllocSize = 4096 };
4241       DualPipe oldGdbHandle = gdbHandle;
4242       incref oldGdbHandle;
4243
4244       app.Lock();
4245       while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4246       {
4247          int result;
4248          app.Unlock();
4249          result = gdbHandle.Read(output, 1, sizeof(output));
4250          app.Lock();
4251          if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4252             break;
4253          if(result)
4254          {
4255             int c;
4256             int start = 0;
4257
4258             for(c = 0; c<result; c++)
4259             {
4260                if(output[c] == '\n')
4261                {
4262                   int pos = dynamicBuffer.size;
4263                   dynamicBuffer.size += c - start;
4264                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4265                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4266                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4267                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4268                      dynamicBuffer.size++;
4269                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4270 #ifdef _DEBUG
4271                   // _dpl(0, dynamicBuffer.array);
4272 #endif
4273                   debugger.GdbThreadMain(&dynamicBuffer[0]);
4274                   dynamicBuffer.size = 0;
4275                   start = c + 1;
4276                }
4277             }
4278             if(c == result)
4279             {
4280                int pos = dynamicBuffer.size;
4281                dynamicBuffer.size += c - start;
4282                memcpy(&dynamicBuffer[pos], output + start, c - start);
4283             }
4284          }
4285          else
4286          {
4287 #ifdef _DEBUG
4288             _dpl(0, "Got end of file from GDB!");
4289 #endif
4290          }
4291       }
4292       delete dynamicBuffer;
4293       //if(oldGdbHandle == gdbHandle)
4294          debugger.GdbThreadExit();
4295       delete oldGdbHandle;
4296       app.Unlock();
4297       return 0;
4298    }
4299 }
4300
4301 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4302 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4303
4304 #if defined(__unix__)
4305 #define uint _uint
4306 #include <errno.h>
4307 #include <stdio.h>
4308 #include <fcntl.h>
4309 #include <sys/types.h>
4310 #undef uint
4311
4312 File fifoFile;
4313
4314 class ProgramThread : Thread
4315 {
4316    bool terminate;
4317    unsigned int Main()
4318    {
4319       bool result = true;
4320       bool fileCreated = false;
4321       mode_t mask = 0600;
4322       static char output[1000];
4323       int fd;
4324
4325       /*if(!mkfifo(progFifoPath, mask))
4326       {
4327          fileCreated = true;
4328       }
4329       else
4330       {
4331          app.Lock();
4332          ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4333          app.Unlock();
4334       }*/
4335
4336       if(FileExists(progFifoPath)) //fileCreated)
4337       {
4338          fifoFile = FileOpen(progFifoPath, read);
4339          if(!fifoFile)
4340          {
4341             app.Lock();
4342             ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4343             app.Unlock();
4344          }
4345          else
4346          {
4347             fd = fileno((FILE *)fifoFile.input);
4348             //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4349          }
4350       }
4351
4352       while(!terminate && fifoFile && !fifoFile.Eof())
4353       {
4354          fd_set rs, es;
4355          struct timeval time;
4356          int selectResult;
4357          time.tv_sec = 1;
4358          time.tv_usec = 0;
4359          FD_ZERO(&rs);
4360          FD_ZERO(&es);
4361          FD_SET(fd, &rs);
4362          FD_SET(fd, &es);
4363          selectResult = select(fd + 1, &rs, null, null, &time);
4364          if(FD_ISSET(fd, &rs))
4365          {
4366             int result = (int)read(fd, output, sizeof(output)-1);
4367             if(!result || (result < 0 && errno != EAGAIN))
4368                break;
4369             if(result > 0)
4370             {
4371                output[result] = '\0';
4372                if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4373                {
4374                   app.Lock();
4375                   ide.outputView.debugBox.Log(output);
4376                   app.Unlock();
4377                }
4378             }
4379          }
4380       }
4381
4382       //if(fifoFile)
4383       {
4384          //fifoFile.CloseInput();
4385          //delete fifoFile;
4386          app.Lock();
4387          ide.outputView.debugBox.Log("\n");
4388          app.Unlock();
4389       }
4390       /*
4391       if(FileExists(progFifoPath)) //fileCreated)
4392       {
4393          DeleteFile(progFifoPath);
4394          progFifoPath[0] = '\0';
4395       }
4396       */
4397       return 0;
4398    }
4399 }
4400 #endif
4401
4402 class Argument : struct
4403 {
4404    Argument prev, next;
4405    char * name;
4406    property char * name { set { delete name; if(value) name = CopyString(value); } }
4407    char * val;
4408    property char * val { set { delete val; if(value) val = CopyString(value); } }
4409
4410    void Free()
4411    {
4412       delete name;
4413       delete val;
4414    }
4415
4416    ~Argument()
4417    {
4418       Free();
4419    }
4420 }
4421
4422 class Frame : struct
4423 {
4424    Frame prev, next;
4425    int level;
4426    char * addr;
4427    property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4428    char * func;
4429    property char * func { set { delete func; if(value) func = CopyString(value); } }
4430    int argsCount;
4431    OldList args;
4432    char * from;
4433    property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4434    char * file;
4435    property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4436    char * absoluteFile;
4437    property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4438    int line;
4439
4440    void Free()
4441    {
4442       delete addr;
4443       delete func;
4444       delete from;
4445       delete file;
4446       delete absoluteFile;
4447       args.Free(Argument::Free);
4448    }
4449
4450    ~Frame()
4451    {
4452       Free();
4453    }
4454 }
4455
4456 class GdbDataStop : struct
4457 {
4458    DebuggerReason reason;
4459    int threadid;
4460    union
4461    {
4462       struct
4463       {
4464          int bkptno;
4465       };
4466       struct
4467       {
4468          char * name;
4469          char * meaning;
4470       };
4471       struct
4472       {
4473          char * gdbResultVar;
4474          char * returnValue;
4475       };
4476    };
4477    Frame frame { };
4478
4479    void Free()
4480    {
4481       if(reason)
4482       {
4483          if(reason == signalReceived)
4484          {
4485             delete name;
4486             delete meaning;
4487          }
4488          else if(reason == functionFinished)
4489          {
4490             delete gdbResultVar;
4491             delete returnValue;
4492          }
4493       }
4494       if(frame) frame.Free();
4495    }
4496
4497    ~GdbDataStop()
4498    {
4499       Free();
4500    }
4501 }
4502
4503 class GdbDataBreakpoint : struct
4504 {
4505    int id;
4506    char * number;
4507    property char * number { set { delete number; if(value) number = CopyString(value); } }
4508    char * type;
4509    property char * type { set { delete type; if(value) type = CopyString(value); } }
4510    char * disp;
4511    property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4512    bool enabled;
4513    char * addr;
4514    property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4515    char * func;
4516    property char * func { set { delete func; if(value) func = CopyString(value); } }
4517    char * file;
4518    property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4519    char * fullname;
4520    property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4521    int line;
4522    char * at;
4523    property char * at { set { delete at; if(value) at = CopyString(value); } }
4524    int times;
4525
4526    Array<GdbDataBreakpoint> multipleBPs;
4527
4528    void Print()
4529    {
4530    _dpl(0, "");
4531       PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4532    }
4533
4534    void Free()
4535    {
4536       delete type;
4537       delete disp;
4538       delete addr;
4539       delete func;
4540       delete file;
4541       delete at;
4542       if(multipleBPs) multipleBPs.Free();
4543       delete multipleBPs;
4544    }
4545
4546    ~GdbDataBreakpoint()
4547    {
4548       Free();
4549    }
4550 }
4551
4552 class Breakpoint : struct
4553 {
4554    class_no_expansion;
4555
4556    char * function;
4557    property char * function { set { delete function; if(value) function = CopyString(value); } }
4558    char * relativeFilePath;
4559    property char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4560    char * absoluteFilePath;
4561    property char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4562    int line;
4563    bool enabled;
4564    int hits;
4565    int breaks;
4566    int ignore;
4567    int level;
4568    Watch condition;
4569    bool inserted;
4570    BreakpointType type;
4571    DataRow row;
4572    GdbDataBreakpoint bp;
4573
4574    char * CopyLocationString(bool removePath)
4575    {
4576       char * location;
4577       char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4578       bool removingPath = removePath && file;
4579       if(removingPath)
4580       {
4581          char * fileName = new char[MAX_FILENAME];
4582          GetLastDirectory(file, fileName);
4583          file = fileName;
4584       }
4585       if(function)
4586       {
4587          if(file)
4588             location = PrintString(file, ":", function);
4589          else
4590             location = CopyString(function);
4591       }
4592       else
4593          location = PrintString(file, ":", line);
4594       if(removingPath)
4595          delete file;
4596       return location;
4597    }
4598
4599    char * CopyUserLocationString()
4600    {
4601       char * location;
4602       char * loc = CopyLocationString(false);
4603       Project prj = null;
4604       for(p : ide.workspace.projects)
4605       {
4606          if(p.topNode.FindByFullPath(absoluteFilePath, false))
4607          {
4608             prj = p;
4609             break;
4610          }
4611       }
4612       if(prj)
4613       {
4614          location = PrintString("(", prj.name, ")", loc);
4615          delete loc;
4616       }
4617       else
4618          location = loc;
4619       return location;
4620    }
4621
4622    void Save(File f)
4623    {
4624       if(relativeFilePath && relativeFilePath[0])
4625       {
4626          f.Printf("    * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, relativeFilePath);
4627          if(condition)
4628             f.Printf("       ~ %s\n", condition.expression);
4629       }
4630    }
4631
4632    void Free()
4633    {
4634       if(bp)
4635          bp.Free();
4636       delete bp;
4637       delete function;
4638       delete relativeFilePath;
4639       delete absoluteFilePath;
4640    }
4641
4642    ~Breakpoint()
4643    {
4644       Free();
4645    }
4646
4647 }
4648
4649 class Watch : struct
4650 {
4651    class_no_expansion;
4652    
4653    Type type;
4654    char * expression;
4655    char * value;
4656    DataRow row;
4657
4658    void Save(File f)
4659    {
4660       f.Printf("    ~ %s\n", expression);
4661    }
4662
4663    void Free()
4664    {
4665       delete expression;
4666       delete value;
4667       FreeType(type);
4668       type = null;
4669    }
4670
4671    void Reset()
4672    {
4673       delete value;
4674       FreeType(type);
4675       type = null;
4676    }
4677
4678    ~Watch()
4679    {
4680       Free();
4681    }
4682 }
4683
4684 class DebugListItem : struct
4685 {
4686    char * name;
4687    char * value;
4688 }
4689
4690 struct DebugEvaluationData
4691 {
4692    bool active;
4693    char * result;
4694    int bytes;
4695    uint64 nextBlockAddress;
4696
4697    DebuggerEvaluationError error;
4698 };
4699
4700 class CodeLocation : struct
4701 {
4702    char * file;
4703    char * absoluteFile;
4704    int line;
4705
4706    CodeLocation ::ParseCodeLocation(char * location)
4707    {
4708       if(location)
4709       {
4710          char * colon = null;
4711          char * temp;
4712          char loc[MAX_LOCATION];
4713          strcpy(loc, location);
4714          for(temp = loc; temp = strstr(temp, ":"); temp++)
4715             colon = temp;
4716          if(colon)
4717          {
4718             colon[0] = '\0';
4719             colon++;
4720             if(colon)
4721             {
4722                int line = atoi(colon);
4723                if(line)
4724                {
4725                   CodeLocation codloc { line = line };
4726                   codloc.file = CopyString(loc);
4727                   codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
4728                   return codloc;
4729                }
4730             }
4731          }
4732       }
4733       return null;
4734    }
4735
4736    void Free()
4737    {
4738       delete file;
4739       delete absoluteFile;
4740    }
4741
4742    ~CodeLocation()
4743    {
4744       Free();
4745    }
4746 }
4747
4748 void GDBFallBack(Expression exp, String expString)
4749 {
4750    char * result;
4751    ExpressionType evalError = dummyExp;
4752    result = Debugger::EvaluateExpression(expString, &evalError);
4753    if(result)
4754    {
4755       exp.constant = result;
4756       exp.type = constantExp;
4757    }
4758 }