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