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
) {
342 * The --fullname option makes gdb send a special normalized sequence print
343 * each time the program stops and at some other points. The sequence has
344 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
346 void GdbDriver::parseMarker(CmdQueueItem
* cmd
)
348 char* startMarker
= strstr(m_output
, "\032\032");
349 if (startMarker
== 0)
352 // extract the marker
354 TRACE(QString("found marker: ") + startMarker
);
355 char* endMarker
= strchr(startMarker
, '\n');
361 // extract filename and line number
362 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
365 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
366 if (lineNoStart
>= 0) {
367 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
369 // get address unless there is one in cmd
370 DbgAddr address
= cmd
->m_addr
;
371 if (address
.isEmpty()) {
372 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
373 address
= QString(addrStart
).stripWhiteSpace();
376 // now show the window
377 startMarker
[lineNoStart
] = '\0'; /* split off file name */
378 emit
activateFileLine(startMarker
, lineNo
-1, address
);
384 * Escapes characters that might lead to problems when they appear on gdb's
387 static void normalizeStringArg(QString
& arg
)
390 * Remove trailing backslashes. This approach is a little simplistic,
391 * but we know that there is at the moment no case where a trailing
392 * backslash would make sense.
394 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
395 arg
= arg
.left(arg
.length()-1);
400 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
402 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
403 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
405 normalizeStringArg(strArg
);
408 // need the working directory when parsing the output
409 m_programWD
= strArg
;
410 } else if (cmd
== DCsetargs
&& !m_redirect
.isEmpty()) {
412 * Use saved redirection. We prepend it in front of the user's
413 * arguments so that the user can override the redirections.
415 strArg
= m_redirect
+ " " + strArg
;
419 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
423 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
425 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
426 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
429 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
433 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
435 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
436 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
437 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
441 normalizeStringArg(strArg
);
448 * intArg specifies which channels should be redirected to
449 * /dev/null. It is a value or'ed together from RDNstdin,
450 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
453 * Note: We rely on that after the DCtty a DCsetargs will follow,
454 * which will ultimately apply the redirection.
456 static const char* const runRedir
[8] = {
460 "</dev/null >/dev/null",
462 "</dev/null 2>/dev/null",
464 "</dev/null >/dev/null 2>&1"
466 if (strArg
.isEmpty())
467 intArg
= 7; /* failsafe if no tty */
468 m_redirect
= runRedir
[intArg
& 7];
470 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
473 if (cmd
== DCexamine
) {
474 // make a format specifier from the intArg
475 static const char size
[16] = {
476 '\0', 'b', 'h', 'w', 'g'
478 static const char format
[16] = {
479 '\0', 'x', 'd', 'u', 'o', 't',
480 'a', 'c', 'f', 's', 'i'
482 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
483 assert(MDTformatmask
== 0xf0); /* next 4 bits */
484 int count
= 16; /* number of entities to print */
485 char sizeSpec
= size
[intArg
& MDTsizemask
];
486 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
487 assert(sizeSpec
!= '\0');
488 assert(formatSpec
!= '\0');
489 // adjust count such that 16 lines are printed
490 switch (intArg
& MDTformatmask
) {
491 case MDTstring
: case MDTinsn
:
492 break; /* no modification needed */
494 // all cases drop through:
495 switch (intArg
& MDTsizemask
) {
507 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
509 return makeCmdString(DCexamine
, spec
, strArg
);
512 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
514 // line numbers are zero-based
515 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
516 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
520 if (cmd
== DCinfoline
)
522 // must split off file name part
523 int slash
= strArg
.findRev('/');
525 strArg
= strArg
.right(strArg
.length()-slash
-1);
527 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
531 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
536 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
538 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
539 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
541 normalizeStringArg(strArg1
);
542 normalizeStringArg(strArg2
);
545 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
549 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
551 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
552 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
555 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
559 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
561 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
562 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
565 m_haveCoreFile
= false;
568 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
571 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
574 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
577 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
581 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
584 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
587 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
590 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
593 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
596 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
599 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
602 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
604 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
607 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
610 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
613 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
616 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
619 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
622 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
625 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
628 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
631 void GdbDriver::terminate()
637 void GdbDriver::detachAndTerminate()
641 executeCmdString(DCinitialize
, "detach\nquit\n", true);
644 void GdbDriver::interruptInferior()
647 // remove accidentally queued commands
651 static bool isErrorExpr(const char* output
)
654 strncmp(output
, "Cannot access memory at", 23) == 0 ||
655 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
656 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
657 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
658 strncmp(output
, "There is no member or method named", 34) == 0 ||
659 strncmp(output
, "A parse error in expression", 27) == 0 ||
660 strncmp(output
, "No symbol \"", 11) == 0 ||
661 strncmp(output
, "Internal error: ", 16) == 0;
665 * Returns true if the output is an error message. If wantErrorValue is
666 * true, a new ExprValue object is created and filled with the error message.
667 * If there are warnings, they are skipped and output points past the warnings
668 * on return (even if there \e are errors).
670 static bool parseErrorMessage(const char*& output
,
671 ExprValue
*& variable
, bool wantErrorValue
)
674 while (strncmp(output
, "warning:", 8) == 0)
676 char* end
= strchr(output
+8, '\n');
678 output
+= strlen(output
);
683 if (isErrorExpr(output
))
685 if (wantErrorValue
) {
686 // put the error message as value in the variable
687 variable
= new ExprValue(QString(), VarTree::NKplain
);
688 const char* endMsg
= strchr(output
, '\n');
690 endMsg
= output
+ strlen(output
);
691 variable
->m_value
= QString::fromLatin1(output
, endMsg
-output
);
700 #if QT_VERSION >= 300
710 void GdbDriver::setPrintQStringDataCmd(const char* cmd
)
712 // don't accept the command if it is empty
713 if (cmd
== 0 || *cmd
== '\0')
715 assert(strlen(cmd
) <= MAX_FMTLEN
);
716 cmds
[DCprintQStringStruct
].fmt
= cmd
;
719 ExprValue
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
721 ExprValue
* variable
= 0;
724 * Parse off white space. gdb sometimes prints white space first if the
725 * printed array leaded to an error.
727 while (isspace(*output
))
730 // special case: empty string (0 repetitions)
731 if (strncmp(output
, "Invalid number 0 of repetitions", 31) == 0)
733 variable
= new ExprValue(QString(), VarTree::NKplain
);
734 variable
->m_value
= "\"\"";
738 // check for error conditions
739 if (parseErrorMessage(output
, variable
, wantErrorValue
))
745 const char* p
= output
;
753 } while (isspace(*p
));
757 // this is the real data
763 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
765 * A matrix for separators between the individual "things"
766 * that are added to the string. The first index is a bool,
767 * the second index is from the enum above.
769 static const char* separator
[2][3] = {
770 { "\"", 0, ", \"" }, /* normal char is added */
771 { "'", "\", '", ", '" } /* repeated char is added */
774 while (isdigit(*p
)) {
777 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
779 goto error
; /* huh? no valid digits */
780 // skip separator and search for a repeat count
782 while (isspace(*p
) || *p
== ',')
784 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
786 const char* start
= p
;
787 p
= strchr(p
+9, '>'); /* search end and advance */
791 repeatCount
= QString::fromLatin1(start
, p
-start
);
792 while (isspace(*p
) || *p
== ',')
795 // p is now at the next char (or the end)
797 // interpret the value as a QChar
798 // TODO: make cross-architecture compatible
804 (unsigned short&)ch
= value
;
808 ch
.setRow(c
.qch
.row
);
809 ch
.setCell(c
.qch
.cell
);
813 // escape a few frequently used characters
814 char escapeCode
= '\0';
815 switch (ch
.latin1()) {
816 case '\n': escapeCode
= 'n'; break;
817 case '\r': escapeCode
= 'r'; break;
818 case '\t': escapeCode
= 't'; break;
819 case '\b': escapeCode
= 'b'; break;
820 case '\"': escapeCode
= '\"'; break;
821 case '\\': escapeCode
= '\\'; break;
822 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
826 result
+= separator
[repeats
][lastThing
];
828 if (escapeCode
!= '\0') {
834 // fixup repeat count and lastThing
837 result
+= repeatCount
;
838 lastThing
= wasRepeat
;
847 if (lastThing
== wasChar
)
851 variable
= new ExprValue(QString(), VarTree::NKplain
);
852 variable
->m_value
= result
;
854 else if (strncmp(p
, "true", 4) == 0)
856 variable
= new ExprValue(QString(), VarTree::NKplain
);
857 variable
->m_value
= "QString::null";
859 else if (strncmp(p
, "false", 5) == 0)
861 variable
= new ExprValue(QString(), VarTree::NKplain
);
862 variable
->m_value
= "(null)";
869 if (wantErrorValue
) {
870 variable
= new ExprValue(QString(), VarTree::NKplain
);
871 variable
->m_value
= "internal parse error";
876 static ExprValue
* parseVar(const char*& s
)
885 VarTree::NameKind kind
;
887 * Detect anonymouse struct values: The 'name =' part is missing:
888 * s = { a = 1, { b = 2 }}
889 * Note that this detection works only inside structs when the anonymous
890 * struct is not the first member:
891 * s = {{ a = 1 }, b = 2}
892 * This is misparsed (by parseNested()) because it is mistakenly
893 * interprets the second opening brace as the first element of an array
898 name
= i18n("<anonymous struct or union>");
899 kind
= VarTree::NKanonymous
;
903 if (!parseName(p
, name
, kind
)) {
911 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
914 // skip the '=' and more whitespace
920 ExprValue
* variable
= new ExprValue(name
, kind
);
922 if (!parseValue(p
, variable
)) {
930 static void skipNested(const char*& s
, char opening
, char closing
)
934 // parse a nested type
938 * Search for next matching `closing' char, skipping nested pairs of
939 * `opening' and `closing'.
941 while (*p
&& nest
> 0) {
944 } else if (*p
== closing
) {
950 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
956 * This function skips text that is delimited by nested angle bracktes, '<>'.
957 * A complication arises because the delimited text can contain the names of
958 * operator<<, operator>>, operator<, and operator>, which have to be treated
959 * specially so that they do not count towards the nesting of '<>'.
960 * This function assumes that the delimited text does not contain strings.
962 static void skipNestedAngles(const char*& s
)
967 p
++; // skip the initial '<'
968 while (*p
&& nest
> 0)
970 // Below we can check for p-s >= 9 instead of 8 because
971 // *s is '<' and cannot be part of "operator".
974 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
983 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
993 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s
));
999 * Find the end of line that is not inside braces
1001 static void findEnd(const char*& s
)
1004 while (*p
&& *p
!='\n') {
1005 while (*p
&& *p
!='\n' && *p
!='{')
1009 skipNested(p
, '{', '}'); p
--;
1015 static bool isNumberish(const char ch
)
1017 return (ch
>='0' && ch
<='9') || ch
=='.' || ch
=='x';
1020 void skipString(const char*& p
)
1025 while (*p
!= quote
) {
1027 // skip escaped character
1028 // no special treatment for octal values necessary
1031 // simply return if no more characters
1039 * Strings can consist of several parts, some of which contain repeated
1042 if (quote
== '\'') {
1043 // look ahaead for <repeats 123 times>
1044 const char* q
= p
+1;
1047 if (strncmp(q
, "<repeats ", 9) == 0) {
1049 while (*p
!= '\0' && *p
!= '>')
1052 p
++; /* skip the '>' */
1056 // is the string continued?
1058 // look ahead for another quote
1059 const char* q
= p
+1;
1062 if (*q
== '"' || *q
== '\'') {
1068 /* very long strings are followed by `...' */
1069 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
1074 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
1078 // parse a nested expression
1082 * Search for next matching `closing' char, skipping nested pairs of
1083 * `opening' and `closing' as well as strings.
1085 while (*p
&& nest
> 0) {
1086 if (*p
== opening
) {
1088 } else if (*p
== closing
) {
1090 } else if (*p
== '\'' || *p
== '\"') {
1097 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1102 inline void skipName(const char*& p
)
1104 // allow : (for enumeration values) and $ and . (for _vtbl.)
1105 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1109 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1111 kind
= VarTree::NKplain
;
1114 // examples of names:
1117 // <string<a,b<c>,7> >
1120 skipNestedAngles(p
);
1121 name
= QString::fromLatin1(s
, p
- s
);
1122 kind
= VarTree::NKtype
;
1126 // name, which might be "static"; allow dot for "_vtbl."
1129 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1133 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1134 kind
= VarTree::NKstatic
;
1136 // its a static variable, name comes now
1142 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1147 name
= QString::fromLatin1(s
, len
);
1149 // return the new position
1154 static bool parseValue(const char*& s
, ExprValue
* variable
)
1156 variable
->m_value
= "";
1160 // Sometimes we find the following output:
1161 // {<text variable, no debug info>} 0x40012000 <access>
1162 // {<data variable, no debug info>}
1163 // {<variable (not text or data), no debug info>}
1164 if (strncmp(s
, "{<text variable, ", 17) == 0 ||
1165 strncmp(s
, "{<data variable, ", 17) == 0 ||
1166 strncmp(s
, "{<variable (not text or data), ", 31) == 0)
1168 const char* start
= s
;
1169 skipNested(s
, '{', '}');
1170 variable
->m_value
= QString::fromLatin1(start
, s
-start
);
1171 variable
->m_value
+= ' '; // add only a single space
1179 if (!parseNested(s
, variable
)) {
1182 // must be the closing brace
1184 TRACE("parse error: missing } of " + variable
->m_name
);
1188 // final white space
1193 // examples of leaf values (cannot be the empty string):
1199 // (DwContentType&) @0x8123456: {...}
1203 // 0x823abc <Array<int> virtual table>
1204 // (void (*)()) 0x8048480 <f(E *, char)>
1207 // &parseP (HTMLClueV *, char *)
1208 // Variable "x" is not available.
1209 // The value of variable 'x' is distributed...
1210 // -nan(0xfffff081defa0)
1217 skipNested(p
, '(', ')');
1221 variable
->m_value
= QString::fromLatin1(s
, p
- s
);
1224 bool reference
= false;
1226 // skip reference marker
1230 const char* start
= p
;
1234 // some values consist of more than one token
1235 bool checkMultiPart
= false;
1237 if (p
[0] == '0' && p
[1] == 'x') {
1240 while (isxdigit(*p
))
1244 * Assume this is a pointer, but only if it's not a reference, since
1245 * references can't be expanded.
1248 variable
->m_varKind
= VarTree::VKpointer
;
1251 * References are followed by a colon, in which case we'll
1252 * find the value following the reference address.
1257 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1261 checkMultiPart
= true;
1262 } else if (isdigit(*p
)) {
1263 // parse decimal number, possibly a float
1266 if (*p
== '.') { /* TODO: obey i18n? */
1267 // In long arrays an integer may be followed by '...'.
1268 // We test for this situation and don't gobble the '...'.
1269 if (p
[1] != '.' || p
[0] != '.') {
1276 if (*p
== 'e' || *p
== 'E') {
1279 if (*p
== '-' || *p
== '+')
1285 // for char variables there is the char, eg. 10 '\n'
1286 checkMultiPart
= true;
1287 } else if (*p
== '<') {
1288 // e.g. <optimized out>
1289 skipNestedAngles(p
);
1290 } else if (*p
== '"' || *p
== '\'') {
1291 // character may have multipart: '\000' <repeats 11 times>
1292 checkMultiPart
= *p
== '\'';
1295 } else if (*p
== '&') {
1299 while (isspace(*p
)) {
1303 skipNested(p
, '(', ')');
1305 } else if (strncmp(p
, "Variable \"", 10) == 0) {
1306 // Variable "x" is not available.
1307 p
+= 10; // skip to "
1309 if (strncmp(p
, "\" is not available.", 19) == 0) {
1312 } else if (strncmp(p
, "The value of variable '", 23) == 0) {
1315 const char* e
= strchr(p
, '.');
1322 // must be an enumeration value
1324 // hmm, not necessarily: nan (floating point Not a Number)
1325 // is followed by a number in ()
1327 skipNested(p
, '(', ')');
1329 variable
->m_value
+= QString::fromLatin1(start
, p
- start
);
1331 // remove line breaks from the value; this is ok since
1332 // string values never contain a literal line break
1333 variable
->m_value
.replace('\n', ' ');
1335 if (checkMultiPart
) {
1339 // may be followed by a string or <...>
1342 if (*p
== '"' || *p
== '\'') {
1344 } else if (*p
== '<') {
1345 // if this value is part of an array, it might be followed
1346 // by <repeats 15 times>, which we don't skip here
1347 if (strncmp(p
, "<repeats ", 9) != 0)
1348 skipNestedAngles(p
);
1351 // there is always a blank before the string,
1352 // which we will include in the final string value
1353 variable
->m_value
+= QString::fromLatin1(start
-1, (p
- start
)+1);
1354 // if this was a pointer, reset that flag since we
1355 // now got the value
1356 variable
->m_varKind
= VarTree::VKsimple
;
1360 if (variable
->m_value
.length() == 0) {
1361 TRACE("parse error: no value for " + variable
->m_name
);
1365 // final white space
1371 * If this was a reference, the value follows. It might even be a
1372 * composite variable!
1382 static bool parseNested(const char*& s
, ExprValue
* variable
)
1384 // could be a structure or an array
1389 bool isStruct
= false;
1391 * If there is a name followed by an = or an < -- which starts a type
1392 * name -- or "static", it is a structure
1394 if (*p
== '<' || *p
== '}') {
1396 } else if (strncmp(p
, "static ", 7) == 0) {
1398 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1399 // look ahead for a comma after the name
1406 p
= s
; /* rescan the name */
1409 if (!parseVarSeq(p
, variable
)) {
1412 variable
->m_varKind
= VarTree::VKstruct
;
1414 if (!parseValueSeq(p
, variable
)) {
1417 variable
->m_varKind
= VarTree::VKarray
;
1423 static bool parseVarSeq(const char*& s
, ExprValue
* variable
)
1425 // parse a comma-separated sequence of variables
1426 ExprValue
* var
= variable
; /* var != 0 to indicate success if empty seq */
1430 if (strncmp(s
, "<No data fields>}", 17) == 0)
1432 // no member variables, so break out immediately
1433 s
+= 16; /* go to the closing brace */
1438 break; /* syntax error */
1439 variable
->appendChild(var
);
1442 // skip the comma and whitespace
1450 static bool parseValueSeq(const char*& s
, ExprValue
* variable
)
1452 // parse a comma-separated sequence of variables
1457 name
.sprintf("[%d]", index
);
1458 ExprValue
* var
= new ExprValue(name
, VarTree::NKplain
);
1459 good
= parseValue(s
, var
);
1464 // a value may be followed by "<repeats 45 times>"
1465 if (strncmp(s
, "<repeats ", 9) == 0) {
1468 int l
= strtol(s
, &end
, 10);
1469 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1470 // should not happen
1474 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1475 // replace name and advance index
1476 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1479 // skip " times>" and space
1481 // possible final space
1487 variable
->appendChild(var
);
1488 // long arrays may be terminated by '...'
1489 if (strncmp(s
, "...", 3) == 0) {
1491 ExprValue
* var
= new ExprValue("...", VarTree::NKplain
);
1492 var
->m_value
= i18n("<additional entries of the array suppressed>");
1493 variable
->appendChild(var
);
1499 // skip the comma and whitespace
1503 // sometimes there is a closing brace after a comma
1511 * Parses a stack frame.
1513 static void parseFrameInfo(const char*& s
, QString
& func
,
1514 QString
& file
, int& lineNo
, DbgAddr
& address
)
1518 // next may be a hexadecimal address
1520 const char* start
= p
;
1524 while (isxdigit(*p
))
1526 address
= QString::fromLatin1(start
, p
-start
);
1527 if (strncmp(p
, " in ", 4) == 0)
1530 address
= DbgAddr();
1532 const char* start
= p
;
1533 // check for special signal handler frame
1534 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1535 func
= QString::fromLatin1(start
, 23);
1545 * Skip the function name. It is terminated by a left parenthesis
1546 * which does not delimit "(anonymous namespace)" and which is
1547 * outside the angle brackets <> of template parameter lists
1548 * and is preceded by a space.
1553 // check for operator<< and operator<
1554 if (p
-start
>= 8 && strncmp(p
-8, "operator", 8) == 0)
1562 // skip template parameter list
1563 skipNestedAngles(p
);
1565 } else if (*p
== '(') {
1566 // this skips "(anonymous namespace)" as well as the formal
1567 // parameter list of the containing function if this is a member
1568 // of a nested class
1569 skipNestedWithString(p
, '(', ')');
1570 } else if (*p
== ' ') {
1573 break; // parameter list found
1587 * Skip parameters. But notice that for complicated conversion
1588 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1589 * pointer to function) as well as operator()(...) we have to skip
1590 * additional pairs of parentheses. Furthermore, recent gdbs write the
1591 * demangled name followed by the arguments in a pair of parentheses,
1592 * where the demangled name can end in "const".
1595 skipNestedWithString(p
, '(', ')');
1599 if (strncmp(p
, "const", 5) == 0) {
1604 } while (*p
== '(');
1606 // check for file position
1607 if (strncmp(p
, "at ", 3) == 0) {
1609 const char* fileStart
= p
;
1610 // go for the end of the line
1611 while (*p
!= '\0' && *p
!= '\n')
1613 // search back for colon
1614 const char* colon
= p
;
1617 } while (*colon
!= ':');
1618 file
= QString::fromLatin1(fileStart
, colon
-fileStart
);
1619 lineNo
= atoi(colon
+1)-1;
1624 // check for "from shared lib"
1625 if (strncmp(p
, "from ", 5) == 0) {
1627 // go for the end of the line
1628 while (*p
!= '\0' && *p
!= '\n')
1637 // construct the function name (including file info)
1641 func
= QString::fromLatin1(start
, p
-start
-1); /* don't include \n */
1646 * Replace \n (and whitespace around it) in func by a blank. We cannot
1647 * use QString::simplifyWhiteSpace() for this because this would also
1648 * simplify space that belongs to a string arguments that gdb sometimes
1649 * prints in the argument lists of the function.
1651 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1653 while ((nl
= func
.find('\n', nl
)) >= 0) {
1654 // search back to the beginning of the whitespace
1655 int startWhite
= nl
;
1658 } while (isspace(func
[startWhite
].latin1()));
1660 // search forward to the end of the whitespace
1663 } while (isspace(func
[nl
].latin1()));
1665 func
.replace(startWhite
, nl
-startWhite
, " ");
1666 /* continue searching for more \n's at this place: */
1673 * Parses a stack frame including its frame number
1675 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1676 QString
& file
, int& lineNo
, DbgAddr
& address
)
1679 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1680 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1682 // must start with a hash mark followed by number
1683 // or with "Breakpoint " followed by number and comma
1687 s
++; /* skip the hash mark */
1688 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1689 if (!isdigit(s
[11]))
1691 s
+= 11; /* skip "Breakpoint" */
1700 while (isspace(*s
) || *s
== ',')
1702 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1706 void GdbDriver::parseBackTrace(const char* output
, std::list
<StackFrame
>& stack
)
1709 int lineNo
, frameNo
;
1712 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1713 stack
.push_back(StackFrame());
1714 StackFrame
* frm
= &stack
.back();
1715 frm
->frameNo
= frameNo
;
1716 frm
->fileName
= file
;
1717 frm
->lineNo
= lineNo
;
1718 frm
->address
= address
;
1719 frm
->var
= new ExprValue(func
, VarTree::NKplain
);
1723 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1724 QString
& file
, int& lineNo
, DbgAddr
& address
)
1727 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1731 bool GdbDriver::parseBreakList(const char* output
, std::list
<Breakpoint
>& brks
)
1733 // skip first line, which is the headline
1734 const char* p
= strchr(output
, '\n');
1744 while (*p
!= '\0') {
1747 bp
.id
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1752 if (strncmp(p
, "breakpoint", 10) == 0) {
1754 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1755 bp
.type
= Breakpoint::watchpoint
;
1757 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1758 bp
.type
= Breakpoint::watchpoint
;
1766 bp
.temporary
= *p
++ == 'd';
1767 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1774 bp
.enabled
= *p
++ == 'y';
1775 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1781 // the address, if present
1782 if (bp
.type
== Breakpoint::breakpoint
&&
1783 strncmp(p
, "0x", 2) == 0)
1785 const char* start
= p
;
1786 while (*p
!= '\0' && !isspace(*p
))
1788 bp
.address
= QString::fromLatin1(start
, p
-start
);
1789 while (isspace(*p
) && *p
!= '\n')
1794 // remainder is location, hit and ignore count, condition
1795 end
= strchr(p
, '\n');
1798 p
+= bp
.location
.length();
1800 bp
.location
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1801 p
= end
+1; /* skip over \n */
1804 // may be continued in next line
1805 while (isspace(*p
)) { /* p points to beginning of line */
1806 // skip white space at beginning of line
1811 end
= strchr(p
, '\n');
1815 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1816 // extract the hit count
1818 bp
.hitCount
= strtol(p
, &dummy
, 10);
1819 TRACE(QString("hit count %1").arg(bp
.hitCount
));
1820 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1821 // extract condition
1823 bp
.condition
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1824 TRACE("condition: "+bp
.condition
);
1825 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1826 // extract ignore count
1828 bp
.ignoreCount
= strtol(p
, &dummy
, 10);
1829 TRACE(QString("ignore count %1").arg(bp
.ignoreCount
));
1831 // indeed a continuation
1832 bp
.location
+= " " + QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1836 p
++; /* skip '\n' */
1843 std::list
<ThreadInfo
> GdbDriver::parseThreadList(const char* output
)
1845 std::list
<ThreadInfo
> threads
;
1846 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1851 const char* p
= output
;
1852 while (*p
!= '\0') {
1854 // seach look for thread id, watching out for the focus indicator
1855 thr
.hasFocus
= false;
1856 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1859 thr
.hasFocus
= true;
1861 // there follows only whitespace
1864 thr
.id
= strtol(p
, &end
, 10);
1866 // syntax error: no number found; bail out
1876 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1878 end
= strstr(p
, " ");
1880 // syntax error; bail out
1883 thr
.threadName
= QString::fromLatin1(p
, end
-p
);
1887 * Now follows a standard stack frame. Sometimes, however, gdb
1888 * catches a thread at an instant where it doesn't have a stack.
1890 if (strncmp(p
, "[No stack.]", 11) != 0) {
1891 ::parseFrameInfo(p
, thr
.function
, thr
.fileName
, thr
.lineNo
, thr
.address
);
1893 thr
.function
= "[No stack]";
1895 p
+= 11; /* \n is skipped above */
1898 threads
.push_back(thr
);
1903 static bool parseNewBreakpoint(const char* o
, int& id
,
1904 QString
& file
, int& lineNo
, QString
& address
);
1905 static bool parseNewWatchpoint(const char* o
, int& id
,
1908 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1909 QString
& file
, int& lineNo
, QString
& address
)
1911 const char* o
= output
;
1912 // skip lines of that begin with "(Cannot find"
1913 while (strncmp(o
, "(Cannot find", 12) == 0) {
1914 o
= strchr(o
, '\n');
1917 o
++; /* skip newline */
1920 if (strncmp(o
, "Breakpoint ", 11) == 0) {
1921 output
+= 11; /* skip "Breakpoint " */
1922 return ::parseNewBreakpoint(output
, id
, file
, lineNo
, address
);
1923 } else if (strncmp(o
, "Hardware watchpoint ", 20) == 0) {
1925 return ::parseNewWatchpoint(output
, id
, address
);
1926 } else if (strncmp(o
, "Watchpoint ", 11) == 0) {
1928 return ::parseNewWatchpoint(output
, id
, address
);
1933 static bool parseNewBreakpoint(const char* o
, int& id
,
1934 QString
& file
, int& lineNo
, QString
& address
)
1938 id
= strtoul(o
, &p
, 10);
1942 // check for the address
1943 if (strncmp(p
, " at 0x", 6) == 0) {
1944 char* start
= p
+4; /* skip " at ", but not 0x */
1946 while (isxdigit(*p
))
1948 address
= QString::fromLatin1(start
, p
-start
);
1952 char* fileStart
= strstr(p
, "file ");
1954 return !address
.isEmpty(); /* parse error only if there's no address */
1958 char* numStart
= strstr(fileStart
, ", line ");
1959 QString fileName
= QString::fromLatin1(fileStart
, numStart
-fileStart
);
1961 int line
= strtoul(numStart
, &p
, 10);
1966 lineNo
= line
-1; /* zero-based! */
1970 static bool parseNewWatchpoint(const char* o
, int& id
,
1975 id
= strtoul(o
, &p
, 10);
1979 if (strncmp(p
, ": ", 2) != 0)
1983 // all the rest on the line is the expression
1984 expr
= QString::fromLatin1(p
, strlen(p
)).stripWhiteSpace();
1988 void GdbDriver::parseLocals(const char* output
, std::list
<ExprValue
*>& newVars
)
1990 // check for possible error conditions
1991 if (strncmp(output
, "No symbol table", 15) == 0)
1996 while (*output
!= '\0') {
1997 while (isspace(*output
))
1999 if (*output
== '\0')
2001 // skip occurrences of "No locals" and "No args"
2002 if (strncmp(output
, "No locals", 9) == 0 ||
2003 strncmp(output
, "No arguments", 12) == 0)
2005 output
= strchr(output
, '\n');
2012 ExprValue
* variable
= parseVar(output
);
2013 if (variable
== 0) {
2016 // do not add duplicates
2017 for (std::list
<ExprValue
*>::iterator o
= newVars
.begin(); o
!= newVars
.end(); ++o
) {
2018 if ((*o
)->m_name
== variable
->m_name
) {
2023 newVars
.push_back(variable
);
2028 ExprValue
* GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
)
2031 // check for error conditions
2032 if (!parseErrorMessage(output
, var
, wantErrorValue
))
2034 // parse the variable
2035 var
= parseVar(output
);
2040 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
2042 bool isGood
= false;
2043 message
= QString(output
).simplifyWhiteSpace();
2044 if (message
.isEmpty()) {
2045 message
= i18n("New working directory: ") + m_programWD
;
2051 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
2055 m_haveCoreFile
= false;
2058 * Lines starting with the following do not indicate errors:
2059 * Using host libthread_db
2060 * (no debugging symbols found)
2062 while (strncmp(output
, "Using host libthread_db", 23) == 0 ||
2063 strncmp(output
, "(no debugging symbols found)", 28) == 0)
2065 // this line is good, go to the next one
2066 const char* end
= strchr(output
, '\n');
2068 output
+= strlen(output
);
2074 * If we've parsed all lines, there was no error.
2076 return output
[0] == '\0';
2079 bool GdbDriver::parseCoreFile(const char* output
)
2081 // if command succeeded, gdb emits a line starting with "#0 "
2082 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
2083 return m_haveCoreFile
;
2086 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
2088 // optionally: "program changed, rereading symbols",
2090 // "Program exited normally"
2091 // "Program terminated with wignal SIGSEGV"
2092 // "Program received signal SIGINT" or other signal
2095 // go through the output, line by line, checking what we have
2096 const char* start
= output
- 1;
2097 uint flags
= SFprogramActive
;
2098 message
= QString();
2100 start
++; /* skip '\n' */
2102 if (strncmp(start
, "Program ", 8) == 0 ||
2103 strncmp(start
, "ptrace: ", 8) == 0) {
2105 * When we receive a signal, the program remains active.
2107 * Special: If we "stopped" in a corefile, the string "Program
2108 * terminated with signal"... is displayed. (Normally, we see
2109 * "Program received signal"... when a signal happens.)
2111 if (strncmp(start
, "Program exited", 14) == 0 ||
2112 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
2113 strncmp(start
, "ptrace: ", 8) == 0)
2115 flags
&= ~SFprogramActive
;
2119 const char* endOfMessage
= strchr(start
, '\n');
2120 if (endOfMessage
== 0)
2121 endOfMessage
= start
+ strlen(start
);
2122 message
= QString::fromLatin1(start
, endOfMessage
-start
);
2123 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
2125 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2126 * that it stopped at a temporary breakpoint).
2128 flags
|= SFrefreshBreak
;
2129 } else if (strstr(start
, "re-reading symbols.") != 0) {
2130 flags
|= SFrefreshSource
;
2133 // next line, please
2134 start
= strchr(start
, '\n');
2135 } while (start
!= 0);
2138 * Gdb only notices when new threads have appeared, but not when a
2139 * thread finishes. So we always have to assume that the list of
2140 * threads has changed.
2142 flags
|= SFrefreshThreads
;
2147 QStringList
GdbDriver::parseSharedLibs(const char* output
)
2150 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
2153 // parse the table of shared libraries
2155 // strip off head line
2156 output
= strchr(output
, '\n');
2159 output
++; /* skip '\n' */
2161 while (*output
!= '\0') {
2162 // format of a line is
2163 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2164 // 3 blocks of non-space followed by space
2165 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
2166 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
2169 while (isspace(*output
)) { /* space */
2173 if (*output
== '\0')
2175 const char* start
= output
;
2176 output
= strchr(output
, '\n');
2178 output
= start
+ strlen(start
);
2179 shlibName
= QString::fromLatin1(start
, output
-start
);
2180 if (*output
!= '\0')
2182 shlibs
.append(shlibName
);
2183 TRACE("found shared lib " + shlibName
);
2188 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
2190 if (strncmp(output
, "type = ", 7) != 0)
2194 * Everything else is the type. We strip off any leading "const" and any
2195 * trailing "&" on the grounds that neither affects the decoding of the
2196 * object. We also strip off all white-space from the type.
2199 if (strncmp(output
, "const ", 6) == 0)
2202 type
.replace(QRegExp("\\s+"), "");
2203 if (type
.endsWith("&"))
2204 type
.truncate(type
.length() - 1);
2208 std::list
<RegisterInfo
> GdbDriver::parseRegisters(const char* output
)
2210 std::list
<RegisterInfo
> regs
;
2211 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2217 // parse register values
2218 while (*output
!= '\0')
2221 // skip space at the start of the line
2222 while (isspace(*output
))
2226 const char* start
= output
;
2227 while (*output
!= '\0' && !isspace(*output
))
2229 if (*output
== '\0')
2231 reg
.regName
= QString::fromLatin1(start
, output
-start
);
2234 while (isspace(*output
))
2238 * If we find a brace now, this is a vector register. We look for
2239 * the closing brace and treat the result as cooked value.
2244 skipNested(output
, '{', '}');
2245 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2246 // skip space, but not the end of line
2247 while (isspace(*output
) && *output
!= '\n')
2249 // get rid of the braces at the begining and the end
2251 if (value
[value
.length()-1] == '}') {
2252 value
= value
.left(value
.length()-1);
2254 // gdb 5.3 doesn't print a separate set of raw values
2255 if (*output
== '{') {
2256 // another set of vector follows
2257 // what we have so far is the raw value
2258 reg
.rawValue
= value
;
2261 skipNested(output
, '{', '}');
2262 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2265 // find first type that does not have an array, this is the RAW value
2266 const char* end
=start
;
2268 const char* cur
=start
;
2270 while (*cur
!= '=' && cur
<end
)
2273 while (isspace(*cur
) && cur
<end
)
2275 if (isNumberish(*cur
)) {
2277 while (*end
&& (*end
!='}') && (*end
!=',') && (*end
!='\n'))
2279 QString rawValue
= QString::fromLatin1(cur
, end
-cur
).simplifyWhiteSpace();
2280 reg
.rawValue
= rawValue
;
2282 if (rawValue
.left(2)=="0x") {
2283 // ok we have a raw value, now get it's type
2285 while (isspace(*end
) || *end
=='=') end
--;
2288 while (*cur
!='{' && *cur
!=' ')
2291 reg
.type
= QString::fromLatin1(cur
, end
-cur
);
2298 // skip to the end of line
2299 while (*output
!= '\0' && *output
!= '\n')
2301 // get rid of the braces at the begining and the end
2303 if (value
[value
.length()-1] == '}') {
2304 value
.truncate(value
.length()-1);
2307 reg
.cookedValue
= value
;
2311 // the rest of the line is the register value
2313 output
= strchr(output
,'\n');
2315 output
= start
+ strlen(start
);
2316 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2319 * We split the raw from the cooked values.
2320 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2321 * Here, the cooked value comes first, and the raw value is in
2324 int pos
= value
.find(" (raw ");
2327 reg
.cookedValue
= value
.left(pos
);
2328 reg
.rawValue
= value
.mid(pos
+6);
2329 if (reg
.rawValue
.right(1) == ")") // remove closing bracket
2330 reg
.rawValue
.truncate(reg
.rawValue
.length()-1);
2335 * In other cases we split off the first token (separated by
2336 * whitespace). It is the raw value. The remainder of the line
2337 * is the cooked value.
2339 int pos
= value
.find(' ');
2341 reg
.rawValue
= value
;
2342 reg
.cookedValue
= QString();
2344 reg
.rawValue
= value
.left(pos
);
2345 reg
.cookedValue
= value
.mid(pos
+1);
2349 if (*output
!= '\0')
2350 output
++; /* skip '\n' */
2352 regs
.push_back(reg
);
2357 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2359 // "is at address" or "starts at address"
2360 const char* start
= strstr(output
, "s at address ");
2365 const char* p
= start
;
2366 while (*p
!= '\0' && !isspace(*p
))
2368 addrFrom
= QString::fromLatin1(start
, p
-start
);
2370 start
= strstr(p
, "and ends at ");
2378 while (*p
!= '\0' && !isspace(*p
))
2380 addrTo
= QString::fromLatin1(start
, p
-start
);
2385 std::list
<DisassembledCode
> GdbDriver::parseDisassemble(const char* output
)
2387 std::list
<DisassembledCode
> code
;
2389 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2397 // remove first line
2398 const char* p
= strchr(output
, '\n');
2400 return code
; /* not a regular output */
2405 const char* end
= strstr(output
, "End of assembler");
2407 end
= p
+ strlen(p
);
2409 // remove function offsets from the lines
2413 const char* start
= p
;
2415 while (p
!= end
&& !isspace(*p
))
2417 c
.address
= QString::fromLatin1(start
, p
-start
);
2419 // function name (enclosed in '<>', followed by ':')
2420 while (p
!= end
&& *p
!= '<')
2423 skipNestedAngles(p
);
2428 while (p
!= end
&& isspace(*p
))
2431 // code until end of line
2433 while (p
!= end
&& *p
!= '\n')
2435 if (p
!= end
) /* include '\n' */
2438 c
.code
= QString::fromLatin1(start
, p
-start
);
2444 QString
GdbDriver::parseMemoryDump(const char* output
, std::list
<MemoryDump
>& memdump
)
2446 if (isErrorExpr(output
)) {
2447 // error; strip space
2448 QString msg
= output
;
2449 return msg
.stripWhiteSpace();
2452 const char* p
= output
; /* save typing */
2458 const char* start
= p
;
2459 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2461 md
.address
= QString::fromLatin1(start
, p
-start
);
2463 // parse function offset
2467 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2469 md
.address
.fnoffs
= QString::fromLatin1(start
, p
-start
);
2473 // skip space; this may skip a new-line char!
2476 // everything to the end of the line is the memory dump
2477 const char* end
= strchr(p
, '\n');
2479 md
.dump
= QString::fromLatin1(p
, end
-p
);
2482 md
.dump
= QString::fromLatin1(p
, strlen(p
));
2485 memdump
.push_back(md
);
2491 QString
GdbDriver::editableValue(VarTree
* value
)
2493 const char* s
= value
->value().latin1();
2495 // if the variable is a pointer value that contains a cast,
2498 skipNested(s
, '(', ')');
2505 const char* start
= s
;
2507 if (strncmp(s
, "0x", 2) == 0)
2510 while (isxdigit(*s
))
2514 * What we saw so far might have been a reference. If so, edit the
2515 * referenced value. Otherwise, edit the pointer.
2523 // if it's a pointer to a string, remove the string
2524 const char* end
= s
;
2529 return QString::fromLatin1(start
, end
-start
);
2532 return QString::fromLatin1(start
, strlen(start
));
2536 // else leave it unchanged (or stripped of the reference preamble)
2540 QString
GdbDriver::parseSetVariable(const char* output
)
2542 // if there is any output, it is an error message
2543 QString msg
= output
;
2544 return msg
.stripWhiteSpace();
2548 #include "gdbdriver.moc"