3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
9 #include <qstringlist.h>
10 #include <klocale.h> /* i18n */
12 #include <stdlib.h> /* strtol, atoi */
13 #include <string.h> /* strcpy */
21 static void skipString(const char*& p
);
22 static void skipNested(const char*& s
, char opening
, char closing
);
23 static VarTree
* parseVar(const char*& s
);
24 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
);
25 static bool parseValue(const char*& s
, VarTree
* variable
);
26 static bool parseNested(const char*& s
, VarTree
* variable
);
27 static bool parseVarSeq(const char*& s
, VarTree
* variable
);
28 static bool parseValueSeq(const char*& s
, VarTree
* variable
);
30 #define PROMPT "(kdbg)"
32 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
35 // TODO: make this cmd info stuff non-static to allow multiple
36 // simultaneous gdbs to run!
40 const char* fmt
; /* format string */
42 argNone
, argString
, argNum
,
43 argStringNum
, argNumString
,
49 // This is how the QString data print statement generally looks like.
50 // It is set by KDebugger via setPrintQStringDataCmd().
52 static const char printQStringStructFmt
[] =
53 // if the string data is junk, fail early
54 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
55 // print an array of shorts
56 "(*(unsigned short*)$qstrunicode)@"
58 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
59 // if unicode data is 0, report a special value
62 static const char printQStringStructFmt
[] = "print (0?\"%s\":$kdbgundef)\n";
65 * The following array of commands must be sorted by the DC* values,
66 * because they are used as indices.
68 static GdbCmdInfo cmds
[] = {
69 { DCinitialize
, "", GdbCmdInfo::argNone
},
70 { DCtty
, "tty %s\n", GdbCmdInfo::argString
},
71 { DCexecutable
, "file \"%s\"\n", GdbCmdInfo::argString
},
72 { DCtargetremote
, "target remote %s\n", GdbCmdInfo::argString
},
73 { DCcorefile
, "core-file %s\n", GdbCmdInfo::argString
},
74 { DCattach
, "attach %s\n", GdbCmdInfo::argString
},
75 { DCinfolinemain
, "kdbg_infolinemain\n", GdbCmdInfo::argNone
},
76 { DCinfolocals
, "kdbg__alllocals\n", GdbCmdInfo::argNone
},
77 { DCinforegisters
, "info all-registers\n", GdbCmdInfo::argNone
},
78 { DCexamine
, "x %s %s\n", GdbCmdInfo::argString2
},
79 { DCinfoline
, "info line %s:%d\n", GdbCmdInfo::argStringNum
},
80 { DCdisassemble
, "disassemble %s %s\n", GdbCmdInfo::argString2
},
81 { DCsetargs
, "set args %s\n", GdbCmdInfo::argString
},
82 { DCsetenv
, "set env %s %s\n", GdbCmdInfo::argString2
},
83 { DCunsetenv
, "unset env %s\n", GdbCmdInfo::argString
},
84 { DCsetoption
, "setoption %s %d\n", GdbCmdInfo::argStringNum
},
85 { DCcd
, "cd %s\n", GdbCmdInfo::argString
},
86 { DCbt
, "bt\n", GdbCmdInfo::argNone
},
87 { DCrun
, "run\n", GdbCmdInfo::argNone
},
88 { DCcont
, "cont\n", GdbCmdInfo::argNone
},
89 { DCstep
, "step\n", GdbCmdInfo::argNone
},
90 { DCstepi
, "stepi\n", GdbCmdInfo::argNone
},
91 { DCnext
, "next\n", GdbCmdInfo::argNone
},
92 { DCnexti
, "nexti\n", GdbCmdInfo::argNone
},
93 { DCfinish
, "finish\n", GdbCmdInfo::argNone
},
94 { DCuntil
, "until %s:%d\n", GdbCmdInfo::argStringNum
},
95 { DCkill
, "kill\n", GdbCmdInfo::argNone
},
96 { DCbreaktext
, "break %s\n", GdbCmdInfo::argString
},
97 { DCbreakline
, "break %s:%d\n", GdbCmdInfo::argStringNum
},
98 { DCtbreakline
, "tbreak %s:%d\n", GdbCmdInfo::argStringNum
},
99 { DCbreakaddr
, "break *%s\n", GdbCmdInfo::argString
},
100 { DCtbreakaddr
, "tbreak *%s\n", GdbCmdInfo::argString
},
101 { DCwatchpoint
, "watch %s\n", GdbCmdInfo::argString
},
102 { DCdelete
, "delete %d\n", GdbCmdInfo::argNum
},
103 { DCenable
, "enable %d\n", GdbCmdInfo::argNum
},
104 { DCdisable
, "disable %d\n", GdbCmdInfo::argNum
},
105 { DCprint
, "print %s\n", GdbCmdInfo::argString
},
106 { DCprintDeref
, "print *(%s)\n", GdbCmdInfo::argString
},
107 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
108 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
109 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
110 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
111 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
112 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
113 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
114 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
115 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
116 { DCsetpc
, "set variable $pc=%s\n", GdbCmdInfo::argString
},
117 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
118 { DCsetvariable
, "set variable %s=%s\n", GdbCmdInfo::argString2
},
121 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
122 #define MAX_FMTLEN 200
124 GdbDriver::GdbDriver() :
126 m_gdbMajor(4), m_gdbMinor(16)
128 strcpy(m_prompt
, PROMPT
);
129 m_promptMinLen
= PROMPT_LEN
;
130 m_promptLastChar
= PROMPT_LAST_CHAR
;
133 // check command info array
135 for (int i
= 0; i
< NUM_CMDS
; i
++) {
136 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
137 assert(i
== cmds
[i
].cmd
);
138 // a format string must be associated
139 assert(cmds
[i
].fmt
!= 0);
140 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
141 // format string must match arg specification
142 switch (cmds
[i
].argsNeeded
) {
143 case GdbCmdInfo::argNone
:
144 assert(strchr(cmds
[i
].fmt
, '%') == 0);
146 case GdbCmdInfo::argString
:
147 perc
= strchr(cmds
[i
].fmt
, '%');
148 assert(perc
!= 0 && perc
[1] == 's');
149 assert(strchr(perc
+2, '%') == 0);
151 case GdbCmdInfo::argNum
:
152 perc
= strchr(cmds
[i
].fmt
, '%');
153 assert(perc
!= 0 && perc
[1] == 'd');
154 assert(strchr(perc
+2, '%') == 0);
156 case GdbCmdInfo::argStringNum
:
157 perc
= strchr(cmds
[i
].fmt
, '%');
158 assert(perc
!= 0 && perc
[1] == 's');
159 perc
= strchr(perc
+2, '%');
160 assert(perc
!= 0 && perc
[1] == 'd');
161 assert(strchr(perc
+2, '%') == 0);
163 case GdbCmdInfo::argNumString
:
164 perc
= strchr(cmds
[i
].fmt
, '%');
165 assert(perc
!= 0 && perc
[1] == 'd');
166 perc
= strchr(perc
+2, '%');
167 assert(perc
!= 0 && perc
[1] == 's');
168 assert(strchr(perc
+2, '%') == 0);
170 case GdbCmdInfo::argString2
:
171 perc
= strchr(cmds
[i
].fmt
, '%');
172 assert(perc
!= 0 && perc
[1] == 's');
173 perc
= strchr(perc
+2, '%');
174 assert(perc
!= 0 && perc
[1] == 's');
175 assert(strchr(perc
+2, '%') == 0);
177 case GdbCmdInfo::argNum2
:
178 perc
= strchr(cmds
[i
].fmt
, '%');
179 assert(perc
!= 0 && perc
[1] == 'd');
180 perc
= strchr(perc
+2, '%');
181 assert(perc
!= 0 && perc
[1] == 'd');
182 assert(strchr(perc
+2, '%') == 0);
186 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
190 GdbDriver::~GdbDriver()
195 QString
GdbDriver::driverName() const
200 QString
GdbDriver::defaultGdb()
204 " --fullname" /* to get standard file names each time the prog stops */
205 " --nx"; /* do not execute initialization files */
208 QString
GdbDriver::defaultInvocation() const
210 if (m_defaultCmd
.isEmpty()) {
217 QStringList
GdbDriver::boolOptionList() const
220 return QStringList();
223 bool GdbDriver::startup(QString cmdStr
)
225 if (!DebuggerDriver::startup(cmdStr
))
228 static const char gdbInitialize
[] =
230 * Work around buggy gdbs that do command line editing even if they
231 * are not on a tty. The readline library echos every command back
232 * in this case, which is confusing for us.
236 "set print static-members off\n"
237 "set print asm-demangle on\n"
239 * Don't assume that program functions invoked from a watch expression
242 "set unwindonsignal on\n"
244 * Write a short macro that prints all locals: local variables and
245 * function arguments.
247 "define kdbg__alllocals\n"
248 "info locals\n" /* local vars supersede args with same name */
249 "info args\n" /* therefore, arguments must come last */
252 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
254 "define kdbg_infolinemain\n"
258 // change prompt string and synchronize with gdb
259 "set prompt " PROMPT
"\n"
262 executeCmdString(DCinitialize
, gdbInitialize
, false);
264 // assume that QString::null is ok
265 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
270 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
272 // command string must be committed
273 if (!cmd
->m_committed
) {
275 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
280 switch (cmd
->m_cmd
) {
282 // get version number from preamble
285 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
286 int offset
= GDBVersion
.match(m_output
, 0, &len
);
288 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
290 m_gdbMajor
= strtol(start
, &end
, 10);
291 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
293 // nothing was parsed
298 // assume some default version (what would make sense?)
302 // use a feasible core-file command
303 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
304 cmds
[DCcorefile
].fmt
= "target core %s\n";
306 cmds
[DCcorefile
].fmt
= "core-file %s\n";
313 /* ok, the command is ready */
314 emit
commandReceived(cmd
, m_output
);
316 switch (cmd
->m_cmd
) {
335 * The --fullname option makes gdb send a special normalized sequence print
336 * each time the program stops and at some other points. The sequence has
337 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
339 void GdbDriver::parseMarker()
341 char* startMarker
= strstr(m_output
, "\032\032");
342 if (startMarker
== 0)
345 // extract the marker
347 TRACE(QString("found marker: ") + startMarker
);
348 char* endMarker
= strchr(startMarker
, '\n');
354 // extract filename and line number
355 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
358 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
359 if (lineNoStart
>= 0) {
360 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
363 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
364 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
366 // now show the window
367 startMarker
[lineNoStart
] = '\0'; /* split off file name */
368 emit
activateFileLine(startMarker
, lineNo
-1, address
);
374 * Escapes characters that might lead to problems when they appear on gdb's
377 static void normalizeStringArg(QString
& arg
)
380 * Remove trailing backslashes. This approach is a little simplistic,
381 * but we know that there is at the moment no case where a trailing
382 * backslash would make sense.
384 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
385 arg
= arg
.left(arg
.length()-1);
390 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
392 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
393 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
395 normalizeStringArg(strArg
);
398 // need the working directory when parsing the output
399 m_programWD
= strArg
;
400 } else if (cmd
== DCsetargs
&& !m_redirect
.isEmpty()) {
402 * Use saved redirection. We prepend it in front of the user's
403 * arguments so that the user can override the redirections.
405 strArg
= m_redirect
+ " " + strArg
;
408 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg
.length());
409 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
413 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
415 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
416 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
418 SIZED_QString(cmdString
, MAX_FMTLEN
+30);
420 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
424 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
426 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
427 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
428 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
432 normalizeStringArg(strArg
);
434 SIZED_QString(cmdString
, MAX_FMTLEN
+30+strArg
.length());
439 * intArg specifies which channels should be redirected to
440 * /dev/null. It is a value or'ed together from RDNstdin,
441 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
444 * Note: We rely on that after the DCtty a DCsetargs will follow,
445 * which will ultimately apply the redirection.
447 static const char* const runRedir
[8] = {
451 "</dev/null >/dev/null",
453 "</dev/null 2>/dev/null",
455 "</dev/null >/dev/null 2>&1"
457 if (strArg
.isEmpty())
458 intArg
= 7; /* failsafe if no tty */
459 m_redirect
= runRedir
[intArg
& 7];
461 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
464 if (cmd
== DCexamine
) {
465 // make a format specifier from the intArg
466 static const char size
[16] = {
467 '\0', 'b', 'h', 'w', 'g'
469 static const char format
[16] = {
470 '\0', 'x', 'd', 'u', 'o', 't',
471 'a', 'c', 'f', 's', 'i'
473 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
474 assert(MDTformatmask
== 0xf0); /* next 4 bits */
475 int count
= 16; /* number of entities to print */
476 char sizeSpec
= size
[intArg
& MDTsizemask
];
477 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
478 assert(sizeSpec
!= '\0');
479 assert(formatSpec
!= '\0');
480 // adjust count such that 16 lines are printed
481 switch (intArg
& MDTformatmask
) {
482 case MDTstring
: case MDTinsn
:
483 break; /* no modification needed */
485 // all cases drop through:
486 switch (intArg
& MDTsizemask
) {
498 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
500 return makeCmdString(DCexamine
, spec
, strArg
);
503 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
505 // line numbers are zero-based
506 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
507 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
511 if (cmd
== DCinfoline
)
513 // must split off file name part
514 int slash
= strArg
.findRev('/');
516 strArg
= strArg
.right(strArg
.length()-slash
-1);
518 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
522 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
527 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
529 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
530 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
532 normalizeStringArg(strArg1
);
533 normalizeStringArg(strArg2
);
535 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg1
.length()+strArg2
.length());
536 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
540 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
542 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
543 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
545 SIZED_QString(cmdString
, MAX_FMTLEN
+60);
546 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
550 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
552 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
553 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
556 m_haveCoreFile
= false;
559 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
562 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
565 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
568 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
572 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
575 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
578 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
581 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
584 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
587 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
590 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
593 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
595 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
598 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
601 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
604 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
607 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
610 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
613 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
616 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
619 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
622 void GdbDriver::terminate()
628 void GdbDriver::detachAndTerminate()
632 executeCmdString(DCinitialize
, "detach\nquit\n", true);
635 void GdbDriver::interruptInferior()
638 // remove accidentally queued commands
642 static bool isErrorExpr(const char* output
)
645 strncmp(output
, "Cannot access memory at", 23) == 0 ||
646 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
647 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
648 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
649 strncmp(output
, "There is no member or method named", 34) == 0 ||
650 strncmp(output
, "A parse error in expression", 27) == 0 ||
651 strncmp(output
, "No symbol \"", 11) == 0 ||
652 strncmp(output
, "Internal error: ", 16) == 0;
656 * Returns true if the output is an error message. If wantErrorValue is
657 * true, a new VarTree object is created and filled with the error message.
658 * If there are warnings, they are skipped and output points past the warnings
659 * on return (even if there \e are errors).
661 static bool parseErrorMessage(const char*& output
,
662 VarTree
*& variable
, bool wantErrorValue
)
665 while (strncmp(output
, "warning:", 8) == 0)
667 char* end
= strchr(output
+8, '\n');
669 output
+= strlen(output
);
674 if (isErrorExpr(output
))
676 if (wantErrorValue
) {
677 // put the error message as value in the variable
678 variable
= new VarTree(QString(), VarTree::NKplain
);
679 const char* endMsg
= strchr(output
, '\n');
681 endMsg
= output
+ strlen(output
);
682 variable
->m_value
= FROM_LATIN1(output
, endMsg
-output
);
691 #if QT_VERSION >= 300
701 void GdbDriver::setPrintQStringDataCmd(const char* cmd
)
703 // don't accept the command if it is empty
704 if (cmd
== 0 || *cmd
== '\0')
706 assert(strlen(cmd
) <= MAX_FMTLEN
);
707 cmds
[DCprintQStringStruct
].fmt
= cmd
;
710 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
712 VarTree
* variable
= 0;
715 * Parse off white space. gdb sometimes prints white space first if the
716 * printed array leaded to an error.
718 while (isspace(*output
))
721 // special case: empty string (0 repetitions)
722 if (strncmp(output
, "Invalid number 0 of repetitions", 31) == 0)
724 variable
= new VarTree(QString(), VarTree::NKplain
);
725 variable
->m_value
= "\"\"";
729 // check for error conditions
730 if (parseErrorMessage(output
, variable
, wantErrorValue
))
736 const char* p
= output
;
744 } while (isspace(*p
));
748 // this is the real data
754 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
756 * A matrix for separators between the individual "things"
757 * that are added to the string. The first index is a bool,
758 * the second index is from the enum above.
760 static const char* separator
[2][3] = {
761 { "\"", 0, ", \"" }, /* normal char is added */
762 { "'", "\", '", ", '" } /* repeated char is added */
765 while (isdigit(*p
)) {
768 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
770 goto error
; /* huh? no valid digits */
771 // skip separator and search for a repeat count
773 while (isspace(*p
) || *p
== ',')
775 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
777 const char* start
= p
;
778 p
= strchr(p
+9, '>'); /* search end and advance */
782 repeatCount
= FROM_LATIN1(start
, p
-start
);
783 while (isspace(*p
) || *p
== ',')
786 // p is now at the next char (or the end)
788 // interpret the value as a QChar
789 // TODO: make cross-architecture compatible
795 (unsigned short&)ch
= value
;
799 ch
.setRow(c
.qch
.row
);
800 ch
.setCell(c
.qch
.cell
);
804 // escape a few frequently used characters
805 char escapeCode
= '\0';
806 switch (ch
.latin1()) {
807 case '\n': escapeCode
= 'n'; break;
808 case '\r': escapeCode
= 'r'; break;
809 case '\t': escapeCode
= 't'; break;
810 case '\b': escapeCode
= 'b'; break;
811 case '\"': escapeCode
= '\"'; break;
812 case '\\': escapeCode
= '\\'; break;
813 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
817 result
+= separator
[repeats
][lastThing
];
819 if (escapeCode
!= '\0') {
825 // fixup repeat count and lastThing
828 result
+= repeatCount
;
829 lastThing
= wasRepeat
;
838 if (lastThing
== wasChar
)
842 variable
= new VarTree(QString(), VarTree::NKplain
);
843 variable
->m_value
= result
;
845 else if (strncmp(p
, "true", 4) == 0)
847 variable
= new VarTree(QString(), VarTree::NKplain
);
848 variable
->m_value
= "QString::null";
850 else if (strncmp(p
, "false", 5) == 0)
852 variable
= new VarTree(QString(), VarTree::NKplain
);
853 variable
->m_value
= "(null)";
860 if (wantErrorValue
) {
861 variable
= new VarTree(QString(), VarTree::NKplain
);
862 variable
->m_value
= "internal parse error";
867 static VarTree
* parseVar(const char*& s
)
876 VarTree::NameKind kind
;
877 if (!parseName(p
, name
, kind
)) {
885 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
888 // skip the '=' and more whitespace
893 VarTree
* variable
= new VarTree(name
, kind
);
894 variable
->setDeleteChildren(true);
896 if (!parseValue(p
, variable
)) {
904 static void skipNested(const char*& s
, char opening
, char closing
)
908 // parse a nested type
912 * Search for next matching `closing' char, skipping nested pairs of
913 * `opening' and `closing'.
915 while (*p
&& nest
> 0) {
918 } else if (*p
== closing
) {
924 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
930 * This function skips text that is delimited by nested angle bracktes, '<>'.
931 * A complication arises because the delimited text can contain the names of
932 * operator<<, operator>>, operator<, and operator>, which have to be treated
933 * specially so that they do not count towards the nesting of '<>'.
934 * This function assumes that the delimited text does not contain strings.
936 static void skipNestedAngles(const char*& s
)
941 p
++; // skip the initial '<'
942 while (*p
&& nest
> 0)
944 // Below we can check for p-s >= 9 instead of 8 because
945 // *s is '<' and cannot be part of "operator".
948 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
957 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
967 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s
));
973 * Find the end of line that is not inside braces
975 static void findEnd(const char*& s
)
978 while (*p
&& *p
!='\n') {
979 while (*p
&& *p
!='\n' && *p
!='{')
983 skipNested(p
, '{', '}'); p
--;
989 static bool isNumberish(const char ch
)
991 return (ch
>='0' && ch
<='9') || ch
=='.' || ch
=='x';
994 void skipString(const char*& p
)
999 while (*p
!= quote
) {
1001 // skip escaped character
1002 // no special treatment for octal values necessary
1005 // simply return if no more characters
1013 * Strings can consist of several parts, some of which contain repeated
1016 if (quote
== '\'') {
1017 // look ahaead for <repeats 123 times>
1018 const char* q
= p
+1;
1021 if (strncmp(q
, "<repeats ", 9) == 0) {
1023 while (*p
!= '\0' && *p
!= '>')
1026 p
++; /* skip the '>' */
1030 // is the string continued?
1032 // look ahead for another quote
1033 const char* q
= p
+1;
1036 if (*q
== '"' || *q
== '\'') {
1042 /* very long strings are followed by `...' */
1043 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
1048 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
1052 // parse a nested expression
1056 * Search for next matching `closing' char, skipping nested pairs of
1057 * `opening' and `closing' as well as strings.
1059 while (*p
&& nest
> 0) {
1060 if (*p
== opening
) {
1062 } else if (*p
== closing
) {
1064 } else if (*p
== '\'' || *p
== '\"') {
1071 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1076 inline void skipName(const char*& p
)
1078 // allow : (for enumeration values) and $ and . (for _vtbl.)
1079 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1083 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1085 kind
= VarTree::NKplain
;
1088 // examples of names:
1091 // <string<a,b<c>,7> >
1094 skipNestedAngles(p
);
1095 name
= FROM_LATIN1(s
, p
- s
);
1096 kind
= VarTree::NKtype
;
1100 // name, which might be "static"; allow dot for "_vtbl."
1103 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1107 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1108 kind
= VarTree::NKstatic
;
1110 // its a static variable, name comes now
1116 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1121 name
= FROM_LATIN1(s
, len
);
1123 // return the new position
1128 static bool parseValue(const char*& s
, VarTree
* variable
)
1130 variable
->m_value
= "";
1134 // Sometimes we find the following output:
1135 // {<text variable, no debug info>} 0x40012000 <access>
1136 // {<data variable, no debug info>}
1137 // {<variable (not text or data), no debug info>}
1138 if (strncmp(s
, "{<text variable, ", 17) == 0 ||
1139 strncmp(s
, "{<data variable, ", 17) == 0 ||
1140 strncmp(s
, "{<variable (not text or data), ", 31) == 0)
1142 const char* start
= s
;
1143 skipNested(s
, '{', '}');
1144 variable
->m_value
= FROM_LATIN1(start
, s
-start
);
1145 variable
->m_value
+= ' '; // add only a single space
1153 if (!parseNested(s
, variable
)) {
1156 // must be the closing brace
1158 TRACE("parse error: missing } of " + variable
->getText());
1162 // final white space
1167 // examples of leaf values (cannot be the empty string):
1173 // (DwContentType&) @0x8123456: {...}
1177 // 0x823abc <Array<int> virtual table>
1178 // (void (*)()) 0x8048480 <f(E *, char)>
1181 // &parseP (HTMLClueV *, char *)
1182 // Variable "x" is not available.
1183 // The value of variable 'x' is distributed...
1184 // -nan(0xfffff081defa0)
1191 skipNested(p
, '(', ')');
1195 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1198 bool reference
= false;
1200 // skip reference marker
1204 const char* start
= p
;
1208 // some values consist of more than one token
1209 bool checkMultiPart
= false;
1211 if (p
[0] == '0' && p
[1] == 'x') {
1214 while (isxdigit(*p
))
1218 * Assume this is a pointer, but only if it's not a reference, since
1219 * references can't be expanded.
1222 variable
->m_varKind
= VarTree::VKpointer
;
1225 * References are followed by a colon, in which case we'll
1226 * find the value following the reference address.
1231 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1235 checkMultiPart
= true;
1236 } else if (isdigit(*p
)) {
1237 // parse decimal number, possibly a float
1240 if (*p
== '.') { /* TODO: obey i18n? */
1241 // In long arrays an integer may be followed by '...'.
1242 // We test for this situation and don't gobble the '...'.
1243 if (p
[1] != '.' || p
[0] != '.') {
1250 if (*p
== 'e' || *p
== 'E') {
1253 if (*p
== '-' || *p
== '+')
1259 // for char variables there is the char, eg. 10 '\n'
1260 checkMultiPart
= true;
1261 } else if (*p
== '<') {
1262 // e.g. <optimized out>
1263 skipNestedAngles(p
);
1264 } else if (*p
== '"' || *p
== '\'') {
1265 // character may have multipart: '\000' <repeats 11 times>
1266 checkMultiPart
= *p
== '\'';
1269 } else if (*p
== '&') {
1273 while (isspace(*p
)) {
1277 skipNested(p
, '(', ')');
1279 } else if (strncmp(p
, "Variable \"", 10) == 0) {
1280 // Variable "x" is not available.
1281 p
+= 10; // skip to "
1283 if (strncmp(p
, "\" is not available.", 19) == 0) {
1286 } else if (strncmp(p
, "The value of variable '", 23) == 0) {
1289 const char* e
= strchr(p
, '.');
1296 // must be an enumeration value
1298 // hmm, not necessarily: nan (floating point Not a Number)
1299 // is followed by a number in ()
1301 skipNested(p
, '(', ')');
1303 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1305 // remove line breaks from the value; this is ok since
1306 // string values never contain a literal line break
1307 variable
->m_value
.replace('\n', ' ');
1309 if (checkMultiPart
) {
1313 // may be followed by a string or <...>
1316 if (*p
== '"' || *p
== '\'') {
1318 } else if (*p
== '<') {
1319 // if this value is part of an array, it might be followed
1320 // by <repeats 15 times>, which we don't skip here
1321 if (strncmp(p
, "<repeats ", 9) != 0)
1322 skipNestedAngles(p
);
1325 // there is always a blank before the string,
1326 // which we will include in the final string value
1327 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1328 // if this was a pointer, reset that flag since we
1329 // now got the value
1330 variable
->m_varKind
= VarTree::VKsimple
;
1334 if (variable
->m_value
.length() == 0) {
1335 TRACE("parse error: no value for " + variable
->getText());
1339 // final white space
1345 * If this was a reference, the value follows. It might even be a
1346 * composite variable!
1352 if (variable
->m_varKind
== VarTree::VKpointer
) {
1353 variable
->setDelayedExpanding(true);
1360 static bool parseNested(const char*& s
, VarTree
* variable
)
1362 // could be a structure or an array
1367 bool isStruct
= false;
1369 * If there is a name followed by an = or an < -- which starts a type
1370 * name -- or "static", it is a structure
1372 if (*p
== '<' || *p
== '}') {
1374 } else if (strncmp(p
, "static ", 7) == 0) {
1376 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1377 // look ahead for a comma after the name
1384 p
= s
; /* rescan the name */
1387 if (!parseVarSeq(p
, variable
)) {
1390 variable
->m_varKind
= VarTree::VKstruct
;
1392 if (!parseValueSeq(p
, variable
)) {
1395 variable
->m_varKind
= VarTree::VKarray
;
1401 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1403 // parse a comma-separated sequence of variables
1404 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1408 if (strncmp(s
, "<No data fields>}", 17) == 0)
1410 // no member variables, so break out immediately
1411 s
+= 16; /* go to the closing brace */
1416 break; /* syntax error */
1417 variable
->appendChild(var
);
1420 // skip the comma and whitespace
1428 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1430 // parse a comma-separated sequence of variables
1435 name
.sprintf("[%d]", index
);
1436 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1437 var
->setDeleteChildren(true);
1438 good
= parseValue(s
, var
);
1443 // a value may be followed by "<repeats 45 times>"
1444 if (strncmp(s
, "<repeats ", 9) == 0) {
1447 int l
= strtol(s
, &end
, 10);
1448 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1449 // should not happen
1453 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1454 // replace name and advance index
1455 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1458 // skip " times>" and space
1460 // possible final space
1466 variable
->appendChild(var
);
1467 // long arrays may be terminated by '...'
1468 if (strncmp(s
, "...", 3) == 0) {
1470 VarTree
* var
= new VarTree("...", VarTree::NKplain
);
1471 var
->setDeleteChildren(true);
1472 var
->m_value
= i18n("<additional entries of the array suppressed>");
1473 variable
->appendChild(var
);
1479 // skip the comma and whitespace
1483 // sometimes there is a closing brace after a comma
1491 * Parses a stack frame.
1493 static void parseFrameInfo(const char*& s
, QString
& func
,
1494 QString
& file
, int& lineNo
, DbgAddr
& address
)
1498 // next may be a hexadecimal address
1500 const char* start
= p
;
1504 while (isxdigit(*p
))
1506 address
= FROM_LATIN1(start
, p
-start
);
1507 if (strncmp(p
, " in ", 4) == 0)
1510 address
= DbgAddr();
1512 const char* start
= p
;
1513 // check for special signal handler frame
1514 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1515 func
= FROM_LATIN1(start
, 23);
1525 * Skip the function name. It is terminated by a left parenthesis
1526 * which does not delimit "(anonymous namespace)" and which is
1527 * outside the angle brackets <> of template parameter lists.
1532 // check for operator<< and operator<
1533 if (p
-start
>= 8 && strncmp(p
-8, "operator", 8) == 0)
1541 // skip template parameter list
1542 skipNestedAngles(p
);
1544 } else if (*p
== '(') {
1545 if (strncmp(p
, "(anonymous namespace)", 21) != 0)
1546 break; // parameter list found
1561 * Skip parameters. But notice that for complicated conversion
1562 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1563 * pointer to function) as well as operator()(...) we have to skip
1564 * additional pairs of parentheses. Furthermore, recent gdbs write the
1565 * demangled name followed by the arguments in a pair of parentheses,
1566 * where the demangled name can end in "const".
1569 skipNestedWithString(p
, '(', ')');
1573 if (strncmp(p
, "const", 5) == 0) {
1578 } while (*p
== '(');
1580 // check for file position
1581 if (strncmp(p
, "at ", 3) == 0) {
1583 const char* fileStart
= p
;
1584 // go for the end of the line
1585 while (*p
!= '\0' && *p
!= '\n')
1587 // search back for colon
1588 const char* colon
= p
;
1591 } while (*colon
!= ':');
1592 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1593 lineNo
= atoi(colon
+1)-1;
1598 // check for "from shared lib"
1599 if (strncmp(p
, "from ", 5) == 0) {
1601 // go for the end of the line
1602 while (*p
!= '\0' && *p
!= '\n')
1611 // construct the function name (including file info)
1615 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1620 * Replace \n (and whitespace around it) in func by a blank. We cannot
1621 * use QString::simplifyWhiteSpace() for this because this would also
1622 * simplify space that belongs to a string arguments that gdb sometimes
1623 * prints in the argument lists of the function.
1625 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1627 while ((nl
= func
.find('\n', nl
)) >= 0) {
1628 // search back to the beginning of the whitespace
1629 int startWhite
= nl
;
1632 } while (isspace(func
[startWhite
].latin1()));
1634 // search forward to the end of the whitespace
1637 } while (isspace(func
[nl
].latin1()));
1639 func
.replace(startWhite
, nl
-startWhite
, " ");
1640 /* continue searching for more \n's at this place: */
1647 * Parses a stack frame including its frame number
1649 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1650 QString
& file
, int& lineNo
, DbgAddr
& address
)
1653 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1654 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1656 // must start with a hash mark followed by number
1657 // or with "Breakpoint " followed by number and comma
1661 s
++; /* skip the hash mark */
1662 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1663 if (!isdigit(s
[11]))
1665 s
+= 11; /* skip "Breakpoint" */
1674 while (isspace(*s
) || *s
== ',')
1676 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1680 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1683 int lineNo
, frameNo
;
1686 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1687 StackFrame
* frm
= new StackFrame
;
1688 frm
->frameNo
= frameNo
;
1689 frm
->fileName
= file
;
1690 frm
->lineNo
= lineNo
;
1691 frm
->address
= address
;
1692 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1697 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1698 QString
& file
, int& lineNo
, DbgAddr
& address
)
1701 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1705 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1707 // skip first line, which is the headline
1708 const char* p
= strchr(output
, '\n');
1719 uint ignoreCount
= 0;
1723 while (*p
!= '\0') {
1725 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1730 Breakpoint::Type bpType
= Breakpoint::breakpoint
;
1731 if (strncmp(p
, "breakpoint", 10) == 0) {
1733 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1734 bpType
= Breakpoint::watchpoint
;
1736 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1737 bpType
= Breakpoint::watchpoint
;
1746 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1754 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1760 // the address, if present
1761 if (bpType
== Breakpoint::breakpoint
&&
1762 strncmp(p
, "0x", 2) == 0)
1764 const char* start
= p
;
1765 while (*p
!= '\0' && !isspace(*p
))
1767 address
= FROM_LATIN1(start
, p
-start
);
1768 while (isspace(*p
) && *p
!= '\n')
1773 address
= QString();
1775 // remainder is location, hit and ignore count, condition
1778 condition
= QString();
1779 end
= strchr(p
, '\n');
1782 p
+= location
.length();
1784 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1785 p
= end
+1; /* skip over \n */
1788 // may be continued in next line
1789 while (isspace(*p
)) { /* p points to beginning of line */
1790 // skip white space at beginning of line
1795 end
= strchr(p
, '\n');
1799 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1800 // extract the hit count
1802 hits
= strtol(p
, &dummy
, 10);
1803 TRACE(QString().sprintf("hit count %d", hits
));
1804 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1805 // extract condition
1807 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1808 TRACE("condition: "+condition
);
1809 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1810 // extract ignore count
1812 ignoreCount
= strtol(p
, &dummy
, 10);
1813 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1815 // indeed a continuation
1816 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1820 p
++; /* skip '\n' */
1822 Breakpoint
* bp
= new Breakpoint
;
1825 bp
->temporary
= disp
== 'd';
1826 bp
->enabled
= enable
== 'y';
1827 bp
->location
= location
;
1828 bp
->address
= address
;
1829 bp
->hitCount
= hits
;
1830 bp
->ignoreCount
= ignoreCount
;
1831 bp
->condition
= condition
;
1837 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1839 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1850 const char* p
= output
;
1851 while (*p
!= '\0') {
1852 // seach look for thread id, watching out for the focus indicator
1853 bool hasFocus
= false;
1854 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1859 // there follows only whitespace
1862 id
= strtol(p
, &end
, 10);
1864 // syntax error: no number found; bail out
1874 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1876 end
= strstr(p
, " ");
1878 // syntax error; bail out
1881 systag
= FROM_LATIN1(p
, end
-p
);
1885 * Now follows a standard stack frame. Sometimes, however, gdb
1886 * catches a thread at an instant where it doesn't have a stack.
1888 if (strncmp(p
, "[No stack.]", 11) != 0) {
1889 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1891 func
= "[No stack]";
1894 address
= QString();
1895 p
+= 11; /* \n is skipped above */
1898 ThreadInfo
* thr
= new ThreadInfo
;
1900 thr
->threadName
= systag
;
1901 thr
->hasFocus
= hasFocus
;
1902 thr
->function
= func
;
1903 thr
->fileName
= file
;
1904 thr
->lineNo
= lineNo
;
1905 thr
->address
= address
;
1906 threads
.append(thr
);
1911 static bool parseNewBreakpoint(const char* o
, int& id
,
1912 QString
& file
, int& lineNo
, QString
& address
);
1913 static bool parseNewWatchpoint(const char* o
, int& id
,
1916 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1917 QString
& file
, int& lineNo
, QString
& address
)
1919 const char* o
= output
;
1920 // skip lines of that begin with "(Cannot find"
1921 while (strncmp(o
, "(Cannot find", 12) == 0) {
1922 o
= strchr(o
, '\n');
1925 o
++; /* skip newline */
1928 if (strncmp(o
, "Breakpoint ", 11) == 0) {
1929 output
+= 11; /* skip "Breakpoint " */
1930 return ::parseNewBreakpoint(output
, id
, file
, lineNo
, address
);
1931 } else if (strncmp(o
, "Hardware watchpoint ", 20) == 0) {
1933 return ::parseNewWatchpoint(output
, id
, address
);
1934 } else if (strncmp(o
, "Watchpoint ", 11) == 0) {
1936 return ::parseNewWatchpoint(output
, id
, address
);
1941 static bool parseNewBreakpoint(const char* o
, int& id
,
1942 QString
& file
, int& lineNo
, QString
& address
)
1946 id
= strtoul(o
, &p
, 10);
1950 // check for the address
1951 if (strncmp(p
, " at 0x", 6) == 0) {
1952 char* start
= p
+4; /* skip " at ", but not 0x */
1954 while (isxdigit(*p
))
1956 address
= FROM_LATIN1(start
, p
-start
);
1960 char* fileStart
= strstr(p
, "file ");
1962 return !address
.isEmpty(); /* parse error only if there's no address */
1966 char* numStart
= strstr(fileStart
, ", line ");
1967 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1969 int line
= strtoul(numStart
, &p
, 10);
1974 lineNo
= line
-1; /* zero-based! */
1978 static bool parseNewWatchpoint(const char* o
, int& id
,
1983 id
= strtoul(o
, &p
, 10);
1987 if (strncmp(p
, ": ", 2) != 0)
1991 // all the rest on the line is the expression
1992 expr
= FROM_LATIN1(p
, strlen(p
)).stripWhiteSpace();
1996 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1998 // check for possible error conditions
1999 if (strncmp(output
, "No symbol table", 15) == 0)
2004 while (*output
!= '\0') {
2005 while (isspace(*output
))
2007 if (*output
== '\0')
2009 // skip occurrences of "No locals" and "No args"
2010 if (strncmp(output
, "No locals", 9) == 0 ||
2011 strncmp(output
, "No arguments", 12) == 0)
2013 output
= strchr(output
, '\n');
2020 VarTree
* variable
= parseVar(output
);
2021 if (variable
== 0) {
2024 // do not add duplicates
2025 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
2026 if (o
->getText() == variable
->getText()) {
2031 newVars
.append(variable
);
2036 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
2038 // check for error conditions
2039 if (parseErrorMessage(output
, var
, wantErrorValue
))
2043 // parse the variable
2044 var
= parseVar(output
);
2049 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
2051 bool isGood
= false;
2052 message
= QString(output
).simplifyWhiteSpace();
2053 if (message
.isEmpty()) {
2054 message
= i18n("New working directory: ") + m_programWD
;
2060 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
2064 m_haveCoreFile
= false;
2067 * Lines starting with the following do not indicate errors:
2068 * Using host libthread_db
2069 * (no debugging symbols found)
2071 while (strncmp(output
, "Using host libthread_db", 23) == 0 ||
2072 strncmp(output
, "(no debugging symbols found)", 28) == 0)
2074 // this line is good, go to the next one
2075 const char* end
= strchr(output
, '\n');
2077 output
+= strlen(output
);
2083 * If we've parsed all lines, there was no error.
2085 return output
[0] == '\0';
2088 bool GdbDriver::parseCoreFile(const char* output
)
2090 // if command succeeded, gdb emits a line starting with "#0 "
2091 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
2092 return m_haveCoreFile
;
2095 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
2097 // optionally: "program changed, rereading symbols",
2099 // "Program exited normally"
2100 // "Program terminated with wignal SIGSEGV"
2101 // "Program received signal SIGINT" or other signal
2104 // go through the output, line by line, checking what we have
2105 const char* start
= output
- 1;
2106 uint flags
= SFprogramActive
;
2107 message
= QString();
2109 start
++; /* skip '\n' */
2111 if (strncmp(start
, "Program ", 8) == 0 ||
2112 strncmp(start
, "ptrace: ", 8) == 0) {
2114 * When we receive a signal, the program remains active.
2116 * Special: If we "stopped" in a corefile, the string "Program
2117 * terminated with signal"... is displayed. (Normally, we see
2118 * "Program received signal"... when a signal happens.)
2120 if (strncmp(start
, "Program exited", 14) == 0 ||
2121 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
2122 strncmp(start
, "ptrace: ", 8) == 0)
2124 flags
&= ~SFprogramActive
;
2128 const char* endOfMessage
= strchr(start
, '\n');
2129 if (endOfMessage
== 0)
2130 endOfMessage
= start
+ strlen(start
);
2131 message
= FROM_LATIN1(start
, endOfMessage
-start
);
2132 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
2134 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2135 * that it stopped at a temporary breakpoint).
2137 flags
|= SFrefreshBreak
;
2138 } else if (strstr(start
, "re-reading symbols.") != 0) {
2139 flags
|= SFrefreshSource
;
2142 // next line, please
2143 start
= strchr(start
, '\n');
2144 } while (start
!= 0);
2147 * Gdb only notices when new threads have appeared, but not when a
2148 * thread finishes. So we always have to assume that the list of
2149 * threads has changed.
2151 flags
|= SFrefreshThreads
;
2156 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
2158 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
2161 // parse the table of shared libraries
2163 // strip off head line
2164 output
= strchr(output
, '\n');
2167 output
++; /* skip '\n' */
2169 while (*output
!= '\0') {
2170 // format of a line is
2171 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2172 // 3 blocks of non-space followed by space
2173 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
2174 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
2177 while (isspace(*output
)) { /* space */
2181 if (*output
== '\0')
2183 const char* start
= output
;
2184 output
= strchr(output
, '\n');
2186 output
= start
+ strlen(start
);
2187 shlibName
= FROM_LATIN1(start
, output
-start
);
2188 if (*output
!= '\0')
2190 shlibs
.append(shlibName
);
2191 TRACE("found shared lib " + shlibName
);
2195 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
2197 if (strncmp(output
, "type = ", 7) != 0)
2201 * Everything else is the type. We strip off any leading "const" and any
2202 * trailing "&" on the grounds that neither affects the decoding of the
2203 * object. We also strip off all white-space from the type.
2206 if (strncmp(output
, "const ", 6) == 0)
2209 type
.replace(QRegExp("\\s+"), "");
2210 if (type
.endsWith("&"))
2211 type
.truncate(type
.length() - 1);
2215 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2217 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2224 // parse register values
2225 while (*output
!= '\0')
2227 // skip space at the start of the line
2228 while (isspace(*output
))
2232 const char* start
= output
;
2233 while (*output
!= '\0' && !isspace(*output
))
2235 if (*output
== '\0')
2237 regName
= FROM_LATIN1(start
, output
-start
);
2240 while (isspace(*output
))
2243 RegisterInfo
* reg
= new RegisterInfo
;
2244 reg
->regName
= regName
;
2247 * If we find a brace now, this is a vector register. We look for
2248 * the closing brace and treat the result as cooked value.
2253 skipNested(output
, '{', '}');
2254 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2255 // skip space, but not the end of line
2256 while (isspace(*output
) && *output
!= '\n')
2258 // get rid of the braces at the begining and the end
2260 if (value
[value
.length()-1] == '}') {
2261 value
= value
.left(value
.length()-1);
2263 // gdb 5.3 doesn't print a separate set of raw values
2264 if (*output
== '{') {
2265 // another set of vector follows
2266 // what we have so far is the raw value
2267 reg
->rawValue
= value
;
2270 skipNested(output
, '{', '}');
2271 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2274 // find first type that does not have an array, this is the RAW value
2275 const char* end
=start
;
2277 const char* cur
=start
;
2279 while (*cur
!= '=' && cur
<end
)
2282 while (isspace(*cur
) && cur
<end
)
2284 if (isNumberish(*cur
)) {
2286 while (*end
&& (*end
!='}') && (*end
!=',') && (*end
!='\n'))
2288 QString rawValue
= FROM_LATIN1(cur
, end
-cur
).simplifyWhiteSpace();
2289 reg
->rawValue
= rawValue
;
2291 if (rawValue
.left(2)=="0x") {
2292 // ok we have a raw value, now get it's type
2294 while (isspace(*end
) || *end
=='=') end
--;
2297 while (*cur
!='{' && *cur
!=' ')
2300 reg
->type
=FROM_LATIN1(cur
, end
-cur
);
2307 // skip to the end of line
2308 while (*output
!= '\0' && *output
!= '\n')
2310 // get rid of the braces at the begining and the end
2312 if (value
[value
.length()-1] == '}') {
2313 value
= value
.left(value
.length()-1);
2316 reg
->cookedValue
= value
;
2320 // the rest of the line is the register value
2322 output
= strchr(output
,'\n');
2324 output
= start
+ strlen(start
);
2325 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2328 * We split the raw from the cooked values.
2329 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2330 * Here, the cooked value comes first, and the raw value is in
2333 int pos
= value
.find(" (raw ");
2336 reg
->cookedValue
= value
.left(pos
);
2337 reg
->rawValue
= value
.mid(pos
+6);
2338 if (reg
->rawValue
.right(1) == ")") // remove closing bracket
2339 reg
->rawValue
.truncate(reg
->rawValue
.length()-1);
2344 * In other cases we split off the first token (separated by
2345 * whitespace). It is the raw value. The remainder of the line
2346 * is the cooked value.
2348 int pos
= value
.find(' ');
2350 reg
->rawValue
= value
;
2351 reg
->cookedValue
= QString();
2353 reg
->rawValue
= value
.left(pos
);
2354 reg
->cookedValue
= value
.mid(pos
+1);
2358 if (*output
!= '\0')
2359 output
++; /* skip '\n' */
2365 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2367 // "is at address" or "starts at address"
2368 const char* start
= strstr(output
, "s at address ");
2373 const char* p
= start
;
2374 while (*p
!= '\0' && !isspace(*p
))
2376 addrFrom
= FROM_LATIN1(start
, p
-start
);
2378 start
= strstr(p
, "and ends at ");
2386 while (*p
!= '\0' && !isspace(*p
))
2388 addrTo
= FROM_LATIN1(start
, p
-start
);
2393 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2397 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2401 code
.append(new DisassembledCode(c
));
2405 // remove first line
2406 const char* p
= strchr(output
, '\n');
2408 return; /* not a regular output */
2413 const char* end
= strstr(output
, "End of assembler");
2415 end
= p
+ strlen(p
);
2419 // remove function offsets from the lines
2422 const char* start
= p
;
2424 while (p
!= end
&& !isspace(*p
))
2426 address
= FROM_LATIN1(start
, p
-start
);
2428 // function name (enclosed in '<>', followed by ':')
2429 while (p
!= end
&& *p
!= '<')
2432 skipNestedAngles(p
);
2437 while (p
!= end
&& isspace(*p
))
2440 // code until end of line
2442 while (p
!= end
&& *p
!= '\n')
2444 if (p
!= end
) /* include '\n' */
2447 DisassembledCode
* c
= new DisassembledCode
;
2448 c
->address
= address
;
2449 c
->code
= FROM_LATIN1(start
, p
-start
);
2454 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2456 if (isErrorExpr(output
)) {
2457 // error; strip space
2458 QString msg
= output
;
2459 return msg
.stripWhiteSpace();
2462 const char* p
= output
; /* save typing */
2468 const char* start
= p
;
2469 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2471 addr
= FROM_LATIN1(start
, p
-start
);
2473 // parse function offset
2477 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2479 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2483 // skip space; this may skip a new-line char!
2486 // everything to the end of the line is the memory dump
2487 const char* end
= strchr(p
, '\n');
2489 dump
= FROM_LATIN1(p
, end
-p
);
2492 dump
= FROM_LATIN1(p
, strlen(p
));
2495 MemoryDump
* md
= new MemoryDump
;
2504 QString
GdbDriver::editableValue(VarTree
* value
)
2506 const char* s
= value
->m_value
.latin1();
2508 // if the variable is a pointer value that contains a cast,
2511 skipNested(s
, '(', ')');
2518 const char* start
= s
;
2520 if (strncmp(s
, "0x", 2) == 0)
2523 while (isxdigit(*s
))
2527 * What we saw so far might have been a reference. If so, edit the
2528 * referenced value. Otherwise, edit the pointer.
2536 // if it's a pointer to a string, remove the string
2537 const char* end
= s
;
2542 return FROM_LATIN1(start
, end
-start
);
2545 return FROM_LATIN1(start
, strlen(start
));
2549 // else leave it unchanged (or stripped of the reference preamble)
2553 QString
GdbDriver::parseSetVariable(const char* output
)
2555 // if there is any output, it is an error message
2556 QString msg
= output
;
2557 return msg
.stripWhiteSpace();
2561 #include "gdbdriver.moc"