2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
10 #include <qstringlist.h>
11 #include <klocale.h> /* i18n */
13 #include <stdlib.h> /* strtol, atoi */
14 #include <string.h> /* strcpy */
22 static void skipString(const char*& p
);
23 static void skipNested(const char*& s
, char opening
, char closing
);
24 static ExprValue
* parseVar(const char*& s
);
25 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
);
26 static bool parseValue(const char*& s
, ExprValue
* variable
);
27 static bool parseNested(const char*& s
, ExprValue
* variable
);
28 static bool parseVarSeq(const char*& s
, ExprValue
* variable
);
29 static bool parseValueSeq(const char*& s
, ExprValue
* variable
);
31 #define PROMPT "(kdbg)"
33 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
36 // TODO: make this cmd info stuff non-static to allow multiple
37 // simultaneous gdbs to run!
41 const char* fmt
; /* format string */
43 argNone
, argString
, argNum
,
44 argStringNum
, argNumString
,
50 // This is how the QString data print statement generally looks like.
51 // It is set by KDebugger via setPrintQStringDataCmd().
53 static const char printQStringStructFmt
[] =
54 // if the string data is junk, fail early
55 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
56 // print an array of shorts
57 "(*(unsigned short*)$qstrunicode)@"
59 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
60 // if unicode data is 0, report a special value
63 static const char printQStringStructFmt
[] = "print (0?\"%s\":$kdbgundef)\n";
66 * The following array of commands must be sorted by the DC* values,
67 * because they are used as indices.
69 static GdbCmdInfo cmds
[] = {
70 { DCinitialize
, "", GdbCmdInfo::argNone
},
71 { DCtty
, "tty %s\n", GdbCmdInfo::argString
},
72 { DCexecutable
, "file \"%s\"\n", GdbCmdInfo::argString
},
73 { DCtargetremote
, "target remote %s\n", GdbCmdInfo::argString
},
74 { DCcorefile
, "core-file %s\n", GdbCmdInfo::argString
},
75 { DCattach
, "attach %s\n", GdbCmdInfo::argString
},
76 { DCinfolinemain
, "kdbg_infolinemain\n", GdbCmdInfo::argNone
},
77 { DCinfolocals
, "kdbg__alllocals\n", GdbCmdInfo::argNone
},
78 { DCinforegisters
, "info all-registers\n", GdbCmdInfo::argNone
},
79 { DCexamine
, "x %s %s\n", GdbCmdInfo::argString2
},
80 { DCinfoline
, "info line %s:%d\n", GdbCmdInfo::argStringNum
},
81 { DCdisassemble
, "disassemble %s %s\n", GdbCmdInfo::argString2
},
82 { DCsetargs
, "set args %s\n", GdbCmdInfo::argString
},
83 { DCsetenv
, "set env %s %s\n", GdbCmdInfo::argString2
},
84 { DCunsetenv
, "unset env %s\n", GdbCmdInfo::argString
},
85 { DCsetoption
, "setoption %s %d\n", GdbCmdInfo::argStringNum
},
86 { DCcd
, "cd %s\n", GdbCmdInfo::argString
},
87 { DCbt
, "bt\n", GdbCmdInfo::argNone
},
88 { DCrun
, "run\n", GdbCmdInfo::argNone
},
89 { DCcont
, "cont\n", GdbCmdInfo::argNone
},
90 { DCstep
, "step\n", GdbCmdInfo::argNone
},
91 { DCstepi
, "stepi\n", GdbCmdInfo::argNone
},
92 { DCnext
, "next\n", GdbCmdInfo::argNone
},
93 { DCnexti
, "nexti\n", GdbCmdInfo::argNone
},
94 { DCfinish
, "finish\n", GdbCmdInfo::argNone
},
95 { DCuntil
, "until %s:%d\n", GdbCmdInfo::argStringNum
},
96 { DCkill
, "kill\n", GdbCmdInfo::argNone
},
97 { DCbreaktext
, "break %s\n", GdbCmdInfo::argString
},
98 { DCbreakline
, "break %s:%d\n", GdbCmdInfo::argStringNum
},
99 { DCtbreakline
, "tbreak %s:%d\n", GdbCmdInfo::argStringNum
},
100 { DCbreakaddr
, "break *%s\n", GdbCmdInfo::argString
},
101 { DCtbreakaddr
, "tbreak *%s\n", GdbCmdInfo::argString
},
102 { DCwatchpoint
, "watch %s\n", GdbCmdInfo::argString
},
103 { DCdelete
, "delete %d\n", GdbCmdInfo::argNum
},
104 { DCenable
, "enable %d\n", GdbCmdInfo::argNum
},
105 { DCdisable
, "disable %d\n", GdbCmdInfo::argNum
},
106 { DCprint
, "print %s\n", GdbCmdInfo::argString
},
107 { DCprintDeref
, "print *(%s)\n", GdbCmdInfo::argString
},
108 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
109 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
110 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
111 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
112 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
113 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
114 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
115 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
116 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
117 { DCsetpc
, "set variable $pc=%s\n", GdbCmdInfo::argString
},
118 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
119 { DCprintWChar
, "print ($s=%s)?*$s@wcslen($s):0x0\n", GdbCmdInfo::argString
},
120 { DCsetvariable
, "set variable %s=%s\n", GdbCmdInfo::argString2
},
123 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
124 #define MAX_FMTLEN 200
126 GdbDriver::GdbDriver() :
128 m_gdbMajor(4), m_gdbMinor(16)
130 strcpy(m_prompt
, PROMPT
);
131 m_promptMinLen
= PROMPT_LEN
;
132 m_promptLastChar
= PROMPT_LAST_CHAR
;
135 // check command info array
137 for (int i
= 0; i
< NUM_CMDS
; i
++) {
138 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
139 assert(i
== cmds
[i
].cmd
);
140 // a format string must be associated
141 assert(cmds
[i
].fmt
!= 0);
142 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
143 // format string must match arg specification
144 switch (cmds
[i
].argsNeeded
) {
145 case GdbCmdInfo::argNone
:
146 assert(strchr(cmds
[i
].fmt
, '%') == 0);
148 case GdbCmdInfo::argString
:
149 perc
= strchr(cmds
[i
].fmt
, '%');
150 assert(perc
!= 0 && perc
[1] == 's');
151 assert(strchr(perc
+2, '%') == 0);
153 case GdbCmdInfo::argNum
:
154 perc
= strchr(cmds
[i
].fmt
, '%');
155 assert(perc
!= 0 && perc
[1] == 'd');
156 assert(strchr(perc
+2, '%') == 0);
158 case GdbCmdInfo::argStringNum
:
159 perc
= strchr(cmds
[i
].fmt
, '%');
160 assert(perc
!= 0 && perc
[1] == 's');
161 perc
= strchr(perc
+2, '%');
162 assert(perc
!= 0 && perc
[1] == 'd');
163 assert(strchr(perc
+2, '%') == 0);
165 case GdbCmdInfo::argNumString
:
166 perc
= strchr(cmds
[i
].fmt
, '%');
167 assert(perc
!= 0 && perc
[1] == 'd');
168 perc
= strchr(perc
+2, '%');
169 assert(perc
!= 0 && perc
[1] == 's');
170 assert(strchr(perc
+2, '%') == 0);
172 case GdbCmdInfo::argString2
:
173 perc
= strchr(cmds
[i
].fmt
, '%');
174 assert(perc
!= 0 && perc
[1] == 's');
175 perc
= strchr(perc
+2, '%');
176 assert(perc
!= 0 && perc
[1] == 's');
177 assert(strchr(perc
+2, '%') == 0);
179 case GdbCmdInfo::argNum2
:
180 perc
= strchr(cmds
[i
].fmt
, '%');
181 assert(perc
!= 0 && perc
[1] == 'd');
182 perc
= strchr(perc
+2, '%');
183 assert(perc
!= 0 && perc
[1] == 'd');
184 assert(strchr(perc
+2, '%') == 0);
188 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
192 GdbDriver::~GdbDriver()
197 QString
GdbDriver::driverName() const
202 QString
GdbDriver::defaultGdb()
206 " --fullname" /* to get standard file names each time the prog stops */
207 " --nx"; /* do not execute initialization files */
210 QString
GdbDriver::defaultInvocation() const
212 if (m_defaultCmd
.isEmpty()) {
219 QStringList
GdbDriver::boolOptionList() const
222 return QStringList();
225 bool GdbDriver::startup(QString cmdStr
)
227 if (!DebuggerDriver::startup(cmdStr
))
230 static const char gdbInitialize
[] =
232 * Work around buggy gdbs that do command line editing even if they
233 * are not on a tty. The readline library echos every command back
234 * in this case, which is confusing for us.
238 "set print static-members off\n"
239 "set print asm-demangle on\n"
241 * Don't assume that program functions invoked from a watch expression
244 "set unwindonsignal on\n"
246 * Write a short macro that prints all locals: local variables and
247 * function arguments.
249 "define kdbg__alllocals\n"
250 "info locals\n" /* local vars supersede args with same name */
251 "info args\n" /* therefore, arguments must come last */
254 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
256 "define kdbg_infolinemain\n"
260 // change prompt string and synchronize with gdb
261 "set prompt " PROMPT
"\n"
264 executeCmdString(DCinitialize
, gdbInitialize
, false);
266 // assume that QString::null is ok
267 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
272 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
274 // command string must be committed
275 if (!cmd
->m_committed
) {
277 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
282 switch (cmd
->m_cmd
) {
284 // get version number from preamble
287 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
288 int offset
= GDBVersion
.match(m_output
, 0, &len
);
290 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
292 m_gdbMajor
= strtol(start
, &end
, 10);
293 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
295 // nothing was parsed
300 // assume some default version (what would make sense?)
304 // use a feasible core-file command
305 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
307 cmds
[DCcorefile
].fmt
= "target FreeBSD-core %s\n";
309 cmds
[DCcorefile
].fmt
= "target core %s\n";
312 cmds
[DCcorefile
].fmt
= "core-file %s\n";
319 /* ok, the command is ready */
320 emit
commandReceived(cmd
, m_output
);
322 switch (cmd
->m_cmd
) {
341 * The --fullname option makes gdb send a special normalized sequence print
342 * each time the program stops and at some other points. The sequence has
343 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
345 void GdbDriver::parseMarker()
347 char* startMarker
= strstr(m_output
, "\032\032");
348 if (startMarker
== 0)
351 // extract the marker
353 TRACE(QString("found marker: ") + startMarker
);
354 char* endMarker
= strchr(startMarker
, '\n');
360 // extract filename and line number
361 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
364 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
365 if (lineNoStart
>= 0) {
366 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
369 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
370 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
372 // now show the window
373 startMarker
[lineNoStart
] = '\0'; /* split off file name */
374 emit
activateFileLine(startMarker
, lineNo
-1, address
);
380 * Escapes characters that might lead to problems when they appear on gdb's
383 static void normalizeStringArg(QString
& arg
)
386 * Remove trailing backslashes. This approach is a little simplistic,
387 * but we know that there is at the moment no case where a trailing
388 * backslash would make sense.
390 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
391 arg
= arg
.left(arg
.length()-1);
396 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
398 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
399 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
401 normalizeStringArg(strArg
);
404 // need the working directory when parsing the output
405 m_programWD
= strArg
;
406 } else if (cmd
== DCsetargs
&& !m_redirect
.isEmpty()) {
408 * Use saved redirection. We prepend it in front of the user's
409 * arguments so that the user can override the redirections.
411 strArg
= m_redirect
+ " " + strArg
;
415 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
419 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
421 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
422 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
425 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
429 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
431 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
432 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
433 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
437 normalizeStringArg(strArg
);
444 * intArg specifies which channels should be redirected to
445 * /dev/null. It is a value or'ed together from RDNstdin,
446 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
449 * Note: We rely on that after the DCtty a DCsetargs will follow,
450 * which will ultimately apply the redirection.
452 static const char* const runRedir
[8] = {
456 "</dev/null >/dev/null",
458 "</dev/null 2>/dev/null",
460 "</dev/null >/dev/null 2>&1"
462 if (strArg
.isEmpty())
463 intArg
= 7; /* failsafe if no tty */
464 m_redirect
= runRedir
[intArg
& 7];
466 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
469 if (cmd
== DCexamine
) {
470 // make a format specifier from the intArg
471 static const char size
[16] = {
472 '\0', 'b', 'h', 'w', 'g'
474 static const char format
[16] = {
475 '\0', 'x', 'd', 'u', 'o', 't',
476 'a', 'c', 'f', 's', 'i'
478 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
479 assert(MDTformatmask
== 0xf0); /* next 4 bits */
480 int count
= 16; /* number of entities to print */
481 char sizeSpec
= size
[intArg
& MDTsizemask
];
482 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
483 assert(sizeSpec
!= '\0');
484 assert(formatSpec
!= '\0');
485 // adjust count such that 16 lines are printed
486 switch (intArg
& MDTformatmask
) {
487 case MDTstring
: case MDTinsn
:
488 break; /* no modification needed */
490 // all cases drop through:
491 switch (intArg
& MDTsizemask
) {
503 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
505 return makeCmdString(DCexamine
, spec
, strArg
);
508 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
510 // line numbers are zero-based
511 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
512 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
516 if (cmd
== DCinfoline
)
518 // must split off file name part
519 int slash
= strArg
.findRev('/');
521 strArg
= strArg
.right(strArg
.length()-slash
-1);
523 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
527 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
532 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
534 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
535 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
537 normalizeStringArg(strArg1
);
538 normalizeStringArg(strArg2
);
541 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
545 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
547 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
548 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
551 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
555 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
557 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
558 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
561 m_haveCoreFile
= false;
564 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
567 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
570 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
573 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
577 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
580 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
583 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
586 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
589 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
592 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
595 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
598 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
600 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
603 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
606 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
609 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
612 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
615 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
618 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
621 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
624 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
627 void GdbDriver::terminate()
633 void GdbDriver::detachAndTerminate()
637 executeCmdString(DCinitialize
, "detach\nquit\n", true);
640 void GdbDriver::interruptInferior()
643 // remove accidentally queued commands
647 static bool isErrorExpr(const char* output
)
650 strncmp(output
, "Cannot access memory at", 23) == 0 ||
651 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
652 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
653 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
654 strncmp(output
, "There is no member or method named", 34) == 0 ||
655 strncmp(output
, "A parse error in expression", 27) == 0 ||
656 strncmp(output
, "No symbol \"", 11) == 0 ||
657 strncmp(output
, "Internal error: ", 16) == 0;
661 * Returns true if the output is an error message. If wantErrorValue is
662 * true, a new ExprValue object is created and filled with the error message.
663 * If there are warnings, they are skipped and output points past the warnings
664 * on return (even if there \e are errors).
666 static bool parseErrorMessage(const char*& output
,
667 ExprValue
*& variable
, bool wantErrorValue
)
670 while (strncmp(output
, "warning:", 8) == 0)
672 char* end
= strchr(output
+8, '\n');
674 output
+= strlen(output
);
679 if (isErrorExpr(output
))
681 if (wantErrorValue
) {
682 // put the error message as value in the variable
683 variable
= new ExprValue(QString(), VarTree::NKplain
);
684 const char* endMsg
= strchr(output
, '\n');
686 endMsg
= output
+ strlen(output
);
687 variable
->m_value
= QString::fromLatin1(output
, endMsg
-output
);
696 #if QT_VERSION >= 300
706 void GdbDriver::setPrintQStringDataCmd(const char* cmd
)
708 // don't accept the command if it is empty
709 if (cmd
== 0 || *cmd
== '\0')
711 assert(strlen(cmd
) <= MAX_FMTLEN
);
712 cmds
[DCprintQStringStruct
].fmt
= cmd
;
715 ExprValue
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
717 ExprValue
* variable
= 0;
720 * Parse off white space. gdb sometimes prints white space first if the
721 * printed array leaded to an error.
723 while (isspace(*output
))
726 // special case: empty string (0 repetitions)
727 if (strncmp(output
, "Invalid number 0 of repetitions", 31) == 0)
729 variable
= new ExprValue(QString(), VarTree::NKplain
);
730 variable
->m_value
= "\"\"";
734 // check for error conditions
735 if (parseErrorMessage(output
, variable
, wantErrorValue
))
741 const char* p
= output
;
749 } while (isspace(*p
));
753 // this is the real data
759 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
761 * A matrix for separators between the individual "things"
762 * that are added to the string. The first index is a bool,
763 * the second index is from the enum above.
765 static const char* separator
[2][3] = {
766 { "\"", 0, ", \"" }, /* normal char is added */
767 { "'", "\", '", ", '" } /* repeated char is added */
770 while (isdigit(*p
)) {
773 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
775 goto error
; /* huh? no valid digits */
776 // skip separator and search for a repeat count
778 while (isspace(*p
) || *p
== ',')
780 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
782 const char* start
= p
;
783 p
= strchr(p
+9, '>'); /* search end and advance */
787 repeatCount
= QString::fromLatin1(start
, p
-start
);
788 while (isspace(*p
) || *p
== ',')
791 // p is now at the next char (or the end)
793 // interpret the value as a QChar
794 // TODO: make cross-architecture compatible
800 (unsigned short&)ch
= value
;
804 ch
.setRow(c
.qch
.row
);
805 ch
.setCell(c
.qch
.cell
);
809 // escape a few frequently used characters
810 char escapeCode
= '\0';
811 switch (ch
.latin1()) {
812 case '\n': escapeCode
= 'n'; break;
813 case '\r': escapeCode
= 'r'; break;
814 case '\t': escapeCode
= 't'; break;
815 case '\b': escapeCode
= 'b'; break;
816 case '\"': escapeCode
= '\"'; break;
817 case '\\': escapeCode
= '\\'; break;
818 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
822 result
+= separator
[repeats
][lastThing
];
824 if (escapeCode
!= '\0') {
830 // fixup repeat count and lastThing
833 result
+= repeatCount
;
834 lastThing
= wasRepeat
;
843 if (lastThing
== wasChar
)
847 variable
= new ExprValue(QString(), VarTree::NKplain
);
848 variable
->m_value
= result
;
850 else if (strncmp(p
, "true", 4) == 0)
852 variable
= new ExprValue(QString(), VarTree::NKplain
);
853 variable
->m_value
= "QString::null";
855 else if (strncmp(p
, "false", 5) == 0)
857 variable
= new ExprValue(QString(), VarTree::NKplain
);
858 variable
->m_value
= "(null)";
865 if (wantErrorValue
) {
866 variable
= new ExprValue(QString(), VarTree::NKplain
);
867 variable
->m_value
= "internal parse error";
872 static ExprValue
* parseVar(const char*& s
)
881 VarTree::NameKind kind
;
883 * Detect anonymouse struct values: The 'name =' part is missing:
884 * s = { a = 1, { b = 2 }}
885 * Note that this detection works only inside structs when the anonymous
886 * struct is not the first member:
887 * s = {{ a = 1 }, b = 2}
888 * This is misparsed (by parseNested()) because it is mistakenly
889 * interprets the second opening brace as the first element of an array
894 name
= i18n("<anonymous struct or union>");
895 kind
= VarTree::NKanonymous
;
899 if (!parseName(p
, name
, kind
)) {
907 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
910 // skip the '=' and more whitespace
916 ExprValue
* variable
= new ExprValue(name
, kind
);
918 if (!parseValue(p
, variable
)) {
926 static void skipNested(const char*& s
, char opening
, char closing
)
930 // parse a nested type
934 * Search for next matching `closing' char, skipping nested pairs of
935 * `opening' and `closing'.
937 while (*p
&& nest
> 0) {
940 } else if (*p
== closing
) {
946 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
952 * This function skips text that is delimited by nested angle bracktes, '<>'.
953 * A complication arises because the delimited text can contain the names of
954 * operator<<, operator>>, operator<, and operator>, which have to be treated
955 * specially so that they do not count towards the nesting of '<>'.
956 * This function assumes that the delimited text does not contain strings.
958 static void skipNestedAngles(const char*& s
)
963 p
++; // skip the initial '<'
964 while (*p
&& nest
> 0)
966 // Below we can check for p-s >= 9 instead of 8 because
967 // *s is '<' and cannot be part of "operator".
970 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
979 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
989 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s
));
995 * Find the end of line that is not inside braces
997 static void findEnd(const char*& s
)
1000 while (*p
&& *p
!='\n') {
1001 while (*p
&& *p
!='\n' && *p
!='{')
1005 skipNested(p
, '{', '}'); p
--;
1011 static bool isNumberish(const char ch
)
1013 return (ch
>='0' && ch
<='9') || ch
=='.' || ch
=='x';
1016 void skipString(const char*& p
)
1021 while (*p
!= quote
) {
1023 // skip escaped character
1024 // no special treatment for octal values necessary
1027 // simply return if no more characters
1035 * Strings can consist of several parts, some of which contain repeated
1038 if (quote
== '\'') {
1039 // look ahaead for <repeats 123 times>
1040 const char* q
= p
+1;
1043 if (strncmp(q
, "<repeats ", 9) == 0) {
1045 while (*p
!= '\0' && *p
!= '>')
1048 p
++; /* skip the '>' */
1052 // is the string continued?
1054 // look ahead for another quote
1055 const char* q
= p
+1;
1058 if (*q
== '"' || *q
== '\'') {
1064 /* very long strings are followed by `...' */
1065 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
1070 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
1074 // parse a nested expression
1078 * Search for next matching `closing' char, skipping nested pairs of
1079 * `opening' and `closing' as well as strings.
1081 while (*p
&& nest
> 0) {
1082 if (*p
== opening
) {
1084 } else if (*p
== closing
) {
1086 } else if (*p
== '\'' || *p
== '\"') {
1093 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1098 inline void skipName(const char*& p
)
1100 // allow : (for enumeration values) and $ and . (for _vtbl.)
1101 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1105 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1107 kind
= VarTree::NKplain
;
1110 // examples of names:
1113 // <string<a,b<c>,7> >
1116 skipNestedAngles(p
);
1117 name
= QString::fromLatin1(s
, p
- s
);
1118 kind
= VarTree::NKtype
;
1122 // name, which might be "static"; allow dot for "_vtbl."
1125 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1129 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1130 kind
= VarTree::NKstatic
;
1132 // its a static variable, name comes now
1138 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1143 name
= QString::fromLatin1(s
, len
);
1145 // return the new position
1150 static bool parseValue(const char*& s
, ExprValue
* variable
)
1152 variable
->m_value
= "";
1156 // Sometimes we find the following output:
1157 // {<text variable, no debug info>} 0x40012000 <access>
1158 // {<data variable, no debug info>}
1159 // {<variable (not text or data), no debug info>}
1160 if (strncmp(s
, "{<text variable, ", 17) == 0 ||
1161 strncmp(s
, "{<data variable, ", 17) == 0 ||
1162 strncmp(s
, "{<variable (not text or data), ", 31) == 0)
1164 const char* start
= s
;
1165 skipNested(s
, '{', '}');
1166 variable
->m_value
= QString::fromLatin1(start
, s
-start
);
1167 variable
->m_value
+= ' '; // add only a single space
1175 if (!parseNested(s
, variable
)) {
1178 // must be the closing brace
1180 TRACE("parse error: missing } of " + variable
->m_name
);
1184 // final white space
1189 // examples of leaf values (cannot be the empty string):
1195 // (DwContentType&) @0x8123456: {...}
1199 // 0x823abc <Array<int> virtual table>
1200 // (void (*)()) 0x8048480 <f(E *, char)>
1203 // &parseP (HTMLClueV *, char *)
1204 // Variable "x" is not available.
1205 // The value of variable 'x' is distributed...
1206 // -nan(0xfffff081defa0)
1213 skipNested(p
, '(', ')');
1217 variable
->m_value
= QString::fromLatin1(s
, p
- s
);
1220 bool reference
= false;
1222 // skip reference marker
1226 const char* start
= p
;
1230 // some values consist of more than one token
1231 bool checkMultiPart
= false;
1233 if (p
[0] == '0' && p
[1] == 'x') {
1236 while (isxdigit(*p
))
1240 * Assume this is a pointer, but only if it's not a reference, since
1241 * references can't be expanded.
1244 variable
->m_varKind
= VarTree::VKpointer
;
1247 * References are followed by a colon, in which case we'll
1248 * find the value following the reference address.
1253 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1257 checkMultiPart
= true;
1258 } else if (isdigit(*p
)) {
1259 // parse decimal number, possibly a float
1262 if (*p
== '.') { /* TODO: obey i18n? */
1263 // In long arrays an integer may be followed by '...'.
1264 // We test for this situation and don't gobble the '...'.
1265 if (p
[1] != '.' || p
[0] != '.') {
1272 if (*p
== 'e' || *p
== 'E') {
1275 if (*p
== '-' || *p
== '+')
1281 // for char variables there is the char, eg. 10 '\n'
1282 checkMultiPart
= true;
1283 } else if (*p
== '<') {
1284 // e.g. <optimized out>
1285 skipNestedAngles(p
);
1286 } else if (*p
== '"' || *p
== '\'') {
1287 // character may have multipart: '\000' <repeats 11 times>
1288 checkMultiPart
= *p
== '\'';
1291 } else if (*p
== '&') {
1295 while (isspace(*p
)) {
1299 skipNested(p
, '(', ')');
1301 } else if (strncmp(p
, "Variable \"", 10) == 0) {
1302 // Variable "x" is not available.
1303 p
+= 10; // skip to "
1305 if (strncmp(p
, "\" is not available.", 19) == 0) {
1308 } else if (strncmp(p
, "The value of variable '", 23) == 0) {
1311 const char* e
= strchr(p
, '.');
1318 // must be an enumeration value
1320 // hmm, not necessarily: nan (floating point Not a Number)
1321 // is followed by a number in ()
1323 skipNested(p
, '(', ')');
1325 variable
->m_value
+= QString::fromLatin1(start
, p
- start
);
1327 // remove line breaks from the value; this is ok since
1328 // string values never contain a literal line break
1329 variable
->m_value
.replace('\n', ' ');
1331 if (checkMultiPart
) {
1335 // may be followed by a string or <...>
1338 if (*p
== '"' || *p
== '\'') {
1340 } else if (*p
== '<') {
1341 // if this value is part of an array, it might be followed
1342 // by <repeats 15 times>, which we don't skip here
1343 if (strncmp(p
, "<repeats ", 9) != 0)
1344 skipNestedAngles(p
);
1347 // there is always a blank before the string,
1348 // which we will include in the final string value
1349 variable
->m_value
+= QString::fromLatin1(start
-1, (p
- start
)+1);
1350 // if this was a pointer, reset that flag since we
1351 // now got the value
1352 variable
->m_varKind
= VarTree::VKsimple
;
1356 if (variable
->m_value
.length() == 0) {
1357 TRACE("parse error: no value for " + variable
->m_name
);
1361 // final white space
1367 * If this was a reference, the value follows. It might even be a
1368 * composite variable!
1378 static bool parseNested(const char*& s
, ExprValue
* variable
)
1380 // could be a structure or an array
1385 bool isStruct
= false;
1387 * If there is a name followed by an = or an < -- which starts a type
1388 * name -- or "static", it is a structure
1390 if (*p
== '<' || *p
== '}') {
1392 } else if (strncmp(p
, "static ", 7) == 0) {
1394 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1395 // look ahead for a comma after the name
1402 p
= s
; /* rescan the name */
1405 if (!parseVarSeq(p
, variable
)) {
1408 variable
->m_varKind
= VarTree::VKstruct
;
1410 if (!parseValueSeq(p
, variable
)) {
1413 variable
->m_varKind
= VarTree::VKarray
;
1419 static bool parseVarSeq(const char*& s
, ExprValue
* variable
)
1421 // parse a comma-separated sequence of variables
1422 ExprValue
* var
= variable
; /* var != 0 to indicate success if empty seq */
1426 if (strncmp(s
, "<No data fields>}", 17) == 0)
1428 // no member variables, so break out immediately
1429 s
+= 16; /* go to the closing brace */
1434 break; /* syntax error */
1435 variable
->appendChild(var
);
1438 // skip the comma and whitespace
1446 static bool parseValueSeq(const char*& s
, ExprValue
* variable
)
1448 // parse a comma-separated sequence of variables
1453 name
.sprintf("[%d]", index
);
1454 ExprValue
* var
= new ExprValue(name
, VarTree::NKplain
);
1455 good
= parseValue(s
, var
);
1460 // a value may be followed by "<repeats 45 times>"
1461 if (strncmp(s
, "<repeats ", 9) == 0) {
1464 int l
= strtol(s
, &end
, 10);
1465 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1466 // should not happen
1470 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1471 // replace name and advance index
1472 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1475 // skip " times>" and space
1477 // possible final space
1483 variable
->appendChild(var
);
1484 // long arrays may be terminated by '...'
1485 if (strncmp(s
, "...", 3) == 0) {
1487 ExprValue
* var
= new ExprValue("...", VarTree::NKplain
);
1488 var
->m_value
= i18n("<additional entries of the array suppressed>");
1489 variable
->appendChild(var
);
1495 // skip the comma and whitespace
1499 // sometimes there is a closing brace after a comma
1507 * Parses a stack frame.
1509 static void parseFrameInfo(const char*& s
, QString
& func
,
1510 QString
& file
, int& lineNo
, DbgAddr
& address
)
1514 // next may be a hexadecimal address
1516 const char* start
= p
;
1520 while (isxdigit(*p
))
1522 address
= QString::fromLatin1(start
, p
-start
);
1523 if (strncmp(p
, " in ", 4) == 0)
1526 address
= DbgAddr();
1528 const char* start
= p
;
1529 // check for special signal handler frame
1530 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1531 func
= QString::fromLatin1(start
, 23);
1541 * Skip the function name. It is terminated by a left parenthesis
1542 * which does not delimit "(anonymous namespace)" and which is
1543 * outside the angle brackets <> of template parameter lists
1544 * and is preceded by a space.
1549 // check for operator<< and operator<
1550 if (p
-start
>= 8 && strncmp(p
-8, "operator", 8) == 0)
1558 // skip template parameter list
1559 skipNestedAngles(p
);
1561 } else if (*p
== '(') {
1562 // this skips "(anonymous namespace)" as well as the formal
1563 // parameter list of the containing function if this is a member
1564 // of a nested class
1565 skipNestedWithString(p
, '(', ')');
1566 } else if (*p
== ' ') {
1569 break; // parameter list found
1583 * Skip parameters. But notice that for complicated conversion
1584 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1585 * pointer to function) as well as operator()(...) we have to skip
1586 * additional pairs of parentheses. Furthermore, recent gdbs write the
1587 * demangled name followed by the arguments in a pair of parentheses,
1588 * where the demangled name can end in "const".
1591 skipNestedWithString(p
, '(', ')');
1595 if (strncmp(p
, "const", 5) == 0) {
1600 } while (*p
== '(');
1602 // check for file position
1603 if (strncmp(p
, "at ", 3) == 0) {
1605 const char* fileStart
= p
;
1606 // go for the end of the line
1607 while (*p
!= '\0' && *p
!= '\n')
1609 // search back for colon
1610 const char* colon
= p
;
1613 } while (*colon
!= ':');
1614 file
= QString::fromLatin1(fileStart
, colon
-fileStart
);
1615 lineNo
= atoi(colon
+1)-1;
1620 // check for "from shared lib"
1621 if (strncmp(p
, "from ", 5) == 0) {
1623 // go for the end of the line
1624 while (*p
!= '\0' && *p
!= '\n')
1633 // construct the function name (including file info)
1637 func
= QString::fromLatin1(start
, p
-start
-1); /* don't include \n */
1642 * Replace \n (and whitespace around it) in func by a blank. We cannot
1643 * use QString::simplifyWhiteSpace() for this because this would also
1644 * simplify space that belongs to a string arguments that gdb sometimes
1645 * prints in the argument lists of the function.
1647 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1649 while ((nl
= func
.find('\n', nl
)) >= 0) {
1650 // search back to the beginning of the whitespace
1651 int startWhite
= nl
;
1654 } while (isspace(func
[startWhite
].latin1()));
1656 // search forward to the end of the whitespace
1659 } while (isspace(func
[nl
].latin1()));
1661 func
.replace(startWhite
, nl
-startWhite
, " ");
1662 /* continue searching for more \n's at this place: */
1669 * Parses a stack frame including its frame number
1671 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1672 QString
& file
, int& lineNo
, DbgAddr
& address
)
1675 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1676 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1678 // must start with a hash mark followed by number
1679 // or with "Breakpoint " followed by number and comma
1683 s
++; /* skip the hash mark */
1684 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1685 if (!isdigit(s
[11]))
1687 s
+= 11; /* skip "Breakpoint" */
1696 while (isspace(*s
) || *s
== ',')
1698 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1702 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1705 int lineNo
, frameNo
;
1708 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1709 StackFrame
* frm
= new StackFrame
;
1710 frm
->frameNo
= frameNo
;
1711 frm
->fileName
= file
;
1712 frm
->lineNo
= lineNo
;
1713 frm
->address
= address
;
1714 frm
->var
= new ExprValue(func
, VarTree::NKplain
);
1719 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1720 QString
& file
, int& lineNo
, DbgAddr
& address
)
1723 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1727 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1729 // skip first line, which is the headline
1730 const char* p
= strchr(output
, '\n');
1741 uint ignoreCount
= 0;
1745 while (*p
!= '\0') {
1747 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1752 Breakpoint::Type bpType
= Breakpoint::breakpoint
;
1753 if (strncmp(p
, "breakpoint", 10) == 0) {
1755 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1756 bpType
= Breakpoint::watchpoint
;
1758 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1759 bpType
= Breakpoint::watchpoint
;
1768 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1776 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1782 // the address, if present
1783 if (bpType
== Breakpoint::breakpoint
&&
1784 strncmp(p
, "0x", 2) == 0)
1786 const char* start
= p
;
1787 while (*p
!= '\0' && !isspace(*p
))
1789 address
= QString::fromLatin1(start
, p
-start
);
1790 while (isspace(*p
) && *p
!= '\n')
1795 address
= QString();
1797 // remainder is location, hit and ignore count, condition
1800 condition
= QString();
1801 end
= strchr(p
, '\n');
1804 p
+= location
.length();
1806 location
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1807 p
= end
+1; /* skip over \n */
1810 // may be continued in next line
1811 while (isspace(*p
)) { /* p points to beginning of line */
1812 // skip white space at beginning of line
1817 end
= strchr(p
, '\n');
1821 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1822 // extract the hit count
1824 hits
= strtol(p
, &dummy
, 10);
1825 TRACE(QString().sprintf("hit count %d", hits
));
1826 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1827 // extract condition
1829 condition
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1830 TRACE("condition: "+condition
);
1831 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1832 // extract ignore count
1834 ignoreCount
= strtol(p
, &dummy
, 10);
1835 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1837 // indeed a continuation
1838 location
+= " " + QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1842 p
++; /* skip '\n' */
1844 Breakpoint
* bp
= new Breakpoint
;
1847 bp
->temporary
= disp
== 'd';
1848 bp
->enabled
= enable
== 'y';
1849 bp
->location
= location
;
1850 bp
->address
= address
;
1851 bp
->hitCount
= hits
;
1852 bp
->ignoreCount
= ignoreCount
;
1853 bp
->condition
= condition
;
1859 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1861 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1872 const char* p
= output
;
1873 while (*p
!= '\0') {
1874 // seach look for thread id, watching out for the focus indicator
1875 bool hasFocus
= false;
1876 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1881 // there follows only whitespace
1884 id
= strtol(p
, &end
, 10);
1886 // syntax error: no number found; bail out
1896 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1898 end
= strstr(p
, " ");
1900 // syntax error; bail out
1903 systag
= QString::fromLatin1(p
, end
-p
);
1907 * Now follows a standard stack frame. Sometimes, however, gdb
1908 * catches a thread at an instant where it doesn't have a stack.
1910 if (strncmp(p
, "[No stack.]", 11) != 0) {
1911 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1913 func
= "[No stack]";
1916 address
= QString();
1917 p
+= 11; /* \n is skipped above */
1920 ThreadInfo
* thr
= new ThreadInfo
;
1922 thr
->threadName
= systag
;
1923 thr
->hasFocus
= hasFocus
;
1924 thr
->function
= func
;
1925 thr
->fileName
= file
;
1926 thr
->lineNo
= lineNo
;
1927 thr
->address
= address
;
1928 threads
.append(thr
);
1933 static bool parseNewBreakpoint(const char* o
, int& id
,
1934 QString
& file
, int& lineNo
, QString
& address
);
1935 static bool parseNewWatchpoint(const char* o
, int& id
,
1938 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1939 QString
& file
, int& lineNo
, QString
& address
)
1941 const char* o
= output
;
1942 // skip lines of that begin with "(Cannot find"
1943 while (strncmp(o
, "(Cannot find", 12) == 0) {
1944 o
= strchr(o
, '\n');
1947 o
++; /* skip newline */
1950 if (strncmp(o
, "Breakpoint ", 11) == 0) {
1951 output
+= 11; /* skip "Breakpoint " */
1952 return ::parseNewBreakpoint(output
, id
, file
, lineNo
, address
);
1953 } else if (strncmp(o
, "Hardware watchpoint ", 20) == 0) {
1955 return ::parseNewWatchpoint(output
, id
, address
);
1956 } else if (strncmp(o
, "Watchpoint ", 11) == 0) {
1958 return ::parseNewWatchpoint(output
, id
, address
);
1963 static bool parseNewBreakpoint(const char* o
, int& id
,
1964 QString
& file
, int& lineNo
, QString
& address
)
1968 id
= strtoul(o
, &p
, 10);
1972 // check for the address
1973 if (strncmp(p
, " at 0x", 6) == 0) {
1974 char* start
= p
+4; /* skip " at ", but not 0x */
1976 while (isxdigit(*p
))
1978 address
= QString::fromLatin1(start
, p
-start
);
1982 char* fileStart
= strstr(p
, "file ");
1984 return !address
.isEmpty(); /* parse error only if there's no address */
1988 char* numStart
= strstr(fileStart
, ", line ");
1989 QString fileName
= QString::fromLatin1(fileStart
, numStart
-fileStart
);
1991 int line
= strtoul(numStart
, &p
, 10);
1996 lineNo
= line
-1; /* zero-based! */
2000 static bool parseNewWatchpoint(const char* o
, int& id
,
2005 id
= strtoul(o
, &p
, 10);
2009 if (strncmp(p
, ": ", 2) != 0)
2013 // all the rest on the line is the expression
2014 expr
= QString::fromLatin1(p
, strlen(p
)).stripWhiteSpace();
2018 void GdbDriver::parseLocals(const char* output
, QList
<ExprValue
>& newVars
)
2020 // check for possible error conditions
2021 if (strncmp(output
, "No symbol table", 15) == 0)
2026 while (*output
!= '\0') {
2027 while (isspace(*output
))
2029 if (*output
== '\0')
2031 // skip occurrences of "No locals" and "No args"
2032 if (strncmp(output
, "No locals", 9) == 0 ||
2033 strncmp(output
, "No arguments", 12) == 0)
2035 output
= strchr(output
, '\n');
2042 ExprValue
* variable
= parseVar(output
);
2043 if (variable
== 0) {
2046 // do not add duplicates
2047 for (ExprValue
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
2048 if (o
->m_name
== variable
->m_name
) {
2053 newVars
.append(variable
);
2058 ExprValue
* GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
)
2061 // check for error conditions
2062 if (!parseErrorMessage(output
, var
, wantErrorValue
))
2064 // parse the variable
2065 var
= parseVar(output
);
2070 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
2072 bool isGood
= false;
2073 message
= QString(output
).simplifyWhiteSpace();
2074 if (message
.isEmpty()) {
2075 message
= i18n("New working directory: ") + m_programWD
;
2081 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
2085 m_haveCoreFile
= false;
2088 * Lines starting with the following do not indicate errors:
2089 * Using host libthread_db
2090 * (no debugging symbols found)
2092 while (strncmp(output
, "Using host libthread_db", 23) == 0 ||
2093 strncmp(output
, "(no debugging symbols found)", 28) == 0)
2095 // this line is good, go to the next one
2096 const char* end
= strchr(output
, '\n');
2098 output
+= strlen(output
);
2104 * If we've parsed all lines, there was no error.
2106 return output
[0] == '\0';
2109 bool GdbDriver::parseCoreFile(const char* output
)
2111 // if command succeeded, gdb emits a line starting with "#0 "
2112 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
2113 return m_haveCoreFile
;
2116 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
2118 // optionally: "program changed, rereading symbols",
2120 // "Program exited normally"
2121 // "Program terminated with wignal SIGSEGV"
2122 // "Program received signal SIGINT" or other signal
2125 // go through the output, line by line, checking what we have
2126 const char* start
= output
- 1;
2127 uint flags
= SFprogramActive
;
2128 message
= QString();
2130 start
++; /* skip '\n' */
2132 if (strncmp(start
, "Program ", 8) == 0 ||
2133 strncmp(start
, "ptrace: ", 8) == 0) {
2135 * When we receive a signal, the program remains active.
2137 * Special: If we "stopped" in a corefile, the string "Program
2138 * terminated with signal"... is displayed. (Normally, we see
2139 * "Program received signal"... when a signal happens.)
2141 if (strncmp(start
, "Program exited", 14) == 0 ||
2142 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
2143 strncmp(start
, "ptrace: ", 8) == 0)
2145 flags
&= ~SFprogramActive
;
2149 const char* endOfMessage
= strchr(start
, '\n');
2150 if (endOfMessage
== 0)
2151 endOfMessage
= start
+ strlen(start
);
2152 message
= QString::fromLatin1(start
, endOfMessage
-start
);
2153 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
2155 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2156 * that it stopped at a temporary breakpoint).
2158 flags
|= SFrefreshBreak
;
2159 } else if (strstr(start
, "re-reading symbols.") != 0) {
2160 flags
|= SFrefreshSource
;
2163 // next line, please
2164 start
= strchr(start
, '\n');
2165 } while (start
!= 0);
2168 * Gdb only notices when new threads have appeared, but not when a
2169 * thread finishes. So we always have to assume that the list of
2170 * threads has changed.
2172 flags
|= SFrefreshThreads
;
2177 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
2179 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
2182 // parse the table of shared libraries
2184 // strip off head line
2185 output
= strchr(output
, '\n');
2188 output
++; /* skip '\n' */
2190 while (*output
!= '\0') {
2191 // format of a line is
2192 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2193 // 3 blocks of non-space followed by space
2194 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
2195 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
2198 while (isspace(*output
)) { /* space */
2202 if (*output
== '\0')
2204 const char* start
= output
;
2205 output
= strchr(output
, '\n');
2207 output
= start
+ strlen(start
);
2208 shlibName
= QString::fromLatin1(start
, output
-start
);
2209 if (*output
!= '\0')
2211 shlibs
.append(shlibName
);
2212 TRACE("found shared lib " + shlibName
);
2216 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
2218 if (strncmp(output
, "type = ", 7) != 0)
2222 * Everything else is the type. We strip off any leading "const" and any
2223 * trailing "&" on the grounds that neither affects the decoding of the
2224 * object. We also strip off all white-space from the type.
2227 if (strncmp(output
, "const ", 6) == 0)
2230 type
.replace(QRegExp("\\s+"), "");
2231 if (type
.endsWith("&"))
2232 type
.truncate(type
.length() - 1);
2236 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2238 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2245 // parse register values
2246 while (*output
!= '\0')
2248 // skip space at the start of the line
2249 while (isspace(*output
))
2253 const char* start
= output
;
2254 while (*output
!= '\0' && !isspace(*output
))
2256 if (*output
== '\0')
2258 regName
= QString::fromLatin1(start
, output
-start
);
2261 while (isspace(*output
))
2264 RegisterInfo
* reg
= new RegisterInfo
;
2265 reg
->regName
= regName
;
2268 * If we find a brace now, this is a vector register. We look for
2269 * the closing brace and treat the result as cooked value.
2274 skipNested(output
, '{', '}');
2275 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2276 // skip space, but not the end of line
2277 while (isspace(*output
) && *output
!= '\n')
2279 // get rid of the braces at the begining and the end
2281 if (value
[value
.length()-1] == '}') {
2282 value
= value
.left(value
.length()-1);
2284 // gdb 5.3 doesn't print a separate set of raw values
2285 if (*output
== '{') {
2286 // another set of vector follows
2287 // what we have so far is the raw value
2288 reg
->rawValue
= value
;
2291 skipNested(output
, '{', '}');
2292 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2295 // find first type that does not have an array, this is the RAW value
2296 const char* end
=start
;
2298 const char* cur
=start
;
2300 while (*cur
!= '=' && cur
<end
)
2303 while (isspace(*cur
) && cur
<end
)
2305 if (isNumberish(*cur
)) {
2307 while (*end
&& (*end
!='}') && (*end
!=',') && (*end
!='\n'))
2309 QString rawValue
= QString::fromLatin1(cur
, end
-cur
).simplifyWhiteSpace();
2310 reg
->rawValue
= rawValue
;
2312 if (rawValue
.left(2)=="0x") {
2313 // ok we have a raw value, now get it's type
2315 while (isspace(*end
) || *end
=='=') end
--;
2318 while (*cur
!='{' && *cur
!=' ')
2321 reg
->type
=QString::fromLatin1(cur
, end
-cur
);
2328 // skip to the end of line
2329 while (*output
!= '\0' && *output
!= '\n')
2331 // get rid of the braces at the begining and the end
2333 if (value
[value
.length()-1] == '}') {
2334 value
= value
.left(value
.length()-1);
2337 reg
->cookedValue
= value
;
2341 // the rest of the line is the register value
2343 output
= strchr(output
,'\n');
2345 output
= start
+ strlen(start
);
2346 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2349 * We split the raw from the cooked values.
2350 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2351 * Here, the cooked value comes first, and the raw value is in
2354 int pos
= value
.find(" (raw ");
2357 reg
->cookedValue
= value
.left(pos
);
2358 reg
->rawValue
= value
.mid(pos
+6);
2359 if (reg
->rawValue
.right(1) == ")") // remove closing bracket
2360 reg
->rawValue
.truncate(reg
->rawValue
.length()-1);
2365 * In other cases we split off the first token (separated by
2366 * whitespace). It is the raw value. The remainder of the line
2367 * is the cooked value.
2369 int pos
= value
.find(' ');
2371 reg
->rawValue
= value
;
2372 reg
->cookedValue
= QString();
2374 reg
->rawValue
= value
.left(pos
);
2375 reg
->cookedValue
= value
.mid(pos
+1);
2379 if (*output
!= '\0')
2380 output
++; /* skip '\n' */
2386 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2388 // "is at address" or "starts at address"
2389 const char* start
= strstr(output
, "s at address ");
2394 const char* p
= start
;
2395 while (*p
!= '\0' && !isspace(*p
))
2397 addrFrom
= QString::fromLatin1(start
, p
-start
);
2399 start
= strstr(p
, "and ends at ");
2407 while (*p
!= '\0' && !isspace(*p
))
2409 addrTo
= QString::fromLatin1(start
, p
-start
);
2414 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2418 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2422 code
.append(new DisassembledCode(c
));
2426 // remove first line
2427 const char* p
= strchr(output
, '\n');
2429 return; /* not a regular output */
2434 const char* end
= strstr(output
, "End of assembler");
2436 end
= p
+ strlen(p
);
2440 // remove function offsets from the lines
2443 const char* start
= p
;
2445 while (p
!= end
&& !isspace(*p
))
2447 address
= QString::fromLatin1(start
, p
-start
);
2449 // function name (enclosed in '<>', followed by ':')
2450 while (p
!= end
&& *p
!= '<')
2453 skipNestedAngles(p
);
2458 while (p
!= end
&& isspace(*p
))
2461 // code until end of line
2463 while (p
!= end
&& *p
!= '\n')
2465 if (p
!= end
) /* include '\n' */
2468 DisassembledCode
* c
= new DisassembledCode
;
2469 c
->address
= address
;
2470 c
->code
= QString::fromLatin1(start
, p
-start
);
2475 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2477 if (isErrorExpr(output
)) {
2478 // error; strip space
2479 QString msg
= output
;
2480 return msg
.stripWhiteSpace();
2483 const char* p
= output
; /* save typing */
2489 const char* start
= p
;
2490 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2492 addr
= QString::fromLatin1(start
, p
-start
);
2494 // parse function offset
2498 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2500 addr
.fnoffs
= QString::fromLatin1(start
, p
-start
);
2504 // skip space; this may skip a new-line char!
2507 // everything to the end of the line is the memory dump
2508 const char* end
= strchr(p
, '\n');
2510 dump
= QString::fromLatin1(p
, end
-p
);
2513 dump
= QString::fromLatin1(p
, strlen(p
));
2516 MemoryDump
* md
= new MemoryDump
;
2525 QString
GdbDriver::editableValue(VarTree
* value
)
2527 const char* s
= value
->value().latin1();
2529 // if the variable is a pointer value that contains a cast,
2532 skipNested(s
, '(', ')');
2539 const char* start
= s
;
2541 if (strncmp(s
, "0x", 2) == 0)
2544 while (isxdigit(*s
))
2548 * What we saw so far might have been a reference. If so, edit the
2549 * referenced value. Otherwise, edit the pointer.
2557 // if it's a pointer to a string, remove the string
2558 const char* end
= s
;
2563 return QString::fromLatin1(start
, end
-start
);
2566 return QString::fromLatin1(start
, strlen(start
));
2570 // else leave it unchanged (or stripped of the reference preamble)
2574 QString
GdbDriver::parseSetVariable(const char* output
)
2576 // if there is any output, it is an error message
2577 QString msg
= output
;
2578 return msg
.stripWhiteSpace();
2582 #include "gdbdriver.moc"