Get rid of the option not to delete tree item children.
[kdbg.git] / kdbg / gdbdriver.cpp
blob3a850f1fbc0db754f032372e1170e054bdf4af55
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "gdbdriver.h"
7 #include "exprwnd.h"
8 #include <qregexp.h>
9 #include <qstringlist.h>
10 #include <klocale.h> /* i18n */
11 #include <ctype.h>
12 #include <stdlib.h> /* strtol, atoi */
13 #include <string.h> /* strcpy */
15 #include "assert.h"
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #include "mydebug.h"
21 static void skipString(const char*& p);
22 static void skipNested(const char*& s, char opening, char closing);
23 static VarTree* parseVar(const char*& s);
24 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind);
25 static bool parseValue(const char*& s, VarTree* variable);
26 static bool parseNested(const char*& s, VarTree* variable);
27 static bool parseVarSeq(const char*& s, VarTree* variable);
28 static bool parseValueSeq(const char*& s, VarTree* variable);
30 #define PROMPT "(kdbg)"
31 #define PROMPT_LEN 6
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!
38 struct GdbCmdInfo {
39 DbgCommand cmd;
40 const char* fmt; /* format string */
41 enum Args {
42 argNone, argString, argNum,
43 argStringNum, argNumString,
44 argString2, argNum2
45 } argsNeeded;
48 #if 0
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)@"
57 // limit the length
58 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
59 // if unicode data is 0, report a special value
60 ":1==0\n";
61 #endif
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() :
126 DebuggerDriver(),
127 m_gdbMajor(4), m_gdbMinor(16)
129 strcpy(m_prompt, PROMPT);
130 m_promptMinLen = PROMPT_LEN;
131 m_promptLastChar = PROMPT_LAST_CHAR;
133 #ifndef NDEBUG
134 // check command info array
135 char* perc;
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);
146 break;
147 case GdbCmdInfo::argString:
148 perc = strchr(cmds[i].fmt, '%');
149 assert(perc != 0 && perc[1] == 's');
150 assert(strchr(perc+2, '%') == 0);
151 break;
152 case GdbCmdInfo::argNum:
153 perc = strchr(cmds[i].fmt, '%');
154 assert(perc != 0 && perc[1] == 'd');
155 assert(strchr(perc+2, '%') == 0);
156 break;
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);
163 break;
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);
170 break;
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);
177 break;
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);
184 break;
187 assert(strlen(printQStringStructFmt) <= MAX_FMTLEN);
188 #endif
191 GdbDriver::~GdbDriver()
196 QString GdbDriver::driverName() const
198 return "GDB";
201 QString GdbDriver::defaultGdb()
203 return
204 "gdb"
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()) {
212 return defaultGdb();
213 } else {
214 return m_defaultCmd;
218 QStringList GdbDriver::boolOptionList() const
220 // no options
221 return QStringList();
224 bool GdbDriver::startup(QString cmdStr)
226 if (!DebuggerDriver::startup(cmdStr))
227 return false;
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.
235 "set editing off\n"
236 "set confirm off\n"
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
241 * always succeed.
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 */
251 "end\n"
253 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
255 "define kdbg_infolinemain\n"
256 "list\n"
257 "info line\n"
258 "end\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;
268 return true;
271 void GdbDriver::commandFinished(CmdQueueItem* cmd)
273 // command string must be committed
274 if (!cmd->m_committed) {
275 // not commited!
276 TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
277 cmd->m_cmdString)));
278 return;
281 switch (cmd->m_cmd) {
282 case DCinitialize:
283 // get version number from preamble
285 int len;
286 QRegExp GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
287 int offset = GDBVersion.match(m_output, 0, &len);
288 if (offset >= 0) {
289 char* start = m_output + offset + 5; // skip "\nGDB "
290 char* end;
291 m_gdbMajor = strtol(start, &end, 10);
292 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
293 if (start == end) {
294 // nothing was parsed
295 m_gdbMajor = 4;
296 m_gdbMinor = 16;
298 } else {
299 // assume some default version (what would make sense?)
300 m_gdbMajor = 4;
301 m_gdbMinor = 16;
303 // use a feasible core-file command
304 if (m_gdbMajor > 4 || (m_gdbMajor == 4 && m_gdbMinor >= 16)) {
305 cmds[DCcorefile].fmt = "target core %s\n";
306 } else {
307 cmds[DCcorefile].fmt = "core-file %s\n";
310 break;
311 default:;
314 /* ok, the command is ready */
315 emit commandReceived(cmd, m_output);
317 switch (cmd->m_cmd) {
318 case DCcorefile:
319 case DCinfolinemain:
320 case DCframe:
321 case DCattach:
322 case DCrun:
323 case DCcont:
324 case DCstep:
325 case DCstepi:
326 case DCnext:
327 case DCnexti:
328 case DCfinish:
329 case DCuntil:
330 parseMarker();
331 default:;
336 * The --fullname option makes gdb send a special normalized sequence print
337 * each time the program stops and at some other points. The sequence has
338 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
340 void GdbDriver::parseMarker()
342 char* startMarker = strstr(m_output, "\032\032");
343 if (startMarker == 0)
344 return;
346 // extract the marker
347 startMarker += 2;
348 TRACE(QString("found marker: ") + startMarker);
349 char* endMarker = strchr(startMarker, '\n');
350 if (endMarker == 0)
351 return;
353 *endMarker = '\0';
355 // extract filename and line number
356 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
358 int len;
359 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
360 if (lineNoStart >= 0) {
361 int lineNo = atoi(startMarker + lineNoStart+1);
363 // get address
364 const char* addrStart = startMarker + lineNoStart + len - 2;
365 DbgAddr address = QString(addrStart).stripWhiteSpace();
367 // now show the window
368 startMarker[lineNoStart] = '\0'; /* split off file name */
369 emit activateFileLine(startMarker, lineNo-1, address);
375 * Escapes characters that might lead to problems when they appear on gdb's
376 * command line.
378 static void normalizeStringArg(QString& arg)
381 * Remove trailing backslashes. This approach is a little simplistic,
382 * but we know that there is at the moment no case where a trailing
383 * backslash would make sense.
385 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
386 arg = arg.left(arg.length()-1);
391 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
393 assert(cmd >= 0 && cmd < NUM_CMDS);
394 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
396 normalizeStringArg(strArg);
398 if (cmd == DCcd) {
399 // need the working directory when parsing the output
400 m_programWD = strArg;
401 } else if (cmd == DCsetargs) {
402 // attach saved redirection
403 strArg += m_redirect;
406 QString cmdString;
407 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
408 return cmdString;
411 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
413 assert(cmd >= 0 && cmd < NUM_CMDS);
414 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
416 QString cmdString;
417 cmdString.sprintf(cmds[cmd].fmt, intArg);
418 return cmdString;
421 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
423 assert(cmd >= 0 && cmd < NUM_CMDS);
424 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
425 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
426 cmd == DCexamine ||
427 cmd == DCtty);
429 normalizeStringArg(strArg);
431 QString cmdString;
433 if (cmd == DCtty)
436 * intArg specifies which channels should be redirected to
437 * /dev/null. It is a value or'ed together from RDNstdin,
438 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
439 * command.
441 * Note: We rely on that after the DCtty a DCsetargs will follow,
442 * which will ultimately apply the redirection.
444 static const char* const runRedir[8] = {
446 " </dev/null",
447 " >/dev/null",
448 " </dev/null >/dev/null",
449 " 2>/dev/null",
450 " </dev/null 2>/dev/null",
451 " >/dev/null 2>&1",
452 " </dev/null >/dev/null 2>&1"
454 if (strArg.isEmpty())
455 intArg = 7; /* failsafe if no tty */
456 m_redirect = runRedir[intArg & 7];
458 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
461 if (cmd == DCexamine) {
462 // make a format specifier from the intArg
463 static const char size[16] = {
464 '\0', 'b', 'h', 'w', 'g'
466 static const char format[16] = {
467 '\0', 'x', 'd', 'u', 'o', 't',
468 'a', 'c', 'f', 's', 'i'
470 assert(MDTsizemask == 0xf); /* lowest 4 bits */
471 assert(MDTformatmask == 0xf0); /* next 4 bits */
472 int count = 16; /* number of entities to print */
473 char sizeSpec = size[intArg & MDTsizemask];
474 char formatSpec = format[(intArg & MDTformatmask) >> 4];
475 assert(sizeSpec != '\0');
476 assert(formatSpec != '\0');
477 // adjust count such that 16 lines are printed
478 switch (intArg & MDTformatmask) {
479 case MDTstring: case MDTinsn:
480 break; /* no modification needed */
481 default:
482 // all cases drop through:
483 switch (intArg & MDTsizemask) {
484 case MDTbyte:
485 case MDThalfword:
486 count *= 2;
487 case MDTword:
488 count *= 2;
489 case MDTgiantword:
490 count *= 2;
492 break;
494 QString spec;
495 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
497 return makeCmdString(DCexamine, spec, strArg);
500 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
502 // line numbers are zero-based
503 if (cmd == DCuntil || cmd == DCbreakline ||
504 cmd == DCtbreakline || cmd == DCinfoline)
506 intArg++;
508 if (cmd == DCinfoline)
510 // must split off file name part
511 int slash = strArg.findRev('/');
512 if (slash >= 0)
513 strArg = strArg.right(strArg.length()-slash-1);
515 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
517 else
519 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
521 return cmdString;
524 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
526 assert(cmd >= 0 && cmd < NUM_CMDS);
527 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
529 normalizeStringArg(strArg1);
530 normalizeStringArg(strArg2);
532 QString cmdString;
533 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
534 return cmdString;
537 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
539 assert(cmd >= 0 && cmd < NUM_CMDS);
540 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
542 QString cmdString;
543 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
544 return cmdString;
547 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
549 assert(cmd >= 0 && cmd < NUM_CMDS);
550 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
552 if (cmd == DCrun) {
553 m_haveCoreFile = false;
556 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
559 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
560 bool clearLow)
562 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
565 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
566 bool clearLow)
569 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
572 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
573 bool clearLow)
575 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
578 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
579 bool clearLow)
581 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
584 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
585 bool clearLow)
587 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
590 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
592 return queueCmdString(cmd, cmds[cmd].fmt, mode);
595 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
596 QueueMode mode)
598 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
601 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
602 QueueMode mode)
604 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
607 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
608 QueueMode mode)
610 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
613 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
614 QueueMode mode)
616 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
619 void GdbDriver::terminate()
621 kill(SIGTERM);
622 m_state = DSidle;
625 void GdbDriver::detachAndTerminate()
627 kill(SIGINT);
628 flushCommands();
629 executeCmdString(DCinitialize, "detach\nquit\n", true);
632 void GdbDriver::interruptInferior()
634 kill(SIGINT);
635 // remove accidentally queued commands
636 flushHiPriQueue();
639 static bool isErrorExpr(const char* output)
641 return
642 strncmp(output, "Cannot access memory at", 23) == 0 ||
643 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
644 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
645 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
646 strncmp(output, "There is no member or method named", 34) == 0 ||
647 strncmp(output, "A parse error in expression", 27) == 0 ||
648 strncmp(output, "No symbol \"", 11) == 0 ||
649 strncmp(output, "Internal error: ", 16) == 0;
653 * Returns true if the output is an error message. If wantErrorValue is
654 * true, a new VarTree object is created and filled with the error message.
655 * If there are warnings, they are skipped and output points past the warnings
656 * on return (even if there \e are errors).
658 static bool parseErrorMessage(const char*& output,
659 VarTree*& variable, bool wantErrorValue)
661 // skip warnings
662 while (strncmp(output, "warning:", 8) == 0)
664 char* end = strchr(output+8, '\n');
665 if (end == 0)
666 output += strlen(output);
667 else
668 output = end+1;
671 if (isErrorExpr(output))
673 if (wantErrorValue) {
674 // put the error message as value in the variable
675 variable = new VarTree(QString(), VarTree::NKplain);
676 const char* endMsg = strchr(output, '\n');
677 if (endMsg == 0)
678 endMsg = output + strlen(output);
679 variable->m_value = QString::fromLatin1(output, endMsg-output);
680 } else {
681 variable = 0;
683 return true;
685 return false;
688 #if QT_VERSION >= 300
689 union Qt2QChar {
690 short s;
691 struct {
692 uchar row;
693 uchar cell;
694 } qch;
696 #endif
698 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
700 // don't accept the command if it is empty
701 if (cmd == 0 || *cmd == '\0')
702 return;
703 assert(strlen(cmd) <= MAX_FMTLEN);
704 cmds[DCprintQStringStruct].fmt = cmd;
707 VarTree* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
709 VarTree* variable = 0;
712 * Parse off white space. gdb sometimes prints white space first if the
713 * printed array leaded to an error.
715 while (isspace(*output))
716 output++;
718 // special case: empty string (0 repetitions)
719 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
721 variable = new VarTree(QString(), VarTree::NKplain);
722 variable->m_value = "\"\"";
723 return variable;
726 // check for error conditions
727 if (parseErrorMessage(output, variable, wantErrorValue))
728 return variable;
730 // parse the array
732 // find '='
733 const char* p = output;
734 p = strchr(p, '=');
735 if (p == 0) {
736 goto error;
738 // skip white space
739 do {
740 p++;
741 } while (isspace(*p));
743 if (*p == '{')
745 // this is the real data
746 p++; /* skip '{' */
748 // parse the array
749 QString result;
750 QString repeatCount;
751 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
753 * A matrix for separators between the individual "things"
754 * that are added to the string. The first index is a bool,
755 * the second index is from the enum above.
757 static const char* separator[2][3] = {
758 { "\"", 0, ", \"" }, /* normal char is added */
759 { "'", "\", '", ", '" } /* repeated char is added */
762 while (isdigit(*p)) {
763 // parse a number
764 char* end;
765 unsigned short value = (unsigned short) strtoul(p, &end, 0);
766 if (end == p)
767 goto error; /* huh? no valid digits */
768 // skip separator and search for a repeat count
769 p = end;
770 while (isspace(*p) || *p == ',')
771 p++;
772 bool repeats = strncmp(p, "<repeats ", 9) == 0;
773 if (repeats) {
774 const char* start = p;
775 p = strchr(p+9, '>'); /* search end and advance */
776 if (p == 0)
777 goto error;
778 p++; /* skip '>' */
779 repeatCount = QString::fromLatin1(start, p-start);
780 while (isspace(*p) || *p == ',')
781 p++;
783 // p is now at the next char (or the end)
785 // interpret the value as a QChar
786 // TODO: make cross-architecture compatible
787 QChar ch;
788 if (qt3like) {
789 ch = QChar(value);
790 } else {
791 #if QT_VERSION < 300
792 (unsigned short&)ch = value;
793 #else
794 Qt2QChar c;
795 c.s = value;
796 ch.setRow(c.qch.row);
797 ch.setCell(c.qch.cell);
798 #endif
801 // escape a few frequently used characters
802 char escapeCode = '\0';
803 switch (ch.latin1()) {
804 case '\n': escapeCode = 'n'; break;
805 case '\r': escapeCode = 'r'; break;
806 case '\t': escapeCode = 't'; break;
807 case '\b': escapeCode = 'b'; break;
808 case '\"': escapeCode = '\"'; break;
809 case '\\': escapeCode = '\\'; break;
810 case '\0': if (value == 0) { escapeCode = '0'; } break;
813 // add separator
814 result += separator[repeats][lastThing];
815 // add char
816 if (escapeCode != '\0') {
817 result += '\\';
818 ch = escapeCode;
820 result += ch;
822 // fixup repeat count and lastThing
823 if (repeats) {
824 result += "' ";
825 result += repeatCount;
826 lastThing = wasRepeat;
827 } else {
828 lastThing = wasChar;
831 if (*p != '}')
832 goto error;
834 // closing quote
835 if (lastThing == wasChar)
836 result += "\"";
838 // assign the value
839 variable = new VarTree(QString(), VarTree::NKplain);
840 variable->m_value = result;
842 else if (strncmp(p, "true", 4) == 0)
844 variable = new VarTree(QString(), VarTree::NKplain);
845 variable->m_value = "QString::null";
847 else if (strncmp(p, "false", 5) == 0)
849 variable = new VarTree(QString(), VarTree::NKplain);
850 variable->m_value = "(null)";
852 else
853 goto error;
854 return variable;
856 error:
857 if (wantErrorValue) {
858 variable = new VarTree(QString(), VarTree::NKplain);
859 variable->m_value = "internal parse error";
861 return variable;
864 static VarTree* parseVar(const char*& s)
866 const char* p = s;
868 // skip whitespace
869 while (isspace(*p))
870 p++;
872 QString name;
873 VarTree::NameKind kind;
874 if (!parseName(p, name, kind)) {
875 return 0;
878 // go for '='
879 while (isspace(*p))
880 p++;
881 if (*p != '=') {
882 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
883 return 0;
885 // skip the '=' and more whitespace
886 p++;
887 while (isspace(*p))
888 p++;
890 VarTree* variable = new VarTree(name, kind);
892 if (!parseValue(p, variable)) {
893 delete variable;
894 return 0;
896 s = p;
897 return variable;
900 static void skipNested(const char*& s, char opening, char closing)
902 const char* p = s;
904 // parse a nested type
905 int nest = 1;
906 p++;
908 * Search for next matching `closing' char, skipping nested pairs of
909 * `opening' and `closing'.
911 while (*p && nest > 0) {
912 if (*p == opening) {
913 nest++;
914 } else if (*p == closing) {
915 nest--;
917 p++;
919 if (nest != 0) {
920 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
922 s = p;
926 * This function skips text that is delimited by nested angle bracktes, '<>'.
927 * A complication arises because the delimited text can contain the names of
928 * operator<<, operator>>, operator<, and operator>, which have to be treated
929 * specially so that they do not count towards the nesting of '<>'.
930 * This function assumes that the delimited text does not contain strings.
932 static void skipNestedAngles(const char*& s)
934 const char* p = s;
936 int nest = 1;
937 p++; // skip the initial '<'
938 while (*p && nest > 0)
940 // Below we can check for p-s >= 9 instead of 8 because
941 // *s is '<' and cannot be part of "operator".
942 if (*p == '<')
944 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
945 if (p[1] == '<')
946 p++;
947 } else {
948 nest++;
951 else if (*p == '>')
953 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
954 if (p[1] == '>')
955 p++;
956 } else {
957 nest--;
960 p++;
962 if (nest != 0) {
963 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
965 s = p;
969 * Find the end of line that is not inside braces
971 static void findEnd(const char*& s)
973 const char* p = s;
974 while (*p && *p!='\n') {
975 while (*p && *p!='\n' && *p!='{')
976 p++;
977 if (*p=='{') {
978 p++;
979 skipNested(p, '{', '}'); p--;
982 s = p;
985 static bool isNumberish(const char ch)
987 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
990 void skipString(const char*& p)
992 moreStrings:
993 // opening quote
994 char quote = *p++;
995 while (*p != quote) {
996 if (*p == '\\') {
997 // skip escaped character
998 // no special treatment for octal values necessary
999 p++;
1001 // simply return if no more characters
1002 if (*p == '\0')
1003 return;
1004 p++;
1006 // closing quote
1007 p++;
1009 * Strings can consist of several parts, some of which contain repeated
1010 * characters.
1012 if (quote == '\'') {
1013 // look ahaead for <repeats 123 times>
1014 const char* q = p+1;
1015 while (isspace(*q))
1016 q++;
1017 if (strncmp(q, "<repeats ", 9) == 0) {
1018 p = q+9;
1019 while (*p != '\0' && *p != '>')
1020 p++;
1021 if (*p != '\0') {
1022 p++; /* skip the '>' */
1026 // is the string continued?
1027 if (*p == ',') {
1028 // look ahead for another quote
1029 const char* q = p+1;
1030 while (isspace(*q))
1031 q++;
1032 if (*q == '"' || *q == '\'') {
1033 // yes!
1034 p = q;
1035 goto moreStrings;
1038 /* very long strings are followed by `...' */
1039 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1040 p += 3;
1044 static void skipNestedWithString(const char*& s, char opening, char closing)
1046 const char* p = s;
1048 // parse a nested expression
1049 int nest = 1;
1050 p++;
1052 * Search for next matching `closing' char, skipping nested pairs of
1053 * `opening' and `closing' as well as strings.
1055 while (*p && nest > 0) {
1056 if (*p == opening) {
1057 nest++;
1058 } else if (*p == closing) {
1059 nest--;
1060 } else if (*p == '\'' || *p == '\"') {
1061 skipString(p);
1062 continue;
1064 p++;
1066 if (nest > 0) {
1067 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1069 s = p;
1072 inline void skipName(const char*& p)
1074 // allow : (for enumeration values) and $ and . (for _vtbl.)
1075 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1076 p++;
1079 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1081 kind = VarTree::NKplain;
1083 const char* p = s;
1084 // examples of names:
1085 // name
1086 // <Object>
1087 // <string<a,b<c>,7> >
1089 if (*p == '<') {
1090 skipNestedAngles(p);
1091 name = QString::fromLatin1(s, p - s);
1092 kind = VarTree::NKtype;
1094 else
1096 // name, which might be "static"; allow dot for "_vtbl."
1097 skipName(p);
1098 if (p == s) {
1099 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1100 return false;
1102 int len = p - s;
1103 if (len == 6 && strncmp(s, "static", 6) == 0) {
1104 kind = VarTree::NKstatic;
1106 // its a static variable, name comes now
1107 while (isspace(*p))
1108 p++;
1109 s = p;
1110 skipName(p);
1111 if (p == s) {
1112 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1113 return false;
1115 len = p - s;
1117 name = QString::fromLatin1(s, len);
1119 // return the new position
1120 s = p;
1121 return true;
1124 static bool parseValue(const char*& s, VarTree* variable)
1126 variable->m_value = "";
1128 repeat:
1129 if (*s == '{') {
1130 // Sometimes we find the following output:
1131 // {<text variable, no debug info>} 0x40012000 <access>
1132 // {<data variable, no debug info>}
1133 // {<variable (not text or data), no debug info>}
1134 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1135 strncmp(s, "{<data variable, ", 17) == 0 ||
1136 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1138 const char* start = s;
1139 skipNested(s, '{', '}');
1140 variable->m_value = QString::fromLatin1(start, s-start);
1141 variable->m_value += ' '; // add only a single space
1142 while (isspace(*s))
1143 s++;
1144 goto repeat;
1146 else
1148 s++;
1149 if (!parseNested(s, variable)) {
1150 return false;
1152 // must be the closing brace
1153 if (*s != '}') {
1154 TRACE("parse error: missing } of " + variable->getText());
1155 return false;
1157 s++;
1158 // final white space
1159 while (isspace(*s))
1160 s++;
1162 } else {
1163 // examples of leaf values (cannot be the empty string):
1164 // 123
1165 // -123
1166 // 23.575e+37
1167 // 0x32a45
1168 // @0x012ab4
1169 // (DwContentType&) @0x8123456: {...}
1170 // 0x32a45 "text"
1171 // 10 '\n'
1172 // <optimized out>
1173 // 0x823abc <Array<int> virtual table>
1174 // (void (*)()) 0x8048480 <f(E *, char)>
1175 // (E *) 0xbffff450
1176 // red
1177 // &parseP (HTMLClueV *, char *)
1178 // Variable "x" is not available.
1179 // The value of variable 'x' is distributed...
1180 // -nan(0xfffff081defa0)
1182 const char*p = s;
1184 // check for type
1185 QString type;
1186 if (*p == '(') {
1187 skipNested(p, '(', ')');
1189 while (isspace(*p))
1190 p++;
1191 variable->m_value = QString::fromLatin1(s, p - s);
1194 bool reference = false;
1195 if (*p == '@') {
1196 // skip reference marker
1197 p++;
1198 reference = true;
1200 const char* start = p;
1201 if (*p == '-')
1202 p++;
1204 // some values consist of more than one token
1205 bool checkMultiPart = false;
1207 if (p[0] == '0' && p[1] == 'x') {
1208 // parse hex number
1209 p += 2;
1210 while (isxdigit(*p))
1211 p++;
1214 * Assume this is a pointer, but only if it's not a reference, since
1215 * references can't be expanded.
1217 if (!reference) {
1218 variable->m_varKind = VarTree::VKpointer;
1219 } else {
1221 * References are followed by a colon, in which case we'll
1222 * find the value following the reference address.
1224 if (*p == ':') {
1225 p++;
1226 } else {
1227 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1228 reference = false;
1231 checkMultiPart = true;
1232 } else if (isdigit(*p)) {
1233 // parse decimal number, possibly a float
1234 while (isdigit(*p))
1235 p++;
1236 if (*p == '.') { /* TODO: obey i18n? */
1237 // In long arrays an integer may be followed by '...'.
1238 // We test for this situation and don't gobble the '...'.
1239 if (p[1] != '.' || p[0] != '.') {
1240 // fractional part
1241 p++;
1242 while (isdigit(*p))
1243 p++;
1246 if (*p == 'e' || *p == 'E') {
1247 p++;
1248 // exponent
1249 if (*p == '-' || *p == '+')
1250 p++;
1251 while (isdigit(*p))
1252 p++;
1255 // for char variables there is the char, eg. 10 '\n'
1256 checkMultiPart = true;
1257 } else if (*p == '<') {
1258 // e.g. <optimized out>
1259 skipNestedAngles(p);
1260 } else if (*p == '"' || *p == '\'') {
1261 // character may have multipart: '\000' <repeats 11 times>
1262 checkMultiPart = *p == '\'';
1263 // found a string
1264 skipString(p);
1265 } else if (*p == '&') {
1266 // function pointer
1267 p++;
1268 skipName(p);
1269 while (isspace(*p)) {
1270 p++;
1272 if (*p == '(') {
1273 skipNested(p, '(', ')');
1275 } else if (strncmp(p, "Variable \"", 10) == 0) {
1276 // Variable "x" is not available.
1277 p += 10; // skip to "
1278 skipName(p);
1279 if (strncmp(p, "\" is not available.", 19) == 0) {
1280 p += 19;
1282 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1283 p += 23;
1284 skipName(p);
1285 const char* e = strchr(p, '.');
1286 if (e == 0) {
1287 p += strlen(p);
1288 } else {
1289 p = e+1;
1291 } else {
1292 // must be an enumeration value
1293 skipName(p);
1294 // hmm, not necessarily: nan (floating point Not a Number)
1295 // is followed by a number in ()
1296 if (*p == '(')
1297 skipNested(p, '(', ')');
1299 variable->m_value += QString::fromLatin1(start, p - start);
1301 // remove line breaks from the value; this is ok since
1302 // string values never contain a literal line break
1303 variable->m_value.replace('\n', ' ');
1305 if (checkMultiPart) {
1306 // white space
1307 while (isspace(*p))
1308 p++;
1309 // may be followed by a string or <...>
1310 start = p;
1312 if (*p == '"' || *p == '\'') {
1313 skipString(p);
1314 } else if (*p == '<') {
1315 // if this value is part of an array, it might be followed
1316 // by <repeats 15 times>, which we don't skip here
1317 if (strncmp(p, "<repeats ", 9) != 0)
1318 skipNestedAngles(p);
1320 if (p != start) {
1321 // there is always a blank before the string,
1322 // which we will include in the final string value
1323 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1324 // if this was a pointer, reset that flag since we
1325 // now got the value
1326 variable->m_varKind = VarTree::VKsimple;
1330 if (variable->m_value.length() == 0) {
1331 TRACE("parse error: no value for " + variable->getText());
1332 return false;
1335 // final white space
1336 while (isspace(*p))
1337 p++;
1338 s = p;
1341 * If this was a reference, the value follows. It might even be a
1342 * composite variable!
1344 if (reference) {
1345 goto repeat;
1348 if (variable->m_varKind == VarTree::VKpointer) {
1349 variable->setDelayedExpanding(true);
1353 return true;
1356 static bool parseNested(const char*& s, VarTree* variable)
1358 // could be a structure or an array
1359 while (isspace(*s))
1360 s++;
1362 const char* p = s;
1363 bool isStruct = false;
1365 * If there is a name followed by an = or an < -- which starts a type
1366 * name -- or "static", it is a structure
1368 if (*p == '<' || *p == '}') {
1369 isStruct = true;
1370 } else if (strncmp(p, "static ", 7) == 0) {
1371 isStruct = true;
1372 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1373 // look ahead for a comma after the name
1374 skipName(p);
1375 while (isspace(*p))
1376 p++;
1377 if (*p == '=') {
1378 isStruct = true;
1380 p = s; /* rescan the name */
1382 if (isStruct) {
1383 if (!parseVarSeq(p, variable)) {
1384 return false;
1386 variable->m_varKind = VarTree::VKstruct;
1387 } else {
1388 if (!parseValueSeq(p, variable)) {
1389 return false;
1391 variable->m_varKind = VarTree::VKarray;
1393 s = p;
1394 return true;
1397 static bool parseVarSeq(const char*& s, VarTree* variable)
1399 // parse a comma-separated sequence of variables
1400 VarTree* var = variable; /* var != 0 to indicate success if empty seq */
1401 for (;;) {
1402 if (*s == '}')
1403 break;
1404 if (strncmp(s, "<No data fields>}", 17) == 0)
1406 // no member variables, so break out immediately
1407 s += 16; /* go to the closing brace */
1408 break;
1410 var = parseVar(s);
1411 if (var == 0)
1412 break; /* syntax error */
1413 variable->appendChild(var);
1414 if (*s != ',')
1415 break;
1416 // skip the comma and whitespace
1417 s++;
1418 while (isspace(*s))
1419 s++;
1421 return var != 0;
1424 static bool parseValueSeq(const char*& s, VarTree* variable)
1426 // parse a comma-separated sequence of variables
1427 int index = 0;
1428 bool good;
1429 for (;;) {
1430 QString name;
1431 name.sprintf("[%d]", index);
1432 VarTree* var = new VarTree(name, VarTree::NKplain);
1433 good = parseValue(s, var);
1434 if (!good) {
1435 delete var;
1436 return false;
1438 // a value may be followed by "<repeats 45 times>"
1439 if (strncmp(s, "<repeats ", 9) == 0) {
1440 s += 9;
1441 char* end;
1442 int l = strtol(s, &end, 10);
1443 if (end == s || strncmp(end, " times>", 7) != 0) {
1444 // should not happen
1445 delete var;
1446 return false;
1448 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1449 // replace name and advance index
1450 name.sprintf("[%d .. %d]", index, index+l-1);
1451 var->setText(name);
1452 index += l;
1453 // skip " times>" and space
1454 s = end+7;
1455 // possible final space
1456 while (isspace(*s))
1457 s++;
1458 } else {
1459 index++;
1461 variable->appendChild(var);
1462 // long arrays may be terminated by '...'
1463 if (strncmp(s, "...", 3) == 0) {
1464 s += 3;
1465 VarTree* var = new VarTree("...", VarTree::NKplain);
1466 var->m_value = i18n("<additional entries of the array suppressed>");
1467 variable->appendChild(var);
1468 break;
1470 if (*s != ',') {
1471 break;
1473 // skip the comma and whitespace
1474 s++;
1475 while (isspace(*s))
1476 s++;
1477 // sometimes there is a closing brace after a comma
1478 // if (*s == '}')
1479 // break;
1481 return true;
1485 * Parses a stack frame.
1487 static void parseFrameInfo(const char*& s, QString& func,
1488 QString& file, int& lineNo, DbgAddr& address)
1490 const char* p = s;
1492 // next may be a hexadecimal address
1493 if (*p == '0') {
1494 const char* start = p;
1495 p++;
1496 if (*p == 'x')
1497 p++;
1498 while (isxdigit(*p))
1499 p++;
1500 address = QString::fromLatin1(start, p-start);
1501 if (strncmp(p, " in ", 4) == 0)
1502 p += 4;
1503 } else {
1504 address = DbgAddr();
1506 const char* start = p;
1507 // check for special signal handler frame
1508 if (strncmp(p, "<signal handler called>", 23) == 0) {
1509 func = QString::fromLatin1(start, 23);
1510 file = QString();
1511 lineNo = -1;
1512 s = p+23;
1513 if (*s == '\n')
1514 s++;
1515 return;
1519 * Skip the function name. It is terminated by a left parenthesis
1520 * which does not delimit "(anonymous namespace)" and which is
1521 * outside the angle brackets <> of template parameter lists.
1523 while (*p != '\0')
1525 if (*p == '<') {
1526 // check for operator<< and operator<
1527 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1529 p++;
1530 if (*p == '<')
1531 p++;
1533 else
1535 // skip template parameter list
1536 skipNestedAngles(p);
1538 } else if (*p == '(') {
1539 if (strncmp(p, "(anonymous namespace)", 21) != 0)
1540 break; // parameter list found
1541 p += 21;
1542 } else {
1543 p++;
1547 if (*p == '\0') {
1548 func = start;
1549 file = QString();
1550 lineNo = -1;
1551 s = p;
1552 return;
1555 * Skip parameters. But notice that for complicated conversion
1556 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1557 * pointer to function) as well as operator()(...) we have to skip
1558 * additional pairs of parentheses. Furthermore, recent gdbs write the
1559 * demangled name followed by the arguments in a pair of parentheses,
1560 * where the demangled name can end in "const".
1562 do {
1563 skipNestedWithString(p, '(', ')');
1564 while (isspace(*p))
1565 p++;
1566 // skip "const"
1567 if (strncmp(p, "const", 5) == 0) {
1568 p += 5;
1569 while (isspace(*p))
1570 p++;
1572 } while (*p == '(');
1574 // check for file position
1575 if (strncmp(p, "at ", 3) == 0) {
1576 p += 3;
1577 const char* fileStart = p;
1578 // go for the end of the line
1579 while (*p != '\0' && *p != '\n')
1580 p++;
1581 // search back for colon
1582 const char* colon = p;
1583 do {
1584 --colon;
1585 } while (*colon != ':');
1586 file = QString::fromLatin1(fileStart, colon-fileStart);
1587 lineNo = atoi(colon+1)-1;
1588 // skip new-line
1589 if (*p != '\0')
1590 p++;
1591 } else {
1592 // check for "from shared lib"
1593 if (strncmp(p, "from ", 5) == 0) {
1594 p += 5;
1595 // go for the end of the line
1596 while (*p != '\0' && *p != '\n')
1597 p++;
1598 // skip new-line
1599 if (*p != '\0')
1600 p++;
1602 file = "";
1603 lineNo = -1;
1605 // construct the function name (including file info)
1606 if (*p == '\0') {
1607 func = start;
1608 } else {
1609 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1611 s = p;
1614 * Replace \n (and whitespace around it) in func by a blank. We cannot
1615 * use QString::simplifyWhiteSpace() for this because this would also
1616 * simplify space that belongs to a string arguments that gdb sometimes
1617 * prints in the argument lists of the function.
1619 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1620 int nl = 0;
1621 while ((nl = func.find('\n', nl)) >= 0) {
1622 // search back to the beginning of the whitespace
1623 int startWhite = nl;
1624 do {
1625 --startWhite;
1626 } while (isspace(func[startWhite].latin1()));
1627 startWhite++;
1628 // search forward to the end of the whitespace
1629 do {
1630 nl++;
1631 } while (isspace(func[nl].latin1()));
1632 // replace
1633 func.replace(startWhite, nl-startWhite, " ");
1634 /* continue searching for more \n's at this place: */
1635 nl = startWhite+1;
1641 * Parses a stack frame including its frame number
1643 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1644 QString& file, int& lineNo, DbgAddr& address)
1646 // Example:
1647 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1648 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1650 // must start with a hash mark followed by number
1651 // or with "Breakpoint " followed by number and comma
1652 if (s[0] == '#') {
1653 if (!isdigit(s[1]))
1654 return false;
1655 s++; /* skip the hash mark */
1656 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1657 if (!isdigit(s[11]))
1658 return false;
1659 s += 11; /* skip "Breakpoint" */
1660 } else
1661 return false;
1663 // frame number
1664 frameNo = atoi(s);
1665 while (isdigit(*s))
1666 s++;
1667 // space and comma
1668 while (isspace(*s) || *s == ',')
1669 s++;
1670 parseFrameInfo(s, func, file, lineNo, address);
1671 return true;
1674 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1676 QString func, file;
1677 int lineNo, frameNo;
1678 DbgAddr address;
1680 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1681 StackFrame* frm = new StackFrame;
1682 frm->frameNo = frameNo;
1683 frm->fileName = file;
1684 frm->lineNo = lineNo;
1685 frm->address = address;
1686 frm->var = new VarTree(func, VarTree::NKplain);
1687 stack.append(frm);
1691 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1692 QString& file, int& lineNo, DbgAddr& address)
1694 QString func;
1695 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1699 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1701 // skip first line, which is the headline
1702 const char* p = strchr(output, '\n');
1703 if (p == 0)
1704 return false;
1705 p++;
1706 if (*p == '\0')
1707 return false;
1709 // split up a line
1710 QString location;
1711 QString address;
1712 int hits = 0;
1713 uint ignoreCount = 0;
1714 QString condition;
1715 const char* end;
1716 char* dummy;
1717 while (*p != '\0') {
1718 // get Num
1719 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1720 p = dummy;
1721 // get Type
1722 while (isspace(*p))
1723 p++;
1724 Breakpoint::Type bpType = Breakpoint::breakpoint;
1725 if (strncmp(p, "breakpoint", 10) == 0) {
1726 p += 10;
1727 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1728 bpType = Breakpoint::watchpoint;
1729 p += 13;
1730 } else if (strncmp(p, "watchpoint", 10) == 0) {
1731 bpType = Breakpoint::watchpoint;
1732 p += 10;
1734 while (isspace(*p))
1735 p++;
1736 if (*p == '\0')
1737 break;
1738 // get Disp
1739 char disp = *p++;
1740 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1741 p++;
1742 while (isspace(*p))
1743 p++;
1744 if (*p == '\0')
1745 break;
1746 // get Enb
1747 char enable = *p++;
1748 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1749 p++;
1750 while (isspace(*p))
1751 p++;
1752 if (*p == '\0')
1753 break;
1754 // the address, if present
1755 if (bpType == Breakpoint::breakpoint &&
1756 strncmp(p, "0x", 2) == 0)
1758 const char* start = p;
1759 while (*p != '\0' && !isspace(*p))
1760 p++;
1761 address = QString::fromLatin1(start, p-start);
1762 while (isspace(*p) && *p != '\n')
1763 p++;
1764 if (*p == '\0')
1765 break;
1766 } else {
1767 address = QString();
1769 // remainder is location, hit and ignore count, condition
1770 hits = 0;
1771 ignoreCount = 0;
1772 condition = QString();
1773 end = strchr(p, '\n');
1774 if (end == 0) {
1775 location = p;
1776 p += location.length();
1777 } else {
1778 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1779 p = end+1; /* skip over \n */
1782 // may be continued in next line
1783 while (isspace(*p)) { /* p points to beginning of line */
1784 // skip white space at beginning of line
1785 while (isspace(*p))
1786 p++;
1788 // seek end of line
1789 end = strchr(p, '\n');
1790 if (end == 0)
1791 end = p+strlen(p);
1793 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1794 // extract the hit count
1795 p += 22;
1796 hits = strtol(p, &dummy, 10);
1797 TRACE(QString().sprintf("hit count %d", hits));
1798 } else if (strncmp(p, "stop only if ", 13) == 0) {
1799 // extract condition
1800 p += 13;
1801 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1802 TRACE("condition: "+condition);
1803 } else if (strncmp(p, "ignore next ", 12) == 0) {
1804 // extract ignore count
1805 p += 12;
1806 ignoreCount = strtol(p, &dummy, 10);
1807 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1808 } else {
1809 // indeed a continuation
1810 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1812 p = end;
1813 if (*p != '\0')
1814 p++; /* skip '\n' */
1816 Breakpoint* bp = new Breakpoint;
1817 bp->id = bpNum;
1818 bp->type = bpType;
1819 bp->temporary = disp == 'd';
1820 bp->enabled = enable == 'y';
1821 bp->location = location;
1822 bp->address = address;
1823 bp->hitCount = hits;
1824 bp->ignoreCount = ignoreCount;
1825 bp->condition = condition;
1826 brks.append(bp);
1828 return true;
1831 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1833 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1834 // no threads
1835 return true;
1838 int id;
1839 QString systag;
1840 QString func, file;
1841 int lineNo;
1842 DbgAddr address;
1844 const char* p = output;
1845 while (*p != '\0') {
1846 // seach look for thread id, watching out for the focus indicator
1847 bool hasFocus = false;
1848 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1849 p++;
1850 if (*p == '*') {
1851 hasFocus = true;
1852 p++;
1853 // there follows only whitespace
1855 char* end;
1856 id = strtol(p, &end, 10);
1857 if (p == end) {
1858 // syntax error: no number found; bail out
1859 return true;
1861 p = end;
1863 // skip space
1864 while (isspace(*p))
1865 p++;
1868 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1870 end = strstr(p, " ");
1871 if (end == 0) {
1872 // syntax error; bail out
1873 return true;
1875 systag = QString::fromLatin1(p, end-p);
1876 p = end+2;
1879 * Now follows a standard stack frame. Sometimes, however, gdb
1880 * catches a thread at an instant where it doesn't have a stack.
1882 if (strncmp(p, "[No stack.]", 11) != 0) {
1883 ::parseFrameInfo(p, func, file, lineNo, address);
1884 } else {
1885 func = "[No stack]";
1886 file = QString();
1887 lineNo = -1;
1888 address = QString();
1889 p += 11; /* \n is skipped above */
1892 ThreadInfo* thr = new ThreadInfo;
1893 thr->id = id;
1894 thr->threadName = systag;
1895 thr->hasFocus = hasFocus;
1896 thr->function = func;
1897 thr->fileName = file;
1898 thr->lineNo = lineNo;
1899 thr->address = address;
1900 threads.append(thr);
1902 return true;
1905 static bool parseNewBreakpoint(const char* o, int& id,
1906 QString& file, int& lineNo, QString& address);
1907 static bool parseNewWatchpoint(const char* o, int& id,
1908 QString& expr);
1910 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1911 QString& file, int& lineNo, QString& address)
1913 const char* o = output;
1914 // skip lines of that begin with "(Cannot find"
1915 while (strncmp(o, "(Cannot find", 12) == 0) {
1916 o = strchr(o, '\n');
1917 if (o == 0)
1918 return false;
1919 o++; /* skip newline */
1922 if (strncmp(o, "Breakpoint ", 11) == 0) {
1923 output += 11; /* skip "Breakpoint " */
1924 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1925 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1926 output += 20;
1927 return ::parseNewWatchpoint(output, id, address);
1928 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1929 output += 11;
1930 return ::parseNewWatchpoint(output, id, address);
1932 return false;
1935 static bool parseNewBreakpoint(const char* o, int& id,
1936 QString& file, int& lineNo, QString& address)
1938 // breakpoint id
1939 char* p;
1940 id = strtoul(o, &p, 10);
1941 if (p == o)
1942 return false;
1944 // check for the address
1945 if (strncmp(p, " at 0x", 6) == 0) {
1946 char* start = p+4; /* skip " at ", but not 0x */
1947 p += 6;
1948 while (isxdigit(*p))
1949 ++p;
1950 address = QString::fromLatin1(start, p-start);
1953 // file name
1954 char* fileStart = strstr(p, "file ");
1955 if (fileStart == 0)
1956 return !address.isEmpty(); /* parse error only if there's no address */
1957 fileStart += 5;
1959 // line number
1960 char* numStart = strstr(fileStart, ", line ");
1961 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1962 numStart += 7;
1963 int line = strtoul(numStart, &p, 10);
1964 if (numStart == p)
1965 return false;
1967 file = fileName;
1968 lineNo = line-1; /* zero-based! */
1969 return true;
1972 static bool parseNewWatchpoint(const char* o, int& id,
1973 QString& expr)
1975 // watchpoint id
1976 char* p;
1977 id = strtoul(o, &p, 10);
1978 if (p == o)
1979 return false;
1981 if (strncmp(p, ": ", 2) != 0)
1982 return false;
1983 p += 2;
1985 // all the rest on the line is the expression
1986 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
1987 return true;
1990 void GdbDriver::parseLocals(const char* output, QList<VarTree>& newVars)
1992 // check for possible error conditions
1993 if (strncmp(output, "No symbol table", 15) == 0)
1995 return;
1998 while (*output != '\0') {
1999 while (isspace(*output))
2000 output++;
2001 if (*output == '\0')
2002 break;
2003 // skip occurrences of "No locals" and "No args"
2004 if (strncmp(output, "No locals", 9) == 0 ||
2005 strncmp(output, "No arguments", 12) == 0)
2007 output = strchr(output, '\n');
2008 if (output == 0) {
2009 break;
2011 continue;
2014 VarTree* variable = parseVar(output);
2015 if (variable == 0) {
2016 break;
2018 // do not add duplicates
2019 for (VarTree* o = newVars.first(); o != 0; o = newVars.next()) {
2020 if (o->getText() == variable->getText()) {
2021 delete variable;
2022 goto skipDuplicate;
2025 if ((variable->m_value.startsWith("(const wchar_t *)") ||
2026 variable->m_value.startsWith("(wchar_t *)")) &&
2027 !variable->m_value.endsWith(" 0x0"))
2029 variable->m_varKind = VarTree::VKsimple;
2030 variable->m_partialValue = variable->m_value;
2031 variable->m_value = QString::null;
2032 CmdQueueItem *c = executeCmd(DCprintWChar, variable->getText());
2033 c->m_expr = new VarTree(*variable);
2035 newVars.append(variable);
2036 skipDuplicate:;
2040 bool GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue, VarTree*& var)
2042 // check for error conditions
2043 if (parseErrorMessage(output, var, wantErrorValue))
2045 return false;
2046 } else {
2047 // parse the variable
2048 var = parseVar(output);
2049 return true;
2053 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2055 bool isGood = false;
2056 message = QString(output).simplifyWhiteSpace();
2057 if (message.isEmpty()) {
2058 message = i18n("New working directory: ") + m_programWD;
2059 isGood = true;
2061 return isGood;
2064 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2066 message = output;
2068 m_haveCoreFile = false;
2071 * Lines starting with the following do not indicate errors:
2072 * Using host libthread_db
2073 * (no debugging symbols found)
2075 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2076 strncmp(output, "(no debugging symbols found)", 28) == 0)
2078 // this line is good, go to the next one
2079 const char* end = strchr(output, '\n');
2080 if (end == 0)
2081 output += strlen(output);
2082 else
2083 output = end+1;
2087 * If we've parsed all lines, there was no error.
2089 return output[0] == '\0';
2092 bool GdbDriver::parseCoreFile(const char* output)
2094 // if command succeeded, gdb emits a line starting with "#0 "
2095 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2096 return m_haveCoreFile;
2099 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2101 // optionally: "program changed, rereading symbols",
2102 // followed by:
2103 // "Program exited normally"
2104 // "Program terminated with wignal SIGSEGV"
2105 // "Program received signal SIGINT" or other signal
2106 // "Breakpoint..."
2108 // go through the output, line by line, checking what we have
2109 const char* start = output - 1;
2110 uint flags = SFprogramActive;
2111 message = QString();
2112 do {
2113 start++; /* skip '\n' */
2115 if (strncmp(start, "Program ", 8) == 0 ||
2116 strncmp(start, "ptrace: ", 8) == 0) {
2118 * When we receive a signal, the program remains active.
2120 * Special: If we "stopped" in a corefile, the string "Program
2121 * terminated with signal"... is displayed. (Normally, we see
2122 * "Program received signal"... when a signal happens.)
2124 if (strncmp(start, "Program exited", 14) == 0 ||
2125 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2126 strncmp(start, "ptrace: ", 8) == 0)
2128 flags &= ~SFprogramActive;
2131 // set message
2132 const char* endOfMessage = strchr(start, '\n');
2133 if (endOfMessage == 0)
2134 endOfMessage = start + strlen(start);
2135 message = QString::fromLatin1(start, endOfMessage-start);
2136 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2138 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2139 * that it stopped at a temporary breakpoint).
2141 flags |= SFrefreshBreak;
2142 } else if (strstr(start, "re-reading symbols.") != 0) {
2143 flags |= SFrefreshSource;
2146 // next line, please
2147 start = strchr(start, '\n');
2148 } while (start != 0);
2151 * Gdb only notices when new threads have appeared, but not when a
2152 * thread finishes. So we always have to assume that the list of
2153 * threads has changed.
2155 flags |= SFrefreshThreads;
2157 return flags;
2160 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2162 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2163 return;
2165 // parse the table of shared libraries
2167 // strip off head line
2168 output = strchr(output, '\n');
2169 if (output == 0)
2170 return;
2171 output++; /* skip '\n' */
2172 QString shlibName;
2173 while (*output != '\0') {
2174 // format of a line is
2175 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2176 // 3 blocks of non-space followed by space
2177 for (int i = 0; *output != '\0' && i < 3; i++) {
2178 while (*output != '\0' && !isspace(*output)) { /* non-space */
2179 output++;
2181 while (isspace(*output)) { /* space */
2182 output++;
2185 if (*output == '\0')
2186 return;
2187 const char* start = output;
2188 output = strchr(output, '\n');
2189 if (output == 0)
2190 output = start + strlen(start);
2191 shlibName = QString::fromLatin1(start, output-start);
2192 if (*output != '\0')
2193 output++;
2194 shlibs.append(shlibName);
2195 TRACE("found shared lib " + shlibName);
2199 bool GdbDriver::parseFindType(const char* output, QString& type)
2201 if (strncmp(output, "type = ", 7) != 0)
2202 return false;
2205 * Everything else is the type. We strip off any leading "const" and any
2206 * trailing "&" on the grounds that neither affects the decoding of the
2207 * object. We also strip off all white-space from the type.
2209 output += 7;
2210 if (strncmp(output, "const ", 6) == 0)
2211 output += 6;
2212 type = output;
2213 type.replace(QRegExp("\\s+"), "");
2214 if (type.endsWith("&"))
2215 type.truncate(type.length() - 1);
2216 return true;
2219 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2221 if (strncmp(output, "The program has no registers now", 32) == 0) {
2222 return;
2225 QString regName;
2226 QString value;
2228 // parse register values
2229 while (*output != '\0')
2231 // skip space at the start of the line
2232 while (isspace(*output))
2233 output++;
2235 // register name
2236 const char* start = output;
2237 while (*output != '\0' && !isspace(*output))
2238 output++;
2239 if (*output == '\0')
2240 break;
2241 regName = QString::fromLatin1(start, output-start);
2243 // skip space
2244 while (isspace(*output))
2245 output++;
2247 RegisterInfo* reg = new RegisterInfo;
2248 reg->regName = regName;
2251 * If we find a brace now, this is a vector register. We look for
2252 * the closing brace and treat the result as cooked value.
2254 if (*output == '{')
2256 start = output;
2257 skipNested(output, '{', '}');
2258 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2259 // skip space, but not the end of line
2260 while (isspace(*output) && *output != '\n')
2261 output++;
2262 // get rid of the braces at the begining and the end
2263 value.remove(0, 1);
2264 if (value[value.length()-1] == '}') {
2265 value = value.left(value.length()-1);
2267 // gdb 5.3 doesn't print a separate set of raw values
2268 if (*output == '{') {
2269 // another set of vector follows
2270 // what we have so far is the raw value
2271 reg->rawValue = value;
2273 start = output;
2274 skipNested(output, '{', '}');
2275 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2276 } else {
2277 // for gdb 5.3
2278 // find first type that does not have an array, this is the RAW value
2279 const char* end=start;
2280 findEnd(end);
2281 const char* cur=start;
2282 while (cur<end) {
2283 while (*cur != '=' && cur<end)
2284 cur++;
2285 cur++;
2286 while (isspace(*cur) && cur<end)
2287 cur++;
2288 if (isNumberish(*cur)) {
2289 end=cur;
2290 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2291 end++;
2292 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2293 reg->rawValue = rawValue;
2295 if (rawValue.left(2)=="0x") {
2296 // ok we have a raw value, now get it's type
2297 end=cur-1;
2298 while (isspace(*end) || *end=='=') end--;
2299 end++;
2300 cur=end-1;
2301 while (*cur!='{' && *cur!=' ')
2302 cur--;
2303 cur++;
2304 reg->type=QString::fromLatin1(cur, end-cur);
2307 // end while loop
2308 cur=end;
2311 // skip to the end of line
2312 while (*output != '\0' && *output != '\n')
2313 output++;
2314 // get rid of the braces at the begining and the end
2315 value.remove(0, 1);
2316 if (value[value.length()-1] == '}') {
2317 value = value.left(value.length()-1);
2320 reg->cookedValue = value;
2322 else
2324 // the rest of the line is the register value
2325 start = output;
2326 output = strchr(output,'\n');
2327 if (output == 0)
2328 output = start + strlen(start);
2329 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2332 * We split the raw from the cooked values.
2333 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2334 * Here, the cooked value comes first, and the raw value is in
2335 * the second part.
2337 int pos = value.find(" (raw ");
2338 if (pos >= 0)
2340 reg->cookedValue = value.left(pos);
2341 reg->rawValue = value.mid(pos+6);
2342 if (reg->rawValue.right(1) == ")") // remove closing bracket
2343 reg->rawValue.truncate(reg->rawValue.length()-1);
2345 else
2348 * In other cases we split off the first token (separated by
2349 * whitespace). It is the raw value. The remainder of the line
2350 * is the cooked value.
2352 int pos = value.find(' ');
2353 if (pos < 0) {
2354 reg->rawValue = value;
2355 reg->cookedValue = QString();
2356 } else {
2357 reg->rawValue = value.left(pos);
2358 reg->cookedValue = value.mid(pos+1);
2362 if (*output != '\0')
2363 output++; /* skip '\n' */
2365 regs.append(reg);
2369 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2371 // "is at address" or "starts at address"
2372 const char* start = strstr(output, "s at address ");
2373 if (start == 0)
2374 return false;
2376 start += 13;
2377 const char* p = start;
2378 while (*p != '\0' && !isspace(*p))
2379 p++;
2380 addrFrom = QString::fromLatin1(start, p-start);
2382 start = strstr(p, "and ends at ");
2383 if (start == 0) {
2384 addrTo = addrFrom;
2385 return true;
2388 start += 12;
2389 p = start;
2390 while (*p != '\0' && !isspace(*p))
2391 p++;
2392 addrTo = QString::fromLatin1(start, p-start);
2394 return true;
2397 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2399 code.clear();
2401 if (strncmp(output, "Dump of assembler", 17) != 0) {
2402 // error message?
2403 DisassembledCode c;
2404 c.code = output;
2405 code.append(new DisassembledCode(c));
2406 return;
2409 // remove first line
2410 const char* p = strchr(output, '\n');
2411 if (p == 0)
2412 return; /* not a regular output */
2414 p++;
2416 // remove last line
2417 const char* end = strstr(output, "End of assembler");
2418 if (end == 0)
2419 end = p + strlen(p);
2421 DbgAddr address;
2423 // remove function offsets from the lines
2424 while (p != end)
2426 const char* start = p;
2427 // address
2428 while (p != end && !isspace(*p))
2429 p++;
2430 address = QString::fromLatin1(start, p-start);
2432 // function name (enclosed in '<>', followed by ':')
2433 while (p != end && *p != '<')
2434 p++;
2435 if (*p == '<')
2436 skipNestedAngles(p);
2437 if (*p == ':')
2438 p++;
2440 // space until code
2441 while (p != end && isspace(*p))
2442 p++;
2444 // code until end of line
2445 start = p;
2446 while (p != end && *p != '\n')
2447 p++;
2448 if (p != end) /* include '\n' */
2449 p++;
2451 DisassembledCode* c = new DisassembledCode;
2452 c->address = address;
2453 c->code = QString::fromLatin1(start, p-start);
2454 code.append(c);
2458 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2460 if (isErrorExpr(output)) {
2461 // error; strip space
2462 QString msg = output;
2463 return msg.stripWhiteSpace();
2466 const char* p = output; /* save typing */
2467 DbgAddr addr;
2468 QString dump;
2470 // the address
2471 while (*p != 0) {
2472 const char* start = p;
2473 while (*p != '\0' && *p != ':' && !isspace(*p))
2474 p++;
2475 addr = QString::fromLatin1(start, p-start);
2476 if (*p != ':') {
2477 // parse function offset
2478 while (isspace(*p))
2479 p++;
2480 start = p;
2481 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2482 p++;
2483 addr.fnoffs = QString::fromLatin1(start, p-start);
2485 if (*p == ':')
2486 p++;
2487 // skip space; this may skip a new-line char!
2488 while (isspace(*p))
2489 p++;
2490 // everything to the end of the line is the memory dump
2491 const char* end = strchr(p, '\n');
2492 if (end != 0) {
2493 dump = QString::fromLatin1(p, end-p);
2494 p = end+1;
2495 } else {
2496 dump = QString::fromLatin1(p, strlen(p));
2497 p += strlen(p);
2499 MemoryDump* md = new MemoryDump;
2500 md->address = addr;
2501 md->dump = dump;
2502 memdump.append(md);
2505 return QString();
2508 QString GdbDriver::editableValue(VarTree* value)
2510 const char* s = value->m_value.latin1();
2512 // if the variable is a pointer value that contains a cast,
2513 // remove the cast
2514 if (*s == '(') {
2515 skipNested(s, '(', ')');
2516 // skip space
2517 while (isspace(*s))
2518 ++s;
2521 repeat:
2522 const char* start = s;
2524 if (strncmp(s, "0x", 2) == 0)
2526 s += 2;
2527 while (isxdigit(*s))
2528 ++s;
2531 * What we saw so far might have been a reference. If so, edit the
2532 * referenced value. Otherwise, edit the pointer.
2534 if (*s == ':') {
2535 // a reference
2536 ++s;
2537 goto repeat;
2539 // a pointer
2540 // if it's a pointer to a string, remove the string
2541 const char* end = s;
2542 while (isspace(*s))
2543 ++s;
2544 if (*s == '"') {
2545 // a string
2546 return QString::fromLatin1(start, end-start);
2547 } else {
2548 // other pointer
2549 return QString::fromLatin1(start, strlen(start));
2553 // else leave it unchanged (or stripped of the reference preamble)
2554 return s;
2557 QString GdbDriver::parseSetVariable(const char* output)
2559 // if there is any output, it is an error message
2560 QString msg = output;
2561 return msg.stripWhiteSpace();
2565 #include "gdbdriver.moc"