a4af8d384a02b32cfe0484727d6e0f380864c439
[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=3/*3*/, gdbCommand=4/*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                         _dpl2(_dpct, dplchan::debuggerProblem, 0, "Breakpoint bkptno(", stopItem.bkptno, ") address missmatch!");
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       WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
1229
1230       if(bpRunToCursor && bpRunToCursor.inserted && symbols)
1231       {
1232          UnsetBreakpoint(bpRunToCursor);
1233          delete bpRunToCursor;
1234       }
1235
1236       StartSession(compiler, config, bitDepth, useValgrind, false, ignoreBreakpoints);
1237
1238 #if 0
1239       if(oldImplementation)
1240       {
1241          bpRunToCursor = Breakpoint { };
1242          bpRunToCursor.absoluteFilePath = absoluteFilePath;
1243          bpRunToCursor.relativeFilePath = relativeFilePath;
1244          bpRunToCursor.line = lineNumber;
1245          bpRunToCursor.type = runToCursor;
1246          bpRunToCursor.enabled = true;
1247          bpRunToCursor.level = atSameLevel ? frameCount - activeFrameLevel -1 : -1;
1248       }
1249 #endif
1250       if(state == loaded)
1251       {
1252          breakType = advance;
1253          breakString = PrintString(relativeFilePath, ":", lineNumber);
1254          GdbExecRun();
1255       }
1256       else if(state == stopped)
1257       {
1258          if(oldImplementation)
1259             GdbExecContinue(true);
1260          else
1261          {
1262             if(atSameLevel)
1263                GdbExecUntil(absoluteFilePath, lineNumber);
1264             else
1265                GdbExecAdvance(absoluteFilePath, lineNumber);
1266          }
1267       }
1268    }
1269
1270    void GetCallStackCursorLine(bool * error, int * lineCursor, int * lineTopFrame)
1271    {
1272       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetCallStackCursorLine()");
1273       if(activeFrameLevel == -1)
1274       {
1275          *error = false;
1276          *lineCursor = 0;
1277          *lineTopFrame = 0;
1278       }
1279       else
1280       {
1281          *error = signalOn && activeThread == signalThread;
1282          *lineCursor = activeFrameLevel - ((frameCount > 192 && activeFrameLevel > 191) ? frameCount - 192 - 1 : 0) + 1;
1283          *lineTopFrame = activeFrameLevel ? 1 : 0;
1284       }
1285    }
1286
1287    int GetMarginIconsLineNumbers(char * fileName, int lines[], bool enabled[], int max, bool * error, int * lineCursor, int * lineTopFrame)
1288    {
1289       char winFilePath[MAX_LOCATION];
1290       char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1291       int count = 0;
1292       Iterator<Breakpoint> it { ide.workspace.breakpoints };
1293       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GetMarginIconsLineNumbers()");
1294       while(it.Next() && count < max)
1295       {
1296          Breakpoint bp = it.data;
1297          if(bp.type == user)
1298          {
1299             if(bp.absoluteFilePath && bp.absoluteFilePath[0] && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1300             {
1301                lines[count] = bp.line;
1302                enabled[count] = bp.enabled;
1303                count++;
1304             }
1305          }
1306       }
1307       if(activeFrameLevel == -1)
1308       {
1309          *error = false;
1310          *lineCursor = 0;
1311          *lineTopFrame = 0;
1312       }
1313       else
1314       {
1315          *error = signalOn && activeThread == signalThread;
1316          if(activeFrame && activeFrame.absoluteFile && !fstrcmp(absoluteFilePath, activeFrame.absoluteFile))
1317             *lineCursor = activeFrame.line;
1318          else
1319             *lineCursor = 0;
1320          if(activeFrame && stopItem && stopItem.frame && activeFrame.level == stopItem.frame.level)
1321             *lineTopFrame = 0;
1322          else if(stopItem && stopItem.frame && stopItem.frame.absoluteFile && !fstrcmp(absoluteFilePath, stopItem.frame.absoluteFile))
1323             *lineTopFrame = stopItem.frame.line;
1324          else
1325             *lineTopFrame = 0;
1326          
1327          if(*lineTopFrame == *lineCursor && *lineTopFrame)
1328             *lineTopFrame = 0;
1329       }
1330       return count;
1331    }
1332
1333    void ChangeWatch(DataRow row, char * expression)
1334    {
1335       Watch wh = (Watch)row.tag;
1336       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ChangeWatch(", expression, ")");
1337       if(wh)
1338       {
1339          delete wh.expression;
1340          if(expression)
1341             wh.expression = CopyString(expression);
1342          else
1343          {
1344             Iterator<Watch> it { ide.workspace.watches };
1345             if(it.Find(wh))
1346                ide.workspace.watches.Delete(it.pointer);
1347          }
1348       }
1349       else if(expression)
1350       {
1351          wh = Watch { };
1352          row.tag = (int64)wh;
1353          ide.workspace.watches.Add(wh);
1354          wh.row = row;
1355          wh.expression = CopyString(expression);
1356       }
1357       ide.workspace.Save();
1358       //if(expression && state == stopped)
1359       if(expression)
1360          ResolveWatch(wh);
1361    }
1362
1363    void MoveIcons(char * fileName, int lineNumber, int move, bool start)
1364    {
1365       char winFilePath[MAX_LOCATION];
1366       char * absoluteFilePath = GetSlashPathBuffer(winFilePath, fileName);
1367
1368       Link bpLink, next;
1369       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::MoveIcons()");
1370       for(bpLink = ide.workspace.breakpoints.first; bpLink; bpLink = next)
1371       {
1372          Breakpoint bp = (Breakpoint)bpLink.data;
1373          next = bpLink.next;
1374
1375          if(bp.type == user && bp.absoluteFilePath && !fstrcmp(bp.absoluteFilePath, absoluteFilePath))
1376          {
1377             if(bp.line > lineNumber || (bp.line == lineNumber && start))
1378             {
1379                if(move < 0 && (bp.line < lineNumber - move))
1380                   ide.workspace.RemoveBreakpoint(bp);
1381                else
1382                {
1383                   bp.line += move;
1384                   ide.breakpointsView.UpdateBreakpoint(bp.row);
1385                   ide.workspace.Save();
1386                }
1387             }
1388          }
1389       }
1390       
1391       // moving code cursors is futile, on next step, stop, hit, cursors will be offset anyways
1392    }
1393
1394    bool SourceDirDialog(char * title, char * startDir, char * test, char * sourceDir)
1395    {
1396       bool result;
1397       bool retry;
1398       String srcDir = null;
1399
1400       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SourceDirDialog()");
1401       debuggerFileDialog.text = title;
1402       debuggerFileDialog.currentDirectory = startDir;
1403       debuggerFileDialog.master = ide;
1404
1405       while(debuggerFileDialog.Modal())
1406       {
1407          strcpy(sourceDir, debuggerFileDialog.filePath);
1408          if(!fstrcmp(ide.workspace.projectDir, sourceDir) && 
1409                   MessageBox { type = yesNo, master = ide, 
1410                               contents = $"This is the project directory.\nWould you like to try again?", 
1411                               text = $"Invalid Source Directory" }.Modal() == no)
1412             return false;
1413          else
1414          {
1415             for(dir : ide.workspace.sourceDirs)
1416             {
1417                if(!fstrcmp(dir, sourceDir))
1418                {
1419                   srcDir = dir;
1420                   break;
1421                }
1422             }
1423             
1424             if(srcDir && 
1425                   MessageBox { type = yesNo, master = ide, 
1426                               contents = $"This source directory is already specified.\nWould you like to try again?", 
1427                               text = $"Invalid Source Directory" }.Modal() == no)
1428                return false;
1429             else
1430             {
1431                if(test)
1432                {
1433                   char file[MAX_LOCATION];
1434                   strcpy(file, sourceDir);
1435                   PathCat(file, test);
1436                   result = FileExists(file);
1437                   if(!result && 
1438                         MessageBox { type = yesNo, master = ide, 
1439                                     contents = $"Unable to locate source file.\nWould you like to try again?", 
1440                                     text = $"Invalid Source Directory" }.Modal() == no)
1441                         return false;
1442                }
1443                else
1444                   result = true;
1445                
1446                if(result)
1447                   return true;
1448             }
1449          }
1450       }
1451       return false;
1452    }
1453
1454    void AddSourceDir(char * sourceDir)
1455    {
1456       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::AddSourceDir(", sourceDir, ")");
1457       ide.workspace.sourceDirs.Add(CopyString(sourceDir));
1458       ide.workspace.Save();
1459       
1460       if(targeted)
1461       {
1462          DebuggerState oldState = state;
1463          switch(state)
1464          {
1465             case running:
1466                if(targetProcessId)
1467                   GdbDebugBreak(true);
1468             case stopped:
1469             case loaded:
1470                GdbCommand(false, "-environment-directory \"%s\"", sourceDir);
1471                break;
1472          }
1473          if(oldState == running)
1474             GdbExecContinue(false);
1475       }
1476    }
1477
1478    void ToggleBreakpoint(char * fileName, int lineNumber)
1479    {
1480       char absolutePath[MAX_LOCATION];
1481       Breakpoint bp = null;
1482
1483       _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::ToggleBreakpoint(", fileName, ":", lineNumber, ")");
1484
1485       GetSlashPathBuffer(absolutePath, fileName);
1486       for(i : ide.workspace.breakpoints; i.type == user && i.absoluteFilePath && !fstrcmp(i.absoluteFilePath, absolutePath) && i.line == lineNumber)
1487       {
1488          bp = i;
1489          break;
1490       }
1491       if(bp)
1492       {
1493          if(bp.enabled)
1494          {
1495             ide.workspace.RemoveBreakpoint(bp);
1496             bp = null;
1497          }
1498          else
1499             bp.enabled = true;
1500       }
1501       else
1502       {
1503          Project owner;
1504          char relativePath[MAX_LOCATION];
1505
1506          WorkspaceGetRelativePath(absolutePath, relativePath, &owner);
1507
1508          if(!owner)
1509          {
1510             char title[MAX_LOCATION];
1511             char directory[MAX_LOCATION];
1512             char sourceDir[MAX_LOCATION];
1513             StripLastDirectory(absolutePath, directory);
1514             snprintf(title, sizeof(title), $"Provide source files location directory for %s", absolutePath);
1515             title[sizeof(title)-1] = 0;
1516             while(true)
1517             {
1518                String srcDir = null;
1519                for(dir : ide.workspace.sourceDirs)
1520                {
1521                   if(IsPathInsideOf(absolutePath, dir))
1522                   {
1523                      MakePathRelative(absolutePath, dir, relativePath);
1524                      srcDir = dir;
1525                      break;
1526                   }
1527                }
1528                if(srcDir)
1529                   break;
1530                
1531                if(SourceDirDialog(title, directory, null, sourceDir))
1532                {
1533                   if(IsPathInsideOf(absolutePath, sourceDir))
1534                   {
1535                      AddSourceDir(sourceDir);
1536                      MakePathRelative(absolutePath, sourceDir, relativePath);
1537                      break;
1538                   }
1539                   else if(MessageBox { type = yesNo, master = ide, 
1540                                  contents = $"You must provide a valid source directory in order to place a breakpoint in this file.\nWould you like to try again?", 
1541                                  text = $"Invalid Source Directory" }.Modal() == no)
1542                      return;
1543                }
1544                else
1545                   return;
1546             }
1547          }
1548          ide.workspace.bpCount++;
1549          bp = { line = lineNumber, type = user, enabled = true, level = -1, project = owner };
1550          ide.workspace.breakpoints.Add(bp);
1551          bp.absoluteFilePath = absolutePath;
1552          bp.relativeFilePath = relativePath;
1553          ide.breakpointsView.AddBreakpoint(bp);
1554       }
1555
1556       if(bp && targeted)
1557       {
1558          DebuggerState oldState = state;
1559          switch(state)
1560          {
1561             case running:
1562                if(targetProcessId)
1563                   GdbDebugBreak(true);
1564             case stopped:
1565             case loaded:
1566                SetBreakpoint(bp, false);
1567                break;
1568          }
1569          if(oldState == running)
1570             GdbExecContinue(false);
1571       }
1572
1573       ide.workspace.Save();
1574    }
1575
1576    void UpdateRemovedBreakpoint(Breakpoint bp)
1577    {
1578       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::UpdateRemovedBreakpoint()");
1579       if(targeted && bp.inserted)
1580       {
1581          DebuggerState oldState = state;
1582          switch(state)
1583          {
1584             case running:
1585                if(targetProcessId)
1586                   GdbDebugBreak(true);
1587             case stopped:
1588             case loaded:
1589                UnsetBreakpoint(bp);
1590                break;
1591          }
1592          if(oldState == running)
1593             GdbExecContinue(false);
1594       }
1595    }
1596
1597    // PRIVATE MEMBERS
1598
1599    void ParseFrame(Frame frame, char * string)
1600    {
1601       int i, j, k;
1602       Array<char *> frameTokens { minAllocSize = 50 };
1603       Array<char *> argsTokens { minAllocSize = 50 };
1604       Array<char *> argumentTokens { minAllocSize = 50 };
1605       DebugListItem item { };
1606       Argument arg;
1607       
1608       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseFrame()");
1609       TokenizeList(string, ',', frameTokens);
1610       for(i = 0; i < frameTokens.count; i++)
1611       {
1612          if(TokenizeListItem(frameTokens[i], item))
1613          {
1614             StripQuotes(item.value, item.value);
1615             if(!strcmp(item.name, "level"))
1616                frame.level = atoi(item.value);
1617             else if(!strcmp(item.name, "addr"))
1618                frame.addr = item.value;
1619             else if(!strcmp(item.name, "func"))
1620                frame.func = item.value;
1621             else if(!strcmp(item.name, "args"))
1622             {
1623                if(!strcmp(item.value, "[]"))
1624                   frame.argsCount = 0;
1625                else
1626                {
1627                   item.value = StripBrackets(item.value);
1628                   TokenizeList(item.value, ',', argsTokens);
1629                   for(j = 0; j < argsTokens.count; j++)
1630                   {
1631                      argsTokens[j] = StripCurlies(argsTokens[j]);
1632                      TokenizeList(argsTokens[j], ',', argumentTokens);
1633                      for(k = 0; k < argumentTokens.count; k++)
1634                      {
1635                         arg = Argument { };
1636                         frame.args.Add(arg);
1637                         if(TokenizeListItem(argumentTokens[k], item))
1638                         {
1639                            if(!strcmp(item.name, "name"))
1640                            {
1641                               StripQuotes(item.value, item.value);
1642                               arg.name = item.value;
1643                            }
1644                            else if(!strcmp(item.name, "value"))
1645                            {
1646                               StripQuotes(item.value, item.value);
1647                               arg.val = item.value;
1648                            }
1649                            else
1650                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame args item (", item.name, "=", item.value, ") is unheard of");
1651                         }
1652                         else
1653                            _dpl(0, "Bad frame args item");
1654                      }
1655                      argumentTokens.RemoveAll();
1656                   }
1657                   frame.argsCount = argsTokens.count;
1658                   argsTokens.RemoveAll();
1659                }
1660             }
1661             else if(!strcmp(item.name, "from"))
1662                frame.from = item.value;
1663             else if(!strcmp(item.name, "file"))
1664                frame.file = item.value;
1665             else if(!strcmp(item.name, "line"))
1666                frame.line = atoi(item.value);
1667             else if(!strcmp(item.name, "fullname"))
1668                frame.absoluteFile = item.value;
1669             /*{
1670                // GDB 6.3 on OS X is giving "fullname" and "dir", all in absolute, but file name only in 'file'
1671                String path = ide.workspace.GetPathWorkspaceRelativeOrAbsolute(item.value);
1672                if(strcmp(frame.file, path))
1673                {
1674                   frame.file = path;
1675                   frame.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(frame.file);
1676                }
1677                delete path;
1678             }*/
1679             else
1680                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "frame member (", item.name, "=", item.value, ") is unheard of");
1681          }
1682          else
1683             _dpl(0, "Bad frame");
1684       }
1685       
1686       delete frameTokens;
1687       delete argsTokens;
1688       delete argumentTokens;
1689       delete item;
1690    }
1691
1692    Breakpoint GetBreakpointById(int id, bool * isInternal)
1693    {
1694       Breakpoint bp = null;
1695       //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::GetBreakpointById(", id, ")");
1696       if(isInternal)
1697          *isInternal = false;
1698       if(id)
1699       {
1700          for(i : sysBPs; i.bp && i.bp.id == id)
1701          {
1702             if(isInternal)
1703                *isInternal = true;
1704             bp = i;
1705             break;
1706          }
1707          if(!bp && bpRunToCursor && bpRunToCursor.bp && bpRunToCursor.bp.id == id)
1708             bp = bpRunToCursor;
1709          if(!bp)
1710          {
1711             for(i : ide.workspace.breakpoints; i.bp && i.bp.id == id)
1712             {
1713                bp = i;
1714                break;
1715             }
1716          }
1717       }
1718       return bp;
1719    }
1720
1721    GdbDataBreakpoint ParseBreakpoint(char * string, Array<char *> outTokens)
1722    {
1723       int i;
1724       GdbDataBreakpoint bp { };
1725       DebugListItem item { };
1726       Array<char *> bpTokens { minAllocSize = 16 };
1727       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ParseBreakpoint()");
1728       string = StripCurlies(string);
1729       TokenizeList(string, ',', bpTokens);
1730       for(i = 0; i < bpTokens.count; i++)
1731       {
1732          if(TokenizeListItem(bpTokens[i], item))
1733          {
1734             StripQuotes(item.value, item.value);
1735             if(!strcmp(item.name, "number"))
1736             {
1737                if(!strchr(item.value, '.'))
1738                   bp.id = atoi(item.value);
1739                bp.number = item.value;
1740             }
1741             else if(!strcmp(item.name, "type"))
1742                bp.type = item.value;
1743             else if(!strcmp(item.name, "disp"))
1744                bp.disp = item.value;
1745             else if(!strcmp(item.name, "enabled"))
1746                bp.enabled = (!strcmpi(item.value, "y"));
1747             else if(!strcmp(item.name, "addr"))
1748             {
1749                if(outTokens && !strcmp(item.value, "<MULTIPLE>"))
1750                {
1751                   int c = 1;
1752                   Array<GdbDataBreakpoint> bpArray = bp.multipleBPs = { };
1753                   while(outTokens.count > ++c)
1754                   {
1755                      GdbDataBreakpoint multBp = ParseBreakpoint(outTokens[c], null);
1756                      bpArray.Add(multBp);
1757                   }
1758                }
1759                else
1760                   bp.addr = item.value;
1761             }
1762             else if(!strcmp(item.name, "func"))
1763                bp.func = item.value;
1764             else if(!strcmp(item.name, "file"))
1765                bp.file = item.value;
1766             else if(!strcmp(item.name, "fullname"))
1767                bp.fullname = item.value;
1768             else if(!strcmp(item.name, "line"))
1769                bp.line = atoi(item.value);
1770             else if(!strcmp(item.name, "at"))
1771                bp.at = item.value;
1772             else if(!strcmp(item.name, "times"))
1773                bp.times = atoi(item.value);
1774             else if(!strcmp(item.name, "original-location") || !strcmp(item.name, "thread-groups"))
1775                _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "breakpoint member (", item.name, "=", item.value, ") is ignored");
1776             else
1777                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "breakpoint member (", item.name, "=", item.value, ") is unheard of");
1778          }
1779       }
1780       return bp;
1781    }
1782
1783    void ShowDebuggerViews()
1784    {
1785       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ShowDebuggerViews()");
1786       ide.outputView.Show();
1787       ide.outputView.SelectTab(debug);
1788       ide.threadsView.Show();
1789       ide.callStackView.Show();
1790       ide.watchesView.Show();
1791       ide.Update(null);
1792    }
1793
1794    void HideDebuggerViews()
1795    {
1796       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::HideDebuggerViews()");
1797       ide.RepositionWindows(true);
1798    }
1799
1800    void ::GdbCommand(bool focus, char * format, ...)
1801    {
1802       if(gdbHandle)
1803       {
1804          // TODO: Improve this limit
1805          static char string[MAX_F_STRING*4];
1806          va_list args;
1807          va_start(args, format);
1808          vsnprintf(string, sizeof(string), format, args);
1809          string[sizeof(string)-1] = 0;
1810          va_end(args);
1811          
1812          gdbReady = false;
1813          ide.debugger.serialSemaphore.TryWait();
1814
1815 #ifdef GDB_DEBUG_CONSOLE
1816          _dpl2(_dpct, dplchan::gdbCommand, 0, string);
1817 #endif
1818 #ifdef GDB_DEBUG_OUTPUT
1819          ide.outputView.gdbBox.Logf("cmd: %s\n", string);
1820 #endif
1821 #ifdef GDB_DEBUG_GUI
1822          if(ide.gdbDialog)
1823             ide.gdbDialog.AddCommand(string);
1824 #endif
1825
1826          strcat(string,"\n");
1827          gdbHandle.Puts(string);
1828
1829          if(focus)
1830             Process_ShowWindows(targetProcessId);
1831
1832          app.Unlock();
1833          ide.debugger.serialSemaphore.Wait();
1834          app.Lock();
1835       } 
1836    }
1837
1838    bool ValidateBreakpoint(Breakpoint bp)
1839    {
1840       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ValidateBreakpoint()");
1841       if(modules && bp.line && bp.bp)
1842       {
1843          if(bp.bp.line != bp.line)
1844          {
1845             if(!bp.bp.line)
1846             {
1847 #ifdef _DEBUG
1848                //here
1849                ide.outputView.debugBox.Logf("WOULD HAVE -- Invalid breakpoint disabled: %s:%d\n", bp.relativeFilePath, bp.line);
1850 #endif
1851                //UnsetBreakpoint(bp);
1852                //bp.enabled = false;
1853                return false;
1854             }
1855             else
1856             {
1857                //here
1858                ide.outputView.debugBox.Logf("Debugger Error: ValidateBreakpoint error\n");
1859                bp.line = bp.bp.line;
1860             }
1861          }
1862       }
1863       return true;
1864    }
1865
1866    void BreakpointsMaintenance()
1867    {
1868       //_dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::BreakpointsMaintenance()");
1869       if(symbols)
1870       {
1871          if(gdbExecution.suspendInternalBreakpoints)
1872          {
1873             for(bp : sysBPs; bp.inserted)
1874                UnsetBreakpoint(bp);
1875          }
1876          else
1877          {
1878             DirExpression objDir = ide.project.GetObjDir(currentCompiler, prjConfig, bitDepth);
1879             for(bp : sysBPs; !bp.inserted)
1880             {
1881                bool insert = false;
1882                if(bp.type == internalModulesLoaded)
1883                {
1884                   char path[MAX_LOCATION];
1885                   char name[MAX_LOCATION];
1886                   char fixedModuleName[MAX_FILENAME];
1887                   char line[16384];
1888                   int lineNumber;
1889                   bool moduleLoadBlock = false;
1890                   File f;
1891                   ReplaceSpaces(fixedModuleName, ide.project.moduleName);
1892                   snprintf(name, sizeof(name),"%s.main.ec", fixedModuleName);
1893                   name[sizeof(name)-1] = 0;
1894                   strcpy(path, ide.workspace.projectDir);
1895                   PathCatSlash(path, objDir.dir);
1896                   PathCatSlash(path, name);
1897                   f = FileOpen(path, read);
1898                   if(f)
1899                   {
1900                      for(lineNumber = 1; !f.Eof(); lineNumber++)
1901                      {
1902                         if(f.GetLine(line, sizeof(line) - 1))
1903                         {
1904                            bool moduleLoadLine;
1905                            TrimLSpaces(line, line);
1906                            moduleLoadLine = !strncmp(line, "eModule_Load", strlen("eModule_Load"));
1907                            if(!moduleLoadBlock && moduleLoadLine)
1908                               moduleLoadBlock = true;
1909                            else if(moduleLoadBlock && !moduleLoadLine && strlen(line) > 0)
1910                               break;
1911                         }
1912                      }
1913                      if(!f.Eof())
1914                      {
1915                         char relative[MAX_LOCATION];
1916                         bp.absoluteFilePath = path;
1917                         MakePathRelative(path, ide.workspace.projectDir, relative);
1918                         bp.relativeFilePath = relative;
1919                         bp.line = lineNumber;
1920                         insert = true;
1921                      }
1922                      delete f;
1923                   }
1924                }
1925                else if(bp.type == internalModuleLoad)
1926                {
1927                   if(modules)
1928                   {
1929                      for(prj : ide.workspace.projects)
1930                      {
1931                         if(!strcmp(prj.moduleName, "ecere"))
1932                         {
1933                            ProjectNode node = prj.topNode.Find("instance.c", false);
1934                            if(node)
1935                            {
1936                               char path[MAX_LOCATION];
1937                               char relative[MAX_LOCATION];
1938                               node.GetFullFilePath(path);
1939                               bp.absoluteFilePath = path;
1940                               MakePathRelative(path, prj.topNode.path, relative);
1941                               bp.relativeFilePath = relative;
1942                               insert = true;
1943                               break;
1944                            }
1945                         }
1946                      }
1947                   }
1948                }
1949                else
1950                   insert = true;
1951                if(insert)
1952                   SetBreakpoint(bp, false);
1953             }
1954             delete objDir;
1955          }
1956
1957          if(userAction != runToCursor && bpRunToCursor && bpRunToCursor.inserted)
1958             UnsetBreakpoint(bpRunToCursor);
1959          if(bpRunToCursor && !bpRunToCursor.inserted)
1960             SetBreakpoint(bpRunToCursor, false);
1961
1962          if(ignoreBreakpoints)
1963          {
1964             for(bp : ide.workspace.breakpoints; bp.inserted)
1965                UnsetBreakpoint(bp);
1966          }
1967          else
1968          {
1969             for(bp : ide.workspace.breakpoints; !bp.inserted && bp.type == user)
1970             {
1971                if(bp.enabled)
1972                {
1973                   if(!SetBreakpoint(bp, false))
1974                      SetBreakpoint(bp, true);
1975                }
1976                else
1977                {
1978 #ifdef _DEBUG
1979                   if(bp.bp)
1980                      _dpl(0, "problem");
1981 #endif
1982                   bp.bp = GdbDataBreakpoint { };
1983                }
1984             }
1985          }
1986       }
1987    }
1988
1989    void UnsetBreakpoint(Breakpoint bp)
1990    {
1991       char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::UnsetBreakpoint(", s=bp.CopyLocationString(false), ") -- ", bp.type); delete s;
1992       if(symbols && bp.inserted)
1993       {
1994          GdbCommand(false, "-break-delete %s", bp.bp.number);
1995          bp.inserted = false;
1996          bp.bp = { };
1997       }
1998    }
1999
2000    bool SetBreakpoint(Breakpoint bp, bool removePath)
2001    {
2002       char * s; _dpl2(_dpct, dplchan::debuggerBreakpoints, 0, "Debugger::SetBreakpoint(", s=bp.CopyLocationString(false), ", ", removePath ? "**** removePath(true) ****" : "", ") -- ", bp.type); delete s;
2003       breakpointError = false;
2004       if(symbols && bp.enabled && (!bp.project || bp.project == ide.project || projectsLibraryLoaded[bp.project.name]))
2005       {
2006          sentBreakInsert = true;
2007          if(bp.address)
2008             GdbCommand(false, "-break-insert *%s", bp.address);
2009          else
2010          {
2011             char * location = bp.CopyLocationString(removePath);
2012             GdbCommand(false, "-break-insert %s", location);
2013             delete location;
2014          }
2015          if(!breakpointError)
2016          {
2017             char * address = null;
2018             if(bpItem && bpItem.multipleBPs && bpItem.multipleBPs.count)
2019             {
2020                int count = 0;
2021                GdbDataBreakpoint first = null;
2022                for(n : bpItem.multipleBPs)
2023                {
2024                   if(!fstrcmp(n.fullname, bp.absoluteFilePath) && !first)
2025                   {
2026                      count++;
2027                      first = n;
2028                      break;
2029                   }
2030                   /*else
2031                   {
2032                      if(n.enabled)
2033                      {
2034                         GdbCommand(false, "-break-disable %s", n.number);
2035                         n.enabled = false;
2036                      }
2037                      else
2038                         _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error breakpoint already disabled.");
2039                   }*/
2040                }
2041                if(first)
2042                {
2043                   address = CopyString(first.addr);
2044                   bpItem.addr = first.addr;
2045                   bpItem.func = first.func;
2046                   bpItem.file = first.file;
2047                   bpItem.fullname = first.fullname;
2048                   bpItem.line = first.line;
2049                   //bpItem.thread-groups = first.thread-groups;*/
2050                }
2051                else if(count == 0)
2052                   _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints all disabled.");
2053                else
2054                   _dpl2(_dpct, dplchan::debuggerProblem, 0, "Debugger::SetBreakpoint -- error multiple breakpoints in exact same file not supported.");
2055                bpItem.multipleBPs.Free();
2056                delete bpItem.multipleBPs;
2057             }
2058             bp.bp = bpItem;
2059             bpItem = null;
2060             bp.inserted = (bp.bp && bp.bp.number && strcmp(bp.bp.number, "0"));
2061             if(bp.inserted)
2062                ValidateBreakpoint(bp);
2063
2064             if(address)
2065             {
2066                UnsetBreakpoint(bp);
2067                bp.address = address;
2068                delete address;
2069                SetBreakpoint(bp, removePath);
2070             }
2071          }
2072       }
2073       return !breakpointError;
2074    }
2075
2076    void GdbGetStack()
2077    {
2078       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbGetStack()");
2079       activeFrame = null;
2080       stackFrames.Free(Frame::Free);
2081       GdbCommand(false, "-stack-info-depth");
2082       if(!frameCount)
2083          GdbCommand(false, "-stack-info-depth 192");
2084       if(frameCount && frameCount <= 192)
2085          GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 191));
2086       else
2087       {
2088          GdbCommand(false, "-stack-list-frames 0 %d", Min(frameCount-1, 95));
2089          GdbCommand(false, "-stack-list-frames %d %d", Max(frameCount - 96, 96), frameCount - 1);
2090       }
2091       GdbCommand(false, "");
2092    }
2093
2094    bool GdbTargetSet()
2095    {
2096       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbTargetSet()");
2097       if(!targeted)
2098       {
2099          char escaped[MAX_LOCATION];
2100          strescpy(escaped, targetFile);
2101          GdbCommand(false, "file \"%s\"", escaped); //GDB/MI Missing Implementation in 5.1.1 but we now have -file-exec-and-symbols / -file-exec-file / -file-symbol-file
2102
2103          if(!symbols)
2104             return true;
2105
2106          if(usingValgrind)
2107          {
2108             const char *vgdbCommand = "/usr/bin/vgdb"; // TODO: vgdb command config option
2109             //GdbCommand(false, "-target-select remote | %s --pid=%d", "vgdb", targetProcessId);
2110             printf("target remote | %s --pid=%d\n", vgdbCommand, targetProcessId);
2111             GdbCommand(false, "target remote | %s --pid=%d", vgdbCommand, targetProcessId); // TODO: vgdb command config option
2112          }
2113          else
2114             GdbCommand(false, "info target"); //GDB/MI Missing Implementation -file-list-symbol-files and -file-list-exec-sections
2115
2116          /*for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
2117             GdbCommand(false, "-environment-directory \"%s\"", prj.topNode.path);*/
2118
2119          for(dir : ide.workspace.sourceDirs; dir && dir[0])
2120          {
2121            bool interference = false;
2122            for(prj : ide.workspace.projects)
2123            {
2124               if(!fstrcmp(prj.topNode.path, dir))
2125               {
2126                  interference = true;
2127                  break;
2128               }
2129            }
2130            if(!interference && dir[0])
2131               GdbCommand(false, "-environment-directory \"%s\"", dir);
2132          }
2133
2134          targeted = true;
2135       }
2136       return true;
2137    }
2138
2139    /*void GdbTargetRelease()
2140    {
2141       if(targeted)
2142       {
2143          BreakpointsDeleteAll();
2144          GdbCommand(false, "file");  //GDB/MI Missing Implementation -target-detach
2145          targeted = false;
2146          symbols = true;
2147       }
2148    }*/
2149
2150    void GdbDebugBreak(bool internal)
2151    {
2152       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbDebugBreak()");
2153       if(targetProcessId)
2154       {
2155          if(internal)
2156             breakType = DebuggerAction::internal;
2157
2158          if(ide) ide.Update(null);
2159          app.Unlock();
2160          if(Process_Break(targetProcessId))  //GdbCommand(false, "-exec-interrupt");
2161             serialSemaphore.Wait();
2162          else
2163          {
2164             _ChangeState(loaded);
2165             targetProcessId = 0;
2166          }
2167          app.Lock();
2168       }
2169       else
2170          ide.outputView.debugBox.Logf("Debugger Error: GdbDebugBreak with not target id should never happen\n");
2171    }
2172
2173    void GdbExecRun()
2174    {
2175       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecRun()");
2176       GdbTargetSet();
2177       if(!usingValgrind)
2178          gdbExecution = run;
2179       GdbExecCommon();
2180       ShowDebuggerViews();
2181       if(usingValgrind)
2182          GdbExecContinue(true);
2183       else
2184          GdbCommand(true, "-exec-run");
2185    }
2186
2187    void GdbExecContinue(bool focus)
2188    {
2189       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecContinue()");
2190       gdbExecution = run;
2191       GdbExecCommon();
2192       GdbCommand(focus, "-exec-continue");
2193    }
2194
2195    void GdbExecNext()
2196    {
2197       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecNext()");
2198       gdbExecution = next;
2199       GdbExecCommon();
2200       GdbCommand(true, "-exec-next");
2201    }
2202
2203    void GdbExecUntil(char * absoluteFilePath, int lineNumber)
2204    {
2205       char relativeFilePath[MAX_LOCATION];
2206       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecUntil()");
2207       gdbExecution = until;
2208       GdbExecCommon();
2209       if(absoluteFilePath)
2210       {
2211          WorkspaceGetRelativePath(absoluteFilePath, relativeFilePath, null);
2212          GdbCommand(true, "-exec-until %s:%d", relativeFilePath, lineNumber);
2213       }
2214       else
2215          GdbCommand(true, "-exec-until");
2216    }
2217
2218    void GdbExecAdvance(char * absoluteFilePathOrLocation, int lineNumber)
2219    {
2220       char relativeFilePath[MAX_LOCATION];
2221       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecAdvance()");
2222       gdbExecution = advance;
2223       GdbExecCommon();
2224       if(lineNumber)
2225       {
2226          WorkspaceGetRelativePath(absoluteFilePathOrLocation, relativeFilePath, null);
2227          GdbCommand(true, "advance %s:%d", relativeFilePath, lineNumber); // should use -exec-advance -- GDB/MI implementation missing
2228       }
2229       else
2230          GdbCommand(true, "advance %s", absoluteFilePathOrLocation); // should use -exec-advance -- GDB/MI implementation missing
2231    }
2232
2233    void GdbExecStep()
2234    {
2235       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecStep()");
2236       gdbExecution = step;
2237       GdbExecCommon();
2238       GdbCommand(true, "-exec-step");
2239    }
2240
2241    void GdbExecFinish()
2242    {
2243       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecFinish()");
2244       gdbExecution = finish;
2245       GdbExecCommon();
2246       GdbCommand(true, "-exec-finish");
2247    }
2248
2249    void GdbExecCommon()
2250    {
2251       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExecCommon()");
2252       BreakpointsMaintenance();
2253    }
2254
2255 #ifdef GDB_DEBUG_GUI
2256    void SendGDBCommand(char * command)
2257    {
2258       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::SendGDBCommand()");
2259       DebuggerState oldState = state;
2260       switch(state)
2261       {
2262          case running:
2263             if(targetProcessId)
2264                GdbDebugBreak(true);
2265          case stopped:
2266          case loaded:
2267             GdbCommand(false, command);
2268             break;
2269       }
2270       if(oldState == running)
2271          GdbExecContinue(false);
2272    }
2273 #endif
2274
2275    void ClearBreakDisplay()
2276    {
2277       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ClearBreakDisplay()");
2278       activeThread = 0;
2279       activeFrameLevel = -1;
2280       hitThread = 0;
2281       signalThread = 0;
2282       signalOn = false;
2283       frameCount = 0;
2284       if(stopItem)
2285          stopItem.Free();
2286       delete stopItem;
2287       event = none;
2288       activeFrame = null;
2289       stackFrames.Free(Frame::Free);
2290       ide.callStackView.Clear();
2291       ide.threadsView.Clear();
2292       ide.Update(null);
2293    }
2294
2295    bool GdbAbortExec()
2296    {
2297       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbAbortExec()");
2298       sentKill = true;
2299       GdbCommand(false, "-interpreter-exec console \"kill\""); // should use -exec-abort -- GDB/MI implementation incomplete
2300       return true;
2301    }
2302
2303    bool GdbInit(CompilerConfig compiler, ProjectConfig config, int bitDepth, bool useValgrind)
2304    {
2305       bool result = true;
2306       char oldDirectory[MAX_LOCATION];
2307       char tempPath[MAX_LOCATION];
2308       char command[MAX_F_STRING*4];
2309       Project project = ide.project;
2310       DirExpression targetDirExp = project.GetTargetDir(compiler, config, bitDepth);
2311       PathBackup pathBackup { };
2312
2313       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbInit()");
2314       if(currentCompiler != compiler)
2315       {
2316          delete currentCompiler;
2317          currentCompiler = compiler;
2318          incref currentCompiler;
2319       }
2320       prjConfig = config;
2321       this.bitDepth = bitDepth;
2322       usingValgrind = useValgrind;
2323
2324       _ChangeState(loaded);
2325       sentKill = false;
2326       sentBreakInsert = false;
2327       breakpointError = false;
2328       ignoreBreakpoints = false;
2329       symbols = true;
2330       targeted = false;
2331       modules = false;
2332       needReset = false;
2333       projectsLibraryLoaded.Free();
2334
2335       ide.outputView.ShowClearSelectTab(debug);
2336       ide.outputView.debugBox.Logf($"Starting debug mode\n");
2337
2338 #ifdef GDB_DEBUG_OUTPUT
2339       ide.outputView.gdbBox.Logf("run: Starting GDB\n");
2340 #endif
2341
2342       strcpy(tempPath, ide.workspace.projectDir);
2343       PathCatSlash(tempPath, targetDirExp.dir);
2344       delete targetDir;
2345       targetDir = CopyString(tempPath);
2346       project.CatTargetFileName(tempPath, compiler, config);
2347       delete targetFile;
2348       targetFile = CopyString(tempPath);
2349
2350       GetWorkingDir(oldDirectory, MAX_LOCATION);
2351       if(ide.workspace.debugDir && ide.workspace.debugDir[0])
2352       {
2353          char temp[MAX_LOCATION];
2354          strcpy(temp, ide.workspace.projectDir);
2355          PathCatSlash(temp, ide.workspace.debugDir);
2356          ChangeWorkingDir(temp);
2357       }
2358       else
2359          ChangeWorkingDir(ide.workspace.projectDir);
2360       
2361       ide.SetPath(true, compiler, config, bitDepth);
2362
2363       // TODO: This pollutes the environment, but at least it works
2364       // It shouldn't really affect the IDE as the PATH gets restored and other variables set for testing will unlikely cause problems
2365       // What is the proper solution for this? DualPipeOpenEnv?
2366       // gdb set environment commands don't seem to take effect
2367       for(e : ide.workspace.environmentVars)
2368       {
2369          SetEnvironment(e.name, e.string);
2370       }
2371
2372       if(usingValgrind)
2373       {
2374          char * clArgs = ide.workspace.commandLineArgs;
2375          const char *valgrindCommand = "valgrind"; // TODO: valgrind command config option //TODO: valgrind options
2376          ValgrindLeakCheck vgLeakCheck = ide.workspace.vgLeakCheck;
2377          int vgRedzoneSize = ide.workspace.vgRedzoneSize;
2378          bool vgTrackOrigins = ide.workspace.vgTrackOrigins;
2379          vgLogFile = CreateTemporaryFile(vgLogPath, "ecereidevglog");
2380          if(vgLogFile)
2381          {
2382             incref vgLogFile;
2383             vgLogThread.Create();
2384          }
2385          else
2386          {
2387             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't open temporary log file for Valgrind output\n");
2388             result = false;
2389          }
2390          if(result && !CheckCommandAvailable(valgrindCommand))
2391          {
2392             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for Valgrind is not available.\n", valgrindCommand);
2393             result = false;
2394          }
2395          if(result)
2396          {
2397             char * vgRedzoneSizeFlag = vgRedzoneSize == -1 ? "" : PrintString(" --redzone-size=", vgRedzoneSize);
2398             sprintf(command, "%s --vgdb=yes --vgdb-error=0 --log-file=%s --leak-check=%s%s --track-origins=%s %s%s%s",
2399                   valgrindCommand, vgLogPath, (char*)vgLeakCheck, vgRedzoneSizeFlag, vgTrackOrigins ? "yes" : "no", targetFile, clArgs ? " " : "", clArgs ? clArgs : "");
2400             if(vgRedzoneSize != -1)
2401                delete vgRedzoneSizeFlag;
2402             vgTargetHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2403             if(!vgTargetHandle)
2404             {
2405                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start Valgrind\n");
2406                result = false;
2407             }
2408          }
2409          if(result)
2410          {
2411             incref vgTargetHandle;
2412             vgTargetThread.Create();
2413
2414             targetProcessId = vgTargetHandle.GetProcessID();
2415             waitingForPID = false;
2416             if(!targetProcessId)
2417             {
2418                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get Valgrind process ID\n");
2419                result = false;
2420             }
2421          }
2422          if(result)
2423          {
2424             app.Unlock();
2425             serialSemaphore.Wait();
2426             app.Lock();
2427          }
2428       }
2429
2430       if(result)
2431       {
2432          strcpy(command,
2433             (compiler.targetPlatform == win32 && bitDepth == 64) ? "x86_64-w64-mingw32-gdb" :
2434             (compiler.targetPlatform == win32 && bitDepth == 32) ? "i686-w64-mingw32-gdb" :
2435             "gdb");
2436          if(!CheckCommandAvailable(command))
2437          {
2438             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Command %s for GDB is not available.\n", command);
2439             result = false;
2440          }
2441          else
2442          {
2443             strcat(command, " -n -silent --interpreter=mi2"); //-async //\"%s\"
2444             gdbTimer.Start();
2445             gdbHandle = DualPipeOpen(PipeOpenMode { output = true, /*error = true, */input = true }, command);
2446             if(!gdbHandle)
2447             {
2448                ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't start GDB\n");
2449                result = false;
2450             }
2451          }
2452       }
2453       if(result)
2454       {
2455          incref gdbHandle;
2456          gdbThread.Create();
2457
2458          gdbProcessId = gdbHandle.GetProcessID();
2459          if(!gdbProcessId)
2460          {
2461             ide.outputView.debugBox.Logf($"Debugger Fatal Error: Couldn't get GDB process ID\n");
2462             result = false;
2463          }
2464       }
2465       if(result)
2466       {
2467          app.Unlock();
2468          serialSemaphore.Wait();
2469          app.Lock();
2470
2471          GdbCommand(false, "-gdb-set verbose off");
2472          //GdbCommand(false, "-gdb-set exec-done-display on");
2473          GdbCommand(false, "-gdb-set step-mode off");
2474          GdbCommand(false, "-gdb-set unwindonsignal on");
2475          //GdbCommand(false, "-gdb-set shell on");
2476          GdbCommand(false, "set print elements 992");
2477          GdbCommand(false, "-gdb-set backtrace limit 100000");
2478
2479          if(!GdbTargetSet())
2480          {
2481             //_ChangeState(terminated);
2482             result = false;
2483          }
2484       }
2485       if(result)
2486       {
2487 #if defined(__unix__)
2488          {
2489             CreateTemporaryDir(progFifoDir, "ecereide");
2490             strcpy(progFifoPath, progFifoDir);
2491             PathCat(progFifoPath, "ideprogfifo");
2492             if(!mkfifo(progFifoPath, 0600))
2493             {
2494                //fileCreated = true;
2495             }
2496             else
2497             {
2498                //app.Lock();
2499                ide.outputView.debugBox.Logf(createFIFOMsg, progFifoPath);
2500                //app.Unlock();
2501             }
2502          }
2503
2504          if(!usingValgrind)
2505          {
2506             progThread.terminate = false;
2507             progThread.Create();
2508          }
2509 #endif
2510
2511 #if defined(__WIN32__)
2512          GdbCommand(false, "-gdb-set new-console on");
2513 #endif
2514
2515 #if defined(__unix__)
2516          if(!usingValgrind)
2517             GdbCommand(false, "-inferior-tty-set %s", progFifoPath);
2518 #endif
2519
2520          if(!usingValgrind)
2521             GdbCommand(false, "-gdb-set args %s", ide.workspace.commandLineArgs ? ide.workspace.commandLineArgs : "");
2522          /*
2523          for(e : ide.workspace.environmentVars)
2524          {
2525             GdbCommand(false, "set environment %s=%s", e.name, e.string);
2526          }
2527          */
2528       }
2529
2530       ChangeWorkingDir(oldDirectory);
2531
2532       delete pathBackup;
2533
2534       if(!result)
2535          GdbExit();
2536       delete targetDirExp;
2537       return result;
2538    }
2539
2540    void GdbExit()
2541    {
2542       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbExit()");
2543       if(gdbHandle && gdbProcessId)
2544       {
2545          gdbTimer.Stop();
2546          GdbCommand(false, "-gdb-exit");
2547
2548          if(gdbThread)
2549          {
2550             app.Unlock();
2551             gdbThread.Wait();
2552             app.Lock();
2553          }
2554          if(gdbHandle)
2555          {
2556             gdbHandle.Wait();
2557             delete gdbHandle;
2558          }
2559       }
2560       gdbTimer.Stop();
2561       _ChangeState(terminated); // this state change seems to be superfluous, is it safety for something?
2562       prjConfig = null;
2563       needReset = false;
2564
2565       if(ide.workspace)
2566       {
2567          for(bp : ide.workspace.breakpoints)
2568             bp.Reset();
2569       }
2570       for(bp : sysBPs)
2571          bp.Reset();
2572       if(bpRunToCursor)
2573          bpRunToCursor.Reset();
2574       
2575       ide.outputView.debugBox.Logf($"Debugging stopped\n");
2576       ClearBreakDisplay();
2577       ide.Update(null);
2578
2579 #if defined(__unix__)
2580       if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
2581       {
2582          progThread.terminate = true;
2583          if(fifoFile)
2584          {
2585             fifoFile.CloseInput();
2586             app.Unlock();
2587             progThread.Wait();
2588             app.Lock();
2589             delete fifoFile;
2590          }         
2591          DeleteFile(progFifoPath);
2592          progFifoPath[0] = '\0';
2593          rmdir(progFifoDir);
2594       }
2595 #endif
2596    }
2597
2598    bool WatchesLinkCodeEditor()
2599    {
2600       bool goodFrame = activeFrame && activeFrame.absoluteFile;
2601       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesLinkCodeEditor()");
2602       if(codeEditor && (!goodFrame || fstrcmp(codeEditor.fileName, activeFrame.absoluteFile)))
2603          WatchesReleaseCodeEditor();
2604
2605       if(!codeEditor && goodFrame)
2606       {
2607          codeEditor = (CodeEditor)ide.OpenFile(activeFrame.absoluteFile, normal, false, null, no, normal, false);
2608          if(codeEditor)
2609          {
2610             codeEditor.inUseDebug = true;
2611             incref codeEditor;
2612          }
2613       }
2614       return codeEditor != null;
2615    }
2616
2617    void WatchesReleaseCodeEditor()
2618    {
2619       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::WatchesReleaseCodeEditor()");
2620       if(codeEditor)
2621       {
2622          codeEditor.inUseDebug = false;
2623          if(!codeEditor.visible)
2624             codeEditor.Destroy(0);
2625          delete codeEditor;
2626       }
2627    }
2628
2629    bool ResolveWatch(Watch wh)
2630    {
2631       bool result = false;
2632       
2633       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::ResolveWatch()");
2634       wh.Reset();
2635
2636       /*delete wh.value;
2637       if(wh.type) 
2638       {
2639          FreeType(wh.type);
2640          wh.type = null;
2641       }*/
2642
2643       if(wh.expression)
2644       {
2645          char watchmsg[MAX_F_STRING];
2646          if(state == stopped && !codeEditor)
2647             wh.value = CopyString($"No source file found for selected frame");
2648          //if(codeEditor && state == stopped || state != stopped)
2649          else
2650          {
2651             Module backupPrivateModule;
2652             Context backupContext;
2653             Class backupThisClass;
2654             Expression exp;
2655             parseError = false;
2656
2657             backupPrivateModule = GetPrivateModule();
2658             backupContext = GetCurrentContext();
2659             backupThisClass = GetThisClass();
2660             if(codeEditor)
2661             {
2662                SetPrivateModule(codeEditor.privateModule);
2663                SetCurrentContext(codeEditor.globalContext);
2664                SetTopContext(codeEditor.globalContext);
2665                SetGlobalContext(codeEditor.globalContext);
2666                SetGlobalData(&codeEditor.globalData);
2667             }
2668          
2669             exp = ParseExpressionString(wh.expression);
2670             
2671             if(exp && !parseError)
2672             {
2673                char expString[4096];
2674                expString[0] = 0;
2675                PrintExpression(exp, expString);
2676
2677                if(GetPrivateModule())
2678                {
2679                   if(codeEditor)
2680                      DebugFindCtxTree(codeEditor.ast, activeFrame.line, 0);
2681                   ProcessExpressionType(exp);
2682                }
2683                wh.type = exp.expType;
2684                if(wh.type)
2685                   wh.type.refCount++;
2686                DebugComputeExpression(exp);
2687                if(ExpressionIsError(exp))
2688                {
2689                   GDBFallBack(exp, expString);
2690                }
2691
2692                /*if(exp.hasAddress)
2693                {
2694                   char temp[MAX_F_STRING];
2695                   sprintf(temp, "0x%x", exp.address);
2696                   wh.address = CopyString(temp);
2697                   // wh.address = CopyStringf("0x%x", exp.address);
2698                }*/
2699 /*
2700 //#ifdef _DEBUG
2701                {
2702                   Type dataType = exp.expType;
2703                   if(dataType)
2704                   {
2705                      char temp[MAX_F_STRING];
2706                      switch(dataType.kind)
2707                      {
2708                         case charType:
2709                            sprintf(temp, "%i", exp.val.c);
2710                            break;
2711                         case shortType:
2712                            sprintf(temp, "%i", exp.val.s);
2713                            break;
2714                         case intType:
2715                         case longType:
2716                         case enumType:
2717                            sprintf(temp, "%i", exp.val.i);
2718                            break;
2719                         case int64Type:
2720                            sprintf(temp, "%i", exp.val.i64);
2721                            break;
2722                         case pointerType:
2723                            sprintf(temp, "%i", exp.val.p);
2724                            break;
2725
2726                         case floatType:
2727                         {
2728                            long v = (long)exp.val.f;
2729                            sprintf(temp, "%i", v);
2730                            break;
2731                         } 
2732                         case doubleType:
2733                         {
2734                            long v = (long)exp.val.d;
2735                            sprintf(temp, "%i", v);
2736                            break;
2737                         }
2738                      }
2739                      if(temp)
2740                         wh.intVal = CopyString(temp);
2741                      switch(dataType.kind)
2742                      {
2743                         case charType:
2744                            sprintf(temp, "0x%x", exp.val.c);
2745                            break;
2746                         case shortType:
2747                            sprintf(temp, "0x%x", exp.val.s);
2748                            break;
2749                         case enumType:
2750                         case intType:
2751                            sprintf(temp, "0x%x", exp.val.i);
2752                            break;
2753                         case int64Type:
2754                            sprintf(temp, "0x%x", exp.val.i64);
2755                            break;
2756                         case longType:
2757                            sprintf(temp, "0x%x", exp.val.i64);
2758                            break;
2759                         case pointerType:
2760                            sprintf(temp, "0x%x", exp.val.p);
2761                            break;
2762
2763                         case floatType:
2764                         {
2765                            long v = (long)exp.val.f;
2766                            sprintf(temp, "0x%x", v);
2767                            break;
2768                         } 
2769                         case doubleType:
2770                         {
2771                            long v = (long)exp.val.d;
2772                            sprintf(temp, "0x%x", v);
2773                            break;
2774                         }
2775                      }
2776                      if(temp)
2777                         wh.hexVal = CopyString(temp);
2778                      switch(dataType.kind)
2779                      {
2780                         case charType:
2781                            sprintf(temp, "0o%o", exp.val.c);
2782                            break;
2783                         case shortType:
2784                            sprintf(temp, "0o%o", exp.val.s);
2785                            break;
2786                         case enumType:
2787                         case intType:
2788                            sprintf(temp, "0o%o", exp.val.i);
2789                            break;
2790                         case int64Type:
2791                            sprintf(temp, "0o%o", exp.val.i64);
2792                            break;
2793                         case longType:
2794                            sprintf(temp, "0o%o", exp.val.i64);
2795                            break;
2796                         case pointerType:
2797                            sprintf(temp, "0o%o", exp.val.p);
2798                            break;
2799
2800                         case floatType:
2801                         {
2802                            long v = (long)exp.val.f;
2803                            sprintf(temp, "0o%o", v);
2804                            break;
2805                         } 
2806                         case doubleType:
2807                         {
2808                            long v = (long)exp.val.d;
2809                            sprintf(temp, "0o%o", v);
2810                            break;
2811                         }
2812                      }
2813                      if(temp)
2814                         wh.octVal = CopyString(temp);
2815                   }
2816                }
2817                // WHATS THIS HERE ?
2818                if(exp.type == constantExp && exp.constant)
2819                   wh.constant = CopyString(exp.constant);
2820 //#endif
2821 */
2822
2823                switch(exp.type)
2824                {
2825                   case symbolErrorExp:
2826                      snprintf(watchmsg, sizeof(watchmsg), $"Symbol \"%s\" not found", exp.identifier.string);
2827                      break;
2828                   case structMemberSymbolErrorExp:
2829                      // todo get info as in next case (ExpClassMemberSymbolError)
2830                      snprintf(watchmsg, sizeof(watchmsg), $"Error: Struct member not found for \"%s\"", wh.expression);
2831                      break;
2832                   case classMemberSymbolErrorExp:
2833                      {
2834                         Class _class;
2835                         Expression memberExp = exp.member.exp;
2836                         Identifier memberID = exp.member.member;
2837                         Type type = memberExp.expType;
2838                         if(type)
2839                         {
2840                            _class = (memberID && memberID.classSym) ? memberID.classSym.registered : ((type.kind == classType && type._class) ? type._class.registered : null);
2841                            if(!_class)
2842                            {
2843                               char string[256] = "";
2844                               Symbol classSym;
2845                               PrintTypeNoConst(type, string, false, true);
2846                               classSym = FindClass(string);
2847                               _class = classSym ? classSym.registered : null;
2848                            }
2849                            if(_class)
2850                               snprintf(watchmsg, sizeof(watchmsg), $"Member \"%s\" not found in class \"%s\"", memberID ? memberID.string : "", _class.name);
2851                            else
2852                               snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in unregistered class? (Should never get this message)", memberID ? memberID.string : "");
2853                         }
2854                         else
2855                            snprintf(watchmsg, sizeof(watchmsg), "Member \"%s\" not found in no type? (Should never get this message)", memberID ? memberID.string : "");
2856                      }
2857                      break;
2858                   case memoryErrorExp:
2859                      // Need to ensure when set to memoryErrorExp, constant is set
2860                      snprintf(watchmsg, sizeof(watchmsg), $"Memory can't be read at %s", /*(exp.type == constantExp) ? */exp.constant /*: null*/);
2861                      break;
2862                   case dereferenceErrorExp:
2863                      snprintf(watchmsg, sizeof(watchmsg), $"Dereference failure for \"%s\"", wh.expression);
2864                      break;
2865                   case unknownErrorExp:
2866                      snprintf(watchmsg, sizeof(watchmsg), $"Unknown error for \"%s\"", wh.expression);
2867                      break;
2868                   case noDebuggerErrorExp:
2869                      snprintf(watchmsg, sizeof(watchmsg), $"Debugger required for symbol evaluation in \"%s\"", wh.expression);
2870                      break;
2871                   case debugStateErrorExp:
2872                      snprintf(watchmsg, sizeof(watchmsg), $"Incorrect debugger state for symbol evaluation in \"%s\"", wh.expression);
2873                      break;
2874                   case 0:
2875                      snprintf(watchmsg, sizeof(watchmsg), $"Null type for \"%s\"", wh.expression);
2876                      break;
2877                   case constantExp:
2878                   case stringExp:
2879                      // Temporary Code for displaying Strings
2880                      if((exp.expType && ((exp.expType.kind == pointerType || 
2881                               exp.expType.kind == arrayType) && exp.expType.type.kind == charType)) || 
2882                            (wh.type && wh.type.kind == classType && wh.type._class && 
2883                               wh.type._class.registered && wh.type._class.registered.type == normalClass &&
2884                               !strcmp(wh.type._class.registered.name, "String")))
2885                      {
2886
2887                         if(exp.expType.kind != arrayType || exp.hasAddress)
2888                         {
2889                            uint64 address;
2890                            char * string;
2891                            char value[4196];
2892                            int len;
2893                            //char temp[MAX_F_STRING * 32];
2894
2895                            ExpressionType evalError = dummyExp;
2896                            /*if(exp.expType.kind == arrayType)
2897                               sprintf(temp, "(char*)0x%x", exp.address);
2898                            else
2899                               sprintf(temp, "(char*)%s", exp.constant);*/
2900
2901                            //evaluation = Debugger::EvaluateExpression(temp, &evalError);
2902                            // address = strtoul(exp.constant, null, 0);
2903                            address = _strtoui64(exp.constant, null, 0);
2904                            //_dpl(0, "0x", address);
2905                            // snprintf(value, sizeof(value), "0x%08x ", address);
2906
2907                            if(address > 0xFFFFFFFFLL)
2908                               snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%016I64x " : "0x%016llx ", address);
2909                            else
2910                               snprintf(value, sizeof(value), (GetRuntimePlatform() == win32) ? "0x%08I64x " : "0x%08llx ", address);
2911                            value[sizeof(value)-1] = 0;
2912                            
2913                            if(!address)
2914                               strcat(value, $"Null string");
2915                            else
2916                            {
2917                               int size = 4096;
2918                               len = strlen(value);
2919                               string = null;
2920                               while(!string && size > 2)
2921                               {
2922                                  string = GdbReadMemory(address, size);
2923                                  size /= 2;
2924                               }
2925                               if(string && string[0])
2926                               {
2927                                  value[len++] = '(';
2928                                  if(UTF8Validate(string))
2929                                  {
2930                                     int c;
2931                                     char ch;
2932                                     
2933                                     for(c = 0; (ch = string[c]) && c<4096; c++)
2934                                        value[len++] = ch;                                 
2935                                     value[len++] = ')';
2936                                     value[len++] = '\0';
2937                                     
2938                                  }
2939                                  else
2940                                  {
2941                                     ISO8859_1toUTF8(string, value + len, 4096 - len - 30);
2942                                     strcat(value, ") (ISO8859-1)");
2943                                  }
2944
2945                                  delete string;
2946                               }
2947                               else if(string)
2948                               {
2949                                  strcat(value, $"Empty string");
2950                                  delete string;
2951                               }
2952                               else
2953                                  strcat(value, $"Couldn't read memory");
2954                            }
2955                            wh.value = CopyString(value);
2956                         }
2957                      }
2958                      else if(wh.type && wh.type.kind == classType && wh.type._class && 
2959                               wh.type._class.registered && wh.type._class.registered.type == enumClass)
2960                      {
2961                         uint64 value = strtoul(exp.constant, null, 0);
2962                         Class enumClass = eSystem_FindClass(GetPrivateModule(), wh.type._class.registered.name);
2963                         EnumClassData enumeration = (EnumClassData)enumClass.data;
2964                         NamedLink item;
2965                         for(item = enumeration.values.first; item; item = item.next)
2966                            if((int)item.data == value)
2967                               break;
2968                         if(item)
2969                            wh.value = CopyString(item.name);
2970                         else
2971                            wh.value = CopyString($"Invalid Enum Value");
2972                         result = true;
2973                      }
2974                      else if(wh.type && (wh.type.kind == charType || (wh.type.kind == classType && wh.type._class && 
2975                               wh.type._class.registered && !strcmp(wh.type._class.registered.fullName, "ecere::com::unichar"))) )
2976                      {
2977                         unichar value;
2978                         int signedValue;
2979                         char charString[5];
2980                         char string[256];
2981
2982                         if(exp.constant[0] == '\'')
2983                         {
2984                            if((int)((byte *)exp.constant)[1] > 127)
2985                            {
2986                               int nb;
2987                               value = UTF8GetChar(exp.constant + 1, &nb);
2988                               if(nb < 2) value = exp.constant[1];
2989                               signedValue = value;
2990                            }
2991                            else
2992                            {
2993                               signedValue = exp.constant[1];
2994                               {
2995                                  // Precomp Syntax error with boot strap here:
2996                                  byte b = (byte)(char)signedValue;
2997                                  value = (unichar) b;
2998                               }
2999                            }
3000                         }
3001                         else
3002                         {
3003                            if(wh.type.kind == charType && wh.type.isSigned)
3004                            {
3005                               signedValue = (int)(char)strtol(exp.constant, null, 0);
3006                               {
3007                                  // Precomp Syntax error with boot strap here:
3008                                  byte b = (byte)(char)signedValue;
3009                                  value = (unichar) b;
3010                               }
3011                            }
3012                            else
3013                            {
3014                               value = (uint)strtoul(exp.constant, null, 0);
3015                               signedValue = (int)value;
3016                            }
3017                         }
3018                         charString[0] = 0;
3019                         UTF32toUTF8Len(&value, 1, charString, 5);
3020                         if(value == '\0')
3021                            snprintf(string, sizeof(string), "\'\\0' (0)");
3022                         else if(value == '\t')
3023                            snprintf(string, sizeof(string), "\'\\t' (%d)", value);
3024                         else if(value == '\n')
3025                            snprintf(string, sizeof(string), "\'\\n' (%d)", value);
3026                         else if(value == '\r')
3027                            snprintf(string, sizeof(string), "\'\\r' (%d)", value);
3028                         else if(wh.type.kind == charType && wh.type.isSigned)
3029                            snprintf(string, sizeof(string), "\'%s\' (%d)", charString, signedValue);
3030                         else if(value > 256 || wh.type.kind != charType)
3031                         {
3032                            if(value > 0x10FFFF || !GetCharCategory(value))
3033                               snprintf(string, sizeof(string), $"Invalid Unicode Keypoint (0x%08X)", value);
3034                            else
3035                               snprintf(string, sizeof(string), "\'%s\' (U+%04X)", charString, value);
3036                         }
3037                         else
3038                            snprintf(string, sizeof(string), "\'%s\' (%d)", charString, value);
3039                         string[sizeof(string)-1] = 0;
3040                         
3041                         wh.value = CopyString(string);
3042                         result = true;
3043                      }
3044                      else
3045                      {
3046                         wh.value = CopyString(exp.constant);
3047                         result = true;
3048                      }
3049                      break;
3050                   default:
3051                      if(exp.hasAddress)
3052                      {
3053                         wh.value = PrintHexUInt64(exp.address);
3054                         result = true;
3055                      }
3056                      else
3057                      {
3058                         char tempString[256];
3059                         if(exp.member.memberType == propertyMember)
3060                            snprintf(watchmsg, sizeof(watchmsg), $"Missing property evaluation support for \"%s\"", wh.expression);
3061                         else
3062                            snprintf(watchmsg, sizeof(watchmsg), $"Evaluation failed for \"%s\" of type \"%s\"", wh.expression, 
3063                                  exp.type.OnGetString(tempString, null, null));
3064                      }
3065                      break;
3066                }
3067             }
3068             else
3069                snprintf(watchmsg, sizeof(watchmsg), $"Invalid expression: \"%s\"", wh.expression);
3070             if(exp) FreeExpression(exp);
3071
3072             
3073             SetPrivateModule(backupPrivateModule);
3074             SetCurrentContext(backupContext);
3075             SetTopContext(backupContext);
3076             SetGlobalContext(backupContext);
3077             SetThisClass(backupThisClass);
3078          }
3079          //else 
3080          //   wh.value = CopyString("No source file found for selected frame");
3081          
3082          watchmsg[sizeof(watchmsg)-1] = 0;
3083          if(!wh.value)
3084             wh.value = CopyString(watchmsg);
3085       }
3086       ide.watchesView.UpdateWatch(wh);
3087       return result;
3088    }
3089
3090    void EvaluateWatches()
3091    {
3092       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateWatches()");
3093       WatchesLinkCodeEditor();
3094       if(state == stopped)
3095       {
3096          for(wh : ide.workspace.watches)
3097             ResolveWatch(wh);
3098       }
3099    }
3100
3101    char * ::GdbEvaluateExpression(char * expression)
3102    {
3103       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::GdbEvaluateExpression(", expression, ")");
3104       eval.active = true;
3105       eval.error = none;
3106       GdbCommand(false, "-data-evaluate-expression \"%s\"", expression);
3107       if(eval.active)
3108          ide.outputView.debugBox.Logf("Debugger Error: GdbEvaluateExpression\n");
3109       return eval.result;
3110    }
3111
3112    // to be removed... use GdbReadMemory that returns a byte array instead
3113    char * ::GdbReadMemoryString(uint64 address, int size, char format, int rows, int cols)
3114    {
3115       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemoryString(", address, ")");
3116       eval.active = true;
3117       eval.error = none;
3118 #ifdef _DEBUG
3119       if(!size)
3120          _dpl(0, "GdbReadMemoryString called with size = 0!");
3121 #endif
3122       // GdbCommand(false, "-data-read-memory 0x%08x %c, %d, %d, %d", address, format, size, rows, cols);
3123       if(GetRuntimePlatform() == win32)
3124          GdbCommand(false, "-data-read-memory 0x%016I64x %c, %d, %d, %d", address, format, size, rows, cols);
3125       else
3126          GdbCommand(false, "-data-read-memory 0x%016llx %c, %d, %d, %d", address, format, size, rows, cols);
3127       if(eval.active)
3128          ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemoryString\n");
3129       return eval.result;
3130    }
3131
3132    byte * ::GdbReadMemory(uint64 address, int bytes)
3133    {
3134       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbReadMemory(", address, ")");
3135       eval.active = true;
3136       eval.error = none;
3137       //GdbCommand(false, "-data-read-memory 0x%08x %c, 1, 1, %d", address, 'u', bytes);
3138       if(GetRuntimePlatform() == win32)
3139          GdbCommand(false, "-data-read-memory 0x%016I64x %c, 1, 1, %d", address, 'u', bytes);
3140       else
3141          GdbCommand(false, "-data-read-memory 0x%016llx %c, 1, 1, %d", address, 'u', bytes);
3142 #ifdef _DEBUG
3143       if(!bytes)
3144          _dpl(0, "GdbReadMemory called with bytes = 0!");
3145 #endif
3146       if(eval.active)
3147          ide.outputView.debugBox.Logf("Debugger Error: GdbReadMemory\n");
3148       else if(eval.result && strcmp(eval.result, "N/A"))
3149       {
3150          byte * result = new byte[bytes];
3151          byte * string = eval.result;
3152          int c = 0;
3153          while(true)
3154          {
3155             result[c++] = (byte)strtol(string, &string, 10);
3156             if(string)
3157             {
3158                if(*string == ',')
3159                   string++;
3160                 else
3161                   break;
3162             }
3163             else
3164                break;
3165          }
3166          return result;
3167       }
3168       return null;
3169    }
3170
3171    bool BreakpointHit(GdbDataStop stopItem, Breakpoint bpInternal, Breakpoint bpUser)
3172    {
3173       bool result = true;
3174       char * s1 = null; char * s2 = null;
3175       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::BreakpointHit(",
3176             "bpInternal(", bpInternal ? s1=bpInternal.CopyLocationString(false) : null, "), ",
3177             "bpUser(", bpUser ? s2=bpUser.CopyLocationString(false) : null, ")) -- ",
3178             "ignoreBreakpoints(", ignoreBreakpoints, "), ",
3179             "hitCursorBreakpoint(", bpUser && bpUser.type == runToCursor,  ")");
3180       delete s1; delete s2;
3181
3182       if(bpUser)
3183       {
3184          bool conditionMet = true;
3185          if(bpUser.condition)
3186          {
3187             if(WatchesLinkCodeEditor())
3188                conditionMet = ResolveWatch(bpUser.condition);
3189             else
3190                conditionMet = false;
3191          }
3192          bpUser.hits++;
3193          if(conditionMet)
3194          {
3195             if(!bpUser.ignore)
3196                bpUser.breaks++;
3197             else
3198             {
3199                bpUser.ignore--;
3200                result = false;
3201             }
3202          }
3203          else
3204             result = false;
3205          if(stopItem.frame.line && bpUser.line != stopItem.frame.line)
3206          {
3207             // updating user breakpoint on hit location difference
3208             // todo, print something?
3209             bpUser.line = stopItem.frame.line;
3210             ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3211             ide.workspace.Save();
3212          }
3213          else
3214             ide.breakpointsView.UpdateBreakpoint(bpUser.row);
3215       }
3216       if(bpInternal)
3217       {
3218          bpInternal.hits++;
3219          if(bpInternal.type == internalModulesLoaded)
3220             modules = true;
3221          if(userAction == stepOver)
3222          {
3223             if((bpInternal.type == internalEntry && ((intBpMain && intBpMain.inserted) || (intBpWinMain && intBpWinMain.inserted))) ||
3224                   (bpInternal.type == internalMain && intBpWinMain && intBpWinMain.inserted))
3225                result = false;
3226          }
3227          if(!bpUser && !userAction.breaksOnInternalBreakpoint)
3228          {
3229             if(userAction == stepOut)
3230                StepOut(ignoreBreakpoints);
3231             else
3232                result = false;
3233          }
3234       }
3235
3236       if(!bpUser && !bpInternal)
3237          result = false;
3238
3239       return result;
3240    }
3241
3242    void ValgrindTargetThreadExit()
3243    {
3244       ide.outputView.debugBox.Logf($"ValgrindTargetThreadExit\n");
3245       if(vgTargetHandle)
3246       {
3247          vgTargetHandle.Wait();
3248          delete vgTargetHandle;
3249       }
3250       HandleExit(null, null);
3251    }
3252
3253    void GdbThreadExit()
3254    {
3255       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::GdbThreadExit()");
3256       if(state != terminated)
3257       {
3258          _ChangeState(terminated);
3259          targetProcessId = 0;
3260          ClearBreakDisplay();
3261
3262          if(vgLogFile)
3263             delete vgLogFile;
3264          if(gdbHandle)
3265          {
3266             serialSemaphore.Release();
3267             gdbTimer.Stop();
3268             gdbHandle.Wait();
3269             delete gdbHandle;
3270             
3271             ide.outputView.debugBox.Logf($"Debugger Fatal Error: GDB lost\n");
3272             ide.outputView.debugBox.Logf($"Debugging stopped\n");
3273             ide.Update(null);
3274             HideDebuggerViews();
3275          }
3276          //_ChangeState(terminated);
3277       }
3278    }
3279
3280    void GdbThreadMain(char * output)
3281    {
3282       int i;
3283       char * t;
3284       Array<char *> outTokens { minAllocSize = 50 };
3285       Array<char *> subTokens { minAllocSize = 50 };
3286       DebugListItem item { };
3287       DebugListItem item2 { };
3288       bool setWaitingForPID = false;
3289       
3290 #if defined(GDB_DEBUG_CONSOLE) || defined(GDB_DEBUG_GUI)
3291 #ifdef GDB_DEBUG_CONSOLE
3292       _dpl2(_dpct, dplchan::gdbOutput, 0, output);
3293 #endif
3294 #ifdef GDB_DEBUG_OUTPUT
3295       {
3296          int len = strlen(output);
3297          if(len > 1024)
3298          {
3299             int c;
3300             char * start;
3301             char tmp[1025];
3302             tmp[1024] = '\0';
3303             start = output;
3304             for(c = 0; c < len / 1024; c++)
3305             {
3306                strncpy(tmp, start, 1024);
3307                ide.outputView.gdbBox.Logf("out: %s\n", tmp);
3308                start += 1024;
3309             }
3310             ide.outputView.gdbBox.Logf("out: %s\n", start);
3311          }
3312          else
3313          {
3314             ide.outputView.gdbBox.Logf("out: %s\n", output);
3315          }
3316       }
3317 #endif
3318 #ifdef GDB_DEBUG_CONSOLE
3319          strcpy(lastGdbOutput, output);
3320 #endif
3321 #ifdef GDB_DEBUG_GUI
3322          if(ide.gdbDialog) ide.gdbDialog.AddOutput(output);
3323 #endif
3324 #endif
3325       
3326       switch(output[0])
3327       {
3328          case '~':
3329             if(strstr(output, "No debugging symbols found") || strstr(output, "(no debugging symbols found)"))
3330             {
3331                symbols = false;
3332                ide.outputView.debugBox.Logf($"Target doesn't contain debug information!\n");
3333                ide.Update(null);
3334             }
3335             if(!entryPoint && (t = strstr(output, "Entry point:")))
3336             {
3337                char * addr = t + strlen("Entry point:");
3338                t = addr;
3339                if(*t++ == ' ' && *t++ == '0' && *t == 'x')
3340                {
3341                   *addr = '*';
3342                   while(isxdigit(*++t));
3343                   *t = '\0';
3344                   for(bp : sysBPs; bp.type == internalEntry)
3345                   {
3346                      bp.function = addr;
3347                      bp.enabled = entryPoint = true;
3348                      break;
3349                   }
3350                }
3351             }
3352             break;
3353          case '^':
3354             gdbReady = false;
3355             if(TokenizeList(output, ',', outTokens) && !strcmp(outTokens[0], "^done"))
3356             {
3357                //if(outTokens.count == 1)
3358                {
3359                   if(sentKill)
3360                   {
3361                      sentKill = false;
3362                      _ChangeState(loaded);
3363                      targetProcessId = 0;
3364                      if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3365                      {
3366                         if(!strcmp(item.name, "reason"))
3367                         {
3368                            char * reason = item.value;
3369                            StripQuotes(reason, reason);
3370                            if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3371                            {
3372                               char * exitCode;
3373                               if(outTokens.count > 2 && TokenizeListItem(outTokens[2], item2))
3374                               {
3375                                  StripQuotes(item2.value, item2.value);
3376                                  if(!strcmp(item2.name, "exit-code"))
3377                                     exitCode = item2.value;
3378                                  else
3379                                     exitCode = null;
3380                               }
3381                               else
3382                                  exitCode = null;
3383                               HandleExit(reason, exitCode);
3384                            }
3385                         }
3386                         else
3387                            _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "kill reply (", item.name, "=", item.value, ") is unheard of");
3388                      }
3389                      else
3390                         HandleExit(null, null);
3391                   }
3392                }
3393                if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3394                {
3395                   if(!strcmp(item.name, "bkpt"))
3396                   {
3397                      sentBreakInsert = false;
3398 #ifdef _DEBUG
3399                      if(bpItem)
3400                         _dpl(0, "problem");
3401 #endif
3402                      bpItem = ParseBreakpoint(item.value, outTokens);
3403                      //breakType = bpValidation;
3404                   }
3405                   else if(!strcmp(item.name, "depth"))
3406                   {
3407                      StripQuotes(item.value, item.value);
3408                      frameCount = atoi(item.value);
3409                      activeFrame = null;
3410                      stackFrames.Free(Frame::Free);
3411                   }
3412                   else if(!strcmp(item.name, "stack"))
3413                   {
3414                      Frame frame;
3415                      if(stackFrames.count)
3416                         ide.callStackView.Logf("...\n");
3417                      else
3418                         activeFrame = null;
3419                      item.value = StripBrackets(item.value);
3420                      TokenizeList(item.value, ',', subTokens);
3421                      for(i = 0; i < subTokens.count; i++)
3422                      {
3423                         if(TokenizeListItem(subTokens[i], item))
3424                         {
3425                            if(!strcmp(item.name, "frame"))
3426                            {
3427                               frame = Frame { };
3428                               stackFrames.Add(frame);
3429                               item.value = StripCurlies(item.value);
3430                               ParseFrame(frame, item.value);
3431                               if(frame.file && frame.from)
3432                                  _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "unexpected frame file and from members present");
3433                               if(frame.file)
3434                               {
3435                                  char * s;
3436                                  if(activeFrameLevel == -1)
3437                                  {
3438                                     if(ide.projectView.IsModuleInProject(frame.file));
3439                                     {
3440                                        if(frame.level != 0)
3441                                        {
3442                                           //stopItem.frame = frame;
3443                                           breakType = selectFrame;
3444                                        }
3445                                        else
3446                                           activeFrame = frame;
3447                                        activeFrameLevel = frame.level;
3448                                     }
3449                                  }
3450                                  ide.callStackView.Logf("%3d ", frame.level);
3451                                  if(!strncmp(frame.func, "__ecereMethod_", strlen("__ecereMethod_")))
3452                                     ide.callStackView.Logf($"%s Method, %s:%d\n", &frame.func[strlen("__ecereMethod_")], (s = CopySystemPath(frame.file)), frame.line);
3453                                  else if(!strncmp(frame.func, "__ecereProp_", strlen("__ecereProp_")))
3454                                     ide.callStackView.Logf($"%s Property, %s:%d\n", &frame.func[strlen("__ecereProp_")], (s = CopySystemPath(frame.file)), frame.line);
3455                                  else if(!strncmp(frame.func, "__ecereConstructor_", strlen("__ecereConstructor_")))
3456                                     ide.callStackView.Logf($"%s Constructor, %s:%d\n", &frame.func[strlen("__ecereConstructor_")], (s = CopySystemPath(frame.file)), frame.line);
3457                                  else if(!strncmp(frame.func, "__ecereDestructor_", strlen("__ecereDestructor_")))
3458                                     ide.callStackView.Logf($"%s Destructor, %s:%d\n", &frame.func[strlen("__ecereDestructor_")], (s = CopySystemPath(frame.file)), frame.line);
3459                                  else
3460                                     ide.callStackView.Logf($"%s Function, %s:%d\n", frame.func, (s = CopySystemPath(frame.file)), frame.line);
3461                                  delete s;
3462                               }
3463                               else
3464                               {
3465                                  ide.callStackView.Logf("%3d ", frame.level);
3466
3467                                  if(frame.from)
3468                                  {
3469                                     char * s;
3470                                     ide.callStackView.Logf($"inside %s, %s\n", frame.func, (s = CopySystemPath(frame.from)));
3471                                     delete s;
3472                                  }
3473                                  else if(frame.func)
3474                                     ide.callStackView.Logf("%s\n", frame.func);
3475                                  else
3476                                     ide.callStackView.Logf($"unknown source\n");
3477                               }
3478                            }
3479                            else
3480                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "stack content (", item.name, "=", item.value, ") is unheard of");
3481                         }
3482                      }
3483                      if(activeFrameLevel == -1)
3484                      {
3485                         activeFrameLevel = 0;
3486                         activeFrame = stackFrames.first;
3487                      }
3488                      ide.callStackView.Home();
3489                      ide.Update(null);
3490                      subTokens.RemoveAll();
3491                   }
3492                   /*else if(!strcmp(item.name, "frame"))
3493                   {
3494                      Frame frame { };
3495                      item.value = StripCurlies(item.value);
3496                      ParseFrame(&frame, item.value);
3497                   }*/
3498                   else if(!strcmp(item.name, "thread-ids"))
3499                   {
3500                      ide.threadsView.Clear();
3501                      item.value = StripCurlies(item.value);
3502                      TokenizeList(item.value, ',', subTokens);
3503                      for(i = subTokens.count - 1; ; i--)
3504                      {
3505                         if(TokenizeListItem(subTokens[i], item))
3506                         {
3507                            if(!strcmp(item.name, "thread-id"))
3508                            {
3509                               int value;
3510                               StripQuotes(item.value, item.value);
3511                               value = atoi(item.value);
3512                               ide.threadsView.Logf("%3d \n", value);
3513                            }
3514                            else
3515                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "threads content (", item.name, "=", item.value, ") is unheard of");
3516                         }
3517                         if(!i)
3518                            break;
3519                      }
3520                      ide.threadsView.Home();
3521                      ide.Update(null);
3522                      subTokens.RemoveAll();
3523                      //if(!strcmp(outTokens[2], "number-of-threads"))
3524                   }
3525                   else if(!strcmp(item.name, "new-thread-id"))
3526                   {
3527                      StripQuotes(item.value, item.value);
3528                      activeThread = atoi(item.value);
3529                   }
3530                   else if(!strcmp(item.name, "value"))
3531                   {
3532                      StripQuotes(item.value, item.value);
3533                      eval.result = CopyString(item.value);
3534                      eval.active = false;
3535                   }
3536                   else if(!strcmp(item.name, "addr"))
3537                   {
3538                      for(i = 2; i < outTokens.count; i++)
3539                      {
3540                         if(TokenizeListItem(outTokens[i], item))
3541                         {
3542                            if(!strcmp(item.name, "total-bytes"))
3543                            {
3544                               StripQuotes(item.value, item.value);
3545                               eval.bytes = atoi(item.value);
3546                            }
3547                            else if(!strcmp(item.name, "next-row"))
3548                            {
3549                               StripQuotes(item.value, item.value);
3550                               eval.nextBlockAddress = _strtoui64(item.value, null, 0);
3551                            }
3552                            else if(!strcmp(item.name, "memory"))
3553                            {
3554                               int j;
3555                               //int value;
3556                               //StripQuotes(item.value, item.value);
3557                               item.value = StripBrackets(item.value);
3558                               // this should be treated as a list...
3559                               item.value = StripCurlies(item.value);
3560                               TokenizeList(item.value, ',', subTokens);
3561                               for(j = 0; j < subTokens.count; j++)
3562                               {
3563                                  if(TokenizeListItem(subTokens[j], item))
3564                                  {
3565                                     if(!strcmp(item.name, "data"))
3566                                     {
3567                                        item.value = StripBrackets(item.value);
3568                                        StripQuotes2(item.value, item.value);
3569                                        eval.result = CopyString(item.value);
3570                                        eval.active = false;
3571                                     }
3572                                  }
3573                               }
3574                               subTokens.RemoveAll();
3575                            }
3576                         }
3577                      }
3578                   }
3579                   else if(!strcmp(item.name, "source-path") || !strcmp(item.name, "BreakpointTable"))
3580                      _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "command reply (", item.name, "=", item.value, ") is ignored");
3581                   else
3582                      _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "command reply (", item.name, "=", item.value, ") is unheard of");
3583                }
3584             }
3585             else if(!strcmp(outTokens[0], "^running"))
3586             {
3587                waitingForPID = true;
3588                setWaitingForPID = true;
3589                ClearBreakDisplay();
3590             }
3591             else if(!strcmp(outTokens[0], "^exit"))
3592             {
3593                _ChangeState(terminated);
3594                // ide.outputView.debugBox.Logf("Exit\n");
3595                // ide.Update(null);
3596                gdbReady = true;
3597                serialSemaphore.Release();
3598             }
3599             else if(!strcmp(outTokens[0], "^error"))
3600             {
3601                if(sentBreakInsert)
3602                {
3603                   sentBreakInsert = false;
3604                   breakpointError = true;
3605                }
3606
3607                if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
3608                {
3609                   if(!strcmp(item.name, "msg"))
3610                   {
3611                      StripQuotes(item.value, item.value);
3612                      if(eval.active)
3613                      {
3614                         eval.active = false;
3615                         eval.result = null;
3616                         if(strstr(item.value, "No symbol") && strstr(item.value, "in current context"))
3617                            eval.error = symbolNotFound;
3618                         else if(strstr(item.value, "Cannot access memory at address"))
3619                            eval.error = memoryCantBeRead;
3620                         else
3621                            eval.error = unknown;
3622                      }
3623                      else if(!strcmp(item.value, "Previous frame inner to this frame (corrupt stack?)"))
3624                      {
3625                      }
3626                      else if(!strncmp(item.value, "Cannot access memory at address", 31))
3627                      {
3628                      }
3629                      else if(!strcmp(item.value, "Cannot find bounds of current function"))
3630                      {
3631                         _ChangeState(stopped);
3632                         gdbHandle.Printf("-exec-continue\n");
3633                      }
3634                      else if(!strcmp(item.value, "ptrace: No such process."))
3635                      {
3636                         _ChangeState(loaded);
3637                         targetProcessId = 0;
3638                      }
3639                      else if(!strcmp(item.value, "Function \\\"WinMain\\\" not defined."))
3640                      {
3641                      }
3642                      else if(!strcmp(item.value, "You can't do that without a process to debug."))
3643                      {
3644                         _ChangeState(loaded);
3645                         targetProcessId = 0;
3646                      }
3647                      else if(strstr(item.value, "No such file or directory."))
3648                      {
3649                         _ChangeState(loaded);
3650                         targetProcessId = 0;
3651                      }
3652                      else if(strstr(item.value, "During startup program exited with code "))
3653                      {
3654                         _ChangeState(loaded);
3655                         targetProcessId = 0;
3656                      }
3657                      else
3658                      {
3659 #ifdef _DEBUG
3660                         if(strlen(item.value) < MAX_F_STRING)
3661                         {
3662                            char * s;
3663                            ide.outputView.debugBox.Logf("GDB: %s\n", (s = CopyUnescapedString(item.value)));
3664                            delete s;
3665                         }
3666                         else
3667                            ide.outputView.debugBox.Logf("GDB: %s\n", item.value);
3668 #endif
3669                      }
3670                   }
3671                }
3672                else
3673                   _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "error content (", item.name, "=", item.value, ") is unheard of");
3674             }
3675             else
3676                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "result-record: ", outTokens[0]);
3677             outTokens.RemoveAll();
3678             break;
3679          case '+':
3680             _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "status-async-output: ", outTokens[0]);
3681             break;
3682          case '=':
3683             if(TokenizeList(output, ',', outTokens))
3684             {
3685                if(!strcmp(outTokens[0], "=library-loaded"))
3686                   FGODetectLoadedLibraryForAddedProjectIssues(outTokens);
3687                else if(!strcmp(outTokens[0], "=thread-group-created") || !strcmp(outTokens[0], "=thread-group-added") ||
3688                         !strcmp(outTokens[0], "=thread-group-started") || !strcmp(outTokens[0], "=thread-group-exited") ||
3689                         !strcmp(outTokens[0], "=thread-created") || !strcmp(outTokens[0], "=thread-exited") ||
3690                         !strcmp(outTokens[0], "=cmd-param-changed") || !strcmp(outTokens[0], "=library-unloaded") ||
3691                         !strcmp(outTokens[0], "=breakpoint-modified"))
3692                   _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, outTokens[0], outTokens.count>1 ? outTokens[1] : "",
3693                            outTokens.count>2 ? outTokens[2] : "", outTokens.count>3 ? outTokens[3] : "",
3694                            outTokens.count>4 ? outTokens[4] : "", outTokens.count>5 ? outTokens[5] : "",
3695                            outTokens.count>6 ? outTokens[6] : "", outTokens.count>7 ? outTokens[7] : "",
3696                            outTokens.count>8 ? outTokens[8] : "", outTokens.count>9 ? outTokens[9] : "");
3697                else
3698                   _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "notify-async-output: ", outTokens[0]);
3699             }
3700             outTokens.RemoveAll();
3701             break;
3702          case '*':
3703             gdbReady = false;
3704             if(TokenizeList(output, ',', outTokens))
3705             {
3706                if(!strcmp(outTokens[0],"*running"))
3707                {
3708                   waitingForPID = true;
3709                   setWaitingForPID = true;
3710                }
3711                else if(!strcmp(outTokens[0], "*stopped"))
3712                {
3713                   int tk;
3714                   _ChangeState(stopped);
3715
3716                   for(tk = 1; tk < outTokens.count; tk++)
3717                   {
3718                      if(TokenizeListItem(outTokens[tk], item))
3719                      {
3720                         if(!strcmp(item.name, "reason"))
3721                         {
3722                            char * reason = item.value;
3723                            StripQuotes(reason, reason);
3724                            if(!strcmp(reason, "exited-normally") || !strcmp(reason, "exited") || !strcmp(reason, "exited-signalled"))
3725                            {
3726                               char * exitCode;
3727                               if(outTokens.count > tk+1 && TokenizeListItem(outTokens[tk+1], item2))
3728                               {
3729                                  tk++;
3730                                  StripQuotes(item2.value, item2.value);
3731                                  if(!strcmp(item2.name, "exit-code"))
3732                                     exitCode = item2.value;
3733                                  else
3734                                     exitCode = null;
3735                               }
3736                               else
3737                                  exitCode = null;
3738                               HandleExit(reason, exitCode);
3739                               needReset = true;
3740                            }
3741                            else if(!strcmp(reason, "breakpoint-hit") ||
3742                                    !strcmp(reason, "function-finished") ||
3743                                    !strcmp(reason, "end-stepping-range") ||
3744                                    !strcmp(reason, "location-reached") ||
3745                                    !strcmp(reason, "signal-received"))
3746                            {
3747                               char r = reason[0];
3748 #ifdef _DEBUG
3749                               if(stopItem) _dpl(0, "problem");
3750 #endif
3751                               stopItem = GdbDataStop { };
3752                               stopItem.reason = r == 'b' ? breakpointHit : r == 'f' ? functionFinished : r == 'e' ? endSteppingRange : r == 'l' ? locationReached : signalReceived;
3753
3754                               for(i = tk+1; i < outTokens.count; i++)
3755                               {
3756                                  TokenizeListItem(outTokens[i], item);
3757                                  StripQuotes(item.value, item.value);
3758                                  if(!strcmp(item.name, "thread-id"))
3759                                     stopItem.threadid = atoi(item.value);
3760                                  else if(!strcmp(item.name, "frame"))
3761                                  {
3762                                     item.value = StripCurlies(item.value);
3763                                     ParseFrame(stopItem.frame, item.value);
3764                                  }
3765                                  else if(stopItem.reason == breakpointHit && !strcmp(item.name, "bkptno"))
3766                                     stopItem.bkptno = atoi(item.value);
3767                                  else if(stopItem.reason == functionFinished && !strcmp(item.name, "gdb-result-var"))
3768                                     stopItem.gdbResultVar = CopyString(item.value);
3769                                  else if(stopItem.reason == functionFinished && !strcmp(item.name, "return-value"))
3770                                     stopItem.returnValue = CopyString(item.value);
3771                                  else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-name"))
3772                                     stopItem.name = CopyString(item.value);
3773                                  else if(stopItem.reason == signalReceived && !strcmp(item.name, "signal-meaning"))
3774                                     stopItem.meaning = CopyString(item.value);
3775                                  else if(!strcmp(item.name, "stopped-threads"))
3776                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Advanced thread debugging not handled");
3777                                  else if(!strcmp(item.name, "core"))
3778                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": Information (core) not used");
3779                                  else if(!strcmp(item.name, "disp"))
3780                                     _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, reason, ": (", item.name, "=", item.value, ")");
3781                                  else
3782                                     _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown ", reason, " item name (", item.name, "=", item.value, ")");
3783                               }
3784
3785                               if(stopItem.reason == signalReceived && !strcmp(stopItem.name, "SIGTRAP"))
3786                               {
3787                                  switch(breakType)
3788                                  {
3789                                     case internal:
3790                                        breakType = none;
3791                                        break;
3792                                     case restart:
3793                                     case stop:
3794                                        break;
3795                                     default:
3796                                        event = breakEvent;
3797                                  }
3798                               }
3799                               else
3800                               {
3801                                  event = r == 'b' ? hit : r == 'f' ? functionEnd : r == 'e' ? stepEnd : r == 'l' ? locationReached : signal;
3802                                  ide.Update(null);
3803                               }
3804                            }
3805                            else if(!strcmp(reason, "watchpoint-trigger"))
3806                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint trigger not handled");
3807                            else if(!strcmp(reason, "read-watchpoint-trigger"))
3808                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason read watchpoint trigger not handled");
3809                            else if(!strcmp(reason, "access-watchpoint-trigger"))
3810                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason access watchpoint trigger not handled");
3811                            else if(!strcmp(reason, "watchpoint-scope"))
3812                               _dpl2(_dpct, dplchan::gdbProtoIgnored, 0, "Reason watchpoint scope not handled");
3813                            else
3814                               _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown reason: ", reason);
3815                         }
3816                         else
3817                         {
3818                            PrintLn(output);
3819                         }
3820                      }
3821                   }
3822                   if(usingValgrind && event == none && !stopItem)
3823                      event = valgrindStartPause;
3824                   app.SignalEvent();
3825                }
3826             }
3827             else
3828                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, "Unknown exec-async-output: ", outTokens[0]);
3829             outTokens.RemoveAll();
3830             break;
3831          case '(':
3832             if(!strcmpi(output, "(gdb) "))
3833             {
3834                if(waitingForPID)
3835                {
3836                   char exeFile[MAX_LOCATION];
3837                   int oldProcessID = targetProcessId;
3838                   GetLastDirectory(targetFile, exeFile);
3839
3840                   while(!targetProcessId/*true*/)
3841                   {
3842                      targetProcessId = Process_GetChildExeProcessId(gdbProcessId, exeFile);
3843                      if(targetProcessId || gdbHandle.Peek()) break;
3844                      Sleep(0.01);
3845                   }
3846
3847                   if(targetProcessId)
3848                      _ChangeState(running);
3849                   else if(!oldProcessID)
3850                   {
3851                      ide.outputView.debugBox.Logf($"Debugger Error: No target process ID\n");
3852                      // TO VERIFY: The rest of this block has not been thoroughly tested in this particular location
3853                      gdbHandle.Printf("-gdb-exit\n");
3854                      gdbTimer.Stop();
3855                      _ChangeState(terminated); //loaded;
3856                      prjConfig = null;
3857
3858                      if(ide.workspace)
3859                      {
3860                         for(bp : ide.workspace.breakpoints)
3861                            bp.inserted = false;
3862                      }
3863                      for(bp : sysBPs)
3864                         bp.inserted = false;
3865                      if(bpRunToCursor)
3866                         bpRunToCursor.inserted = false;
3867
3868                      ide.outputView.debugBox.Logf($"Debugging stopped\n");
3869                      ClearBreakDisplay();
3870
3871                #if defined(__unix__)
3872                      if(!usingValgrind && FileExists(progFifoPath)) //fileCreated)
3873                      {
3874                         progThread.terminate = true;
3875                         if(fifoFile)
3876                         {
3877                            fifoFile.CloseInput();
3878                            app.Unlock();
3879                            progThread.Wait();
3880                            app.Lock();
3881                            delete fifoFile;
3882                         }
3883
3884                         DeleteFile(progFifoPath);
3885                         progFifoPath[0] = '\0';
3886                         rmdir(progFifoDir);
3887                      }
3888                #endif
3889                   }
3890                }
3891                gdbReady = true;
3892                serialSemaphore.Release();
3893             }
3894             else
3895                _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown prompt", output);
3896
3897             break;
3898          case '&':
3899             if(!strncmp(output, "&\"warning:", 10))
3900             {
3901                char * content;
3902                content = strstr(output, "\"");
3903                StripQuotes(content, content);
3904                content = strstr(content, ":");
3905                if(content)
3906                   content++;
3907                if(content)
3908                {
3909                   char * s;
3910                   ide.outputView.debugBox.LogRaw((s = CopyUnescapedString(content)));
3911                   delete s;
3912                   ide.Update(null);
3913                }
3914             }
3915             break;
3916          default:
3917             _dpl2(_dpct, dplchan::gdbProtoUnknown, 0, $"Unknown output: ", output);
3918       }
3919       if(!setWaitingForPID)
3920          waitingForPID = false;
3921       setWaitingForPID = false;
3922
3923       delete outTokens;
3924       delete subTokens;
3925       delete item;
3926       delete item2;
3927    }
3928
3929    // From GDB Output functions
3930    void FGODetectLoadedLibraryForAddedProjectIssues(Array<char *> outTokens)
3931    {
3932       char path[MAX_LOCATION] = "";
3933       char file[MAX_FILENAME] = "";
3934       bool symbolsLoaded;
3935       DebugListItem item { };
3936       //_dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGODetectLoadedLibraryForAddedProjectIssues()");
3937       for(token : outTokens)
3938       {
3939          if(TokenizeListItem(token, item))
3940          {
3941             if(!strcmp(item.name, "target-name"))
3942             {
3943                StripQuotes(item.value, path);
3944                MakeSystemPath(path);
3945                GetLastDirectory(path, file);
3946             }
3947             else if(!strcmp(item.name, "symbols-loaded"))
3948             {
3949                symbolsLoaded = (atoi(item.value) == 1);
3950             }
3951          }
3952       }
3953       delete item;
3954       if(path[0] && file[0])
3955       {
3956          for(prj : ide.workspace.projects; prj != ide.workspace.projects.firstIterator.data)
3957          {
3958             bool match;
3959             char * dot;
3960             char prjTargetPath[MAX_LOCATION];
3961             char prjTargetFile[MAX_FILENAME];
3962             DirExpression targetDirExp = prj.GetTargetDir(currentCompiler, prj.config, bitDepth);
3963             strcpy(prjTargetPath, prj.topNode.path);
3964             PathCat(prjTargetPath, targetDirExp.dir);
3965             prjTargetFile[0] = '\0';
3966             prj.CatTargetFileName(prjTargetFile, currentCompiler, prj.config);
3967             PathCat(prjTargetPath, prjTargetFile);
3968             MakeSystemPath(prjTargetPath);
3969
3970             match = !fstrcmp(prjTargetFile, file);
3971             if(!match && (dot = strstr(prjTargetFile, ".so.")))
3972             {
3973                char * dot3 = strstr(dot+4, ".");
3974                if(dot3)
3975                {
3976                   dot3[0] = '\0';
3977                   match = !fstrcmp(prjTargetFile, file);
3978                }
3979                if(!match)
3980                {
3981                   dot[3] = '\0';
3982                   match = !fstrcmp(prjTargetFile, file);
3983                }
3984             }
3985             if(match)
3986             {
3987                // TODO: nice visual feedback to better warn user. use some ide notification system or other means.
3988                /* -- this is disabled because we can't trust gdb's symbols-loaded="0" field for =library-loaded (http://sourceware.org/bugzilla/show_bug.cgi?id=10693)
3989                if(!symbolsLoaded)
3990                   ide.outputView.debugBox.Logf($"Attention! No symbols for loaded library %s matched to the %s added project.\n", path, prj.topNode.name);
3991                */
3992                match = !fstrcmp(prjTargetPath, path);
3993                if(!match && (dot = strstr(prjTargetPath, ".so.")))
3994                {
3995                   char * dot3 = strstr(dot+4, ".");
3996                   if(dot3)
3997                   {
3998                      dot3[0] = '\0';
3999                      match = !fstrcmp(prjTargetPath, path);
4000                   }
4001                   if(!match)
4002                   {
4003                      dot[3] = '\0';
4004                      match = !fstrcmp(prjTargetPath, path);
4005                   }
4006                }
4007                if(match)
4008                   projectsLibraryLoaded[prj.name] = true;
4009                else
4010                   ide.outputView.debugBox.Logf($"Loaded library %s doesn't match the %s target of the %s added project.\n", path, prjTargetPath, prj.topNode.name);
4011                break;
4012             }
4013          }
4014       }
4015    }
4016
4017    void FGOBreakpointModified(Array<char *> outTokens)
4018    {
4019       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::FGOBreakpointModified() -- TODO only if needed: support breakpoint modified");
4020 #if 0
4021       DebugListItem item { };
4022       if(outTokens.count > 1 && TokenizeListItem(outTokens[1], item))
4023       {
4024          if(!strcmp(item.name, "bkpt"))
4025          {
4026             GdbDataBreakpoint modBp = ParseBreakpoint(item.value, outTokens);
4027             delete modBp;
4028          }
4029       }
4030 #endif
4031    }
4032
4033
4034    ExpressionType ::DebugEvalExpTypeError(char * result)
4035    {
4036       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::DebugEvalExpTypeError()");
4037       if(result)
4038          return dummyExp;
4039       switch(eval.error)
4040       {
4041          case symbolNotFound:
4042             return symbolErrorExp;
4043          case memoryCantBeRead:
4044             return memoryErrorExp;
4045       }
4046       return unknownErrorExp;
4047    }
4048
4049    char * ::EvaluateExpression(char * expression, ExpressionType * error)
4050    {
4051       char * result;
4052       _dpl2(_dpct, dplchan::debuggerWatches, 0, "Debugger::EvaluateExpression(", expression, ")");
4053       if(ide.projectView && ide.debugger.state == stopped)
4054       {
4055          result = GdbEvaluateExpression(expression);
4056          *error = DebugEvalExpTypeError(result);
4057       }
4058       else
4059       {
4060          result = null;
4061          *error = noDebuggerErrorExp;
4062       }
4063       return result;
4064    }
4065
4066    char * ::ReadMemory(uint64 address, int size, char format, ExpressionType * error)
4067    {
4068       // check for state
4069       char * result = GdbReadMemoryString(address, size, format, 1, 1);
4070       _dpl2(_dpct, dplchan::debuggerCall, 0, "Debugger::ReadMemory(", address, ")");
4071       if(!result || !strcmp(result, "N/A"))
4072          *error = memoryErrorExp;
4073       else
4074          *error = DebugEvalExpTypeError(result);
4075       return result;
4076    }
4077 }
4078
4079 class ValgrindLogThread : Thread
4080 {
4081    Debugger debugger;
4082
4083    unsigned int Main()
4084    {
4085       static char output[4096];
4086       Array<char> dynamicBuffer { minAllocSize = 4096 };
4087       File oldValgrindHandle = vgLogFile;
4088       incref oldValgrindHandle;
4089
4090       app.Lock();
4091       while(debugger.state != terminated && vgLogFile)
4092       {
4093          int result;
4094          app.Unlock();
4095          result = vgLogFile.Read(output, 1, sizeof(output));
4096          app.Lock();
4097          if(debugger.state == terminated || !vgLogFile/* || vgLogFile.Eof()*/)
4098             break;
4099          if(result)
4100          {
4101             int c;
4102             int start = 0;
4103
4104             for(c = 0; c<result; c++)
4105             {
4106                if(output[c] == '\n')
4107                {
4108                   int pos = dynamicBuffer.size;
4109                   dynamicBuffer.size += c - start;
4110                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4111                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4112                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4113                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4114                      dynamicBuffer.size++;
4115                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4116 #ifdef _DEBUG
4117                   // printf("%s\n", dynamicBuffer.array);
4118 #endif
4119                   if(strstr(&dynamicBuffer[0], "vgdb me"))
4120                      debugger.serialSemaphore.Release();
4121                   ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4122                   dynamicBuffer.size = 0;
4123                   start = c + 1;
4124                }
4125             }
4126             if(c == result)
4127             {
4128                int pos = dynamicBuffer.size;
4129                dynamicBuffer.size += c - start;
4130                memcpy(&dynamicBuffer[pos], output + start, c - start);
4131             }
4132          }
4133          else if(debugger.state == stopped)
4134          {
4135 /*#ifdef _DEBUG
4136             printf("Got end of file from GDB!\n");
4137 #endif*/
4138             app.Unlock();
4139             Sleep(0.2);
4140             app.Lock();
4141          }
4142       }
4143       delete dynamicBuffer;
4144       ide.outputView.debugBox.Logf($"ValgrindLogThreadExit\n");
4145       //if(oldValgrindHandle == vgLogFile)
4146          debugger.GdbThreadExit/*ValgrindLogThreadExit*/();
4147       delete oldValgrindHandle;
4148       app.Unlock();
4149       return 0;
4150    }
4151 }
4152
4153 class ValgrindTargetThread : Thread
4154 {
4155    Debugger debugger;
4156
4157    unsigned int Main()
4158    {
4159       static char output[4096];
4160       Array<char> dynamicBuffer { minAllocSize = 4096 };
4161       DualPipe oldValgrindHandle = vgTargetHandle;
4162       incref oldValgrindHandle;
4163
4164       app.Lock();
4165       while(debugger.state != terminated && vgTargetHandle && !vgTargetHandle.Eof())
4166       {
4167          int result;
4168          app.Unlock();
4169          result = vgTargetHandle.Read(output, 1, sizeof(output));
4170          app.Lock();
4171          if(debugger.state == terminated || !vgTargetHandle || vgTargetHandle.Eof())
4172             break;
4173          if(result)
4174          {
4175             int c;
4176             int start = 0;
4177
4178             for(c = 0; c<result; c++)
4179             {
4180                if(output[c] == '\n')
4181                {
4182                   int pos = dynamicBuffer.size;
4183                   dynamicBuffer.size += c - start;
4184                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4185                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4186                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4187                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4188                      dynamicBuffer.size++;
4189                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4190 #ifdef _DEBUG
4191                   // printf("%s\n", dynamicBuffer.array);
4192 #endif
4193                   ide.outputView.debugBox.Logf("%s\n", &dynamicBuffer[0]);
4194
4195                   dynamicBuffer.size = 0;
4196                   start = c + 1;
4197                }
4198             }
4199             if(c == result)
4200             {
4201                int pos = dynamicBuffer.size;
4202                dynamicBuffer.size += c - start;
4203                memcpy(&dynamicBuffer[pos], output + start, c - start);
4204             }
4205          }
4206          else
4207          {
4208 #ifdef _DEBUG
4209             printf("Got end of file from GDB!\n");
4210 #endif
4211          }
4212       }
4213       delete dynamicBuffer;
4214       //if(oldValgrindHandle == vgTargetHandle)
4215          debugger.ValgrindTargetThreadExit();
4216       delete oldValgrindHandle;
4217       app.Unlock();
4218       return 0;
4219    }
4220 }
4221
4222 class GdbThread : Thread
4223 {
4224    Debugger debugger;
4225
4226    unsigned int Main()
4227    {
4228       static char output[4096];
4229       Array<char> dynamicBuffer { minAllocSize = 4096 };
4230       DualPipe oldGdbHandle = gdbHandle;
4231       incref oldGdbHandle;
4232
4233       app.Lock();
4234       while(debugger.state != terminated && gdbHandle && !gdbHandle.Eof())
4235       {
4236          int result;
4237          app.Unlock();
4238          result = gdbHandle.Read(output, 1, sizeof(output));
4239          app.Lock();
4240          if(debugger.state == terminated || !gdbHandle || gdbHandle.Eof())
4241             break;
4242          if(result)
4243          {
4244             int c;
4245             int start = 0;
4246
4247             for(c = 0; c<result; c++)
4248             {
4249                if(output[c] == '\n')
4250                {
4251                   int pos = dynamicBuffer.size;
4252                   dynamicBuffer.size += c - start;
4253                   memcpy(&dynamicBuffer[pos], output + start, c - start);
4254                   if(dynamicBuffer.count && dynamicBuffer[dynamicBuffer.count - 1] != '\r')
4255                   // COMMENTED OUT DUE TO ISSUE #135, FIXED
4256                   //if(dynamicBuffer.array[dynamicBuffer.count - 1] != '\r')
4257                      dynamicBuffer.size++;
4258                   dynamicBuffer[dynamicBuffer.count - 1] = '\0';
4259 #ifdef _DEBUG
4260                   // _dpl(0, dynamicBuffer.array);
4261 #endif
4262                   debugger.GdbThreadMain(&dynamicBuffer[0]);
4263                   dynamicBuffer.size = 0;
4264                   start = c + 1;
4265                }
4266             }
4267             if(c == result)
4268             {
4269                int pos = dynamicBuffer.size;
4270                dynamicBuffer.size += c - start;
4271                memcpy(&dynamicBuffer[pos], output + start, c - start);
4272             }
4273          }
4274          else
4275          {
4276 #ifdef _DEBUG
4277             _dpl(0, "Got end of file from GDB!");
4278 #endif
4279          }
4280       }
4281       delete dynamicBuffer;
4282       //if(oldGdbHandle == gdbHandle)
4283          debugger.GdbThreadExit();
4284       delete oldGdbHandle;
4285       app.Unlock();
4286       return 0;
4287    }
4288 }
4289
4290 static define createFIFOMsg = $"err: Unable to create FIFO %s\n";
4291 static define openFIFOMsg = $"err: Unable to open FIFO %s for read\n";
4292
4293 #if defined(__unix__)
4294 #define uint _uint
4295 #include <errno.h>
4296 #include <stdio.h>
4297 #include <fcntl.h>
4298 #include <sys/types.h>
4299 #undef uint
4300
4301 File fifoFile;
4302
4303 class ProgramThread : Thread
4304 {
4305    bool terminate;
4306    unsigned int Main()
4307    {
4308       bool result = true;
4309       bool fileCreated = false;
4310       mode_t mask = 0600;
4311       static char output[1000];
4312       int fd;
4313
4314       /*if(!mkfifo(progFifoPath, mask))
4315       {
4316          fileCreated = true;
4317       }
4318       else
4319       {
4320          app.Lock();
4321          ide.outputView.debugBox.Logf($"err: Unable to create FIFO %s\n", progFifoPath);
4322          app.Unlock();
4323       }*/
4324
4325       if(FileExists(progFifoPath)) //fileCreated)
4326       {
4327          fifoFile = FileOpen(progFifoPath, read);
4328          if(!fifoFile)
4329          {
4330             app.Lock();
4331             ide.outputView.debugBox.Logf(openFIFOMsg, progFifoPath);
4332             app.Unlock();
4333          }
4334          else
4335          {
4336             fd = fileno((FILE *)fifoFile.input);
4337             //fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
4338          }
4339       }
4340
4341       while(!terminate && fifoFile && !fifoFile.Eof())
4342       {
4343          fd_set rs, es;
4344          struct timeval time;
4345          int selectResult;
4346          time.tv_sec = 1;
4347          time.tv_usec = 0;
4348          FD_ZERO(&rs);
4349          FD_ZERO(&es);
4350          FD_SET(fd, &rs);
4351          FD_SET(fd, &es);
4352          selectResult = select(fd + 1, &rs, null, null, &time);
4353          if(FD_ISSET(fd, &rs))
4354          {
4355             int result = (int)read(fd, output, sizeof(output)-1);
4356             if(!result || (result < 0 && errno != EAGAIN))
4357                break;
4358             if(result > 0)
4359             {
4360                output[result] = '\0';
4361                if(strcmp(output,"&\"warning: GDB: Failed to set controlling terminal: Invalid argument\\n\"\n"))
4362                {
4363                   app.Lock();
4364                   ide.outputView.debugBox.Log(output);
4365                   app.Unlock();
4366                }
4367             }
4368          }
4369       }
4370
4371       //if(fifoFile)
4372       {
4373          //fifoFile.CloseInput();
4374          //delete fifoFile;
4375          app.Lock();
4376          ide.outputView.debugBox.Log("\n");
4377          app.Unlock();
4378       }
4379       /*
4380       if(FileExists(progFifoPath)) //fileCreated)
4381       {
4382          DeleteFile(progFifoPath);
4383          progFifoPath[0] = '\0';
4384       }
4385       */
4386       return 0;
4387    }
4388 }
4389 #endif
4390
4391 class Argument : struct
4392 {
4393    Argument prev, next;
4394    char * name;
4395    property char * name { set { delete name; if(value) name = CopyString(value); } }
4396    char * val;
4397    property char * val { set { delete val; if(value) val = CopyString(value); } }
4398
4399    void Free()
4400    {
4401       delete name;
4402       delete val;
4403    }
4404
4405    ~Argument()
4406    {
4407       Free();
4408    }
4409 }
4410
4411 class Frame : struct
4412 {
4413    Frame prev, next;
4414    int level;
4415    char * addr;
4416    property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4417    char * func;
4418    property char * func { set { delete func; if(value) func = CopyString(value); } }
4419    int argsCount;
4420    OldList args;
4421    char * from;
4422    property char * from { set { delete from; if(value) from = CopyUnescapedUnixPath(value); } }
4423    char * file;
4424    property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4425    char * absoluteFile;
4426    property char * absoluteFile { set { delete absoluteFile; if(value) absoluteFile = CopyUnescapedUnixPath(value); } }
4427    int line;
4428
4429    void Free()
4430    {
4431       delete addr;
4432       delete func;
4433       delete from;
4434       delete file;
4435       delete absoluteFile;
4436       args.Free(Argument::Free);
4437    }
4438
4439    ~Frame()
4440    {
4441       Free();
4442    }
4443 }
4444
4445 class GdbDataStop : struct
4446 {
4447    DebuggerReason reason;
4448    int threadid;
4449    union
4450    {
4451       struct
4452       {
4453          int bkptno;
4454       };
4455       struct
4456       {
4457          char * name;
4458          char * meaning;
4459       };
4460       struct
4461       {
4462          char * gdbResultVar;
4463          char * returnValue;
4464       };
4465    };
4466    Frame frame { };
4467
4468    void Free()
4469    {
4470       if(reason)
4471       {
4472          if(reason == signalReceived)
4473          {
4474             delete name;
4475             delete meaning;
4476          }
4477          else if(reason == functionFinished)
4478          {
4479             delete gdbResultVar;
4480             delete returnValue;
4481          }
4482       }
4483       if(frame) frame.Free();
4484    }
4485
4486    ~GdbDataStop()
4487    {
4488       Free();
4489    }
4490 }
4491
4492 class GdbDataBreakpoint : struct
4493 {
4494    int id;
4495    char * number;
4496    property char * number { set { delete number; if(value) number = CopyString(value); } }
4497    char * type;
4498    property char * type { set { delete type; if(value) type = CopyString(value); } }
4499    char * disp;
4500    property char * disp { set { delete disp; if(value) disp = CopyString(value); } }
4501    bool enabled;
4502    char * addr;
4503    property char * addr { set { delete addr; if(value) addr = CopyString(value); } }
4504    char * func;
4505    property char * func { set { delete func; if(value) func = CopyString(value); } }
4506    char * file;
4507    property char * file { set { delete file; if(value) file = CopyUnescapedUnixPath(value); } }
4508    char * fullname;
4509    property char * fullname { set { delete fullname; if(value) fullname = CopyUnescapedUnixPath(value); } }
4510    int line;
4511    char * at;
4512    property char * at { set { delete at; if(value) at = CopyString(value); } }
4513    int times;
4514
4515    Array<GdbDataBreakpoint> multipleBPs;
4516
4517    void Print()
4518    {
4519    _dpl(0, "");
4520       PrintLn("{", "#", number, " T", type, " D", disp, " E", enabled, " H", times, " (", func, ") (", file, ":", line, ") (", fullname, ") (", addr, ") (", at, ")", "}");
4521    }
4522
4523    void Free()
4524    {
4525       delete type;
4526       delete disp;
4527       delete addr;
4528       delete func;
4529       delete file;
4530       delete at;
4531       if(multipleBPs) multipleBPs.Free();
4532       delete multipleBPs;
4533    }
4534
4535    ~GdbDataBreakpoint()
4536    {
4537       Free();
4538    }
4539 }
4540
4541 class Breakpoint : struct
4542 {
4543    class_no_expansion;
4544
4545    char * function;
4546    property char * function { set { delete function; if(value) function = CopyString(value); } }
4547    char * relativeFilePath;
4548    property char * relativeFilePath { set { delete relativeFilePath; if(value) relativeFilePath = CopyString(value); } }
4549    char * absoluteFilePath;
4550    property char * absoluteFilePath { set { delete absoluteFilePath; if(value) absoluteFilePath = CopyString(value); } }
4551    char * location;
4552    property char * location { set { delete location; if(value) location = CopyString(value); } }
4553    int line;
4554    bool enabled;
4555    int hits;
4556    int breaks;
4557    int ignore;
4558    int level;
4559    Watch condition;
4560    bool inserted;
4561    BreakpointType type;
4562    DataRow row;
4563    GdbDataBreakpoint bp;
4564    Project project;
4565    char * address;
4566    property char * address { set { delete address; if(value) address = CopyString(value); } }
4567
4568    void ParseLocation()
4569    {
4570       char * prjName = null;
4571       char * filePath = null;
4572       char * file;
4573       char * line;
4574       char fullPath[MAX_LOCATION];
4575       if(location[0] == '\(' && location[1] && (file = strchr(location+2, '\)')) && file[1])
4576       {
4577          prjName = new char[file-location];
4578          strncpy(prjName, location+1, file-location-1);
4579          prjName[file-location-1] = '\0';
4580          file++;
4581       }
4582       else
4583          file = location;
4584       if((line = strchr(file+1, ':')))
4585       {
4586          filePath = new char[strlen(file)+1];
4587          strncpy(filePath, file, line-file);
4588          filePath[line-file] = '\0';
4589          line++;
4590       }
4591       else
4592          filePath = CopyString(file);
4593       property::relativeFilePath = filePath;
4594       if(prjName)
4595       {
4596          for(prj : ide.workspace.projects)
4597          {
4598             if(!strcmp(prjName, prj.name))
4599             {
4600                if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4601                {
4602                   property::absoluteFilePath = fullPath;
4603                   project = prj;
4604                   break;
4605                }
4606             }
4607          }
4608          if(line[0])
4609             this.line = atoi(line);
4610       }
4611       else
4612       {
4613          Project prj = ide.project;
4614          if(ProjectGetAbsoluteFromRelativePath(prj, filePath, fullPath))
4615          {
4616             property::absoluteFilePath = fullPath;
4617             project = prj;
4618          }
4619       }
4620       if(!absoluteFilePath)
4621          property::absoluteFilePath = "";
4622       delete prjName;
4623       delete filePath;
4624    }
4625
4626    char * CopyLocationString(bool removePath)
4627    {
4628       char * location;
4629       char * file = relativeFilePath ? relativeFilePath : absoluteFilePath;
4630       bool removingPath = removePath && file;
4631       if(removingPath)
4632       {
4633          char * fileName = new char[MAX_FILENAME];
4634          GetLastDirectory(file, fileName);
4635          file = fileName;
4636       }
4637       if(function)
4638       {
4639          if(file)
4640             location = PrintString(file, ":", function);
4641          else
4642             location = CopyString(function);
4643       }
4644       else
4645          location = PrintString(file, ":", line);
4646       if(removingPath)
4647          delete file;
4648       return location;
4649    }
4650
4651    char * CopyUserLocationString()
4652    {
4653       char * location;
4654       char * loc = CopyLocationString(false);
4655       Project prj = null;
4656       if(absoluteFilePath)
4657       {
4658          for(p : ide.workspace.projects; p != ide.workspace.projects.firstIterator.data)
4659          {
4660             if(p.topNode.FindByFullPath(absoluteFilePath, false))
4661             {
4662                prj = p;
4663                break;
4664             }
4665          }
4666       }
4667       if(prj)
4668       {
4669          location = PrintString("(", prj.name, ")", loc);
4670          delete loc;
4671       }
4672       else
4673          location = loc;
4674       return location;
4675    }
4676
4677    void Save(File f)
4678    {
4679       if(relativeFilePath && relativeFilePath[0])
4680       {
4681          char * location = CopyUserLocationString();
4682          f.Printf("    * %d,%d,%d,%d,%s\n", enabled ? 1 : 0, ignore, level, line, location);
4683          delete location;
4684          if(condition)
4685             f.Printf("       ~ %s\n", condition.expression);
4686       }
4687    }
4688
4689    void Free()
4690    {
4691       Reset();
4692       delete function;
4693       delete relativeFilePath;
4694       delete absoluteFilePath;
4695       delete location;
4696    }
4697
4698    void Reset()
4699    {
4700       inserted = false;
4701       delete address;
4702       if(bp)
4703          bp.Free();
4704       delete bp;
4705    }
4706
4707    ~Breakpoint()
4708    {
4709       Free();
4710    }
4711
4712 }
4713
4714 class Watch : struct
4715 {
4716    class_no_expansion;
4717    
4718    Type type;
4719    char * expression;
4720    char * value;
4721    DataRow row;
4722
4723    void Save(File f)
4724    {
4725       f.Printf("    ~ %s\n", expression);
4726    }
4727
4728    void Free()
4729    {
4730       delete expression;
4731       delete value;
4732       FreeType(type);
4733       type = null;
4734    }
4735
4736    void Reset()
4737    {
4738       delete value;
4739       FreeType(type);
4740       type = null;
4741    }
4742
4743    ~Watch()
4744    {
4745       Free();
4746    }
4747 }
4748
4749 class DebugListItem : struct
4750 {
4751    char * name;
4752    char * value;
4753 }
4754
4755 struct DebugEvaluationData
4756 {
4757    bool active;
4758    char * result;
4759    int bytes;
4760    uint64 nextBlockAddress;
4761
4762    DebuggerEvaluationError error;
4763 };
4764
4765 class CodeLocation : struct
4766 {
4767    char * file;
4768    char * absoluteFile;
4769    int line;
4770
4771    CodeLocation ::ParseCodeLocation(char * location)
4772    {
4773       if(location)
4774       {
4775          char * colon = null;
4776          char * temp;
4777          char loc[MAX_LOCATION];
4778          strcpy(loc, location);
4779          for(temp = loc; temp = strstr(temp, ":"); temp++)
4780             colon = temp;
4781          if(colon)
4782          {
4783             colon[0] = '\0';
4784             colon++;
4785             if(colon)
4786             {
4787                int line = atoi(colon);
4788                if(line)
4789                {
4790                   CodeLocation codloc { line = line };
4791                   codloc.file = CopyString(loc);
4792                   codloc.absoluteFile = ide.workspace.GetAbsolutePathFromRelative(loc);
4793                   return codloc;
4794                }
4795             }
4796          }
4797       }
4798       return null;
4799    }
4800
4801    void Free()
4802    {
4803       delete file;
4804       delete absoluteFile;
4805    }
4806
4807    ~CodeLocation()
4808    {
4809       Free();
4810    }
4811 }
4812
4813 void GDBFallBack(Expression exp, String expString)
4814 {
4815    char * result;
4816    ExpressionType evalError = dummyExp;
4817    result = Debugger::EvaluateExpression(expString, &evalError);
4818    if(result)
4819    {
4820       exp.constant = result;
4821       exp.type = constantExp;
4822    }
4823 }
4824
4825 static Project WorkspaceGetFileOwner(char * absolutePath)
4826 {
4827    Project owner = null;
4828    for(prj : ide.workspace.projects)
4829    {
4830       if(prj.topNode.FindByFullPath(absolutePath, false))
4831       {
4832          owner = prj;
4833          break;
4834       }
4835    }
4836    if(!owner)
4837       WorkspaceGetObjectFileNode(absolutePath, &owner);
4838    return owner;
4839 }
4840
4841 static ProjectNode WorkspaceGetObjectFileNode(char * filePath, Project * project)
4842 {
4843    ProjectNode node = null;
4844    char ext[MAX_EXTENSION];
4845    GetExtension(filePath, ext);
4846    if(ext[0])
4847    {
4848       IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4849       if(type)
4850       {
4851          char fileName[MAX_FILENAME];
4852          GetLastDirectory(filePath, fileName);
4853          if(fileName[0])
4854          {
4855             DotMain dotMain = DotMain::FromFileName(fileName);
4856             for(prj : ide.workspace.projects)
4857             {
4858                if((node = prj.FindNodeByObjectFileName(fileName, type, dotMain, null)))
4859                {
4860                   if(project)
4861                      *project = prj;
4862                   break;
4863                }
4864             }
4865          }
4866       }
4867    }
4868    return node;
4869 }
4870
4871 static ProjectNode ProjectGetObjectFileNode(Project project, char * filePath)
4872 {
4873    ProjectNode node = null;
4874    char ext[MAX_EXTENSION];
4875    GetExtension(filePath, ext);
4876    if(ext[0])
4877    {
4878       IntermediateFileType type = IntermediateFileType::FromExtension(ext);
4879       if(type)
4880       {
4881          char fileName[MAX_FILENAME];
4882          GetLastDirectory(filePath, fileName);
4883          if(fileName[0])
4884          {
4885             DotMain dotMain = DotMain::FromFileName(fileName);
4886             node = project.FindNodeByObjectFileName(fileName, type, dotMain, null);
4887          }
4888       }
4889    }
4890    return node;
4891 }
4892
4893 static void WorkspaceGetRelativePath(char * absolutePath, char * relativePath, Project * owner)
4894 {
4895    Project prj = WorkspaceGetFileOwner(absolutePath);
4896    if(owner)
4897       *owner = prj;
4898    if(!prj)
4899       prj = ide.workspace.projects.firstIterator.data;
4900    if(prj)
4901    {
4902       MakePathRelative(absolutePath, prj.topNode.path, relativePath);
4903       MakeSlashPath(relativePath);
4904    }
4905    else
4906       relativePath[0] = '\0';
4907 }
4908
4909 static bool ProjectGetAbsoluteFromRelativePath(Project project, char * relativePath, char * absolutePath)
4910 {
4911    ProjectNode node = project.topNode.FindWithPath(relativePath, false);
4912    if(!node)
4913       node = ProjectGetObjectFileNode(project, relativePath);
4914    if(node)
4915    {
4916       strcpy(absolutePath, node.project.topNode.path);
4917       PathCat(absolutePath, relativePath);
4918       MakeSlashPath(absolutePath);
4919    }
4920    return node != null;
4921 }