Code cleanup: parsePrintExpr() need not return whether there was an error.
[kdbg.git] / kdbg / gdbdriver.cpp
blob0098f6b2f6122fc010338d3b468b3990c0887e41
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 && !m_redirect.isEmpty()) {
403 * Use saved redirection. We prepend it in front of the user's
404 * arguments so that the user can override the redirections.
406 strArg = m_redirect + " " + strArg;
409 QString cmdString;
410 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
411 return cmdString;
414 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
416 assert(cmd >= 0 && cmd < NUM_CMDS);
417 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
419 QString cmdString;
420 cmdString.sprintf(cmds[cmd].fmt, intArg);
421 return cmdString;
424 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
426 assert(cmd >= 0 && cmd < NUM_CMDS);
427 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
428 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
429 cmd == DCexamine ||
430 cmd == DCtty);
432 normalizeStringArg(strArg);
434 QString cmdString;
436 if (cmd == DCtty)
439 * intArg specifies which channels should be redirected to
440 * /dev/null. It is a value or'ed together from RDNstdin,
441 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
442 * command.
444 * Note: We rely on that after the DCtty a DCsetargs will follow,
445 * which will ultimately apply the redirection.
447 static const char* const runRedir[8] = {
449 "</dev/null",
450 ">/dev/null",
451 "</dev/null >/dev/null",
452 "2>/dev/null",
453 "</dev/null 2>/dev/null",
454 ">/dev/null 2>&1",
455 "</dev/null >/dev/null 2>&1"
457 if (strArg.isEmpty())
458 intArg = 7; /* failsafe if no tty */
459 m_redirect = runRedir[intArg & 7];
461 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
464 if (cmd == DCexamine) {
465 // make a format specifier from the intArg
466 static const char size[16] = {
467 '\0', 'b', 'h', 'w', 'g'
469 static const char format[16] = {
470 '\0', 'x', 'd', 'u', 'o', 't',
471 'a', 'c', 'f', 's', 'i'
473 assert(MDTsizemask == 0xf); /* lowest 4 bits */
474 assert(MDTformatmask == 0xf0); /* next 4 bits */
475 int count = 16; /* number of entities to print */
476 char sizeSpec = size[intArg & MDTsizemask];
477 char formatSpec = format[(intArg & MDTformatmask) >> 4];
478 assert(sizeSpec != '\0');
479 assert(formatSpec != '\0');
480 // adjust count such that 16 lines are printed
481 switch (intArg & MDTformatmask) {
482 case MDTstring: case MDTinsn:
483 break; /* no modification needed */
484 default:
485 // all cases drop through:
486 switch (intArg & MDTsizemask) {
487 case MDTbyte:
488 case MDThalfword:
489 count *= 2;
490 case MDTword:
491 count *= 2;
492 case MDTgiantword:
493 count *= 2;
495 break;
497 QString spec;
498 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
500 return makeCmdString(DCexamine, spec, strArg);
503 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
505 // line numbers are zero-based
506 if (cmd == DCuntil || cmd == DCbreakline ||
507 cmd == DCtbreakline || cmd == DCinfoline)
509 intArg++;
511 if (cmd == DCinfoline)
513 // must split off file name part
514 int slash = strArg.findRev('/');
515 if (slash >= 0)
516 strArg = strArg.right(strArg.length()-slash-1);
518 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
520 else
522 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
524 return cmdString;
527 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
529 assert(cmd >= 0 && cmd < NUM_CMDS);
530 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
532 normalizeStringArg(strArg1);
533 normalizeStringArg(strArg2);
535 QString cmdString;
536 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
537 return cmdString;
540 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
542 assert(cmd >= 0 && cmd < NUM_CMDS);
543 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
545 QString cmdString;
546 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
547 return cmdString;
550 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
552 assert(cmd >= 0 && cmd < NUM_CMDS);
553 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
555 if (cmd == DCrun) {
556 m_haveCoreFile = false;
559 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
562 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
563 bool clearLow)
565 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
568 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
569 bool clearLow)
572 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
575 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
576 bool clearLow)
578 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
581 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
582 bool clearLow)
584 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
587 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
588 bool clearLow)
590 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
593 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
595 return queueCmdString(cmd, cmds[cmd].fmt, mode);
598 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
599 QueueMode mode)
601 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
604 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
605 QueueMode mode)
607 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
610 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
611 QueueMode mode)
613 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
616 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
617 QueueMode mode)
619 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
622 void GdbDriver::terminate()
624 kill(SIGTERM);
625 m_state = DSidle;
628 void GdbDriver::detachAndTerminate()
630 kill(SIGINT);
631 flushCommands();
632 executeCmdString(DCinitialize, "detach\nquit\n", true);
635 void GdbDriver::interruptInferior()
637 kill(SIGINT);
638 // remove accidentally queued commands
639 flushHiPriQueue();
642 static bool isErrorExpr(const char* output)
644 return
645 strncmp(output, "Cannot access memory at", 23) == 0 ||
646 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
647 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
648 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
649 strncmp(output, "There is no member or method named", 34) == 0 ||
650 strncmp(output, "A parse error in expression", 27) == 0 ||
651 strncmp(output, "No symbol \"", 11) == 0 ||
652 strncmp(output, "Internal error: ", 16) == 0;
656 * Returns true if the output is an error message. If wantErrorValue is
657 * true, a new VarTree object is created and filled with the error message.
658 * If there are warnings, they are skipped and output points past the warnings
659 * on return (even if there \e are errors).
661 static bool parseErrorMessage(const char*& output,
662 VarTree*& variable, bool wantErrorValue)
664 // skip warnings
665 while (strncmp(output, "warning:", 8) == 0)
667 char* end = strchr(output+8, '\n');
668 if (end == 0)
669 output += strlen(output);
670 else
671 output = end+1;
674 if (isErrorExpr(output))
676 if (wantErrorValue) {
677 // put the error message as value in the variable
678 variable = new VarTree(QString(), VarTree::NKplain);
679 const char* endMsg = strchr(output, '\n');
680 if (endMsg == 0)
681 endMsg = output + strlen(output);
682 variable->m_value = QString::fromLatin1(output, endMsg-output);
683 } else {
684 variable = 0;
686 return true;
688 return false;
691 #if QT_VERSION >= 300
692 union Qt2QChar {
693 short s;
694 struct {
695 uchar row;
696 uchar cell;
697 } qch;
699 #endif
701 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
703 // don't accept the command if it is empty
704 if (cmd == 0 || *cmd == '\0')
705 return;
706 assert(strlen(cmd) <= MAX_FMTLEN);
707 cmds[DCprintQStringStruct].fmt = cmd;
710 VarTree* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
712 VarTree* variable = 0;
715 * Parse off white space. gdb sometimes prints white space first if the
716 * printed array leaded to an error.
718 while (isspace(*output))
719 output++;
721 // special case: empty string (0 repetitions)
722 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
724 variable = new VarTree(QString(), VarTree::NKplain);
725 variable->m_value = "\"\"";
726 return variable;
729 // check for error conditions
730 if (parseErrorMessage(output, variable, wantErrorValue))
731 return variable;
733 // parse the array
735 // find '='
736 const char* p = output;
737 p = strchr(p, '=');
738 if (p == 0) {
739 goto error;
741 // skip white space
742 do {
743 p++;
744 } while (isspace(*p));
746 if (*p == '{')
748 // this is the real data
749 p++; /* skip '{' */
751 // parse the array
752 QString result;
753 QString repeatCount;
754 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
756 * A matrix for separators between the individual "things"
757 * that are added to the string. The first index is a bool,
758 * the second index is from the enum above.
760 static const char* separator[2][3] = {
761 { "\"", 0, ", \"" }, /* normal char is added */
762 { "'", "\", '", ", '" } /* repeated char is added */
765 while (isdigit(*p)) {
766 // parse a number
767 char* end;
768 unsigned short value = (unsigned short) strtoul(p, &end, 0);
769 if (end == p)
770 goto error; /* huh? no valid digits */
771 // skip separator and search for a repeat count
772 p = end;
773 while (isspace(*p) || *p == ',')
774 p++;
775 bool repeats = strncmp(p, "<repeats ", 9) == 0;
776 if (repeats) {
777 const char* start = p;
778 p = strchr(p+9, '>'); /* search end and advance */
779 if (p == 0)
780 goto error;
781 p++; /* skip '>' */
782 repeatCount = QString::fromLatin1(start, p-start);
783 while (isspace(*p) || *p == ',')
784 p++;
786 // p is now at the next char (or the end)
788 // interpret the value as a QChar
789 // TODO: make cross-architecture compatible
790 QChar ch;
791 if (qt3like) {
792 ch = QChar(value);
793 } else {
794 #if QT_VERSION < 300
795 (unsigned short&)ch = value;
796 #else
797 Qt2QChar c;
798 c.s = value;
799 ch.setRow(c.qch.row);
800 ch.setCell(c.qch.cell);
801 #endif
804 // escape a few frequently used characters
805 char escapeCode = '\0';
806 switch (ch.latin1()) {
807 case '\n': escapeCode = 'n'; break;
808 case '\r': escapeCode = 'r'; break;
809 case '\t': escapeCode = 't'; break;
810 case '\b': escapeCode = 'b'; break;
811 case '\"': escapeCode = '\"'; break;
812 case '\\': escapeCode = '\\'; break;
813 case '\0': if (value == 0) { escapeCode = '0'; } break;
816 // add separator
817 result += separator[repeats][lastThing];
818 // add char
819 if (escapeCode != '\0') {
820 result += '\\';
821 ch = escapeCode;
823 result += ch;
825 // fixup repeat count and lastThing
826 if (repeats) {
827 result += "' ";
828 result += repeatCount;
829 lastThing = wasRepeat;
830 } else {
831 lastThing = wasChar;
834 if (*p != '}')
835 goto error;
837 // closing quote
838 if (lastThing == wasChar)
839 result += "\"";
841 // assign the value
842 variable = new VarTree(QString(), VarTree::NKplain);
843 variable->m_value = result;
845 else if (strncmp(p, "true", 4) == 0)
847 variable = new VarTree(QString(), VarTree::NKplain);
848 variable->m_value = "QString::null";
850 else if (strncmp(p, "false", 5) == 0)
852 variable = new VarTree(QString(), VarTree::NKplain);
853 variable->m_value = "(null)";
855 else
856 goto error;
857 return variable;
859 error:
860 if (wantErrorValue) {
861 variable = new VarTree(QString(), VarTree::NKplain);
862 variable->m_value = "internal parse error";
864 return variable;
867 static VarTree* parseVar(const char*& s)
869 const char* p = s;
871 // skip whitespace
872 while (isspace(*p))
873 p++;
875 QString name;
876 VarTree::NameKind kind;
877 if (!parseName(p, name, kind)) {
878 return 0;
881 // go for '='
882 while (isspace(*p))
883 p++;
884 if (*p != '=') {
885 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
886 return 0;
888 // skip the '=' and more whitespace
889 p++;
890 while (isspace(*p))
891 p++;
893 VarTree* variable = new VarTree(name, kind);
895 if (!parseValue(p, variable)) {
896 delete variable;
897 return 0;
899 s = p;
900 return variable;
903 static void skipNested(const char*& s, char opening, char closing)
905 const char* p = s;
907 // parse a nested type
908 int nest = 1;
909 p++;
911 * Search for next matching `closing' char, skipping nested pairs of
912 * `opening' and `closing'.
914 while (*p && nest > 0) {
915 if (*p == opening) {
916 nest++;
917 } else if (*p == closing) {
918 nest--;
920 p++;
922 if (nest != 0) {
923 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
925 s = p;
929 * This function skips text that is delimited by nested angle bracktes, '<>'.
930 * A complication arises because the delimited text can contain the names of
931 * operator<<, operator>>, operator<, and operator>, which have to be treated
932 * specially so that they do not count towards the nesting of '<>'.
933 * This function assumes that the delimited text does not contain strings.
935 static void skipNestedAngles(const char*& s)
937 const char* p = s;
939 int nest = 1;
940 p++; // skip the initial '<'
941 while (*p && nest > 0)
943 // Below we can check for p-s >= 9 instead of 8 because
944 // *s is '<' and cannot be part of "operator".
945 if (*p == '<')
947 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
948 if (p[1] == '<')
949 p++;
950 } else {
951 nest++;
954 else if (*p == '>')
956 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
957 if (p[1] == '>')
958 p++;
959 } else {
960 nest--;
963 p++;
965 if (nest != 0) {
966 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
968 s = p;
972 * Find the end of line that is not inside braces
974 static void findEnd(const char*& s)
976 const char* p = s;
977 while (*p && *p!='\n') {
978 while (*p && *p!='\n' && *p!='{')
979 p++;
980 if (*p=='{') {
981 p++;
982 skipNested(p, '{', '}'); p--;
985 s = p;
988 static bool isNumberish(const char ch)
990 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
993 void skipString(const char*& p)
995 moreStrings:
996 // opening quote
997 char quote = *p++;
998 while (*p != quote) {
999 if (*p == '\\') {
1000 // skip escaped character
1001 // no special treatment for octal values necessary
1002 p++;
1004 // simply return if no more characters
1005 if (*p == '\0')
1006 return;
1007 p++;
1009 // closing quote
1010 p++;
1012 * Strings can consist of several parts, some of which contain repeated
1013 * characters.
1015 if (quote == '\'') {
1016 // look ahaead for <repeats 123 times>
1017 const char* q = p+1;
1018 while (isspace(*q))
1019 q++;
1020 if (strncmp(q, "<repeats ", 9) == 0) {
1021 p = q+9;
1022 while (*p != '\0' && *p != '>')
1023 p++;
1024 if (*p != '\0') {
1025 p++; /* skip the '>' */
1029 // is the string continued?
1030 if (*p == ',') {
1031 // look ahead for another quote
1032 const char* q = p+1;
1033 while (isspace(*q))
1034 q++;
1035 if (*q == '"' || *q == '\'') {
1036 // yes!
1037 p = q;
1038 goto moreStrings;
1041 /* very long strings are followed by `...' */
1042 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1043 p += 3;
1047 static void skipNestedWithString(const char*& s, char opening, char closing)
1049 const char* p = s;
1051 // parse a nested expression
1052 int nest = 1;
1053 p++;
1055 * Search for next matching `closing' char, skipping nested pairs of
1056 * `opening' and `closing' as well as strings.
1058 while (*p && nest > 0) {
1059 if (*p == opening) {
1060 nest++;
1061 } else if (*p == closing) {
1062 nest--;
1063 } else if (*p == '\'' || *p == '\"') {
1064 skipString(p);
1065 continue;
1067 p++;
1069 if (nest > 0) {
1070 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1072 s = p;
1075 inline void skipName(const char*& p)
1077 // allow : (for enumeration values) and $ and . (for _vtbl.)
1078 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1079 p++;
1082 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1084 kind = VarTree::NKplain;
1086 const char* p = s;
1087 // examples of names:
1088 // name
1089 // <Object>
1090 // <string<a,b<c>,7> >
1092 if (*p == '<') {
1093 skipNestedAngles(p);
1094 name = QString::fromLatin1(s, p - s);
1095 kind = VarTree::NKtype;
1097 else
1099 // name, which might be "static"; allow dot for "_vtbl."
1100 skipName(p);
1101 if (p == s) {
1102 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1103 return false;
1105 int len = p - s;
1106 if (len == 6 && strncmp(s, "static", 6) == 0) {
1107 kind = VarTree::NKstatic;
1109 // its a static variable, name comes now
1110 while (isspace(*p))
1111 p++;
1112 s = p;
1113 skipName(p);
1114 if (p == s) {
1115 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1116 return false;
1118 len = p - s;
1120 name = QString::fromLatin1(s, len);
1122 // return the new position
1123 s = p;
1124 return true;
1127 static bool parseValue(const char*& s, VarTree* variable)
1129 variable->m_value = "";
1131 repeat:
1132 if (*s == '{') {
1133 // Sometimes we find the following output:
1134 // {<text variable, no debug info>} 0x40012000 <access>
1135 // {<data variable, no debug info>}
1136 // {<variable (not text or data), no debug info>}
1137 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1138 strncmp(s, "{<data variable, ", 17) == 0 ||
1139 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1141 const char* start = s;
1142 skipNested(s, '{', '}');
1143 variable->m_value = QString::fromLatin1(start, s-start);
1144 variable->m_value += ' '; // add only a single space
1145 while (isspace(*s))
1146 s++;
1147 goto repeat;
1149 else
1151 s++;
1152 if (!parseNested(s, variable)) {
1153 return false;
1155 // must be the closing brace
1156 if (*s != '}') {
1157 TRACE("parse error: missing } of " + variable->getText());
1158 return false;
1160 s++;
1161 // final white space
1162 while (isspace(*s))
1163 s++;
1165 } else {
1166 // examples of leaf values (cannot be the empty string):
1167 // 123
1168 // -123
1169 // 23.575e+37
1170 // 0x32a45
1171 // @0x012ab4
1172 // (DwContentType&) @0x8123456: {...}
1173 // 0x32a45 "text"
1174 // 10 '\n'
1175 // <optimized out>
1176 // 0x823abc <Array<int> virtual table>
1177 // (void (*)()) 0x8048480 <f(E *, char)>
1178 // (E *) 0xbffff450
1179 // red
1180 // &parseP (HTMLClueV *, char *)
1181 // Variable "x" is not available.
1182 // The value of variable 'x' is distributed...
1183 // -nan(0xfffff081defa0)
1185 const char*p = s;
1187 // check for type
1188 QString type;
1189 if (*p == '(') {
1190 skipNested(p, '(', ')');
1192 while (isspace(*p))
1193 p++;
1194 variable->m_value = QString::fromLatin1(s, p - s);
1197 bool reference = false;
1198 if (*p == '@') {
1199 // skip reference marker
1200 p++;
1201 reference = true;
1203 const char* start = p;
1204 if (*p == '-')
1205 p++;
1207 // some values consist of more than one token
1208 bool checkMultiPart = false;
1210 if (p[0] == '0' && p[1] == 'x') {
1211 // parse hex number
1212 p += 2;
1213 while (isxdigit(*p))
1214 p++;
1217 * Assume this is a pointer, but only if it's not a reference, since
1218 * references can't be expanded.
1220 if (!reference) {
1221 variable->m_varKind = VarTree::VKpointer;
1222 } else {
1224 * References are followed by a colon, in which case we'll
1225 * find the value following the reference address.
1227 if (*p == ':') {
1228 p++;
1229 } else {
1230 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1231 reference = false;
1234 checkMultiPart = true;
1235 } else if (isdigit(*p)) {
1236 // parse decimal number, possibly a float
1237 while (isdigit(*p))
1238 p++;
1239 if (*p == '.') { /* TODO: obey i18n? */
1240 // In long arrays an integer may be followed by '...'.
1241 // We test for this situation and don't gobble the '...'.
1242 if (p[1] != '.' || p[0] != '.') {
1243 // fractional part
1244 p++;
1245 while (isdigit(*p))
1246 p++;
1249 if (*p == 'e' || *p == 'E') {
1250 p++;
1251 // exponent
1252 if (*p == '-' || *p == '+')
1253 p++;
1254 while (isdigit(*p))
1255 p++;
1258 // for char variables there is the char, eg. 10 '\n'
1259 checkMultiPart = true;
1260 } else if (*p == '<') {
1261 // e.g. <optimized out>
1262 skipNestedAngles(p);
1263 } else if (*p == '"' || *p == '\'') {
1264 // character may have multipart: '\000' <repeats 11 times>
1265 checkMultiPart = *p == '\'';
1266 // found a string
1267 skipString(p);
1268 } else if (*p == '&') {
1269 // function pointer
1270 p++;
1271 skipName(p);
1272 while (isspace(*p)) {
1273 p++;
1275 if (*p == '(') {
1276 skipNested(p, '(', ')');
1278 } else if (strncmp(p, "Variable \"", 10) == 0) {
1279 // Variable "x" is not available.
1280 p += 10; // skip to "
1281 skipName(p);
1282 if (strncmp(p, "\" is not available.", 19) == 0) {
1283 p += 19;
1285 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1286 p += 23;
1287 skipName(p);
1288 const char* e = strchr(p, '.');
1289 if (e == 0) {
1290 p += strlen(p);
1291 } else {
1292 p = e+1;
1294 } else {
1295 // must be an enumeration value
1296 skipName(p);
1297 // hmm, not necessarily: nan (floating point Not a Number)
1298 // is followed by a number in ()
1299 if (*p == '(')
1300 skipNested(p, '(', ')');
1302 variable->m_value += QString::fromLatin1(start, p - start);
1304 // remove line breaks from the value; this is ok since
1305 // string values never contain a literal line break
1306 variable->m_value.replace('\n', ' ');
1308 if (checkMultiPart) {
1309 // white space
1310 while (isspace(*p))
1311 p++;
1312 // may be followed by a string or <...>
1313 start = p;
1315 if (*p == '"' || *p == '\'') {
1316 skipString(p);
1317 } else if (*p == '<') {
1318 // if this value is part of an array, it might be followed
1319 // by <repeats 15 times>, which we don't skip here
1320 if (strncmp(p, "<repeats ", 9) != 0)
1321 skipNestedAngles(p);
1323 if (p != start) {
1324 // there is always a blank before the string,
1325 // which we will include in the final string value
1326 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1327 // if this was a pointer, reset that flag since we
1328 // now got the value
1329 variable->m_varKind = VarTree::VKsimple;
1333 if (variable->m_value.length() == 0) {
1334 TRACE("parse error: no value for " + variable->getText());
1335 return false;
1338 // final white space
1339 while (isspace(*p))
1340 p++;
1341 s = p;
1344 * If this was a reference, the value follows. It might even be a
1345 * composite variable!
1347 if (reference) {
1348 goto repeat;
1351 if (variable->m_varKind == VarTree::VKpointer) {
1352 variable->setDelayedExpanding(true);
1356 return true;
1359 static bool parseNested(const char*& s, VarTree* variable)
1361 // could be a structure or an array
1362 while (isspace(*s))
1363 s++;
1365 const char* p = s;
1366 bool isStruct = false;
1368 * If there is a name followed by an = or an < -- which starts a type
1369 * name -- or "static", it is a structure
1371 if (*p == '<' || *p == '}') {
1372 isStruct = true;
1373 } else if (strncmp(p, "static ", 7) == 0) {
1374 isStruct = true;
1375 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1376 // look ahead for a comma after the name
1377 skipName(p);
1378 while (isspace(*p))
1379 p++;
1380 if (*p == '=') {
1381 isStruct = true;
1383 p = s; /* rescan the name */
1385 if (isStruct) {
1386 if (!parseVarSeq(p, variable)) {
1387 return false;
1389 variable->m_varKind = VarTree::VKstruct;
1390 } else {
1391 if (!parseValueSeq(p, variable)) {
1392 return false;
1394 variable->m_varKind = VarTree::VKarray;
1396 s = p;
1397 return true;
1400 static bool parseVarSeq(const char*& s, VarTree* variable)
1402 // parse a comma-separated sequence of variables
1403 VarTree* var = variable; /* var != 0 to indicate success if empty seq */
1404 for (;;) {
1405 if (*s == '}')
1406 break;
1407 if (strncmp(s, "<No data fields>}", 17) == 0)
1409 // no member variables, so break out immediately
1410 s += 16; /* go to the closing brace */
1411 break;
1413 var = parseVar(s);
1414 if (var == 0)
1415 break; /* syntax error */
1416 variable->appendChild(var);
1417 if (*s != ',')
1418 break;
1419 // skip the comma and whitespace
1420 s++;
1421 while (isspace(*s))
1422 s++;
1424 return var != 0;
1427 static bool parseValueSeq(const char*& s, VarTree* variable)
1429 // parse a comma-separated sequence of variables
1430 int index = 0;
1431 bool good;
1432 for (;;) {
1433 QString name;
1434 name.sprintf("[%d]", index);
1435 VarTree* var = new VarTree(name, VarTree::NKplain);
1436 good = parseValue(s, var);
1437 if (!good) {
1438 delete var;
1439 return false;
1441 // a value may be followed by "<repeats 45 times>"
1442 if (strncmp(s, "<repeats ", 9) == 0) {
1443 s += 9;
1444 char* end;
1445 int l = strtol(s, &end, 10);
1446 if (end == s || strncmp(end, " times>", 7) != 0) {
1447 // should not happen
1448 delete var;
1449 return false;
1451 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1452 // replace name and advance index
1453 name.sprintf("[%d .. %d]", index, index+l-1);
1454 var->setText(name);
1455 index += l;
1456 // skip " times>" and space
1457 s = end+7;
1458 // possible final space
1459 while (isspace(*s))
1460 s++;
1461 } else {
1462 index++;
1464 variable->appendChild(var);
1465 // long arrays may be terminated by '...'
1466 if (strncmp(s, "...", 3) == 0) {
1467 s += 3;
1468 VarTree* var = new VarTree("...", VarTree::NKplain);
1469 var->m_value = i18n("<additional entries of the array suppressed>");
1470 variable->appendChild(var);
1471 break;
1473 if (*s != ',') {
1474 break;
1476 // skip the comma and whitespace
1477 s++;
1478 while (isspace(*s))
1479 s++;
1480 // sometimes there is a closing brace after a comma
1481 // if (*s == '}')
1482 // break;
1484 return true;
1488 * Parses a stack frame.
1490 static void parseFrameInfo(const char*& s, QString& func,
1491 QString& file, int& lineNo, DbgAddr& address)
1493 const char* p = s;
1495 // next may be a hexadecimal address
1496 if (*p == '0') {
1497 const char* start = p;
1498 p++;
1499 if (*p == 'x')
1500 p++;
1501 while (isxdigit(*p))
1502 p++;
1503 address = QString::fromLatin1(start, p-start);
1504 if (strncmp(p, " in ", 4) == 0)
1505 p += 4;
1506 } else {
1507 address = DbgAddr();
1509 const char* start = p;
1510 // check for special signal handler frame
1511 if (strncmp(p, "<signal handler called>", 23) == 0) {
1512 func = QString::fromLatin1(start, 23);
1513 file = QString();
1514 lineNo = -1;
1515 s = p+23;
1516 if (*s == '\n')
1517 s++;
1518 return;
1522 * Skip the function name. It is terminated by a left parenthesis
1523 * which does not delimit "(anonymous namespace)" and which is
1524 * outside the angle brackets <> of template parameter lists.
1526 while (*p != '\0')
1528 if (*p == '<') {
1529 // check for operator<< and operator<
1530 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1532 p++;
1533 if (*p == '<')
1534 p++;
1536 else
1538 // skip template parameter list
1539 skipNestedAngles(p);
1541 } else if (*p == '(') {
1542 if (strncmp(p, "(anonymous namespace)", 21) != 0)
1543 break; // parameter list found
1544 p += 21;
1545 } else {
1546 p++;
1550 if (*p == '\0') {
1551 func = start;
1552 file = QString();
1553 lineNo = -1;
1554 s = p;
1555 return;
1558 * Skip parameters. But notice that for complicated conversion
1559 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1560 * pointer to function) as well as operator()(...) we have to skip
1561 * additional pairs of parentheses. Furthermore, recent gdbs write the
1562 * demangled name followed by the arguments in a pair of parentheses,
1563 * where the demangled name can end in "const".
1565 do {
1566 skipNestedWithString(p, '(', ')');
1567 while (isspace(*p))
1568 p++;
1569 // skip "const"
1570 if (strncmp(p, "const", 5) == 0) {
1571 p += 5;
1572 while (isspace(*p))
1573 p++;
1575 } while (*p == '(');
1577 // check for file position
1578 if (strncmp(p, "at ", 3) == 0) {
1579 p += 3;
1580 const char* fileStart = p;
1581 // go for the end of the line
1582 while (*p != '\0' && *p != '\n')
1583 p++;
1584 // search back for colon
1585 const char* colon = p;
1586 do {
1587 --colon;
1588 } while (*colon != ':');
1589 file = QString::fromLatin1(fileStart, colon-fileStart);
1590 lineNo = atoi(colon+1)-1;
1591 // skip new-line
1592 if (*p != '\0')
1593 p++;
1594 } else {
1595 // check for "from shared lib"
1596 if (strncmp(p, "from ", 5) == 0) {
1597 p += 5;
1598 // go for the end of the line
1599 while (*p != '\0' && *p != '\n')
1600 p++;
1601 // skip new-line
1602 if (*p != '\0')
1603 p++;
1605 file = "";
1606 lineNo = -1;
1608 // construct the function name (including file info)
1609 if (*p == '\0') {
1610 func = start;
1611 } else {
1612 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1614 s = p;
1617 * Replace \n (and whitespace around it) in func by a blank. We cannot
1618 * use QString::simplifyWhiteSpace() for this because this would also
1619 * simplify space that belongs to a string arguments that gdb sometimes
1620 * prints in the argument lists of the function.
1622 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1623 int nl = 0;
1624 while ((nl = func.find('\n', nl)) >= 0) {
1625 // search back to the beginning of the whitespace
1626 int startWhite = nl;
1627 do {
1628 --startWhite;
1629 } while (isspace(func[startWhite].latin1()));
1630 startWhite++;
1631 // search forward to the end of the whitespace
1632 do {
1633 nl++;
1634 } while (isspace(func[nl].latin1()));
1635 // replace
1636 func.replace(startWhite, nl-startWhite, " ");
1637 /* continue searching for more \n's at this place: */
1638 nl = startWhite+1;
1644 * Parses a stack frame including its frame number
1646 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1647 QString& file, int& lineNo, DbgAddr& address)
1649 // Example:
1650 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1651 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1653 // must start with a hash mark followed by number
1654 // or with "Breakpoint " followed by number and comma
1655 if (s[0] == '#') {
1656 if (!isdigit(s[1]))
1657 return false;
1658 s++; /* skip the hash mark */
1659 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1660 if (!isdigit(s[11]))
1661 return false;
1662 s += 11; /* skip "Breakpoint" */
1663 } else
1664 return false;
1666 // frame number
1667 frameNo = atoi(s);
1668 while (isdigit(*s))
1669 s++;
1670 // space and comma
1671 while (isspace(*s) || *s == ',')
1672 s++;
1673 parseFrameInfo(s, func, file, lineNo, address);
1674 return true;
1677 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1679 QString func, file;
1680 int lineNo, frameNo;
1681 DbgAddr address;
1683 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1684 StackFrame* frm = new StackFrame;
1685 frm->frameNo = frameNo;
1686 frm->fileName = file;
1687 frm->lineNo = lineNo;
1688 frm->address = address;
1689 frm->var = new VarTree(func, VarTree::NKplain);
1690 stack.append(frm);
1694 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1695 QString& file, int& lineNo, DbgAddr& address)
1697 QString func;
1698 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1702 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1704 // skip first line, which is the headline
1705 const char* p = strchr(output, '\n');
1706 if (p == 0)
1707 return false;
1708 p++;
1709 if (*p == '\0')
1710 return false;
1712 // split up a line
1713 QString location;
1714 QString address;
1715 int hits = 0;
1716 uint ignoreCount = 0;
1717 QString condition;
1718 const char* end;
1719 char* dummy;
1720 while (*p != '\0') {
1721 // get Num
1722 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1723 p = dummy;
1724 // get Type
1725 while (isspace(*p))
1726 p++;
1727 Breakpoint::Type bpType = Breakpoint::breakpoint;
1728 if (strncmp(p, "breakpoint", 10) == 0) {
1729 p += 10;
1730 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1731 bpType = Breakpoint::watchpoint;
1732 p += 13;
1733 } else if (strncmp(p, "watchpoint", 10) == 0) {
1734 bpType = Breakpoint::watchpoint;
1735 p += 10;
1737 while (isspace(*p))
1738 p++;
1739 if (*p == '\0')
1740 break;
1741 // get Disp
1742 char disp = *p++;
1743 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1744 p++;
1745 while (isspace(*p))
1746 p++;
1747 if (*p == '\0')
1748 break;
1749 // get Enb
1750 char enable = *p++;
1751 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1752 p++;
1753 while (isspace(*p))
1754 p++;
1755 if (*p == '\0')
1756 break;
1757 // the address, if present
1758 if (bpType == Breakpoint::breakpoint &&
1759 strncmp(p, "0x", 2) == 0)
1761 const char* start = p;
1762 while (*p != '\0' && !isspace(*p))
1763 p++;
1764 address = QString::fromLatin1(start, p-start);
1765 while (isspace(*p) && *p != '\n')
1766 p++;
1767 if (*p == '\0')
1768 break;
1769 } else {
1770 address = QString();
1772 // remainder is location, hit and ignore count, condition
1773 hits = 0;
1774 ignoreCount = 0;
1775 condition = QString();
1776 end = strchr(p, '\n');
1777 if (end == 0) {
1778 location = p;
1779 p += location.length();
1780 } else {
1781 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1782 p = end+1; /* skip over \n */
1785 // may be continued in next line
1786 while (isspace(*p)) { /* p points to beginning of line */
1787 // skip white space at beginning of line
1788 while (isspace(*p))
1789 p++;
1791 // seek end of line
1792 end = strchr(p, '\n');
1793 if (end == 0)
1794 end = p+strlen(p);
1796 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1797 // extract the hit count
1798 p += 22;
1799 hits = strtol(p, &dummy, 10);
1800 TRACE(QString().sprintf("hit count %d", hits));
1801 } else if (strncmp(p, "stop only if ", 13) == 0) {
1802 // extract condition
1803 p += 13;
1804 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1805 TRACE("condition: "+condition);
1806 } else if (strncmp(p, "ignore next ", 12) == 0) {
1807 // extract ignore count
1808 p += 12;
1809 ignoreCount = strtol(p, &dummy, 10);
1810 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1811 } else {
1812 // indeed a continuation
1813 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1815 p = end;
1816 if (*p != '\0')
1817 p++; /* skip '\n' */
1819 Breakpoint* bp = new Breakpoint;
1820 bp->id = bpNum;
1821 bp->type = bpType;
1822 bp->temporary = disp == 'd';
1823 bp->enabled = enable == 'y';
1824 bp->location = location;
1825 bp->address = address;
1826 bp->hitCount = hits;
1827 bp->ignoreCount = ignoreCount;
1828 bp->condition = condition;
1829 brks.append(bp);
1831 return true;
1834 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1836 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1837 // no threads
1838 return true;
1841 int id;
1842 QString systag;
1843 QString func, file;
1844 int lineNo;
1845 DbgAddr address;
1847 const char* p = output;
1848 while (*p != '\0') {
1849 // seach look for thread id, watching out for the focus indicator
1850 bool hasFocus = false;
1851 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1852 p++;
1853 if (*p == '*') {
1854 hasFocus = true;
1855 p++;
1856 // there follows only whitespace
1858 char* end;
1859 id = strtol(p, &end, 10);
1860 if (p == end) {
1861 // syntax error: no number found; bail out
1862 return true;
1864 p = end;
1866 // skip space
1867 while (isspace(*p))
1868 p++;
1871 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1873 end = strstr(p, " ");
1874 if (end == 0) {
1875 // syntax error; bail out
1876 return true;
1878 systag = QString::fromLatin1(p, end-p);
1879 p = end+2;
1882 * Now follows a standard stack frame. Sometimes, however, gdb
1883 * catches a thread at an instant where it doesn't have a stack.
1885 if (strncmp(p, "[No stack.]", 11) != 0) {
1886 ::parseFrameInfo(p, func, file, lineNo, address);
1887 } else {
1888 func = "[No stack]";
1889 file = QString();
1890 lineNo = -1;
1891 address = QString();
1892 p += 11; /* \n is skipped above */
1895 ThreadInfo* thr = new ThreadInfo;
1896 thr->id = id;
1897 thr->threadName = systag;
1898 thr->hasFocus = hasFocus;
1899 thr->function = func;
1900 thr->fileName = file;
1901 thr->lineNo = lineNo;
1902 thr->address = address;
1903 threads.append(thr);
1905 return true;
1908 static bool parseNewBreakpoint(const char* o, int& id,
1909 QString& file, int& lineNo, QString& address);
1910 static bool parseNewWatchpoint(const char* o, int& id,
1911 QString& expr);
1913 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1914 QString& file, int& lineNo, QString& address)
1916 const char* o = output;
1917 // skip lines of that begin with "(Cannot find"
1918 while (strncmp(o, "(Cannot find", 12) == 0) {
1919 o = strchr(o, '\n');
1920 if (o == 0)
1921 return false;
1922 o++; /* skip newline */
1925 if (strncmp(o, "Breakpoint ", 11) == 0) {
1926 output += 11; /* skip "Breakpoint " */
1927 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1928 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1929 output += 20;
1930 return ::parseNewWatchpoint(output, id, address);
1931 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1932 output += 11;
1933 return ::parseNewWatchpoint(output, id, address);
1935 return false;
1938 static bool parseNewBreakpoint(const char* o, int& id,
1939 QString& file, int& lineNo, QString& address)
1941 // breakpoint id
1942 char* p;
1943 id = strtoul(o, &p, 10);
1944 if (p == o)
1945 return false;
1947 // check for the address
1948 if (strncmp(p, " at 0x", 6) == 0) {
1949 char* start = p+4; /* skip " at ", but not 0x */
1950 p += 6;
1951 while (isxdigit(*p))
1952 ++p;
1953 address = QString::fromLatin1(start, p-start);
1956 // file name
1957 char* fileStart = strstr(p, "file ");
1958 if (fileStart == 0)
1959 return !address.isEmpty(); /* parse error only if there's no address */
1960 fileStart += 5;
1962 // line number
1963 char* numStart = strstr(fileStart, ", line ");
1964 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1965 numStart += 7;
1966 int line = strtoul(numStart, &p, 10);
1967 if (numStart == p)
1968 return false;
1970 file = fileName;
1971 lineNo = line-1; /* zero-based! */
1972 return true;
1975 static bool parseNewWatchpoint(const char* o, int& id,
1976 QString& expr)
1978 // watchpoint id
1979 char* p;
1980 id = strtoul(o, &p, 10);
1981 if (p == o)
1982 return false;
1984 if (strncmp(p, ": ", 2) != 0)
1985 return false;
1986 p += 2;
1988 // all the rest on the line is the expression
1989 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
1990 return true;
1993 void GdbDriver::parseLocals(const char* output, QList<VarTree>& newVars)
1995 // check for possible error conditions
1996 if (strncmp(output, "No symbol table", 15) == 0)
1998 return;
2001 while (*output != '\0') {
2002 while (isspace(*output))
2003 output++;
2004 if (*output == '\0')
2005 break;
2006 // skip occurrences of "No locals" and "No args"
2007 if (strncmp(output, "No locals", 9) == 0 ||
2008 strncmp(output, "No arguments", 12) == 0)
2010 output = strchr(output, '\n');
2011 if (output == 0) {
2012 break;
2014 continue;
2017 VarTree* variable = parseVar(output);
2018 if (variable == 0) {
2019 break;
2021 // do not add duplicates
2022 for (VarTree* o = newVars.first(); o != 0; o = newVars.next()) {
2023 if (o->getText() == variable->getText()) {
2024 delete variable;
2025 goto skipDuplicate;
2028 newVars.append(variable);
2029 skipDuplicate:;
2033 VarTree* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2035 VarTree* var = 0;
2036 // check for error conditions
2037 if (!parseErrorMessage(output, var, wantErrorValue))
2039 // parse the variable
2040 var = parseVar(output);
2042 return var;
2045 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2047 bool isGood = false;
2048 message = QString(output).simplifyWhiteSpace();
2049 if (message.isEmpty()) {
2050 message = i18n("New working directory: ") + m_programWD;
2051 isGood = true;
2053 return isGood;
2056 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2058 message = output;
2060 m_haveCoreFile = false;
2063 * Lines starting with the following do not indicate errors:
2064 * Using host libthread_db
2065 * (no debugging symbols found)
2067 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2068 strncmp(output, "(no debugging symbols found)", 28) == 0)
2070 // this line is good, go to the next one
2071 const char* end = strchr(output, '\n');
2072 if (end == 0)
2073 output += strlen(output);
2074 else
2075 output = end+1;
2079 * If we've parsed all lines, there was no error.
2081 return output[0] == '\0';
2084 bool GdbDriver::parseCoreFile(const char* output)
2086 // if command succeeded, gdb emits a line starting with "#0 "
2087 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2088 return m_haveCoreFile;
2091 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2093 // optionally: "program changed, rereading symbols",
2094 // followed by:
2095 // "Program exited normally"
2096 // "Program terminated with wignal SIGSEGV"
2097 // "Program received signal SIGINT" or other signal
2098 // "Breakpoint..."
2100 // go through the output, line by line, checking what we have
2101 const char* start = output - 1;
2102 uint flags = SFprogramActive;
2103 message = QString();
2104 do {
2105 start++; /* skip '\n' */
2107 if (strncmp(start, "Program ", 8) == 0 ||
2108 strncmp(start, "ptrace: ", 8) == 0) {
2110 * When we receive a signal, the program remains active.
2112 * Special: If we "stopped" in a corefile, the string "Program
2113 * terminated with signal"... is displayed. (Normally, we see
2114 * "Program received signal"... when a signal happens.)
2116 if (strncmp(start, "Program exited", 14) == 0 ||
2117 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2118 strncmp(start, "ptrace: ", 8) == 0)
2120 flags &= ~SFprogramActive;
2123 // set message
2124 const char* endOfMessage = strchr(start, '\n');
2125 if (endOfMessage == 0)
2126 endOfMessage = start + strlen(start);
2127 message = QString::fromLatin1(start, endOfMessage-start);
2128 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2130 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2131 * that it stopped at a temporary breakpoint).
2133 flags |= SFrefreshBreak;
2134 } else if (strstr(start, "re-reading symbols.") != 0) {
2135 flags |= SFrefreshSource;
2138 // next line, please
2139 start = strchr(start, '\n');
2140 } while (start != 0);
2143 * Gdb only notices when new threads have appeared, but not when a
2144 * thread finishes. So we always have to assume that the list of
2145 * threads has changed.
2147 flags |= SFrefreshThreads;
2149 return flags;
2152 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2154 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2155 return;
2157 // parse the table of shared libraries
2159 // strip off head line
2160 output = strchr(output, '\n');
2161 if (output == 0)
2162 return;
2163 output++; /* skip '\n' */
2164 QString shlibName;
2165 while (*output != '\0') {
2166 // format of a line is
2167 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2168 // 3 blocks of non-space followed by space
2169 for (int i = 0; *output != '\0' && i < 3; i++) {
2170 while (*output != '\0' && !isspace(*output)) { /* non-space */
2171 output++;
2173 while (isspace(*output)) { /* space */
2174 output++;
2177 if (*output == '\0')
2178 return;
2179 const char* start = output;
2180 output = strchr(output, '\n');
2181 if (output == 0)
2182 output = start + strlen(start);
2183 shlibName = QString::fromLatin1(start, output-start);
2184 if (*output != '\0')
2185 output++;
2186 shlibs.append(shlibName);
2187 TRACE("found shared lib " + shlibName);
2191 bool GdbDriver::parseFindType(const char* output, QString& type)
2193 if (strncmp(output, "type = ", 7) != 0)
2194 return false;
2197 * Everything else is the type. We strip off any leading "const" and any
2198 * trailing "&" on the grounds that neither affects the decoding of the
2199 * object. We also strip off all white-space from the type.
2201 output += 7;
2202 if (strncmp(output, "const ", 6) == 0)
2203 output += 6;
2204 type = output;
2205 type.replace(QRegExp("\\s+"), "");
2206 if (type.endsWith("&"))
2207 type.truncate(type.length() - 1);
2208 return true;
2211 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2213 if (strncmp(output, "The program has no registers now", 32) == 0) {
2214 return;
2217 QString regName;
2218 QString value;
2220 // parse register values
2221 while (*output != '\0')
2223 // skip space at the start of the line
2224 while (isspace(*output))
2225 output++;
2227 // register name
2228 const char* start = output;
2229 while (*output != '\0' && !isspace(*output))
2230 output++;
2231 if (*output == '\0')
2232 break;
2233 regName = QString::fromLatin1(start, output-start);
2235 // skip space
2236 while (isspace(*output))
2237 output++;
2239 RegisterInfo* reg = new RegisterInfo;
2240 reg->regName = regName;
2243 * If we find a brace now, this is a vector register. We look for
2244 * the closing brace and treat the result as cooked value.
2246 if (*output == '{')
2248 start = output;
2249 skipNested(output, '{', '}');
2250 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2251 // skip space, but not the end of line
2252 while (isspace(*output) && *output != '\n')
2253 output++;
2254 // get rid of the braces at the begining and the end
2255 value.remove(0, 1);
2256 if (value[value.length()-1] == '}') {
2257 value = value.left(value.length()-1);
2259 // gdb 5.3 doesn't print a separate set of raw values
2260 if (*output == '{') {
2261 // another set of vector follows
2262 // what we have so far is the raw value
2263 reg->rawValue = value;
2265 start = output;
2266 skipNested(output, '{', '}');
2267 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2268 } else {
2269 // for gdb 5.3
2270 // find first type that does not have an array, this is the RAW value
2271 const char* end=start;
2272 findEnd(end);
2273 const char* cur=start;
2274 while (cur<end) {
2275 while (*cur != '=' && cur<end)
2276 cur++;
2277 cur++;
2278 while (isspace(*cur) && cur<end)
2279 cur++;
2280 if (isNumberish(*cur)) {
2281 end=cur;
2282 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2283 end++;
2284 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2285 reg->rawValue = rawValue;
2287 if (rawValue.left(2)=="0x") {
2288 // ok we have a raw value, now get it's type
2289 end=cur-1;
2290 while (isspace(*end) || *end=='=') end--;
2291 end++;
2292 cur=end-1;
2293 while (*cur!='{' && *cur!=' ')
2294 cur--;
2295 cur++;
2296 reg->type=QString::fromLatin1(cur, end-cur);
2299 // end while loop
2300 cur=end;
2303 // skip to the end of line
2304 while (*output != '\0' && *output != '\n')
2305 output++;
2306 // get rid of the braces at the begining and the end
2307 value.remove(0, 1);
2308 if (value[value.length()-1] == '}') {
2309 value = value.left(value.length()-1);
2312 reg->cookedValue = value;
2314 else
2316 // the rest of the line is the register value
2317 start = output;
2318 output = strchr(output,'\n');
2319 if (output == 0)
2320 output = start + strlen(start);
2321 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2324 * We split the raw from the cooked values.
2325 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2326 * Here, the cooked value comes first, and the raw value is in
2327 * the second part.
2329 int pos = value.find(" (raw ");
2330 if (pos >= 0)
2332 reg->cookedValue = value.left(pos);
2333 reg->rawValue = value.mid(pos+6);
2334 if (reg->rawValue.right(1) == ")") // remove closing bracket
2335 reg->rawValue.truncate(reg->rawValue.length()-1);
2337 else
2340 * In other cases we split off the first token (separated by
2341 * whitespace). It is the raw value. The remainder of the line
2342 * is the cooked value.
2344 int pos = value.find(' ');
2345 if (pos < 0) {
2346 reg->rawValue = value;
2347 reg->cookedValue = QString();
2348 } else {
2349 reg->rawValue = value.left(pos);
2350 reg->cookedValue = value.mid(pos+1);
2354 if (*output != '\0')
2355 output++; /* skip '\n' */
2357 regs.append(reg);
2361 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2363 // "is at address" or "starts at address"
2364 const char* start = strstr(output, "s at address ");
2365 if (start == 0)
2366 return false;
2368 start += 13;
2369 const char* p = start;
2370 while (*p != '\0' && !isspace(*p))
2371 p++;
2372 addrFrom = QString::fromLatin1(start, p-start);
2374 start = strstr(p, "and ends at ");
2375 if (start == 0) {
2376 addrTo = addrFrom;
2377 return true;
2380 start += 12;
2381 p = start;
2382 while (*p != '\0' && !isspace(*p))
2383 p++;
2384 addrTo = QString::fromLatin1(start, p-start);
2386 return true;
2389 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2391 code.clear();
2393 if (strncmp(output, "Dump of assembler", 17) != 0) {
2394 // error message?
2395 DisassembledCode c;
2396 c.code = output;
2397 code.append(new DisassembledCode(c));
2398 return;
2401 // remove first line
2402 const char* p = strchr(output, '\n');
2403 if (p == 0)
2404 return; /* not a regular output */
2406 p++;
2408 // remove last line
2409 const char* end = strstr(output, "End of assembler");
2410 if (end == 0)
2411 end = p + strlen(p);
2413 DbgAddr address;
2415 // remove function offsets from the lines
2416 while (p != end)
2418 const char* start = p;
2419 // address
2420 while (p != end && !isspace(*p))
2421 p++;
2422 address = QString::fromLatin1(start, p-start);
2424 // function name (enclosed in '<>', followed by ':')
2425 while (p != end && *p != '<')
2426 p++;
2427 if (*p == '<')
2428 skipNestedAngles(p);
2429 if (*p == ':')
2430 p++;
2432 // space until code
2433 while (p != end && isspace(*p))
2434 p++;
2436 // code until end of line
2437 start = p;
2438 while (p != end && *p != '\n')
2439 p++;
2440 if (p != end) /* include '\n' */
2441 p++;
2443 DisassembledCode* c = new DisassembledCode;
2444 c->address = address;
2445 c->code = QString::fromLatin1(start, p-start);
2446 code.append(c);
2450 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2452 if (isErrorExpr(output)) {
2453 // error; strip space
2454 QString msg = output;
2455 return msg.stripWhiteSpace();
2458 const char* p = output; /* save typing */
2459 DbgAddr addr;
2460 QString dump;
2462 // the address
2463 while (*p != 0) {
2464 const char* start = p;
2465 while (*p != '\0' && *p != ':' && !isspace(*p))
2466 p++;
2467 addr = QString::fromLatin1(start, p-start);
2468 if (*p != ':') {
2469 // parse function offset
2470 while (isspace(*p))
2471 p++;
2472 start = p;
2473 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2474 p++;
2475 addr.fnoffs = QString::fromLatin1(start, p-start);
2477 if (*p == ':')
2478 p++;
2479 // skip space; this may skip a new-line char!
2480 while (isspace(*p))
2481 p++;
2482 // everything to the end of the line is the memory dump
2483 const char* end = strchr(p, '\n');
2484 if (end != 0) {
2485 dump = QString::fromLatin1(p, end-p);
2486 p = end+1;
2487 } else {
2488 dump = QString::fromLatin1(p, strlen(p));
2489 p += strlen(p);
2491 MemoryDump* md = new MemoryDump;
2492 md->address = addr;
2493 md->dump = dump;
2494 memdump.append(md);
2497 return QString();
2500 QString GdbDriver::editableValue(VarTree* value)
2502 const char* s = value->m_value.latin1();
2504 // if the variable is a pointer value that contains a cast,
2505 // remove the cast
2506 if (*s == '(') {
2507 skipNested(s, '(', ')');
2508 // skip space
2509 while (isspace(*s))
2510 ++s;
2513 repeat:
2514 const char* start = s;
2516 if (strncmp(s, "0x", 2) == 0)
2518 s += 2;
2519 while (isxdigit(*s))
2520 ++s;
2523 * What we saw so far might have been a reference. If so, edit the
2524 * referenced value. Otherwise, edit the pointer.
2526 if (*s == ':') {
2527 // a reference
2528 ++s;
2529 goto repeat;
2531 // a pointer
2532 // if it's a pointer to a string, remove the string
2533 const char* end = s;
2534 while (isspace(*s))
2535 ++s;
2536 if (*s == '"') {
2537 // a string
2538 return QString::fromLatin1(start, end-start);
2539 } else {
2540 // other pointer
2541 return QString::fromLatin1(start, strlen(start));
2545 // else leave it unchanged (or stripped of the reference preamble)
2546 return s;
2549 QString GdbDriver::parseSetVariable(const char* output)
2551 // if there is any output, it is an error message
2552 QString msg = output;
2553 return msg.stripWhiteSpace();
2557 #include "gdbdriver.moc"