3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
9 #include <qstringlist.h>
10 #include <klocale.h> /* i18n */
12 #include <stdlib.h> /* strtol, atoi */
13 #include <string.h> /* strcpy */
21 static void skipString(const char*& p
);
22 static void skipNested(const char*& s
, char opening
, char closing
);
23 static ExprValue
* parseVar(const char*& s
);
24 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
);
25 static bool parseValue(const char*& s
, ExprValue
* variable
);
26 static bool parseNested(const char*& s
, ExprValue
* variable
);
27 static bool parseVarSeq(const char*& s
, ExprValue
* variable
);
28 static bool parseValueSeq(const char*& s
, ExprValue
* variable
);
30 #define PROMPT "(kdbg)"
32 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
35 // TODO: make this cmd info stuff non-static to allow multiple
36 // simultaneous gdbs to run!
40 const char* fmt
; /* format string */
42 argNone
, argString
, argNum
,
43 argStringNum
, argNumString
,
49 // This is how the QString data print statement generally looks like.
50 // It is set by KDebugger via setPrintQStringDataCmd().
52 static const char printQStringStructFmt
[] =
53 // if the string data is junk, fail early
54 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
55 // print an array of shorts
56 "(*(unsigned short*)$qstrunicode)@"
58 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
59 // if unicode data is 0, report a special value
62 static const char printQStringStructFmt
[] = "print (0?\"%s\":$kdbgundef)\n";
65 * The following array of commands must be sorted by the DC* values,
66 * because they are used as indices.
68 static GdbCmdInfo cmds
[] = {
69 { DCinitialize
, "", GdbCmdInfo::argNone
},
70 { DCtty
, "tty %s\n", GdbCmdInfo::argString
},
71 { DCexecutable
, "file \"%s\"\n", GdbCmdInfo::argString
},
72 { DCtargetremote
, "target remote %s\n", GdbCmdInfo::argString
},
73 { DCcorefile
, "core-file %s\n", GdbCmdInfo::argString
},
74 { DCattach
, "attach %s\n", GdbCmdInfo::argString
},
75 { DCinfolinemain
, "kdbg_infolinemain\n", GdbCmdInfo::argNone
},
76 { DCinfolocals
, "kdbg__alllocals\n", GdbCmdInfo::argNone
},
77 { DCinforegisters
, "info all-registers\n", GdbCmdInfo::argNone
},
78 { DCexamine
, "x %s %s\n", GdbCmdInfo::argString2
},
79 { DCinfoline
, "info line %s:%d\n", GdbCmdInfo::argStringNum
},
80 { DCdisassemble
, "disassemble %s %s\n", GdbCmdInfo::argString2
},
81 { DCsetargs
, "set args %s\n", GdbCmdInfo::argString
},
82 { DCsetenv
, "set env %s %s\n", GdbCmdInfo::argString2
},
83 { DCunsetenv
, "unset env %s\n", GdbCmdInfo::argString
},
84 { DCsetoption
, "setoption %s %d\n", GdbCmdInfo::argStringNum
},
85 { DCcd
, "cd %s\n", GdbCmdInfo::argString
},
86 { DCbt
, "bt\n", GdbCmdInfo::argNone
},
87 { DCrun
, "run\n", GdbCmdInfo::argNone
},
88 { DCcont
, "cont\n", GdbCmdInfo::argNone
},
89 { DCstep
, "step\n", GdbCmdInfo::argNone
},
90 { DCstepi
, "stepi\n", GdbCmdInfo::argNone
},
91 { DCnext
, "next\n", GdbCmdInfo::argNone
},
92 { DCnexti
, "nexti\n", GdbCmdInfo::argNone
},
93 { DCfinish
, "finish\n", GdbCmdInfo::argNone
},
94 { DCuntil
, "until %s:%d\n", GdbCmdInfo::argStringNum
},
95 { DCkill
, "kill\n", GdbCmdInfo::argNone
},
96 { DCbreaktext
, "break %s\n", GdbCmdInfo::argString
},
97 { DCbreakline
, "break %s:%d\n", GdbCmdInfo::argStringNum
},
98 { DCtbreakline
, "tbreak %s:%d\n", GdbCmdInfo::argStringNum
},
99 { DCbreakaddr
, "break *%s\n", GdbCmdInfo::argString
},
100 { DCtbreakaddr
, "tbreak *%s\n", GdbCmdInfo::argString
},
101 { DCwatchpoint
, "watch %s\n", GdbCmdInfo::argString
},
102 { DCdelete
, "delete %d\n", GdbCmdInfo::argNum
},
103 { DCenable
, "enable %d\n", GdbCmdInfo::argNum
},
104 { DCdisable
, "disable %d\n", GdbCmdInfo::argNum
},
105 { DCprint
, "print %s\n", GdbCmdInfo::argString
},
106 { DCprintDeref
, "print *(%s)\n", GdbCmdInfo::argString
},
107 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
108 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
109 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
110 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
111 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
112 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
113 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
114 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
115 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
116 { DCsetpc
, "set variable $pc=%s\n", GdbCmdInfo::argString
},
117 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
118 { DCprintWChar
, "print ($s=%s)?*$s@wcslen($s):0x0\n", GdbCmdInfo::argString
},
119 { DCsetvariable
, "set variable %s=%s\n", GdbCmdInfo::argString2
},
122 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
123 #define MAX_FMTLEN 200
125 GdbDriver::GdbDriver() :
127 m_gdbMajor(4), m_gdbMinor(16)
129 strcpy(m_prompt
, PROMPT
);
130 m_promptMinLen
= PROMPT_LEN
;
131 m_promptLastChar
= PROMPT_LAST_CHAR
;
134 // check command info array
136 for (int i
= 0; i
< NUM_CMDS
; i
++) {
137 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
138 assert(i
== cmds
[i
].cmd
);
139 // a format string must be associated
140 assert(cmds
[i
].fmt
!= 0);
141 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
142 // format string must match arg specification
143 switch (cmds
[i
].argsNeeded
) {
144 case GdbCmdInfo::argNone
:
145 assert(strchr(cmds
[i
].fmt
, '%') == 0);
147 case GdbCmdInfo::argString
:
148 perc
= strchr(cmds
[i
].fmt
, '%');
149 assert(perc
!= 0 && perc
[1] == 's');
150 assert(strchr(perc
+2, '%') == 0);
152 case GdbCmdInfo::argNum
:
153 perc
= strchr(cmds
[i
].fmt
, '%');
154 assert(perc
!= 0 && perc
[1] == 'd');
155 assert(strchr(perc
+2, '%') == 0);
157 case GdbCmdInfo::argStringNum
:
158 perc
= strchr(cmds
[i
].fmt
, '%');
159 assert(perc
!= 0 && perc
[1] == 's');
160 perc
= strchr(perc
+2, '%');
161 assert(perc
!= 0 && perc
[1] == 'd');
162 assert(strchr(perc
+2, '%') == 0);
164 case GdbCmdInfo::argNumString
:
165 perc
= strchr(cmds
[i
].fmt
, '%');
166 assert(perc
!= 0 && perc
[1] == 'd');
167 perc
= strchr(perc
+2, '%');
168 assert(perc
!= 0 && perc
[1] == 's');
169 assert(strchr(perc
+2, '%') == 0);
171 case GdbCmdInfo::argString2
:
172 perc
= strchr(cmds
[i
].fmt
, '%');
173 assert(perc
!= 0 && perc
[1] == 's');
174 perc
= strchr(perc
+2, '%');
175 assert(perc
!= 0 && perc
[1] == 's');
176 assert(strchr(perc
+2, '%') == 0);
178 case GdbCmdInfo::argNum2
:
179 perc
= strchr(cmds
[i
].fmt
, '%');
180 assert(perc
!= 0 && perc
[1] == 'd');
181 perc
= strchr(perc
+2, '%');
182 assert(perc
!= 0 && perc
[1] == 'd');
183 assert(strchr(perc
+2, '%') == 0);
187 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
191 GdbDriver::~GdbDriver()
196 QString
GdbDriver::driverName() const
201 QString
GdbDriver::defaultGdb()
205 " --fullname" /* to get standard file names each time the prog stops */
206 " --nx"; /* do not execute initialization files */
209 QString
GdbDriver::defaultInvocation() const
211 if (m_defaultCmd
.isEmpty()) {
218 QStringList
GdbDriver::boolOptionList() const
221 return QStringList();
224 bool GdbDriver::startup(QString cmdStr
)
226 if (!DebuggerDriver::startup(cmdStr
))
229 static const char gdbInitialize
[] =
231 * Work around buggy gdbs that do command line editing even if they
232 * are not on a tty. The readline library echos every command back
233 * in this case, which is confusing for us.
237 "set print static-members off\n"
238 "set print asm-demangle on\n"
240 * Don't assume that program functions invoked from a watch expression
243 "set unwindonsignal on\n"
245 * Write a short macro that prints all locals: local variables and
246 * function arguments.
248 "define kdbg__alllocals\n"
249 "info locals\n" /* local vars supersede args with same name */
250 "info args\n" /* therefore, arguments must come last */
253 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
255 "define kdbg_infolinemain\n"
259 // change prompt string and synchronize with gdb
260 "set prompt " PROMPT
"\n"
263 executeCmdString(DCinitialize
, gdbInitialize
, false);
265 // assume that QString::null is ok
266 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
271 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
273 // command string must be committed
274 if (!cmd
->m_committed
) {
276 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
281 switch (cmd
->m_cmd
) {
283 // get version number from preamble
286 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
287 int offset
= GDBVersion
.match(m_output
, 0, &len
);
289 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
291 m_gdbMajor
= strtol(start
, &end
, 10);
292 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
294 // nothing was parsed
299 // assume some default version (what would make sense?)
303 // use a feasible core-file command
304 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
306 cmds
[DCcorefile
].fmt
= "target FreeBSD-core %s\n";
308 cmds
[DCcorefile
].fmt
= "target core %s\n";
311 cmds
[DCcorefile
].fmt
= "core-file %s\n";
318 /* ok, the command is ready */
319 emit
commandReceived(cmd
, m_output
);
321 switch (cmd
->m_cmd
) {
340 * The --fullname option makes gdb send a special normalized sequence print
341 * each time the program stops and at some other points. The sequence has
342 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
344 void GdbDriver::parseMarker()
346 char* startMarker
= strstr(m_output
, "\032\032");
347 if (startMarker
== 0)
350 // extract the marker
352 TRACE(QString("found marker: ") + startMarker
);
353 char* endMarker
= strchr(startMarker
, '\n');
359 // extract filename and line number
360 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
363 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
364 if (lineNoStart
>= 0) {
365 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
368 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
369 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
371 // now show the window
372 startMarker
[lineNoStart
] = '\0'; /* split off file name */
373 emit
activateFileLine(startMarker
, lineNo
-1, address
);
379 * Escapes characters that might lead to problems when they appear on gdb's
382 static void normalizeStringArg(QString
& arg
)
385 * Remove trailing backslashes. This approach is a little simplistic,
386 * but we know that there is at the moment no case where a trailing
387 * backslash would make sense.
389 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
390 arg
= arg
.left(arg
.length()-1);
395 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
397 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
398 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
400 normalizeStringArg(strArg
);
403 // need the working directory when parsing the output
404 m_programWD
= strArg
;
405 } else if (cmd
== DCsetargs
&& !m_redirect
.isEmpty()) {
407 * Use saved redirection. We prepend it in front of the user's
408 * arguments so that the user can override the redirections.
410 strArg
= m_redirect
+ " " + strArg
;
414 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
418 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
420 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
421 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
424 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
428 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
430 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
431 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
432 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
436 normalizeStringArg(strArg
);
443 * intArg specifies which channels should be redirected to
444 * /dev/null. It is a value or'ed together from RDNstdin,
445 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
448 * Note: We rely on that after the DCtty a DCsetargs will follow,
449 * which will ultimately apply the redirection.
451 static const char* const runRedir
[8] = {
455 "</dev/null >/dev/null",
457 "</dev/null 2>/dev/null",
459 "</dev/null >/dev/null 2>&1"
461 if (strArg
.isEmpty())
462 intArg
= 7; /* failsafe if no tty */
463 m_redirect
= runRedir
[intArg
& 7];
465 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
468 if (cmd
== DCexamine
) {
469 // make a format specifier from the intArg
470 static const char size
[16] = {
471 '\0', 'b', 'h', 'w', 'g'
473 static const char format
[16] = {
474 '\0', 'x', 'd', 'u', 'o', 't',
475 'a', 'c', 'f', 's', 'i'
477 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
478 assert(MDTformatmask
== 0xf0); /* next 4 bits */
479 int count
= 16; /* number of entities to print */
480 char sizeSpec
= size
[intArg
& MDTsizemask
];
481 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
482 assert(sizeSpec
!= '\0');
483 assert(formatSpec
!= '\0');
484 // adjust count such that 16 lines are printed
485 switch (intArg
& MDTformatmask
) {
486 case MDTstring
: case MDTinsn
:
487 break; /* no modification needed */
489 // all cases drop through:
490 switch (intArg
& MDTsizemask
) {
502 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
504 return makeCmdString(DCexamine
, spec
, strArg
);
507 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
509 // line numbers are zero-based
510 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
511 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
515 if (cmd
== DCinfoline
)
517 // must split off file name part
518 int slash
= strArg
.findRev('/');
520 strArg
= strArg
.right(strArg
.length()-slash
-1);
522 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
526 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
531 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
533 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
534 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
536 normalizeStringArg(strArg1
);
537 normalizeStringArg(strArg2
);
540 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
544 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
546 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
547 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
550 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
554 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
556 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
557 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
560 m_haveCoreFile
= false;
563 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
566 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
569 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
572 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
576 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
579 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
582 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
585 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
588 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
591 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
594 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
597 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
599 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
602 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
605 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
608 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
611 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
614 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
617 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
620 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
623 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
626 void GdbDriver::terminate()
632 void GdbDriver::detachAndTerminate()
636 executeCmdString(DCinitialize
, "detach\nquit\n", true);
639 void GdbDriver::interruptInferior()
642 // remove accidentally queued commands
646 static bool isErrorExpr(const char* output
)
649 strncmp(output
, "Cannot access memory at", 23) == 0 ||
650 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
651 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
652 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
653 strncmp(output
, "There is no member or method named", 34) == 0 ||
654 strncmp(output
, "A parse error in expression", 27) == 0 ||
655 strncmp(output
, "No symbol \"", 11) == 0 ||
656 strncmp(output
, "Internal error: ", 16) == 0;
660 * Returns true if the output is an error message. If wantErrorValue is
661 * true, a new ExprValue object is created and filled with the error message.
662 * If there are warnings, they are skipped and output points past the warnings
663 * on return (even if there \e are errors).
665 static bool parseErrorMessage(const char*& output
,
666 ExprValue
*& variable
, bool wantErrorValue
)
669 while (strncmp(output
, "warning:", 8) == 0)
671 char* end
= strchr(output
+8, '\n');
673 output
+= strlen(output
);
678 if (isErrorExpr(output
))
680 if (wantErrorValue
) {
681 // put the error message as value in the variable
682 variable
= new ExprValue(QString(), VarTree::NKplain
);
683 const char* endMsg
= strchr(output
, '\n');
685 endMsg
= output
+ strlen(output
);
686 variable
->m_value
= QString::fromLatin1(output
, endMsg
-output
);
695 #if QT_VERSION >= 300
705 void GdbDriver::setPrintQStringDataCmd(const char* cmd
)
707 // don't accept the command if it is empty
708 if (cmd
== 0 || *cmd
== '\0')
710 assert(strlen(cmd
) <= MAX_FMTLEN
);
711 cmds
[DCprintQStringStruct
].fmt
= cmd
;
714 ExprValue
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
716 ExprValue
* variable
= 0;
719 * Parse off white space. gdb sometimes prints white space first if the
720 * printed array leaded to an error.
722 while (isspace(*output
))
725 // special case: empty string (0 repetitions)
726 if (strncmp(output
, "Invalid number 0 of repetitions", 31) == 0)
728 variable
= new ExprValue(QString(), VarTree::NKplain
);
729 variable
->m_value
= "\"\"";
733 // check for error conditions
734 if (parseErrorMessage(output
, variable
, wantErrorValue
))
740 const char* p
= output
;
748 } while (isspace(*p
));
752 // this is the real data
758 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
760 * A matrix for separators between the individual "things"
761 * that are added to the string. The first index is a bool,
762 * the second index is from the enum above.
764 static const char* separator
[2][3] = {
765 { "\"", 0, ", \"" }, /* normal char is added */
766 { "'", "\", '", ", '" } /* repeated char is added */
769 while (isdigit(*p
)) {
772 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
774 goto error
; /* huh? no valid digits */
775 // skip separator and search for a repeat count
777 while (isspace(*p
) || *p
== ',')
779 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
781 const char* start
= p
;
782 p
= strchr(p
+9, '>'); /* search end and advance */
786 repeatCount
= QString::fromLatin1(start
, p
-start
);
787 while (isspace(*p
) || *p
== ',')
790 // p is now at the next char (or the end)
792 // interpret the value as a QChar
793 // TODO: make cross-architecture compatible
799 (unsigned short&)ch
= value
;
803 ch
.setRow(c
.qch
.row
);
804 ch
.setCell(c
.qch
.cell
);
808 // escape a few frequently used characters
809 char escapeCode
= '\0';
810 switch (ch
.latin1()) {
811 case '\n': escapeCode
= 'n'; break;
812 case '\r': escapeCode
= 'r'; break;
813 case '\t': escapeCode
= 't'; break;
814 case '\b': escapeCode
= 'b'; break;
815 case '\"': escapeCode
= '\"'; break;
816 case '\\': escapeCode
= '\\'; break;
817 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
821 result
+= separator
[repeats
][lastThing
];
823 if (escapeCode
!= '\0') {
829 // fixup repeat count and lastThing
832 result
+= repeatCount
;
833 lastThing
= wasRepeat
;
842 if (lastThing
== wasChar
)
846 variable
= new ExprValue(QString(), VarTree::NKplain
);
847 variable
->m_value
= result
;
849 else if (strncmp(p
, "true", 4) == 0)
851 variable
= new ExprValue(QString(), VarTree::NKplain
);
852 variable
->m_value
= "QString::null";
854 else if (strncmp(p
, "false", 5) == 0)
856 variable
= new ExprValue(QString(), VarTree::NKplain
);
857 variable
->m_value
= "(null)";
864 if (wantErrorValue
) {
865 variable
= new ExprValue(QString(), VarTree::NKplain
);
866 variable
->m_value
= "internal parse error";
871 static ExprValue
* parseVar(const char*& s
)
880 VarTree::NameKind kind
;
882 * Detect anonymouse struct values: The 'name =' part is missing:
883 * s = { a = 1, { b = 2 }}
884 * Note that this detection works only inside structs when the anonymous
885 * struct is not the first member:
886 * s = {{ a = 1 }, b = 2}
887 * This is misparsed (by parseNested()) because it is mistakenly
888 * interprets the second opening brace as the first element of an array
893 name
= i18n("<anonymous struct or union>");
894 kind
= VarTree::NKanonymous
;
898 if (!parseName(p
, name
, kind
)) {
906 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
909 // skip the '=' and more whitespace
915 ExprValue
* variable
= new ExprValue(name
, kind
);
917 if (!parseValue(p
, variable
)) {
925 static void skipNested(const char*& s
, char opening
, char closing
)
929 // parse a nested type
933 * Search for next matching `closing' char, skipping nested pairs of
934 * `opening' and `closing'.
936 while (*p
&& nest
> 0) {
939 } else if (*p
== closing
) {
945 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
951 * This function skips text that is delimited by nested angle bracktes, '<>'.
952 * A complication arises because the delimited text can contain the names of
953 * operator<<, operator>>, operator<, and operator>, which have to be treated
954 * specially so that they do not count towards the nesting of '<>'.
955 * This function assumes that the delimited text does not contain strings.
957 static void skipNestedAngles(const char*& s
)
962 p
++; // skip the initial '<'
963 while (*p
&& nest
> 0)
965 // Below we can check for p-s >= 9 instead of 8 because
966 // *s is '<' and cannot be part of "operator".
969 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
978 if (p
-s
>= 9 && strncmp(p
-8, "operator", 8) == 0) {
988 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s
));
994 * Find the end of line that is not inside braces
996 static void findEnd(const char*& s
)
999 while (*p
&& *p
!='\n') {
1000 while (*p
&& *p
!='\n' && *p
!='{')
1004 skipNested(p
, '{', '}'); p
--;
1010 static bool isNumberish(const char ch
)
1012 return (ch
>='0' && ch
<='9') || ch
=='.' || ch
=='x';
1015 void skipString(const char*& p
)
1020 while (*p
!= quote
) {
1022 // skip escaped character
1023 // no special treatment for octal values necessary
1026 // simply return if no more characters
1034 * Strings can consist of several parts, some of which contain repeated
1037 if (quote
== '\'') {
1038 // look ahaead for <repeats 123 times>
1039 const char* q
= p
+1;
1042 if (strncmp(q
, "<repeats ", 9) == 0) {
1044 while (*p
!= '\0' && *p
!= '>')
1047 p
++; /* skip the '>' */
1051 // is the string continued?
1053 // look ahead for another quote
1054 const char* q
= p
+1;
1057 if (*q
== '"' || *q
== '\'') {
1063 /* very long strings are followed by `...' */
1064 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
1069 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
1073 // parse a nested expression
1077 * Search for next matching `closing' char, skipping nested pairs of
1078 * `opening' and `closing' as well as strings.
1080 while (*p
&& nest
> 0) {
1081 if (*p
== opening
) {
1083 } else if (*p
== closing
) {
1085 } else if (*p
== '\'' || *p
== '\"') {
1092 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1097 inline void skipName(const char*& p
)
1099 // allow : (for enumeration values) and $ and . (for _vtbl.)
1100 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1104 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1106 kind
= VarTree::NKplain
;
1109 // examples of names:
1112 // <string<a,b<c>,7> >
1115 skipNestedAngles(p
);
1116 name
= QString::fromLatin1(s
, p
- s
);
1117 kind
= VarTree::NKtype
;
1121 // name, which might be "static"; allow dot for "_vtbl."
1124 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1128 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1129 kind
= VarTree::NKstatic
;
1131 // its a static variable, name comes now
1137 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1142 name
= QString::fromLatin1(s
, len
);
1144 // return the new position
1149 static bool parseValue(const char*& s
, ExprValue
* variable
)
1151 variable
->m_value
= "";
1155 // Sometimes we find the following output:
1156 // {<text variable, no debug info>} 0x40012000 <access>
1157 // {<data variable, no debug info>}
1158 // {<variable (not text or data), no debug info>}
1159 if (strncmp(s
, "{<text variable, ", 17) == 0 ||
1160 strncmp(s
, "{<data variable, ", 17) == 0 ||
1161 strncmp(s
, "{<variable (not text or data), ", 31) == 0)
1163 const char* start
= s
;
1164 skipNested(s
, '{', '}');
1165 variable
->m_value
= QString::fromLatin1(start
, s
-start
);
1166 variable
->m_value
+= ' '; // add only a single space
1174 if (!parseNested(s
, variable
)) {
1177 // must be the closing brace
1179 TRACE("parse error: missing } of " + variable
->m_name
);
1183 // final white space
1188 // examples of leaf values (cannot be the empty string):
1194 // (DwContentType&) @0x8123456: {...}
1198 // 0x823abc <Array<int> virtual table>
1199 // (void (*)()) 0x8048480 <f(E *, char)>
1202 // &parseP (HTMLClueV *, char *)
1203 // Variable "x" is not available.
1204 // The value of variable 'x' is distributed...
1205 // -nan(0xfffff081defa0)
1212 skipNested(p
, '(', ')');
1216 variable
->m_value
= QString::fromLatin1(s
, p
- s
);
1219 bool reference
= false;
1221 // skip reference marker
1225 const char* start
= p
;
1229 // some values consist of more than one token
1230 bool checkMultiPart
= false;
1232 if (p
[0] == '0' && p
[1] == 'x') {
1235 while (isxdigit(*p
))
1239 * Assume this is a pointer, but only if it's not a reference, since
1240 * references can't be expanded.
1243 variable
->m_varKind
= VarTree::VKpointer
;
1246 * References are followed by a colon, in which case we'll
1247 * find the value following the reference address.
1252 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1256 checkMultiPart
= true;
1257 } else if (isdigit(*p
)) {
1258 // parse decimal number, possibly a float
1261 if (*p
== '.') { /* TODO: obey i18n? */
1262 // In long arrays an integer may be followed by '...'.
1263 // We test for this situation and don't gobble the '...'.
1264 if (p
[1] != '.' || p
[0] != '.') {
1271 if (*p
== 'e' || *p
== 'E') {
1274 if (*p
== '-' || *p
== '+')
1280 // for char variables there is the char, eg. 10 '\n'
1281 checkMultiPart
= true;
1282 } else if (*p
== '<') {
1283 // e.g. <optimized out>
1284 skipNestedAngles(p
);
1285 } else if (*p
== '"' || *p
== '\'') {
1286 // character may have multipart: '\000' <repeats 11 times>
1287 checkMultiPart
= *p
== '\'';
1290 } else if (*p
== '&') {
1294 while (isspace(*p
)) {
1298 skipNested(p
, '(', ')');
1300 } else if (strncmp(p
, "Variable \"", 10) == 0) {
1301 // Variable "x" is not available.
1302 p
+= 10; // skip to "
1304 if (strncmp(p
, "\" is not available.", 19) == 0) {
1307 } else if (strncmp(p
, "The value of variable '", 23) == 0) {
1310 const char* e
= strchr(p
, '.');
1317 // must be an enumeration value
1319 // hmm, not necessarily: nan (floating point Not a Number)
1320 // is followed by a number in ()
1322 skipNested(p
, '(', ')');
1324 variable
->m_value
+= QString::fromLatin1(start
, p
- start
);
1326 // remove line breaks from the value; this is ok since
1327 // string values never contain a literal line break
1328 variable
->m_value
.replace('\n', ' ');
1330 if (checkMultiPart
) {
1334 // may be followed by a string or <...>
1337 if (*p
== '"' || *p
== '\'') {
1339 } else if (*p
== '<') {
1340 // if this value is part of an array, it might be followed
1341 // by <repeats 15 times>, which we don't skip here
1342 if (strncmp(p
, "<repeats ", 9) != 0)
1343 skipNestedAngles(p
);
1346 // there is always a blank before the string,
1347 // which we will include in the final string value
1348 variable
->m_value
+= QString::fromLatin1(start
-1, (p
- start
)+1);
1349 // if this was a pointer, reset that flag since we
1350 // now got the value
1351 variable
->m_varKind
= VarTree::VKsimple
;
1355 if (variable
->m_value
.length() == 0) {
1356 TRACE("parse error: no value for " + variable
->m_name
);
1360 // final white space
1366 * If this was a reference, the value follows. It might even be a
1367 * composite variable!
1377 static bool parseNested(const char*& s
, ExprValue
* variable
)
1379 // could be a structure or an array
1384 bool isStruct
= false;
1386 * If there is a name followed by an = or an < -- which starts a type
1387 * name -- or "static", it is a structure
1389 if (*p
== '<' || *p
== '}') {
1391 } else if (strncmp(p
, "static ", 7) == 0) {
1393 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1394 // look ahead for a comma after the name
1401 p
= s
; /* rescan the name */
1404 if (!parseVarSeq(p
, variable
)) {
1407 variable
->m_varKind
= VarTree::VKstruct
;
1409 if (!parseValueSeq(p
, variable
)) {
1412 variable
->m_varKind
= VarTree::VKarray
;
1418 static bool parseVarSeq(const char*& s
, ExprValue
* variable
)
1420 // parse a comma-separated sequence of variables
1421 ExprValue
* var
= variable
; /* var != 0 to indicate success if empty seq */
1425 if (strncmp(s
, "<No data fields>}", 17) == 0)
1427 // no member variables, so break out immediately
1428 s
+= 16; /* go to the closing brace */
1433 break; /* syntax error */
1434 variable
->appendChild(var
);
1437 // skip the comma and whitespace
1445 static bool parseValueSeq(const char*& s
, ExprValue
* variable
)
1447 // parse a comma-separated sequence of variables
1452 name
.sprintf("[%d]", index
);
1453 ExprValue
* var
= new ExprValue(name
, VarTree::NKplain
);
1454 good
= parseValue(s
, var
);
1459 // a value may be followed by "<repeats 45 times>"
1460 if (strncmp(s
, "<repeats ", 9) == 0) {
1463 int l
= strtol(s
, &end
, 10);
1464 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1465 // should not happen
1469 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1470 // replace name and advance index
1471 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1474 // skip " times>" and space
1476 // possible final space
1482 variable
->appendChild(var
);
1483 // long arrays may be terminated by '...'
1484 if (strncmp(s
, "...", 3) == 0) {
1486 ExprValue
* var
= new ExprValue("...", VarTree::NKplain
);
1487 var
->m_value
= i18n("<additional entries of the array suppressed>");
1488 variable
->appendChild(var
);
1494 // skip the comma and whitespace
1498 // sometimes there is a closing brace after a comma
1506 * Parses a stack frame.
1508 static void parseFrameInfo(const char*& s
, QString
& func
,
1509 QString
& file
, int& lineNo
, DbgAddr
& address
)
1513 // next may be a hexadecimal address
1515 const char* start
= p
;
1519 while (isxdigit(*p
))
1521 address
= QString::fromLatin1(start
, p
-start
);
1522 if (strncmp(p
, " in ", 4) == 0)
1525 address
= DbgAddr();
1527 const char* start
= p
;
1528 // check for special signal handler frame
1529 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1530 func
= QString::fromLatin1(start
, 23);
1540 * Skip the function name. It is terminated by a left parenthesis
1541 * which does not delimit "(anonymous namespace)" and which is
1542 * outside the angle brackets <> of template parameter lists
1543 * and is preceded by a space.
1548 // check for operator<< and operator<
1549 if (p
-start
>= 8 && strncmp(p
-8, "operator", 8) == 0)
1557 // skip template parameter list
1558 skipNestedAngles(p
);
1560 } else if (*p
== '(') {
1561 // this skips "(anonymous namespace)" as well as the formal
1562 // parameter list of the containing function if this is a member
1563 // of a nested class
1564 skipNestedWithString(p
, '(', ')');
1565 } else if (*p
== ' ') {
1568 break; // parameter list found
1582 * Skip parameters. But notice that for complicated conversion
1583 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1584 * pointer to function) as well as operator()(...) we have to skip
1585 * additional pairs of parentheses. Furthermore, recent gdbs write the
1586 * demangled name followed by the arguments in a pair of parentheses,
1587 * where the demangled name can end in "const".
1590 skipNestedWithString(p
, '(', ')');
1594 if (strncmp(p
, "const", 5) == 0) {
1599 } while (*p
== '(');
1601 // check for file position
1602 if (strncmp(p
, "at ", 3) == 0) {
1604 const char* fileStart
= p
;
1605 // go for the end of the line
1606 while (*p
!= '\0' && *p
!= '\n')
1608 // search back for colon
1609 const char* colon
= p
;
1612 } while (*colon
!= ':');
1613 file
= QString::fromLatin1(fileStart
, colon
-fileStart
);
1614 lineNo
= atoi(colon
+1)-1;
1619 // check for "from shared lib"
1620 if (strncmp(p
, "from ", 5) == 0) {
1622 // go for the end of the line
1623 while (*p
!= '\0' && *p
!= '\n')
1632 // construct the function name (including file info)
1636 func
= QString::fromLatin1(start
, p
-start
-1); /* don't include \n */
1641 * Replace \n (and whitespace around it) in func by a blank. We cannot
1642 * use QString::simplifyWhiteSpace() for this because this would also
1643 * simplify space that belongs to a string arguments that gdb sometimes
1644 * prints in the argument lists of the function.
1646 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1648 while ((nl
= func
.find('\n', nl
)) >= 0) {
1649 // search back to the beginning of the whitespace
1650 int startWhite
= nl
;
1653 } while (isspace(func
[startWhite
].latin1()));
1655 // search forward to the end of the whitespace
1658 } while (isspace(func
[nl
].latin1()));
1660 func
.replace(startWhite
, nl
-startWhite
, " ");
1661 /* continue searching for more \n's at this place: */
1668 * Parses a stack frame including its frame number
1670 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1671 QString
& file
, int& lineNo
, DbgAddr
& address
)
1674 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1675 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1677 // must start with a hash mark followed by number
1678 // or with "Breakpoint " followed by number and comma
1682 s
++; /* skip the hash mark */
1683 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1684 if (!isdigit(s
[11]))
1686 s
+= 11; /* skip "Breakpoint" */
1695 while (isspace(*s
) || *s
== ',')
1697 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1701 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1704 int lineNo
, frameNo
;
1707 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1708 StackFrame
* frm
= new StackFrame
;
1709 frm
->frameNo
= frameNo
;
1710 frm
->fileName
= file
;
1711 frm
->lineNo
= lineNo
;
1712 frm
->address
= address
;
1713 frm
->var
= new ExprValue(func
, VarTree::NKplain
);
1718 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1719 QString
& file
, int& lineNo
, DbgAddr
& address
)
1722 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1726 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1728 // skip first line, which is the headline
1729 const char* p
= strchr(output
, '\n');
1740 uint ignoreCount
= 0;
1744 while (*p
!= '\0') {
1746 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1751 Breakpoint::Type bpType
= Breakpoint::breakpoint
;
1752 if (strncmp(p
, "breakpoint", 10) == 0) {
1754 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1755 bpType
= Breakpoint::watchpoint
;
1757 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1758 bpType
= Breakpoint::watchpoint
;
1767 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1775 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1781 // the address, if present
1782 if (bpType
== Breakpoint::breakpoint
&&
1783 strncmp(p
, "0x", 2) == 0)
1785 const char* start
= p
;
1786 while (*p
!= '\0' && !isspace(*p
))
1788 address
= QString::fromLatin1(start
, p
-start
);
1789 while (isspace(*p
) && *p
!= '\n')
1794 address
= QString();
1796 // remainder is location, hit and ignore count, condition
1799 condition
= QString();
1800 end
= strchr(p
, '\n');
1803 p
+= location
.length();
1805 location
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1806 p
= end
+1; /* skip over \n */
1809 // may be continued in next line
1810 while (isspace(*p
)) { /* p points to beginning of line */
1811 // skip white space at beginning of line
1816 end
= strchr(p
, '\n');
1820 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1821 // extract the hit count
1823 hits
= strtol(p
, &dummy
, 10);
1824 TRACE(QString().sprintf("hit count %d", hits
));
1825 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1826 // extract condition
1828 condition
= QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1829 TRACE("condition: "+condition
);
1830 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1831 // extract ignore count
1833 ignoreCount
= strtol(p
, &dummy
, 10);
1834 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1836 // indeed a continuation
1837 location
+= " " + QString::fromLatin1(p
, end
-p
).stripWhiteSpace();
1841 p
++; /* skip '\n' */
1843 Breakpoint
* bp
= new Breakpoint
;
1846 bp
->temporary
= disp
== 'd';
1847 bp
->enabled
= enable
== 'y';
1848 bp
->location
= location
;
1849 bp
->address
= address
;
1850 bp
->hitCount
= hits
;
1851 bp
->ignoreCount
= ignoreCount
;
1852 bp
->condition
= condition
;
1858 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1860 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1871 const char* p
= output
;
1872 while (*p
!= '\0') {
1873 // seach look for thread id, watching out for the focus indicator
1874 bool hasFocus
= false;
1875 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1880 // there follows only whitespace
1883 id
= strtol(p
, &end
, 10);
1885 // syntax error: no number found; bail out
1895 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1897 end
= strstr(p
, " ");
1899 // syntax error; bail out
1902 systag
= QString::fromLatin1(p
, end
-p
);
1906 * Now follows a standard stack frame. Sometimes, however, gdb
1907 * catches a thread at an instant where it doesn't have a stack.
1909 if (strncmp(p
, "[No stack.]", 11) != 0) {
1910 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1912 func
= "[No stack]";
1915 address
= QString();
1916 p
+= 11; /* \n is skipped above */
1919 ThreadInfo
* thr
= new ThreadInfo
;
1921 thr
->threadName
= systag
;
1922 thr
->hasFocus
= hasFocus
;
1923 thr
->function
= func
;
1924 thr
->fileName
= file
;
1925 thr
->lineNo
= lineNo
;
1926 thr
->address
= address
;
1927 threads
.append(thr
);
1932 static bool parseNewBreakpoint(const char* o
, int& id
,
1933 QString
& file
, int& lineNo
, QString
& address
);
1934 static bool parseNewWatchpoint(const char* o
, int& id
,
1937 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1938 QString
& file
, int& lineNo
, QString
& address
)
1940 const char* o
= output
;
1941 // skip lines of that begin with "(Cannot find"
1942 while (strncmp(o
, "(Cannot find", 12) == 0) {
1943 o
= strchr(o
, '\n');
1946 o
++; /* skip newline */
1949 if (strncmp(o
, "Breakpoint ", 11) == 0) {
1950 output
+= 11; /* skip "Breakpoint " */
1951 return ::parseNewBreakpoint(output
, id
, file
, lineNo
, address
);
1952 } else if (strncmp(o
, "Hardware watchpoint ", 20) == 0) {
1954 return ::parseNewWatchpoint(output
, id
, address
);
1955 } else if (strncmp(o
, "Watchpoint ", 11) == 0) {
1957 return ::parseNewWatchpoint(output
, id
, address
);
1962 static bool parseNewBreakpoint(const char* o
, int& id
,
1963 QString
& file
, int& lineNo
, QString
& address
)
1967 id
= strtoul(o
, &p
, 10);
1971 // check for the address
1972 if (strncmp(p
, " at 0x", 6) == 0) {
1973 char* start
= p
+4; /* skip " at ", but not 0x */
1975 while (isxdigit(*p
))
1977 address
= QString::fromLatin1(start
, p
-start
);
1981 char* fileStart
= strstr(p
, "file ");
1983 return !address
.isEmpty(); /* parse error only if there's no address */
1987 char* numStart
= strstr(fileStart
, ", line ");
1988 QString fileName
= QString::fromLatin1(fileStart
, numStart
-fileStart
);
1990 int line
= strtoul(numStart
, &p
, 10);
1995 lineNo
= line
-1; /* zero-based! */
1999 static bool parseNewWatchpoint(const char* o
, int& id
,
2004 id
= strtoul(o
, &p
, 10);
2008 if (strncmp(p
, ": ", 2) != 0)
2012 // all the rest on the line is the expression
2013 expr
= QString::fromLatin1(p
, strlen(p
)).stripWhiteSpace();
2017 void GdbDriver::parseLocals(const char* output
, QList
<ExprValue
>& newVars
)
2019 // check for possible error conditions
2020 if (strncmp(output
, "No symbol table", 15) == 0)
2025 while (*output
!= '\0') {
2026 while (isspace(*output
))
2028 if (*output
== '\0')
2030 // skip occurrences of "No locals" and "No args"
2031 if (strncmp(output
, "No locals", 9) == 0 ||
2032 strncmp(output
, "No arguments", 12) == 0)
2034 output
= strchr(output
, '\n');
2041 ExprValue
* variable
= parseVar(output
);
2042 if (variable
== 0) {
2045 // do not add duplicates
2046 for (ExprValue
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
2047 if (o
->m_name
== variable
->m_name
) {
2052 newVars
.append(variable
);
2057 ExprValue
* GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
)
2060 // check for error conditions
2061 if (!parseErrorMessage(output
, var
, wantErrorValue
))
2063 // parse the variable
2064 var
= parseVar(output
);
2069 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
2071 bool isGood
= false;
2072 message
= QString(output
).simplifyWhiteSpace();
2073 if (message
.isEmpty()) {
2074 message
= i18n("New working directory: ") + m_programWD
;
2080 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
2084 m_haveCoreFile
= false;
2087 * Lines starting with the following do not indicate errors:
2088 * Using host libthread_db
2089 * (no debugging symbols found)
2091 while (strncmp(output
, "Using host libthread_db", 23) == 0 ||
2092 strncmp(output
, "(no debugging symbols found)", 28) == 0)
2094 // this line is good, go to the next one
2095 const char* end
= strchr(output
, '\n');
2097 output
+= strlen(output
);
2103 * If we've parsed all lines, there was no error.
2105 return output
[0] == '\0';
2108 bool GdbDriver::parseCoreFile(const char* output
)
2110 // if command succeeded, gdb emits a line starting with "#0 "
2111 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
2112 return m_haveCoreFile
;
2115 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
2117 // optionally: "program changed, rereading symbols",
2119 // "Program exited normally"
2120 // "Program terminated with wignal SIGSEGV"
2121 // "Program received signal SIGINT" or other signal
2124 // go through the output, line by line, checking what we have
2125 const char* start
= output
- 1;
2126 uint flags
= SFprogramActive
;
2127 message
= QString();
2129 start
++; /* skip '\n' */
2131 if (strncmp(start
, "Program ", 8) == 0 ||
2132 strncmp(start
, "ptrace: ", 8) == 0) {
2134 * When we receive a signal, the program remains active.
2136 * Special: If we "stopped" in a corefile, the string "Program
2137 * terminated with signal"... is displayed. (Normally, we see
2138 * "Program received signal"... when a signal happens.)
2140 if (strncmp(start
, "Program exited", 14) == 0 ||
2141 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
2142 strncmp(start
, "ptrace: ", 8) == 0)
2144 flags
&= ~SFprogramActive
;
2148 const char* endOfMessage
= strchr(start
, '\n');
2149 if (endOfMessage
== 0)
2150 endOfMessage
= start
+ strlen(start
);
2151 message
= QString::fromLatin1(start
, endOfMessage
-start
);
2152 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
2154 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2155 * that it stopped at a temporary breakpoint).
2157 flags
|= SFrefreshBreak
;
2158 } else if (strstr(start
, "re-reading symbols.") != 0) {
2159 flags
|= SFrefreshSource
;
2162 // next line, please
2163 start
= strchr(start
, '\n');
2164 } while (start
!= 0);
2167 * Gdb only notices when new threads have appeared, but not when a
2168 * thread finishes. So we always have to assume that the list of
2169 * threads has changed.
2171 flags
|= SFrefreshThreads
;
2176 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
2178 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
2181 // parse the table of shared libraries
2183 // strip off head line
2184 output
= strchr(output
, '\n');
2187 output
++; /* skip '\n' */
2189 while (*output
!= '\0') {
2190 // format of a line is
2191 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2192 // 3 blocks of non-space followed by space
2193 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
2194 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
2197 while (isspace(*output
)) { /* space */
2201 if (*output
== '\0')
2203 const char* start
= output
;
2204 output
= strchr(output
, '\n');
2206 output
= start
+ strlen(start
);
2207 shlibName
= QString::fromLatin1(start
, output
-start
);
2208 if (*output
!= '\0')
2210 shlibs
.append(shlibName
);
2211 TRACE("found shared lib " + shlibName
);
2215 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
2217 if (strncmp(output
, "type = ", 7) != 0)
2221 * Everything else is the type. We strip off any leading "const" and any
2222 * trailing "&" on the grounds that neither affects the decoding of the
2223 * object. We also strip off all white-space from the type.
2226 if (strncmp(output
, "const ", 6) == 0)
2229 type
.replace(QRegExp("\\s+"), "");
2230 if (type
.endsWith("&"))
2231 type
.truncate(type
.length() - 1);
2235 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2237 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2244 // parse register values
2245 while (*output
!= '\0')
2247 // skip space at the start of the line
2248 while (isspace(*output
))
2252 const char* start
= output
;
2253 while (*output
!= '\0' && !isspace(*output
))
2255 if (*output
== '\0')
2257 regName
= QString::fromLatin1(start
, output
-start
);
2260 while (isspace(*output
))
2263 RegisterInfo
* reg
= new RegisterInfo
;
2264 reg
->regName
= regName
;
2267 * If we find a brace now, this is a vector register. We look for
2268 * the closing brace and treat the result as cooked value.
2273 skipNested(output
, '{', '}');
2274 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2275 // skip space, but not the end of line
2276 while (isspace(*output
) && *output
!= '\n')
2278 // get rid of the braces at the begining and the end
2280 if (value
[value
.length()-1] == '}') {
2281 value
= value
.left(value
.length()-1);
2283 // gdb 5.3 doesn't print a separate set of raw values
2284 if (*output
== '{') {
2285 // another set of vector follows
2286 // what we have so far is the raw value
2287 reg
->rawValue
= value
;
2290 skipNested(output
, '{', '}');
2291 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2294 // find first type that does not have an array, this is the RAW value
2295 const char* end
=start
;
2297 const char* cur
=start
;
2299 while (*cur
!= '=' && cur
<end
)
2302 while (isspace(*cur
) && cur
<end
)
2304 if (isNumberish(*cur
)) {
2306 while (*end
&& (*end
!='}') && (*end
!=',') && (*end
!='\n'))
2308 QString rawValue
= QString::fromLatin1(cur
, end
-cur
).simplifyWhiteSpace();
2309 reg
->rawValue
= rawValue
;
2311 if (rawValue
.left(2)=="0x") {
2312 // ok we have a raw value, now get it's type
2314 while (isspace(*end
) || *end
=='=') end
--;
2317 while (*cur
!='{' && *cur
!=' ')
2320 reg
->type
=QString::fromLatin1(cur
, end
-cur
);
2327 // skip to the end of line
2328 while (*output
!= '\0' && *output
!= '\n')
2330 // get rid of the braces at the begining and the end
2332 if (value
[value
.length()-1] == '}') {
2333 value
= value
.left(value
.length()-1);
2336 reg
->cookedValue
= value
;
2340 // the rest of the line is the register value
2342 output
= strchr(output
,'\n');
2344 output
= start
+ strlen(start
);
2345 value
= QString::fromLatin1(start
, output
-start
).simplifyWhiteSpace();
2348 * We split the raw from the cooked values.
2349 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2350 * Here, the cooked value comes first, and the raw value is in
2353 int pos
= value
.find(" (raw ");
2356 reg
->cookedValue
= value
.left(pos
);
2357 reg
->rawValue
= value
.mid(pos
+6);
2358 if (reg
->rawValue
.right(1) == ")") // remove closing bracket
2359 reg
->rawValue
.truncate(reg
->rawValue
.length()-1);
2364 * In other cases we split off the first token (separated by
2365 * whitespace). It is the raw value. The remainder of the line
2366 * is the cooked value.
2368 int pos
= value
.find(' ');
2370 reg
->rawValue
= value
;
2371 reg
->cookedValue
= QString();
2373 reg
->rawValue
= value
.left(pos
);
2374 reg
->cookedValue
= value
.mid(pos
+1);
2378 if (*output
!= '\0')
2379 output
++; /* skip '\n' */
2385 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2387 // "is at address" or "starts at address"
2388 const char* start
= strstr(output
, "s at address ");
2393 const char* p
= start
;
2394 while (*p
!= '\0' && !isspace(*p
))
2396 addrFrom
= QString::fromLatin1(start
, p
-start
);
2398 start
= strstr(p
, "and ends at ");
2406 while (*p
!= '\0' && !isspace(*p
))
2408 addrTo
= QString::fromLatin1(start
, p
-start
);
2413 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2417 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2421 code
.append(new DisassembledCode(c
));
2425 // remove first line
2426 const char* p
= strchr(output
, '\n');
2428 return; /* not a regular output */
2433 const char* end
= strstr(output
, "End of assembler");
2435 end
= p
+ strlen(p
);
2439 // remove function offsets from the lines
2442 const char* start
= p
;
2444 while (p
!= end
&& !isspace(*p
))
2446 address
= QString::fromLatin1(start
, p
-start
);
2448 // function name (enclosed in '<>', followed by ':')
2449 while (p
!= end
&& *p
!= '<')
2452 skipNestedAngles(p
);
2457 while (p
!= end
&& isspace(*p
))
2460 // code until end of line
2462 while (p
!= end
&& *p
!= '\n')
2464 if (p
!= end
) /* include '\n' */
2467 DisassembledCode
* c
= new DisassembledCode
;
2468 c
->address
= address
;
2469 c
->code
= QString::fromLatin1(start
, p
-start
);
2474 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2476 if (isErrorExpr(output
)) {
2477 // error; strip space
2478 QString msg
= output
;
2479 return msg
.stripWhiteSpace();
2482 const char* p
= output
; /* save typing */
2488 const char* start
= p
;
2489 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2491 addr
= QString::fromLatin1(start
, p
-start
);
2493 // parse function offset
2497 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2499 addr
.fnoffs
= QString::fromLatin1(start
, p
-start
);
2503 // skip space; this may skip a new-line char!
2506 // everything to the end of the line is the memory dump
2507 const char* end
= strchr(p
, '\n');
2509 dump
= QString::fromLatin1(p
, end
-p
);
2512 dump
= QString::fromLatin1(p
, strlen(p
));
2515 MemoryDump
* md
= new MemoryDump
;
2524 QString
GdbDriver::editableValue(VarTree
* value
)
2526 const char* s
= value
->value().latin1();
2528 // if the variable is a pointer value that contains a cast,
2531 skipNested(s
, '(', ')');
2538 const char* start
= s
;
2540 if (strncmp(s
, "0x", 2) == 0)
2543 while (isxdigit(*s
))
2547 * What we saw so far might have been a reference. If so, edit the
2548 * referenced value. Otherwise, edit the pointer.
2556 // if it's a pointer to a string, remove the string
2557 const char* end
= s
;
2562 return QString::fromLatin1(start
, end
-start
);
2565 return QString::fromLatin1(start
, strlen(start
));
2569 // else leave it unchanged (or stripped of the reference preamble)
2573 QString
GdbDriver::parseSetVariable(const char* output
)
2575 // if there is any output, it is an error message
2576 QString msg
= output
;
2577 return msg
.stripWhiteSpace();
2581 #include "gdbdriver.moc"