Document how to specify templates in type tables.
[kdbg.git] / kdbg / gdbdriver.cpp
bloba6b967bed2f820981174c1239e3d19958b046714
1 /*
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.
5 */
7 #include "gdbdriver.h"
8 #include "exprwnd.h"
9 #include <qregexp.h>
10 #include <qstringlist.h>
11 #include <klocale.h> /* i18n */
12 #include <ctype.h>
13 #include <stdlib.h> /* strtol, atoi */
14 #include <string.h> /* strcpy */
16 #include "assert.h"
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 #include "mydebug.h"
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)"
32 #define PROMPT_LEN 6
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!
39 struct GdbCmdInfo {
40 DbgCommand cmd;
41 const char* fmt; /* format string */
42 enum Args {
43 argNone, argString, argNum,
44 argStringNum, argNumString,
45 argString2, argNum2
46 } argsNeeded;
49 #if 0
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)@"
58 // limit the length
59 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
60 // if unicode data is 0, report a special value
61 ":1==0\n";
62 #endif
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() :
127 DebuggerDriver(),
128 m_gdbMajor(4), m_gdbMinor(16)
130 strcpy(m_prompt, PROMPT);
131 m_promptMinLen = PROMPT_LEN;
132 m_promptLastChar = PROMPT_LAST_CHAR;
134 #ifndef NDEBUG
135 // check command info array
136 char* perc;
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);
147 break;
148 case GdbCmdInfo::argString:
149 perc = strchr(cmds[i].fmt, '%');
150 assert(perc != 0 && perc[1] == 's');
151 assert(strchr(perc+2, '%') == 0);
152 break;
153 case GdbCmdInfo::argNum:
154 perc = strchr(cmds[i].fmt, '%');
155 assert(perc != 0 && perc[1] == 'd');
156 assert(strchr(perc+2, '%') == 0);
157 break;
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);
164 break;
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);
171 break;
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);
178 break;
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);
185 break;
188 assert(strlen(printQStringStructFmt) <= MAX_FMTLEN);
189 #endif
192 GdbDriver::~GdbDriver()
197 QString GdbDriver::driverName() const
199 return "GDB";
202 QString GdbDriver::defaultGdb()
204 return
205 "gdb"
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()) {
213 return defaultGdb();
214 } else {
215 return m_defaultCmd;
219 QStringList GdbDriver::boolOptionList() const
221 // no options
222 return QStringList();
225 bool GdbDriver::startup(QString cmdStr)
227 if (!DebuggerDriver::startup(cmdStr))
228 return false;
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.
236 "set editing off\n"
237 "set confirm off\n"
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
242 * always succeed.
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 */
252 "end\n"
254 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
256 "define kdbg_infolinemain\n"
257 "list\n"
258 "info line\n"
259 "end\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;
269 return true;
272 void GdbDriver::commandFinished(CmdQueueItem* cmd)
274 // command string must be committed
275 if (!cmd->m_committed) {
276 // not commited!
277 TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
278 cmd->m_cmdString)));
279 return;
282 switch (cmd->m_cmd) {
283 case DCinitialize:
284 // get version number from preamble
286 int len;
287 QRegExp GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
288 int offset = GDBVersion.match(m_output, 0, &len);
289 if (offset >= 0) {
290 char* start = m_output + offset + 5; // skip "\nGDB "
291 char* end;
292 m_gdbMajor = strtol(start, &end, 10);
293 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
294 if (start == end) {
295 // nothing was parsed
296 m_gdbMajor = 4;
297 m_gdbMinor = 16;
299 } else {
300 // assume some default version (what would make sense?)
301 m_gdbMajor = 4;
302 m_gdbMinor = 16;
304 // use a feasible core-file command
305 if (m_gdbMajor > 4 || (m_gdbMajor == 4 && m_gdbMinor >= 16)) {
306 #ifdef __FreeBSD__
307 cmds[DCcorefile].fmt = "target FreeBSD-core %s\n";
308 #else
309 cmds[DCcorefile].fmt = "target core %s\n";
310 #endif
311 } else {
312 cmds[DCcorefile].fmt = "core-file %s\n";
315 break;
316 default:;
319 /* ok, the command is ready */
320 emit commandReceived(cmd, m_output);
322 switch (cmd->m_cmd) {
323 case DCcorefile:
324 case DCinfolinemain:
325 case DCframe:
326 case DCattach:
327 case DCrun:
328 case DCcont:
329 case DCstep:
330 case DCstepi:
331 case DCnext:
332 case DCnexti:
333 case DCfinish:
334 case DCuntil:
335 parseMarker();
336 default:;
341 * The --fullname option makes gdb send a special normalized sequence print
342 * each time the program stops and at some other points. The sequence has
343 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
345 void GdbDriver::parseMarker()
347 char* startMarker = strstr(m_output, "\032\032");
348 if (startMarker == 0)
349 return;
351 // extract the marker
352 startMarker += 2;
353 TRACE(QString("found marker: ") + startMarker);
354 char* endMarker = strchr(startMarker, '\n');
355 if (endMarker == 0)
356 return;
358 *endMarker = '\0';
360 // extract filename and line number
361 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
363 int len;
364 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
365 if (lineNoStart >= 0) {
366 int lineNo = atoi(startMarker + lineNoStart+1);
368 // get address
369 const char* addrStart = startMarker + lineNoStart + len - 2;
370 DbgAddr address = QString(addrStart).stripWhiteSpace();
372 // now show the window
373 startMarker[lineNoStart] = '\0'; /* split off file name */
374 emit activateFileLine(startMarker, lineNo-1, address);
380 * Escapes characters that might lead to problems when they appear on gdb's
381 * command line.
383 static void normalizeStringArg(QString& arg)
386 * Remove trailing backslashes. This approach is a little simplistic,
387 * but we know that there is at the moment no case where a trailing
388 * backslash would make sense.
390 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
391 arg = arg.left(arg.length()-1);
396 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
398 assert(cmd >= 0 && cmd < NUM_CMDS);
399 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
401 normalizeStringArg(strArg);
403 if (cmd == DCcd) {
404 // need the working directory when parsing the output
405 m_programWD = strArg;
406 } else if (cmd == DCsetargs && !m_redirect.isEmpty()) {
408 * Use saved redirection. We prepend it in front of the user's
409 * arguments so that the user can override the redirections.
411 strArg = m_redirect + " " + strArg;
414 QString cmdString;
415 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
416 return cmdString;
419 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
421 assert(cmd >= 0 && cmd < NUM_CMDS);
422 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
424 QString cmdString;
425 cmdString.sprintf(cmds[cmd].fmt, intArg);
426 return cmdString;
429 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
431 assert(cmd >= 0 && cmd < NUM_CMDS);
432 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
433 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
434 cmd == DCexamine ||
435 cmd == DCtty);
437 normalizeStringArg(strArg);
439 QString cmdString;
441 if (cmd == DCtty)
444 * intArg specifies which channels should be redirected to
445 * /dev/null. It is a value or'ed together from RDNstdin,
446 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
447 * command.
449 * Note: We rely on that after the DCtty a DCsetargs will follow,
450 * which will ultimately apply the redirection.
452 static const char* const runRedir[8] = {
454 "</dev/null",
455 ">/dev/null",
456 "</dev/null >/dev/null",
457 "2>/dev/null",
458 "</dev/null 2>/dev/null",
459 ">/dev/null 2>&1",
460 "</dev/null >/dev/null 2>&1"
462 if (strArg.isEmpty())
463 intArg = 7; /* failsafe if no tty */
464 m_redirect = runRedir[intArg & 7];
466 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
469 if (cmd == DCexamine) {
470 // make a format specifier from the intArg
471 static const char size[16] = {
472 '\0', 'b', 'h', 'w', 'g'
474 static const char format[16] = {
475 '\0', 'x', 'd', 'u', 'o', 't',
476 'a', 'c', 'f', 's', 'i'
478 assert(MDTsizemask == 0xf); /* lowest 4 bits */
479 assert(MDTformatmask == 0xf0); /* next 4 bits */
480 int count = 16; /* number of entities to print */
481 char sizeSpec = size[intArg & MDTsizemask];
482 char formatSpec = format[(intArg & MDTformatmask) >> 4];
483 assert(sizeSpec != '\0');
484 assert(formatSpec != '\0');
485 // adjust count such that 16 lines are printed
486 switch (intArg & MDTformatmask) {
487 case MDTstring: case MDTinsn:
488 break; /* no modification needed */
489 default:
490 // all cases drop through:
491 switch (intArg & MDTsizemask) {
492 case MDTbyte:
493 case MDThalfword:
494 count *= 2;
495 case MDTword:
496 count *= 2;
497 case MDTgiantword:
498 count *= 2;
500 break;
502 QString spec;
503 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
505 return makeCmdString(DCexamine, spec, strArg);
508 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
510 // line numbers are zero-based
511 if (cmd == DCuntil || cmd == DCbreakline ||
512 cmd == DCtbreakline || cmd == DCinfoline)
514 intArg++;
516 if (cmd == DCinfoline)
518 // must split off file name part
519 int slash = strArg.findRev('/');
520 if (slash >= 0)
521 strArg = strArg.right(strArg.length()-slash-1);
523 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
525 else
527 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
529 return cmdString;
532 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
534 assert(cmd >= 0 && cmd < NUM_CMDS);
535 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
537 normalizeStringArg(strArg1);
538 normalizeStringArg(strArg2);
540 QString cmdString;
541 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
542 return cmdString;
545 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
547 assert(cmd >= 0 && cmd < NUM_CMDS);
548 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
550 QString cmdString;
551 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
552 return cmdString;
555 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
557 assert(cmd >= 0 && cmd < NUM_CMDS);
558 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
560 if (cmd == DCrun) {
561 m_haveCoreFile = false;
564 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
567 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
568 bool clearLow)
570 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
573 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
574 bool clearLow)
577 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
580 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
581 bool clearLow)
583 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
586 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
587 bool clearLow)
589 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
592 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
593 bool clearLow)
595 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
598 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
600 return queueCmdString(cmd, cmds[cmd].fmt, mode);
603 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
604 QueueMode mode)
606 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
609 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
610 QueueMode mode)
612 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
615 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
616 QueueMode mode)
618 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
621 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
622 QueueMode mode)
624 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
627 void GdbDriver::terminate()
629 kill(SIGTERM);
630 m_state = DSidle;
633 void GdbDriver::detachAndTerminate()
635 kill(SIGINT);
636 flushCommands();
637 executeCmdString(DCinitialize, "detach\nquit\n", true);
640 void GdbDriver::interruptInferior()
642 kill(SIGINT);
643 // remove accidentally queued commands
644 flushHiPriQueue();
647 static bool isErrorExpr(const char* output)
649 return
650 strncmp(output, "Cannot access memory at", 23) == 0 ||
651 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
652 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
653 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
654 strncmp(output, "There is no member or method named", 34) == 0 ||
655 strncmp(output, "A parse error in expression", 27) == 0 ||
656 strncmp(output, "No symbol \"", 11) == 0 ||
657 strncmp(output, "Internal error: ", 16) == 0;
661 * Returns true if the output is an error message. If wantErrorValue is
662 * true, a new ExprValue object is created and filled with the error message.
663 * If there are warnings, they are skipped and output points past the warnings
664 * on return (even if there \e are errors).
666 static bool parseErrorMessage(const char*& output,
667 ExprValue*& variable, bool wantErrorValue)
669 // skip warnings
670 while (strncmp(output, "warning:", 8) == 0)
672 char* end = strchr(output+8, '\n');
673 if (end == 0)
674 output += strlen(output);
675 else
676 output = end+1;
679 if (isErrorExpr(output))
681 if (wantErrorValue) {
682 // put the error message as value in the variable
683 variable = new ExprValue(QString(), VarTree::NKplain);
684 const char* endMsg = strchr(output, '\n');
685 if (endMsg == 0)
686 endMsg = output + strlen(output);
687 variable->m_value = QString::fromLatin1(output, endMsg-output);
688 } else {
689 variable = 0;
691 return true;
693 return false;
696 #if QT_VERSION >= 300
697 union Qt2QChar {
698 short s;
699 struct {
700 uchar row;
701 uchar cell;
702 } qch;
704 #endif
706 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
708 // don't accept the command if it is empty
709 if (cmd == 0 || *cmd == '\0')
710 return;
711 assert(strlen(cmd) <= MAX_FMTLEN);
712 cmds[DCprintQStringStruct].fmt = cmd;
715 ExprValue* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
717 ExprValue* variable = 0;
720 * Parse off white space. gdb sometimes prints white space first if the
721 * printed array leaded to an error.
723 while (isspace(*output))
724 output++;
726 // special case: empty string (0 repetitions)
727 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
729 variable = new ExprValue(QString(), VarTree::NKplain);
730 variable->m_value = "\"\"";
731 return variable;
734 // check for error conditions
735 if (parseErrorMessage(output, variable, wantErrorValue))
736 return variable;
738 // parse the array
740 // find '='
741 const char* p = output;
742 p = strchr(p, '=');
743 if (p == 0) {
744 goto error;
746 // skip white space
747 do {
748 p++;
749 } while (isspace(*p));
751 if (*p == '{')
753 // this is the real data
754 p++; /* skip '{' */
756 // parse the array
757 QString result;
758 QString repeatCount;
759 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
761 * A matrix for separators between the individual "things"
762 * that are added to the string. The first index is a bool,
763 * the second index is from the enum above.
765 static const char* separator[2][3] = {
766 { "\"", 0, ", \"" }, /* normal char is added */
767 { "'", "\", '", ", '" } /* repeated char is added */
770 while (isdigit(*p)) {
771 // parse a number
772 char* end;
773 unsigned short value = (unsigned short) strtoul(p, &end, 0);
774 if (end == p)
775 goto error; /* huh? no valid digits */
776 // skip separator and search for a repeat count
777 p = end;
778 while (isspace(*p) || *p == ',')
779 p++;
780 bool repeats = strncmp(p, "<repeats ", 9) == 0;
781 if (repeats) {
782 const char* start = p;
783 p = strchr(p+9, '>'); /* search end and advance */
784 if (p == 0)
785 goto error;
786 p++; /* skip '>' */
787 repeatCount = QString::fromLatin1(start, p-start);
788 while (isspace(*p) || *p == ',')
789 p++;
791 // p is now at the next char (or the end)
793 // interpret the value as a QChar
794 // TODO: make cross-architecture compatible
795 QChar ch;
796 if (qt3like) {
797 ch = QChar(value);
798 } else {
799 #if QT_VERSION < 300
800 (unsigned short&)ch = value;
801 #else
802 Qt2QChar c;
803 c.s = value;
804 ch.setRow(c.qch.row);
805 ch.setCell(c.qch.cell);
806 #endif
809 // escape a few frequently used characters
810 char escapeCode = '\0';
811 switch (ch.latin1()) {
812 case '\n': escapeCode = 'n'; break;
813 case '\r': escapeCode = 'r'; break;
814 case '\t': escapeCode = 't'; break;
815 case '\b': escapeCode = 'b'; break;
816 case '\"': escapeCode = '\"'; break;
817 case '\\': escapeCode = '\\'; break;
818 case '\0': if (value == 0) { escapeCode = '0'; } break;
821 // add separator
822 result += separator[repeats][lastThing];
823 // add char
824 if (escapeCode != '\0') {
825 result += '\\';
826 ch = escapeCode;
828 result += ch;
830 // fixup repeat count and lastThing
831 if (repeats) {
832 result += "' ";
833 result += repeatCount;
834 lastThing = wasRepeat;
835 } else {
836 lastThing = wasChar;
839 if (*p != '}')
840 goto error;
842 // closing quote
843 if (lastThing == wasChar)
844 result += "\"";
846 // assign the value
847 variable = new ExprValue(QString(), VarTree::NKplain);
848 variable->m_value = result;
850 else if (strncmp(p, "true", 4) == 0)
852 variable = new ExprValue(QString(), VarTree::NKplain);
853 variable->m_value = "QString::null";
855 else if (strncmp(p, "false", 5) == 0)
857 variable = new ExprValue(QString(), VarTree::NKplain);
858 variable->m_value = "(null)";
860 else
861 goto error;
862 return variable;
864 error:
865 if (wantErrorValue) {
866 variable = new ExprValue(QString(), VarTree::NKplain);
867 variable->m_value = "internal parse error";
869 return variable;
872 static ExprValue* parseVar(const char*& s)
874 const char* p = s;
876 // skip whitespace
877 while (isspace(*p))
878 p++;
880 QString name;
881 VarTree::NameKind kind;
883 * Detect anonymouse struct values: The 'name =' part is missing:
884 * s = { a = 1, { b = 2 }}
885 * Note that this detection works only inside structs when the anonymous
886 * struct is not the first member:
887 * s = {{ a = 1 }, b = 2}
888 * This is misparsed (by parseNested()) because it is mistakenly
889 * interprets the second opening brace as the first element of an array
890 * of structs.
892 if (*p == '{')
894 name = i18n("<anonymous struct or union>");
895 kind = VarTree::NKanonymous;
897 else
899 if (!parseName(p, name, kind)) {
900 return 0;
903 // go for '='
904 while (isspace(*p))
905 p++;
906 if (*p != '=') {
907 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
908 return 0;
910 // skip the '=' and more whitespace
911 p++;
912 while (isspace(*p))
913 p++;
916 ExprValue* variable = new ExprValue(name, kind);
918 if (!parseValue(p, variable)) {
919 delete variable;
920 return 0;
922 s = p;
923 return variable;
926 static void skipNested(const char*& s, char opening, char closing)
928 const char* p = s;
930 // parse a nested type
931 int nest = 1;
932 p++;
934 * Search for next matching `closing' char, skipping nested pairs of
935 * `opening' and `closing'.
937 while (*p && nest > 0) {
938 if (*p == opening) {
939 nest++;
940 } else if (*p == closing) {
941 nest--;
943 p++;
945 if (nest != 0) {
946 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
948 s = p;
952 * This function skips text that is delimited by nested angle bracktes, '<>'.
953 * A complication arises because the delimited text can contain the names of
954 * operator<<, operator>>, operator<, and operator>, which have to be treated
955 * specially so that they do not count towards the nesting of '<>'.
956 * This function assumes that the delimited text does not contain strings.
958 static void skipNestedAngles(const char*& s)
960 const char* p = s;
962 int nest = 1;
963 p++; // skip the initial '<'
964 while (*p && nest > 0)
966 // Below we can check for p-s >= 9 instead of 8 because
967 // *s is '<' and cannot be part of "operator".
968 if (*p == '<')
970 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
971 if (p[1] == '<')
972 p++;
973 } else {
974 nest++;
977 else if (*p == '>')
979 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
980 if (p[1] == '>')
981 p++;
982 } else {
983 nest--;
986 p++;
988 if (nest != 0) {
989 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
991 s = p;
995 * Find the end of line that is not inside braces
997 static void findEnd(const char*& s)
999 const char* p = s;
1000 while (*p && *p!='\n') {
1001 while (*p && *p!='\n' && *p!='{')
1002 p++;
1003 if (*p=='{') {
1004 p++;
1005 skipNested(p, '{', '}'); p--;
1008 s = p;
1011 static bool isNumberish(const char ch)
1013 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
1016 void skipString(const char*& p)
1018 moreStrings:
1019 // opening quote
1020 char quote = *p++;
1021 while (*p != quote) {
1022 if (*p == '\\') {
1023 // skip escaped character
1024 // no special treatment for octal values necessary
1025 p++;
1027 // simply return if no more characters
1028 if (*p == '\0')
1029 return;
1030 p++;
1032 // closing quote
1033 p++;
1035 * Strings can consist of several parts, some of which contain repeated
1036 * characters.
1038 if (quote == '\'') {
1039 // look ahaead for <repeats 123 times>
1040 const char* q = p+1;
1041 while (isspace(*q))
1042 q++;
1043 if (strncmp(q, "<repeats ", 9) == 0) {
1044 p = q+9;
1045 while (*p != '\0' && *p != '>')
1046 p++;
1047 if (*p != '\0') {
1048 p++; /* skip the '>' */
1052 // is the string continued?
1053 if (*p == ',') {
1054 // look ahead for another quote
1055 const char* q = p+1;
1056 while (isspace(*q))
1057 q++;
1058 if (*q == '"' || *q == '\'') {
1059 // yes!
1060 p = q;
1061 goto moreStrings;
1064 /* very long strings are followed by `...' */
1065 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1066 p += 3;
1070 static void skipNestedWithString(const char*& s, char opening, char closing)
1072 const char* p = s;
1074 // parse a nested expression
1075 int nest = 1;
1076 p++;
1078 * Search for next matching `closing' char, skipping nested pairs of
1079 * `opening' and `closing' as well as strings.
1081 while (*p && nest > 0) {
1082 if (*p == opening) {
1083 nest++;
1084 } else if (*p == closing) {
1085 nest--;
1086 } else if (*p == '\'' || *p == '\"') {
1087 skipString(p);
1088 continue;
1090 p++;
1092 if (nest > 0) {
1093 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1095 s = p;
1098 inline void skipName(const char*& p)
1100 // allow : (for enumeration values) and $ and . (for _vtbl.)
1101 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1102 p++;
1105 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1107 kind = VarTree::NKplain;
1109 const char* p = s;
1110 // examples of names:
1111 // name
1112 // <Object>
1113 // <string<a,b<c>,7> >
1115 if (*p == '<') {
1116 skipNestedAngles(p);
1117 name = QString::fromLatin1(s, p - s);
1118 kind = VarTree::NKtype;
1120 else
1122 // name, which might be "static"; allow dot for "_vtbl."
1123 skipName(p);
1124 if (p == s) {
1125 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1126 return false;
1128 int len = p - s;
1129 if (len == 6 && strncmp(s, "static", 6) == 0) {
1130 kind = VarTree::NKstatic;
1132 // its a static variable, name comes now
1133 while (isspace(*p))
1134 p++;
1135 s = p;
1136 skipName(p);
1137 if (p == s) {
1138 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1139 return false;
1141 len = p - s;
1143 name = QString::fromLatin1(s, len);
1145 // return the new position
1146 s = p;
1147 return true;
1150 static bool parseValue(const char*& s, ExprValue* variable)
1152 variable->m_value = "";
1154 repeat:
1155 if (*s == '{') {
1156 // Sometimes we find the following output:
1157 // {<text variable, no debug info>} 0x40012000 <access>
1158 // {<data variable, no debug info>}
1159 // {<variable (not text or data), no debug info>}
1160 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1161 strncmp(s, "{<data variable, ", 17) == 0 ||
1162 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1164 const char* start = s;
1165 skipNested(s, '{', '}');
1166 variable->m_value = QString::fromLatin1(start, s-start);
1167 variable->m_value += ' '; // add only a single space
1168 while (isspace(*s))
1169 s++;
1170 goto repeat;
1172 else
1174 s++;
1175 if (!parseNested(s, variable)) {
1176 return false;
1178 // must be the closing brace
1179 if (*s != '}') {
1180 TRACE("parse error: missing } of " + variable->m_name);
1181 return false;
1183 s++;
1184 // final white space
1185 while (isspace(*s))
1186 s++;
1188 } else {
1189 // examples of leaf values (cannot be the empty string):
1190 // 123
1191 // -123
1192 // 23.575e+37
1193 // 0x32a45
1194 // @0x012ab4
1195 // (DwContentType&) @0x8123456: {...}
1196 // 0x32a45 "text"
1197 // 10 '\n'
1198 // <optimized out>
1199 // 0x823abc <Array<int> virtual table>
1200 // (void (*)()) 0x8048480 <f(E *, char)>
1201 // (E *) 0xbffff450
1202 // red
1203 // &parseP (HTMLClueV *, char *)
1204 // Variable "x" is not available.
1205 // The value of variable 'x' is distributed...
1206 // -nan(0xfffff081defa0)
1208 const char*p = s;
1210 // check for type
1211 QString type;
1212 if (*p == '(') {
1213 skipNested(p, '(', ')');
1215 while (isspace(*p))
1216 p++;
1217 variable->m_value = QString::fromLatin1(s, p - s);
1220 bool reference = false;
1221 if (*p == '@') {
1222 // skip reference marker
1223 p++;
1224 reference = true;
1226 const char* start = p;
1227 if (*p == '-')
1228 p++;
1230 // some values consist of more than one token
1231 bool checkMultiPart = false;
1233 if (p[0] == '0' && p[1] == 'x') {
1234 // parse hex number
1235 p += 2;
1236 while (isxdigit(*p))
1237 p++;
1240 * Assume this is a pointer, but only if it's not a reference, since
1241 * references can't be expanded.
1243 if (!reference) {
1244 variable->m_varKind = VarTree::VKpointer;
1245 } else {
1247 * References are followed by a colon, in which case we'll
1248 * find the value following the reference address.
1250 if (*p == ':') {
1251 p++;
1252 } else {
1253 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1254 reference = false;
1257 checkMultiPart = true;
1258 } else if (isdigit(*p)) {
1259 // parse decimal number, possibly a float
1260 while (isdigit(*p))
1261 p++;
1262 if (*p == '.') { /* TODO: obey i18n? */
1263 // In long arrays an integer may be followed by '...'.
1264 // We test for this situation and don't gobble the '...'.
1265 if (p[1] != '.' || p[0] != '.') {
1266 // fractional part
1267 p++;
1268 while (isdigit(*p))
1269 p++;
1272 if (*p == 'e' || *p == 'E') {
1273 p++;
1274 // exponent
1275 if (*p == '-' || *p == '+')
1276 p++;
1277 while (isdigit(*p))
1278 p++;
1281 // for char variables there is the char, eg. 10 '\n'
1282 checkMultiPart = true;
1283 } else if (*p == '<') {
1284 // e.g. <optimized out>
1285 skipNestedAngles(p);
1286 } else if (*p == '"' || *p == '\'') {
1287 // character may have multipart: '\000' <repeats 11 times>
1288 checkMultiPart = *p == '\'';
1289 // found a string
1290 skipString(p);
1291 } else if (*p == '&') {
1292 // function pointer
1293 p++;
1294 skipName(p);
1295 while (isspace(*p)) {
1296 p++;
1298 if (*p == '(') {
1299 skipNested(p, '(', ')');
1301 } else if (strncmp(p, "Variable \"", 10) == 0) {
1302 // Variable "x" is not available.
1303 p += 10; // skip to "
1304 skipName(p);
1305 if (strncmp(p, "\" is not available.", 19) == 0) {
1306 p += 19;
1308 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1309 p += 23;
1310 skipName(p);
1311 const char* e = strchr(p, '.');
1312 if (e == 0) {
1313 p += strlen(p);
1314 } else {
1315 p = e+1;
1317 } else {
1318 // must be an enumeration value
1319 skipName(p);
1320 // hmm, not necessarily: nan (floating point Not a Number)
1321 // is followed by a number in ()
1322 if (*p == '(')
1323 skipNested(p, '(', ')');
1325 variable->m_value += QString::fromLatin1(start, p - start);
1327 // remove line breaks from the value; this is ok since
1328 // string values never contain a literal line break
1329 variable->m_value.replace('\n', ' ');
1331 if (checkMultiPart) {
1332 // white space
1333 while (isspace(*p))
1334 p++;
1335 // may be followed by a string or <...>
1336 start = p;
1338 if (*p == '"' || *p == '\'') {
1339 skipString(p);
1340 } else if (*p == '<') {
1341 // if this value is part of an array, it might be followed
1342 // by <repeats 15 times>, which we don't skip here
1343 if (strncmp(p, "<repeats ", 9) != 0)
1344 skipNestedAngles(p);
1346 if (p != start) {
1347 // there is always a blank before the string,
1348 // which we will include in the final string value
1349 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1350 // if this was a pointer, reset that flag since we
1351 // now got the value
1352 variable->m_varKind = VarTree::VKsimple;
1356 if (variable->m_value.length() == 0) {
1357 TRACE("parse error: no value for " + variable->m_name);
1358 return false;
1361 // final white space
1362 while (isspace(*p))
1363 p++;
1364 s = p;
1367 * If this was a reference, the value follows. It might even be a
1368 * composite variable!
1370 if (reference) {
1371 goto repeat;
1375 return true;
1378 static bool parseNested(const char*& s, ExprValue* variable)
1380 // could be a structure or an array
1381 while (isspace(*s))
1382 s++;
1384 const char* p = s;
1385 bool isStruct = false;
1387 * If there is a name followed by an = or an < -- which starts a type
1388 * name -- or "static", it is a structure
1390 if (*p == '<' || *p == '}') {
1391 isStruct = true;
1392 } else if (strncmp(p, "static ", 7) == 0) {
1393 isStruct = true;
1394 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1395 // look ahead for a comma after the name
1396 skipName(p);
1397 while (isspace(*p))
1398 p++;
1399 if (*p == '=') {
1400 isStruct = true;
1402 p = s; /* rescan the name */
1404 if (isStruct) {
1405 if (!parseVarSeq(p, variable)) {
1406 return false;
1408 variable->m_varKind = VarTree::VKstruct;
1409 } else {
1410 if (!parseValueSeq(p, variable)) {
1411 return false;
1413 variable->m_varKind = VarTree::VKarray;
1415 s = p;
1416 return true;
1419 static bool parseVarSeq(const char*& s, ExprValue* variable)
1421 // parse a comma-separated sequence of variables
1422 ExprValue* var = variable; /* var != 0 to indicate success if empty seq */
1423 for (;;) {
1424 if (*s == '}')
1425 break;
1426 if (strncmp(s, "<No data fields>}", 17) == 0)
1428 // no member variables, so break out immediately
1429 s += 16; /* go to the closing brace */
1430 break;
1432 var = parseVar(s);
1433 if (var == 0)
1434 break; /* syntax error */
1435 variable->appendChild(var);
1436 if (*s != ',')
1437 break;
1438 // skip the comma and whitespace
1439 s++;
1440 while (isspace(*s))
1441 s++;
1443 return var != 0;
1446 static bool parseValueSeq(const char*& s, ExprValue* variable)
1448 // parse a comma-separated sequence of variables
1449 int index = 0;
1450 bool good;
1451 for (;;) {
1452 QString name;
1453 name.sprintf("[%d]", index);
1454 ExprValue* var = new ExprValue(name, VarTree::NKplain);
1455 good = parseValue(s, var);
1456 if (!good) {
1457 delete var;
1458 return false;
1460 // a value may be followed by "<repeats 45 times>"
1461 if (strncmp(s, "<repeats ", 9) == 0) {
1462 s += 9;
1463 char* end;
1464 int l = strtol(s, &end, 10);
1465 if (end == s || strncmp(end, " times>", 7) != 0) {
1466 // should not happen
1467 delete var;
1468 return false;
1470 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1471 // replace name and advance index
1472 name.sprintf("[%d .. %d]", index, index+l-1);
1473 var->m_name = name;
1474 index += l;
1475 // skip " times>" and space
1476 s = end+7;
1477 // possible final space
1478 while (isspace(*s))
1479 s++;
1480 } else {
1481 index++;
1483 variable->appendChild(var);
1484 // long arrays may be terminated by '...'
1485 if (strncmp(s, "...", 3) == 0) {
1486 s += 3;
1487 ExprValue* var = new ExprValue("...", VarTree::NKplain);
1488 var->m_value = i18n("<additional entries of the array suppressed>");
1489 variable->appendChild(var);
1490 break;
1492 if (*s != ',') {
1493 break;
1495 // skip the comma and whitespace
1496 s++;
1497 while (isspace(*s))
1498 s++;
1499 // sometimes there is a closing brace after a comma
1500 // if (*s == '}')
1501 // break;
1503 return true;
1507 * Parses a stack frame.
1509 static void parseFrameInfo(const char*& s, QString& func,
1510 QString& file, int& lineNo, DbgAddr& address)
1512 const char* p = s;
1514 // next may be a hexadecimal address
1515 if (*p == '0') {
1516 const char* start = p;
1517 p++;
1518 if (*p == 'x')
1519 p++;
1520 while (isxdigit(*p))
1521 p++;
1522 address = QString::fromLatin1(start, p-start);
1523 if (strncmp(p, " in ", 4) == 0)
1524 p += 4;
1525 } else {
1526 address = DbgAddr();
1528 const char* start = p;
1529 // check for special signal handler frame
1530 if (strncmp(p, "<signal handler called>", 23) == 0) {
1531 func = QString::fromLatin1(start, 23);
1532 file = QString();
1533 lineNo = -1;
1534 s = p+23;
1535 if (*s == '\n')
1536 s++;
1537 return;
1541 * Skip the function name. It is terminated by a left parenthesis
1542 * which does not delimit "(anonymous namespace)" and which is
1543 * outside the angle brackets <> of template parameter lists
1544 * and is preceded by a space.
1546 while (*p != '\0')
1548 if (*p == '<') {
1549 // check for operator<< and operator<
1550 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1552 p++;
1553 if (*p == '<')
1554 p++;
1556 else
1558 // skip template parameter list
1559 skipNestedAngles(p);
1561 } else if (*p == '(') {
1562 // this skips "(anonymous namespace)" as well as the formal
1563 // parameter list of the containing function if this is a member
1564 // of a nested class
1565 skipNestedWithString(p, '(', ')');
1566 } else if (*p == ' ') {
1567 ++p;
1568 if (*p == '(')
1569 break; // parameter list found
1570 } else {
1571 p++;
1575 if (*p == '\0') {
1576 func = start;
1577 file = QString();
1578 lineNo = -1;
1579 s = p;
1580 return;
1583 * Skip parameters. But notice that for complicated conversion
1584 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1585 * pointer to function) as well as operator()(...) we have to skip
1586 * additional pairs of parentheses. Furthermore, recent gdbs write the
1587 * demangled name followed by the arguments in a pair of parentheses,
1588 * where the demangled name can end in "const".
1590 do {
1591 skipNestedWithString(p, '(', ')');
1592 while (isspace(*p))
1593 p++;
1594 // skip "const"
1595 if (strncmp(p, "const", 5) == 0) {
1596 p += 5;
1597 while (isspace(*p))
1598 p++;
1600 } while (*p == '(');
1602 // check for file position
1603 if (strncmp(p, "at ", 3) == 0) {
1604 p += 3;
1605 const char* fileStart = p;
1606 // go for the end of the line
1607 while (*p != '\0' && *p != '\n')
1608 p++;
1609 // search back for colon
1610 const char* colon = p;
1611 do {
1612 --colon;
1613 } while (*colon != ':');
1614 file = QString::fromLatin1(fileStart, colon-fileStart);
1615 lineNo = atoi(colon+1)-1;
1616 // skip new-line
1617 if (*p != '\0')
1618 p++;
1619 } else {
1620 // check for "from shared lib"
1621 if (strncmp(p, "from ", 5) == 0) {
1622 p += 5;
1623 // go for the end of the line
1624 while (*p != '\0' && *p != '\n')
1625 p++;
1626 // skip new-line
1627 if (*p != '\0')
1628 p++;
1630 file = "";
1631 lineNo = -1;
1633 // construct the function name (including file info)
1634 if (*p == '\0') {
1635 func = start;
1636 } else {
1637 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1639 s = p;
1642 * Replace \n (and whitespace around it) in func by a blank. We cannot
1643 * use QString::simplifyWhiteSpace() for this because this would also
1644 * simplify space that belongs to a string arguments that gdb sometimes
1645 * prints in the argument lists of the function.
1647 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1648 int nl = 0;
1649 while ((nl = func.find('\n', nl)) >= 0) {
1650 // search back to the beginning of the whitespace
1651 int startWhite = nl;
1652 do {
1653 --startWhite;
1654 } while (isspace(func[startWhite].latin1()));
1655 startWhite++;
1656 // search forward to the end of the whitespace
1657 do {
1658 nl++;
1659 } while (isspace(func[nl].latin1()));
1660 // replace
1661 func.replace(startWhite, nl-startWhite, " ");
1662 /* continue searching for more \n's at this place: */
1663 nl = startWhite+1;
1669 * Parses a stack frame including its frame number
1671 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1672 QString& file, int& lineNo, DbgAddr& address)
1674 // Example:
1675 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1676 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1678 // must start with a hash mark followed by number
1679 // or with "Breakpoint " followed by number and comma
1680 if (s[0] == '#') {
1681 if (!isdigit(s[1]))
1682 return false;
1683 s++; /* skip the hash mark */
1684 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1685 if (!isdigit(s[11]))
1686 return false;
1687 s += 11; /* skip "Breakpoint" */
1688 } else
1689 return false;
1691 // frame number
1692 frameNo = atoi(s);
1693 while (isdigit(*s))
1694 s++;
1695 // space and comma
1696 while (isspace(*s) || *s == ',')
1697 s++;
1698 parseFrameInfo(s, func, file, lineNo, address);
1699 return true;
1702 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1704 QString func, file;
1705 int lineNo, frameNo;
1706 DbgAddr address;
1708 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1709 StackFrame* frm = new StackFrame;
1710 frm->frameNo = frameNo;
1711 frm->fileName = file;
1712 frm->lineNo = lineNo;
1713 frm->address = address;
1714 frm->var = new ExprValue(func, VarTree::NKplain);
1715 stack.append(frm);
1719 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1720 QString& file, int& lineNo, DbgAddr& address)
1722 QString func;
1723 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1727 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1729 // skip first line, which is the headline
1730 const char* p = strchr(output, '\n');
1731 if (p == 0)
1732 return false;
1733 p++;
1734 if (*p == '\0')
1735 return false;
1737 // split up a line
1738 QString location;
1739 QString address;
1740 int hits = 0;
1741 uint ignoreCount = 0;
1742 QString condition;
1743 const char* end;
1744 char* dummy;
1745 while (*p != '\0') {
1746 // get Num
1747 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1748 p = dummy;
1749 // get Type
1750 while (isspace(*p))
1751 p++;
1752 Breakpoint::Type bpType = Breakpoint::breakpoint;
1753 if (strncmp(p, "breakpoint", 10) == 0) {
1754 p += 10;
1755 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1756 bpType = Breakpoint::watchpoint;
1757 p += 13;
1758 } else if (strncmp(p, "watchpoint", 10) == 0) {
1759 bpType = Breakpoint::watchpoint;
1760 p += 10;
1762 while (isspace(*p))
1763 p++;
1764 if (*p == '\0')
1765 break;
1766 // get Disp
1767 char disp = *p++;
1768 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1769 p++;
1770 while (isspace(*p))
1771 p++;
1772 if (*p == '\0')
1773 break;
1774 // get Enb
1775 char enable = *p++;
1776 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1777 p++;
1778 while (isspace(*p))
1779 p++;
1780 if (*p == '\0')
1781 break;
1782 // the address, if present
1783 if (bpType == Breakpoint::breakpoint &&
1784 strncmp(p, "0x", 2) == 0)
1786 const char* start = p;
1787 while (*p != '\0' && !isspace(*p))
1788 p++;
1789 address = QString::fromLatin1(start, p-start);
1790 while (isspace(*p) && *p != '\n')
1791 p++;
1792 if (*p == '\0')
1793 break;
1794 } else {
1795 address = QString();
1797 // remainder is location, hit and ignore count, condition
1798 hits = 0;
1799 ignoreCount = 0;
1800 condition = QString();
1801 end = strchr(p, '\n');
1802 if (end == 0) {
1803 location = p;
1804 p += location.length();
1805 } else {
1806 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1807 p = end+1; /* skip over \n */
1810 // may be continued in next line
1811 while (isspace(*p)) { /* p points to beginning of line */
1812 // skip white space at beginning of line
1813 while (isspace(*p))
1814 p++;
1816 // seek end of line
1817 end = strchr(p, '\n');
1818 if (end == 0)
1819 end = p+strlen(p);
1821 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1822 // extract the hit count
1823 p += 22;
1824 hits = strtol(p, &dummy, 10);
1825 TRACE(QString().sprintf("hit count %d", hits));
1826 } else if (strncmp(p, "stop only if ", 13) == 0) {
1827 // extract condition
1828 p += 13;
1829 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1830 TRACE("condition: "+condition);
1831 } else if (strncmp(p, "ignore next ", 12) == 0) {
1832 // extract ignore count
1833 p += 12;
1834 ignoreCount = strtol(p, &dummy, 10);
1835 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1836 } else {
1837 // indeed a continuation
1838 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1840 p = end;
1841 if (*p != '\0')
1842 p++; /* skip '\n' */
1844 Breakpoint* bp = new Breakpoint;
1845 bp->id = bpNum;
1846 bp->type = bpType;
1847 bp->temporary = disp == 'd';
1848 bp->enabled = enable == 'y';
1849 bp->location = location;
1850 bp->address = address;
1851 bp->hitCount = hits;
1852 bp->ignoreCount = ignoreCount;
1853 bp->condition = condition;
1854 brks.append(bp);
1856 return true;
1859 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1861 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1862 // no threads
1863 return true;
1866 int id;
1867 QString systag;
1868 QString func, file;
1869 int lineNo;
1870 DbgAddr address;
1872 const char* p = output;
1873 while (*p != '\0') {
1874 // seach look for thread id, watching out for the focus indicator
1875 bool hasFocus = false;
1876 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1877 p++;
1878 if (*p == '*') {
1879 hasFocus = true;
1880 p++;
1881 // there follows only whitespace
1883 char* end;
1884 id = strtol(p, &end, 10);
1885 if (p == end) {
1886 // syntax error: no number found; bail out
1887 return true;
1889 p = end;
1891 // skip space
1892 while (isspace(*p))
1893 p++;
1896 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1898 end = strstr(p, " ");
1899 if (end == 0) {
1900 // syntax error; bail out
1901 return true;
1903 systag = QString::fromLatin1(p, end-p);
1904 p = end+2;
1907 * Now follows a standard stack frame. Sometimes, however, gdb
1908 * catches a thread at an instant where it doesn't have a stack.
1910 if (strncmp(p, "[No stack.]", 11) != 0) {
1911 ::parseFrameInfo(p, func, file, lineNo, address);
1912 } else {
1913 func = "[No stack]";
1914 file = QString();
1915 lineNo = -1;
1916 address = QString();
1917 p += 11; /* \n is skipped above */
1920 ThreadInfo* thr = new ThreadInfo;
1921 thr->id = id;
1922 thr->threadName = systag;
1923 thr->hasFocus = hasFocus;
1924 thr->function = func;
1925 thr->fileName = file;
1926 thr->lineNo = lineNo;
1927 thr->address = address;
1928 threads.append(thr);
1930 return true;
1933 static bool parseNewBreakpoint(const char* o, int& id,
1934 QString& file, int& lineNo, QString& address);
1935 static bool parseNewWatchpoint(const char* o, int& id,
1936 QString& expr);
1938 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1939 QString& file, int& lineNo, QString& address)
1941 const char* o = output;
1942 // skip lines of that begin with "(Cannot find"
1943 while (strncmp(o, "(Cannot find", 12) == 0) {
1944 o = strchr(o, '\n');
1945 if (o == 0)
1946 return false;
1947 o++; /* skip newline */
1950 if (strncmp(o, "Breakpoint ", 11) == 0) {
1951 output += 11; /* skip "Breakpoint " */
1952 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1953 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1954 output += 20;
1955 return ::parseNewWatchpoint(output, id, address);
1956 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1957 output += 11;
1958 return ::parseNewWatchpoint(output, id, address);
1960 return false;
1963 static bool parseNewBreakpoint(const char* o, int& id,
1964 QString& file, int& lineNo, QString& address)
1966 // breakpoint id
1967 char* p;
1968 id = strtoul(o, &p, 10);
1969 if (p == o)
1970 return false;
1972 // check for the address
1973 if (strncmp(p, " at 0x", 6) == 0) {
1974 char* start = p+4; /* skip " at ", but not 0x */
1975 p += 6;
1976 while (isxdigit(*p))
1977 ++p;
1978 address = QString::fromLatin1(start, p-start);
1981 // file name
1982 char* fileStart = strstr(p, "file ");
1983 if (fileStart == 0)
1984 return !address.isEmpty(); /* parse error only if there's no address */
1985 fileStart += 5;
1987 // line number
1988 char* numStart = strstr(fileStart, ", line ");
1989 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1990 numStart += 7;
1991 int line = strtoul(numStart, &p, 10);
1992 if (numStart == p)
1993 return false;
1995 file = fileName;
1996 lineNo = line-1; /* zero-based! */
1997 return true;
2000 static bool parseNewWatchpoint(const char* o, int& id,
2001 QString& expr)
2003 // watchpoint id
2004 char* p;
2005 id = strtoul(o, &p, 10);
2006 if (p == o)
2007 return false;
2009 if (strncmp(p, ": ", 2) != 0)
2010 return false;
2011 p += 2;
2013 // all the rest on the line is the expression
2014 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
2015 return true;
2018 void GdbDriver::parseLocals(const char* output, QList<ExprValue>& newVars)
2020 // check for possible error conditions
2021 if (strncmp(output, "No symbol table", 15) == 0)
2023 return;
2026 while (*output != '\0') {
2027 while (isspace(*output))
2028 output++;
2029 if (*output == '\0')
2030 break;
2031 // skip occurrences of "No locals" and "No args"
2032 if (strncmp(output, "No locals", 9) == 0 ||
2033 strncmp(output, "No arguments", 12) == 0)
2035 output = strchr(output, '\n');
2036 if (output == 0) {
2037 break;
2039 continue;
2042 ExprValue* variable = parseVar(output);
2043 if (variable == 0) {
2044 break;
2046 // do not add duplicates
2047 for (ExprValue* o = newVars.first(); o != 0; o = newVars.next()) {
2048 if (o->m_name == variable->m_name) {
2049 delete variable;
2050 goto skipDuplicate;
2053 newVars.append(variable);
2054 skipDuplicate:;
2058 ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2060 ExprValue* var = 0;
2061 // check for error conditions
2062 if (!parseErrorMessage(output, var, wantErrorValue))
2064 // parse the variable
2065 var = parseVar(output);
2067 return var;
2070 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2072 bool isGood = false;
2073 message = QString(output).simplifyWhiteSpace();
2074 if (message.isEmpty()) {
2075 message = i18n("New working directory: ") + m_programWD;
2076 isGood = true;
2078 return isGood;
2081 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2083 message = output;
2085 m_haveCoreFile = false;
2088 * Lines starting with the following do not indicate errors:
2089 * Using host libthread_db
2090 * (no debugging symbols found)
2092 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2093 strncmp(output, "(no debugging symbols found)", 28) == 0)
2095 // this line is good, go to the next one
2096 const char* end = strchr(output, '\n');
2097 if (end == 0)
2098 output += strlen(output);
2099 else
2100 output = end+1;
2104 * If we've parsed all lines, there was no error.
2106 return output[0] == '\0';
2109 bool GdbDriver::parseCoreFile(const char* output)
2111 // if command succeeded, gdb emits a line starting with "#0 "
2112 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2113 return m_haveCoreFile;
2116 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2118 // optionally: "program changed, rereading symbols",
2119 // followed by:
2120 // "Program exited normally"
2121 // "Program terminated with wignal SIGSEGV"
2122 // "Program received signal SIGINT" or other signal
2123 // "Breakpoint..."
2125 // go through the output, line by line, checking what we have
2126 const char* start = output - 1;
2127 uint flags = SFprogramActive;
2128 message = QString();
2129 do {
2130 start++; /* skip '\n' */
2132 if (strncmp(start, "Program ", 8) == 0 ||
2133 strncmp(start, "ptrace: ", 8) == 0) {
2135 * When we receive a signal, the program remains active.
2137 * Special: If we "stopped" in a corefile, the string "Program
2138 * terminated with signal"... is displayed. (Normally, we see
2139 * "Program received signal"... when a signal happens.)
2141 if (strncmp(start, "Program exited", 14) == 0 ||
2142 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2143 strncmp(start, "ptrace: ", 8) == 0)
2145 flags &= ~SFprogramActive;
2148 // set message
2149 const char* endOfMessage = strchr(start, '\n');
2150 if (endOfMessage == 0)
2151 endOfMessage = start + strlen(start);
2152 message = QString::fromLatin1(start, endOfMessage-start);
2153 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2155 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2156 * that it stopped at a temporary breakpoint).
2158 flags |= SFrefreshBreak;
2159 } else if (strstr(start, "re-reading symbols.") != 0) {
2160 flags |= SFrefreshSource;
2163 // next line, please
2164 start = strchr(start, '\n');
2165 } while (start != 0);
2168 * Gdb only notices when new threads have appeared, but not when a
2169 * thread finishes. So we always have to assume that the list of
2170 * threads has changed.
2172 flags |= SFrefreshThreads;
2174 return flags;
2177 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2179 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2180 return;
2182 // parse the table of shared libraries
2184 // strip off head line
2185 output = strchr(output, '\n');
2186 if (output == 0)
2187 return;
2188 output++; /* skip '\n' */
2189 QString shlibName;
2190 while (*output != '\0') {
2191 // format of a line is
2192 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2193 // 3 blocks of non-space followed by space
2194 for (int i = 0; *output != '\0' && i < 3; i++) {
2195 while (*output != '\0' && !isspace(*output)) { /* non-space */
2196 output++;
2198 while (isspace(*output)) { /* space */
2199 output++;
2202 if (*output == '\0')
2203 return;
2204 const char* start = output;
2205 output = strchr(output, '\n');
2206 if (output == 0)
2207 output = start + strlen(start);
2208 shlibName = QString::fromLatin1(start, output-start);
2209 if (*output != '\0')
2210 output++;
2211 shlibs.append(shlibName);
2212 TRACE("found shared lib " + shlibName);
2216 bool GdbDriver::parseFindType(const char* output, QString& type)
2218 if (strncmp(output, "type = ", 7) != 0)
2219 return false;
2222 * Everything else is the type. We strip off any leading "const" and any
2223 * trailing "&" on the grounds that neither affects the decoding of the
2224 * object. We also strip off all white-space from the type.
2226 output += 7;
2227 if (strncmp(output, "const ", 6) == 0)
2228 output += 6;
2229 type = output;
2230 type.replace(QRegExp("\\s+"), "");
2231 if (type.endsWith("&"))
2232 type.truncate(type.length() - 1);
2233 return true;
2236 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2238 if (strncmp(output, "The program has no registers now", 32) == 0) {
2239 return;
2242 QString regName;
2243 QString value;
2245 // parse register values
2246 while (*output != '\0')
2248 // skip space at the start of the line
2249 while (isspace(*output))
2250 output++;
2252 // register name
2253 const char* start = output;
2254 while (*output != '\0' && !isspace(*output))
2255 output++;
2256 if (*output == '\0')
2257 break;
2258 regName = QString::fromLatin1(start, output-start);
2260 // skip space
2261 while (isspace(*output))
2262 output++;
2264 RegisterInfo* reg = new RegisterInfo;
2265 reg->regName = regName;
2268 * If we find a brace now, this is a vector register. We look for
2269 * the closing brace and treat the result as cooked value.
2271 if (*output == '{')
2273 start = output;
2274 skipNested(output, '{', '}');
2275 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2276 // skip space, but not the end of line
2277 while (isspace(*output) && *output != '\n')
2278 output++;
2279 // get rid of the braces at the begining and the end
2280 value.remove(0, 1);
2281 if (value[value.length()-1] == '}') {
2282 value = value.left(value.length()-1);
2284 // gdb 5.3 doesn't print a separate set of raw values
2285 if (*output == '{') {
2286 // another set of vector follows
2287 // what we have so far is the raw value
2288 reg->rawValue = value;
2290 start = output;
2291 skipNested(output, '{', '}');
2292 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2293 } else {
2294 // for gdb 5.3
2295 // find first type that does not have an array, this is the RAW value
2296 const char* end=start;
2297 findEnd(end);
2298 const char* cur=start;
2299 while (cur<end) {
2300 while (*cur != '=' && cur<end)
2301 cur++;
2302 cur++;
2303 while (isspace(*cur) && cur<end)
2304 cur++;
2305 if (isNumberish(*cur)) {
2306 end=cur;
2307 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2308 end++;
2309 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2310 reg->rawValue = rawValue;
2312 if (rawValue.left(2)=="0x") {
2313 // ok we have a raw value, now get it's type
2314 end=cur-1;
2315 while (isspace(*end) || *end=='=') end--;
2316 end++;
2317 cur=end-1;
2318 while (*cur!='{' && *cur!=' ')
2319 cur--;
2320 cur++;
2321 reg->type=QString::fromLatin1(cur, end-cur);
2324 // end while loop
2325 cur=end;
2328 // skip to the end of line
2329 while (*output != '\0' && *output != '\n')
2330 output++;
2331 // get rid of the braces at the begining and the end
2332 value.remove(0, 1);
2333 if (value[value.length()-1] == '}') {
2334 value = value.left(value.length()-1);
2337 reg->cookedValue = value;
2339 else
2341 // the rest of the line is the register value
2342 start = output;
2343 output = strchr(output,'\n');
2344 if (output == 0)
2345 output = start + strlen(start);
2346 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2349 * We split the raw from the cooked values.
2350 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2351 * Here, the cooked value comes first, and the raw value is in
2352 * the second part.
2354 int pos = value.find(" (raw ");
2355 if (pos >= 0)
2357 reg->cookedValue = value.left(pos);
2358 reg->rawValue = value.mid(pos+6);
2359 if (reg->rawValue.right(1) == ")") // remove closing bracket
2360 reg->rawValue.truncate(reg->rawValue.length()-1);
2362 else
2365 * In other cases we split off the first token (separated by
2366 * whitespace). It is the raw value. The remainder of the line
2367 * is the cooked value.
2369 int pos = value.find(' ');
2370 if (pos < 0) {
2371 reg->rawValue = value;
2372 reg->cookedValue = QString();
2373 } else {
2374 reg->rawValue = value.left(pos);
2375 reg->cookedValue = value.mid(pos+1);
2379 if (*output != '\0')
2380 output++; /* skip '\n' */
2382 regs.append(reg);
2386 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2388 // "is at address" or "starts at address"
2389 const char* start = strstr(output, "s at address ");
2390 if (start == 0)
2391 return false;
2393 start += 13;
2394 const char* p = start;
2395 while (*p != '\0' && !isspace(*p))
2396 p++;
2397 addrFrom = QString::fromLatin1(start, p-start);
2399 start = strstr(p, "and ends at ");
2400 if (start == 0) {
2401 addrTo = addrFrom;
2402 return true;
2405 start += 12;
2406 p = start;
2407 while (*p != '\0' && !isspace(*p))
2408 p++;
2409 addrTo = QString::fromLatin1(start, p-start);
2411 return true;
2414 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2416 code.clear();
2418 if (strncmp(output, "Dump of assembler", 17) != 0) {
2419 // error message?
2420 DisassembledCode c;
2421 c.code = output;
2422 code.append(new DisassembledCode(c));
2423 return;
2426 // remove first line
2427 const char* p = strchr(output, '\n');
2428 if (p == 0)
2429 return; /* not a regular output */
2431 p++;
2433 // remove last line
2434 const char* end = strstr(output, "End of assembler");
2435 if (end == 0)
2436 end = p + strlen(p);
2438 DbgAddr address;
2440 // remove function offsets from the lines
2441 while (p != end)
2443 const char* start = p;
2444 // address
2445 while (p != end && !isspace(*p))
2446 p++;
2447 address = QString::fromLatin1(start, p-start);
2449 // function name (enclosed in '<>', followed by ':')
2450 while (p != end && *p != '<')
2451 p++;
2452 if (*p == '<')
2453 skipNestedAngles(p);
2454 if (*p == ':')
2455 p++;
2457 // space until code
2458 while (p != end && isspace(*p))
2459 p++;
2461 // code until end of line
2462 start = p;
2463 while (p != end && *p != '\n')
2464 p++;
2465 if (p != end) /* include '\n' */
2466 p++;
2468 DisassembledCode* c = new DisassembledCode;
2469 c->address = address;
2470 c->code = QString::fromLatin1(start, p-start);
2471 code.append(c);
2475 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2477 if (isErrorExpr(output)) {
2478 // error; strip space
2479 QString msg = output;
2480 return msg.stripWhiteSpace();
2483 const char* p = output; /* save typing */
2484 DbgAddr addr;
2485 QString dump;
2487 // the address
2488 while (*p != 0) {
2489 const char* start = p;
2490 while (*p != '\0' && *p != ':' && !isspace(*p))
2491 p++;
2492 addr = QString::fromLatin1(start, p-start);
2493 if (*p != ':') {
2494 // parse function offset
2495 while (isspace(*p))
2496 p++;
2497 start = p;
2498 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2499 p++;
2500 addr.fnoffs = QString::fromLatin1(start, p-start);
2502 if (*p == ':')
2503 p++;
2504 // skip space; this may skip a new-line char!
2505 while (isspace(*p))
2506 p++;
2507 // everything to the end of the line is the memory dump
2508 const char* end = strchr(p, '\n');
2509 if (end != 0) {
2510 dump = QString::fromLatin1(p, end-p);
2511 p = end+1;
2512 } else {
2513 dump = QString::fromLatin1(p, strlen(p));
2514 p += strlen(p);
2516 MemoryDump* md = new MemoryDump;
2517 md->address = addr;
2518 md->dump = dump;
2519 memdump.append(md);
2522 return QString();
2525 QString GdbDriver::editableValue(VarTree* value)
2527 const char* s = value->value().latin1();
2529 // if the variable is a pointer value that contains a cast,
2530 // remove the cast
2531 if (*s == '(') {
2532 skipNested(s, '(', ')');
2533 // skip space
2534 while (isspace(*s))
2535 ++s;
2538 repeat:
2539 const char* start = s;
2541 if (strncmp(s, "0x", 2) == 0)
2543 s += 2;
2544 while (isxdigit(*s))
2545 ++s;
2548 * What we saw so far might have been a reference. If so, edit the
2549 * referenced value. Otherwise, edit the pointer.
2551 if (*s == ':') {
2552 // a reference
2553 ++s;
2554 goto repeat;
2556 // a pointer
2557 // if it's a pointer to a string, remove the string
2558 const char* end = s;
2559 while (isspace(*s))
2560 ++s;
2561 if (*s == '"') {
2562 // a string
2563 return QString::fromLatin1(start, end-start);
2564 } else {
2565 // other pointer
2566 return QString::fromLatin1(start, strlen(start));
2570 // else leave it unchanged (or stripped of the reference preamble)
2571 return s;
2574 QString GdbDriver::parseSetVariable(const char* output)
2576 // if there is any output, it is an error message
2577 QString msg = output;
2578 return msg.stripWhiteSpace();
2582 #include "gdbdriver.moc"