Merge with 2.0.
[kdbg.git] / kdbg / gdbdriver.cpp
blob45cda07754f019b4f0bf46a6aff418268351d20e
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 ExprValue* parseVar(const char*& s);
24 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind);
25 static bool parseValue(const char*& s, ExprValue* variable);
26 static bool parseNested(const char*& s, ExprValue* variable);
27 static bool parseVarSeq(const char*& s, ExprValue* variable);
28 static bool parseValueSeq(const char*& s, ExprValue* variable);
30 #define PROMPT "(kdbg)"
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 ExprValue 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 ExprValue*& 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 ExprValue(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 ExprValue* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
712 ExprValue* 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 ExprValue(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 ExprValue(QString(), VarTree::NKplain);
843 variable->m_value = result;
845 else if (strncmp(p, "true", 4) == 0)
847 variable = new ExprValue(QString(), VarTree::NKplain);
848 variable->m_value = "QString::null";
850 else if (strncmp(p, "false", 5) == 0)
852 variable = new ExprValue(QString(), VarTree::NKplain);
853 variable->m_value = "(null)";
855 else
856 goto error;
857 return variable;
859 error:
860 if (wantErrorValue) {
861 variable = new ExprValue(QString(), VarTree::NKplain);
862 variable->m_value = "internal parse error";
864 return variable;
867 static ExprValue* 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;
878 * Detect anonymouse struct values: The 'name =' part is missing:
879 * s = { a = 1, { b = 2 }}
880 * Note that this detection works only inside structs when the anonymous
881 * struct is not the first member:
882 * s = {{ a = 1 }, b = 2}
883 * This is misparsed (by parseNested()) because it is mistakenly
884 * interprets the second opening brace as the first element of an array
885 * of structs.
887 if (*p == '{')
889 name = i18n("<anonymous struct or union>");
890 kind = VarTree::NKanonymous;
892 else
894 if (!parseName(p, name, kind)) {
895 return 0;
898 // go for '='
899 while (isspace(*p))
900 p++;
901 if (*p != '=') {
902 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
903 return 0;
905 // skip the '=' and more whitespace
906 p++;
907 while (isspace(*p))
908 p++;
911 ExprValue* variable = new ExprValue(name, kind);
913 if (!parseValue(p, variable)) {
914 delete variable;
915 return 0;
917 s = p;
918 return variable;
921 static void skipNested(const char*& s, char opening, char closing)
923 const char* p = s;
925 // parse a nested type
926 int nest = 1;
927 p++;
929 * Search for next matching `closing' char, skipping nested pairs of
930 * `opening' and `closing'.
932 while (*p && nest > 0) {
933 if (*p == opening) {
934 nest++;
935 } else if (*p == closing) {
936 nest--;
938 p++;
940 if (nest != 0) {
941 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
943 s = p;
947 * This function skips text that is delimited by nested angle bracktes, '<>'.
948 * A complication arises because the delimited text can contain the names of
949 * operator<<, operator>>, operator<, and operator>, which have to be treated
950 * specially so that they do not count towards the nesting of '<>'.
951 * This function assumes that the delimited text does not contain strings.
953 static void skipNestedAngles(const char*& s)
955 const char* p = s;
957 int nest = 1;
958 p++; // skip the initial '<'
959 while (*p && nest > 0)
961 // Below we can check for p-s >= 9 instead of 8 because
962 // *s is '<' and cannot be part of "operator".
963 if (*p == '<')
965 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
966 if (p[1] == '<')
967 p++;
968 } else {
969 nest++;
972 else if (*p == '>')
974 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
975 if (p[1] == '>')
976 p++;
977 } else {
978 nest--;
981 p++;
983 if (nest != 0) {
984 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
986 s = p;
990 * Find the end of line that is not inside braces
992 static void findEnd(const char*& s)
994 const char* p = s;
995 while (*p && *p!='\n') {
996 while (*p && *p!='\n' && *p!='{')
997 p++;
998 if (*p=='{') {
999 p++;
1000 skipNested(p, '{', '}'); p--;
1003 s = p;
1006 static bool isNumberish(const char ch)
1008 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
1011 void skipString(const char*& p)
1013 moreStrings:
1014 // opening quote
1015 char quote = *p++;
1016 while (*p != quote) {
1017 if (*p == '\\') {
1018 // skip escaped character
1019 // no special treatment for octal values necessary
1020 p++;
1022 // simply return if no more characters
1023 if (*p == '\0')
1024 return;
1025 p++;
1027 // closing quote
1028 p++;
1030 * Strings can consist of several parts, some of which contain repeated
1031 * characters.
1033 if (quote == '\'') {
1034 // look ahaead for <repeats 123 times>
1035 const char* q = p+1;
1036 while (isspace(*q))
1037 q++;
1038 if (strncmp(q, "<repeats ", 9) == 0) {
1039 p = q+9;
1040 while (*p != '\0' && *p != '>')
1041 p++;
1042 if (*p != '\0') {
1043 p++; /* skip the '>' */
1047 // is the string continued?
1048 if (*p == ',') {
1049 // look ahead for another quote
1050 const char* q = p+1;
1051 while (isspace(*q))
1052 q++;
1053 if (*q == '"' || *q == '\'') {
1054 // yes!
1055 p = q;
1056 goto moreStrings;
1059 /* very long strings are followed by `...' */
1060 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1061 p += 3;
1065 static void skipNestedWithString(const char*& s, char opening, char closing)
1067 const char* p = s;
1069 // parse a nested expression
1070 int nest = 1;
1071 p++;
1073 * Search for next matching `closing' char, skipping nested pairs of
1074 * `opening' and `closing' as well as strings.
1076 while (*p && nest > 0) {
1077 if (*p == opening) {
1078 nest++;
1079 } else if (*p == closing) {
1080 nest--;
1081 } else if (*p == '\'' || *p == '\"') {
1082 skipString(p);
1083 continue;
1085 p++;
1087 if (nest > 0) {
1088 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1090 s = p;
1093 inline void skipName(const char*& p)
1095 // allow : (for enumeration values) and $ and . (for _vtbl.)
1096 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1097 p++;
1100 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1102 kind = VarTree::NKplain;
1104 const char* p = s;
1105 // examples of names:
1106 // name
1107 // <Object>
1108 // <string<a,b<c>,7> >
1110 if (*p == '<') {
1111 skipNestedAngles(p);
1112 name = QString::fromLatin1(s, p - s);
1113 kind = VarTree::NKtype;
1115 else
1117 // name, which might be "static"; allow dot for "_vtbl."
1118 skipName(p);
1119 if (p == s) {
1120 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1121 return false;
1123 int len = p - s;
1124 if (len == 6 && strncmp(s, "static", 6) == 0) {
1125 kind = VarTree::NKstatic;
1127 // its a static variable, name comes now
1128 while (isspace(*p))
1129 p++;
1130 s = p;
1131 skipName(p);
1132 if (p == s) {
1133 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1134 return false;
1136 len = p - s;
1138 name = QString::fromLatin1(s, len);
1140 // return the new position
1141 s = p;
1142 return true;
1145 static bool parseValue(const char*& s, ExprValue* variable)
1147 variable->m_value = "";
1149 repeat:
1150 if (*s == '{') {
1151 // Sometimes we find the following output:
1152 // {<text variable, no debug info>} 0x40012000 <access>
1153 // {<data variable, no debug info>}
1154 // {<variable (not text or data), no debug info>}
1155 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1156 strncmp(s, "{<data variable, ", 17) == 0 ||
1157 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1159 const char* start = s;
1160 skipNested(s, '{', '}');
1161 variable->m_value = QString::fromLatin1(start, s-start);
1162 variable->m_value += ' '; // add only a single space
1163 while (isspace(*s))
1164 s++;
1165 goto repeat;
1167 else
1169 s++;
1170 if (!parseNested(s, variable)) {
1171 return false;
1173 // must be the closing brace
1174 if (*s != '}') {
1175 TRACE("parse error: missing } of " + variable->m_name);
1176 return false;
1178 s++;
1179 // final white space
1180 while (isspace(*s))
1181 s++;
1183 } else {
1184 // examples of leaf values (cannot be the empty string):
1185 // 123
1186 // -123
1187 // 23.575e+37
1188 // 0x32a45
1189 // @0x012ab4
1190 // (DwContentType&) @0x8123456: {...}
1191 // 0x32a45 "text"
1192 // 10 '\n'
1193 // <optimized out>
1194 // 0x823abc <Array<int> virtual table>
1195 // (void (*)()) 0x8048480 <f(E *, char)>
1196 // (E *) 0xbffff450
1197 // red
1198 // &parseP (HTMLClueV *, char *)
1199 // Variable "x" is not available.
1200 // The value of variable 'x' is distributed...
1201 // -nan(0xfffff081defa0)
1203 const char*p = s;
1205 // check for type
1206 QString type;
1207 if (*p == '(') {
1208 skipNested(p, '(', ')');
1210 while (isspace(*p))
1211 p++;
1212 variable->m_value = QString::fromLatin1(s, p - s);
1215 bool reference = false;
1216 if (*p == '@') {
1217 // skip reference marker
1218 p++;
1219 reference = true;
1221 const char* start = p;
1222 if (*p == '-')
1223 p++;
1225 // some values consist of more than one token
1226 bool checkMultiPart = false;
1228 if (p[0] == '0' && p[1] == 'x') {
1229 // parse hex number
1230 p += 2;
1231 while (isxdigit(*p))
1232 p++;
1235 * Assume this is a pointer, but only if it's not a reference, since
1236 * references can't be expanded.
1238 if (!reference) {
1239 variable->m_varKind = VarTree::VKpointer;
1240 } else {
1242 * References are followed by a colon, in which case we'll
1243 * find the value following the reference address.
1245 if (*p == ':') {
1246 p++;
1247 } else {
1248 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1249 reference = false;
1252 checkMultiPart = true;
1253 } else if (isdigit(*p)) {
1254 // parse decimal number, possibly a float
1255 while (isdigit(*p))
1256 p++;
1257 if (*p == '.') { /* TODO: obey i18n? */
1258 // In long arrays an integer may be followed by '...'.
1259 // We test for this situation and don't gobble the '...'.
1260 if (p[1] != '.' || p[0] != '.') {
1261 // fractional part
1262 p++;
1263 while (isdigit(*p))
1264 p++;
1267 if (*p == 'e' || *p == 'E') {
1268 p++;
1269 // exponent
1270 if (*p == '-' || *p == '+')
1271 p++;
1272 while (isdigit(*p))
1273 p++;
1276 // for char variables there is the char, eg. 10 '\n'
1277 checkMultiPart = true;
1278 } else if (*p == '<') {
1279 // e.g. <optimized out>
1280 skipNestedAngles(p);
1281 } else if (*p == '"' || *p == '\'') {
1282 // character may have multipart: '\000' <repeats 11 times>
1283 checkMultiPart = *p == '\'';
1284 // found a string
1285 skipString(p);
1286 } else if (*p == '&') {
1287 // function pointer
1288 p++;
1289 skipName(p);
1290 while (isspace(*p)) {
1291 p++;
1293 if (*p == '(') {
1294 skipNested(p, '(', ')');
1296 } else if (strncmp(p, "Variable \"", 10) == 0) {
1297 // Variable "x" is not available.
1298 p += 10; // skip to "
1299 skipName(p);
1300 if (strncmp(p, "\" is not available.", 19) == 0) {
1301 p += 19;
1303 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1304 p += 23;
1305 skipName(p);
1306 const char* e = strchr(p, '.');
1307 if (e == 0) {
1308 p += strlen(p);
1309 } else {
1310 p = e+1;
1312 } else {
1313 // must be an enumeration value
1314 skipName(p);
1315 // hmm, not necessarily: nan (floating point Not a Number)
1316 // is followed by a number in ()
1317 if (*p == '(')
1318 skipNested(p, '(', ')');
1320 variable->m_value += QString::fromLatin1(start, p - start);
1322 // remove line breaks from the value; this is ok since
1323 // string values never contain a literal line break
1324 variable->m_value.replace('\n', ' ');
1326 if (checkMultiPart) {
1327 // white space
1328 while (isspace(*p))
1329 p++;
1330 // may be followed by a string or <...>
1331 start = p;
1333 if (*p == '"' || *p == '\'') {
1334 skipString(p);
1335 } else if (*p == '<') {
1336 // if this value is part of an array, it might be followed
1337 // by <repeats 15 times>, which we don't skip here
1338 if (strncmp(p, "<repeats ", 9) != 0)
1339 skipNestedAngles(p);
1341 if (p != start) {
1342 // there is always a blank before the string,
1343 // which we will include in the final string value
1344 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1345 // if this was a pointer, reset that flag since we
1346 // now got the value
1347 variable->m_varKind = VarTree::VKsimple;
1351 if (variable->m_value.length() == 0) {
1352 TRACE("parse error: no value for " + variable->m_name);
1353 return false;
1356 // final white space
1357 while (isspace(*p))
1358 p++;
1359 s = p;
1362 * If this was a reference, the value follows. It might even be a
1363 * composite variable!
1365 if (reference) {
1366 goto repeat;
1370 return true;
1373 static bool parseNested(const char*& s, ExprValue* variable)
1375 // could be a structure or an array
1376 while (isspace(*s))
1377 s++;
1379 const char* p = s;
1380 bool isStruct = false;
1382 * If there is a name followed by an = or an < -- which starts a type
1383 * name -- or "static", it is a structure
1385 if (*p == '<' || *p == '}') {
1386 isStruct = true;
1387 } else if (strncmp(p, "static ", 7) == 0) {
1388 isStruct = true;
1389 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1390 // look ahead for a comma after the name
1391 skipName(p);
1392 while (isspace(*p))
1393 p++;
1394 if (*p == '=') {
1395 isStruct = true;
1397 p = s; /* rescan the name */
1399 if (isStruct) {
1400 if (!parseVarSeq(p, variable)) {
1401 return false;
1403 variable->m_varKind = VarTree::VKstruct;
1404 } else {
1405 if (!parseValueSeq(p, variable)) {
1406 return false;
1408 variable->m_varKind = VarTree::VKarray;
1410 s = p;
1411 return true;
1414 static bool parseVarSeq(const char*& s, ExprValue* variable)
1416 // parse a comma-separated sequence of variables
1417 ExprValue* var = variable; /* var != 0 to indicate success if empty seq */
1418 for (;;) {
1419 if (*s == '}')
1420 break;
1421 if (strncmp(s, "<No data fields>}", 17) == 0)
1423 // no member variables, so break out immediately
1424 s += 16; /* go to the closing brace */
1425 break;
1427 var = parseVar(s);
1428 if (var == 0)
1429 break; /* syntax error */
1430 variable->appendChild(var);
1431 if (*s != ',')
1432 break;
1433 // skip the comma and whitespace
1434 s++;
1435 while (isspace(*s))
1436 s++;
1438 return var != 0;
1441 static bool parseValueSeq(const char*& s, ExprValue* variable)
1443 // parse a comma-separated sequence of variables
1444 int index = 0;
1445 bool good;
1446 for (;;) {
1447 QString name;
1448 name.sprintf("[%d]", index);
1449 ExprValue* var = new ExprValue(name, VarTree::NKplain);
1450 good = parseValue(s, var);
1451 if (!good) {
1452 delete var;
1453 return false;
1455 // a value may be followed by "<repeats 45 times>"
1456 if (strncmp(s, "<repeats ", 9) == 0) {
1457 s += 9;
1458 char* end;
1459 int l = strtol(s, &end, 10);
1460 if (end == s || strncmp(end, " times>", 7) != 0) {
1461 // should not happen
1462 delete var;
1463 return false;
1465 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1466 // replace name and advance index
1467 name.sprintf("[%d .. %d]", index, index+l-1);
1468 var->m_name = name;
1469 index += l;
1470 // skip " times>" and space
1471 s = end+7;
1472 // possible final space
1473 while (isspace(*s))
1474 s++;
1475 } else {
1476 index++;
1478 variable->appendChild(var);
1479 // long arrays may be terminated by '...'
1480 if (strncmp(s, "...", 3) == 0) {
1481 s += 3;
1482 ExprValue* var = new ExprValue("...", VarTree::NKplain);
1483 var->m_value = i18n("<additional entries of the array suppressed>");
1484 variable->appendChild(var);
1485 break;
1487 if (*s != ',') {
1488 break;
1490 // skip the comma and whitespace
1491 s++;
1492 while (isspace(*s))
1493 s++;
1494 // sometimes there is a closing brace after a comma
1495 // if (*s == '}')
1496 // break;
1498 return true;
1502 * Parses a stack frame.
1504 static void parseFrameInfo(const char*& s, QString& func,
1505 QString& file, int& lineNo, DbgAddr& address)
1507 const char* p = s;
1509 // next may be a hexadecimal address
1510 if (*p == '0') {
1511 const char* start = p;
1512 p++;
1513 if (*p == 'x')
1514 p++;
1515 while (isxdigit(*p))
1516 p++;
1517 address = QString::fromLatin1(start, p-start);
1518 if (strncmp(p, " in ", 4) == 0)
1519 p += 4;
1520 } else {
1521 address = DbgAddr();
1523 const char* start = p;
1524 // check for special signal handler frame
1525 if (strncmp(p, "<signal handler called>", 23) == 0) {
1526 func = QString::fromLatin1(start, 23);
1527 file = QString();
1528 lineNo = -1;
1529 s = p+23;
1530 if (*s == '\n')
1531 s++;
1532 return;
1536 * Skip the function name. It is terminated by a left parenthesis
1537 * which does not delimit "(anonymous namespace)" and which is
1538 * outside the angle brackets <> of template parameter lists.
1540 while (*p != '\0')
1542 if (*p == '<') {
1543 // check for operator<< and operator<
1544 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1546 p++;
1547 if (*p == '<')
1548 p++;
1550 else
1552 // skip template parameter list
1553 skipNestedAngles(p);
1555 } else if (*p == '(') {
1556 if (strncmp(p, "(anonymous namespace)", 21) != 0)
1557 break; // parameter list found
1558 p += 21;
1559 } else {
1560 p++;
1564 if (*p == '\0') {
1565 func = start;
1566 file = QString();
1567 lineNo = -1;
1568 s = p;
1569 return;
1572 * Skip parameters. But notice that for complicated conversion
1573 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1574 * pointer to function) as well as operator()(...) we have to skip
1575 * additional pairs of parentheses. Furthermore, recent gdbs write the
1576 * demangled name followed by the arguments in a pair of parentheses,
1577 * where the demangled name can end in "const".
1579 do {
1580 skipNestedWithString(p, '(', ')');
1581 while (isspace(*p))
1582 p++;
1583 // skip "const"
1584 if (strncmp(p, "const", 5) == 0) {
1585 p += 5;
1586 while (isspace(*p))
1587 p++;
1589 } while (*p == '(');
1591 // check for file position
1592 if (strncmp(p, "at ", 3) == 0) {
1593 p += 3;
1594 const char* fileStart = p;
1595 // go for the end of the line
1596 while (*p != '\0' && *p != '\n')
1597 p++;
1598 // search back for colon
1599 const char* colon = p;
1600 do {
1601 --colon;
1602 } while (*colon != ':');
1603 file = QString::fromLatin1(fileStart, colon-fileStart);
1604 lineNo = atoi(colon+1)-1;
1605 // skip new-line
1606 if (*p != '\0')
1607 p++;
1608 } else {
1609 // check for "from shared lib"
1610 if (strncmp(p, "from ", 5) == 0) {
1611 p += 5;
1612 // go for the end of the line
1613 while (*p != '\0' && *p != '\n')
1614 p++;
1615 // skip new-line
1616 if (*p != '\0')
1617 p++;
1619 file = "";
1620 lineNo = -1;
1622 // construct the function name (including file info)
1623 if (*p == '\0') {
1624 func = start;
1625 } else {
1626 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1628 s = p;
1631 * Replace \n (and whitespace around it) in func by a blank. We cannot
1632 * use QString::simplifyWhiteSpace() for this because this would also
1633 * simplify space that belongs to a string arguments that gdb sometimes
1634 * prints in the argument lists of the function.
1636 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1637 int nl = 0;
1638 while ((nl = func.find('\n', nl)) >= 0) {
1639 // search back to the beginning of the whitespace
1640 int startWhite = nl;
1641 do {
1642 --startWhite;
1643 } while (isspace(func[startWhite].latin1()));
1644 startWhite++;
1645 // search forward to the end of the whitespace
1646 do {
1647 nl++;
1648 } while (isspace(func[nl].latin1()));
1649 // replace
1650 func.replace(startWhite, nl-startWhite, " ");
1651 /* continue searching for more \n's at this place: */
1652 nl = startWhite+1;
1658 * Parses a stack frame including its frame number
1660 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1661 QString& file, int& lineNo, DbgAddr& address)
1663 // Example:
1664 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1665 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1667 // must start with a hash mark followed by number
1668 // or with "Breakpoint " followed by number and comma
1669 if (s[0] == '#') {
1670 if (!isdigit(s[1]))
1671 return false;
1672 s++; /* skip the hash mark */
1673 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1674 if (!isdigit(s[11]))
1675 return false;
1676 s += 11; /* skip "Breakpoint" */
1677 } else
1678 return false;
1680 // frame number
1681 frameNo = atoi(s);
1682 while (isdigit(*s))
1683 s++;
1684 // space and comma
1685 while (isspace(*s) || *s == ',')
1686 s++;
1687 parseFrameInfo(s, func, file, lineNo, address);
1688 return true;
1691 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1693 QString func, file;
1694 int lineNo, frameNo;
1695 DbgAddr address;
1697 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1698 StackFrame* frm = new StackFrame;
1699 frm->frameNo = frameNo;
1700 frm->fileName = file;
1701 frm->lineNo = lineNo;
1702 frm->address = address;
1703 frm->var = new ExprValue(func, VarTree::NKplain);
1704 stack.append(frm);
1708 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1709 QString& file, int& lineNo, DbgAddr& address)
1711 QString func;
1712 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1716 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1718 // skip first line, which is the headline
1719 const char* p = strchr(output, '\n');
1720 if (p == 0)
1721 return false;
1722 p++;
1723 if (*p == '\0')
1724 return false;
1726 // split up a line
1727 QString location;
1728 QString address;
1729 int hits = 0;
1730 uint ignoreCount = 0;
1731 QString condition;
1732 const char* end;
1733 char* dummy;
1734 while (*p != '\0') {
1735 // get Num
1736 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1737 p = dummy;
1738 // get Type
1739 while (isspace(*p))
1740 p++;
1741 Breakpoint::Type bpType = Breakpoint::breakpoint;
1742 if (strncmp(p, "breakpoint", 10) == 0) {
1743 p += 10;
1744 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1745 bpType = Breakpoint::watchpoint;
1746 p += 13;
1747 } else if (strncmp(p, "watchpoint", 10) == 0) {
1748 bpType = Breakpoint::watchpoint;
1749 p += 10;
1751 while (isspace(*p))
1752 p++;
1753 if (*p == '\0')
1754 break;
1755 // get Disp
1756 char disp = *p++;
1757 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1758 p++;
1759 while (isspace(*p))
1760 p++;
1761 if (*p == '\0')
1762 break;
1763 // get Enb
1764 char enable = *p++;
1765 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1766 p++;
1767 while (isspace(*p))
1768 p++;
1769 if (*p == '\0')
1770 break;
1771 // the address, if present
1772 if (bpType == Breakpoint::breakpoint &&
1773 strncmp(p, "0x", 2) == 0)
1775 const char* start = p;
1776 while (*p != '\0' && !isspace(*p))
1777 p++;
1778 address = QString::fromLatin1(start, p-start);
1779 while (isspace(*p) && *p != '\n')
1780 p++;
1781 if (*p == '\0')
1782 break;
1783 } else {
1784 address = QString();
1786 // remainder is location, hit and ignore count, condition
1787 hits = 0;
1788 ignoreCount = 0;
1789 condition = QString();
1790 end = strchr(p, '\n');
1791 if (end == 0) {
1792 location = p;
1793 p += location.length();
1794 } else {
1795 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1796 p = end+1; /* skip over \n */
1799 // may be continued in next line
1800 while (isspace(*p)) { /* p points to beginning of line */
1801 // skip white space at beginning of line
1802 while (isspace(*p))
1803 p++;
1805 // seek end of line
1806 end = strchr(p, '\n');
1807 if (end == 0)
1808 end = p+strlen(p);
1810 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1811 // extract the hit count
1812 p += 22;
1813 hits = strtol(p, &dummy, 10);
1814 TRACE(QString().sprintf("hit count %d", hits));
1815 } else if (strncmp(p, "stop only if ", 13) == 0) {
1816 // extract condition
1817 p += 13;
1818 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1819 TRACE("condition: "+condition);
1820 } else if (strncmp(p, "ignore next ", 12) == 0) {
1821 // extract ignore count
1822 p += 12;
1823 ignoreCount = strtol(p, &dummy, 10);
1824 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1825 } else {
1826 // indeed a continuation
1827 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1829 p = end;
1830 if (*p != '\0')
1831 p++; /* skip '\n' */
1833 Breakpoint* bp = new Breakpoint;
1834 bp->id = bpNum;
1835 bp->type = bpType;
1836 bp->temporary = disp == 'd';
1837 bp->enabled = enable == 'y';
1838 bp->location = location;
1839 bp->address = address;
1840 bp->hitCount = hits;
1841 bp->ignoreCount = ignoreCount;
1842 bp->condition = condition;
1843 brks.append(bp);
1845 return true;
1848 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1850 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1851 // no threads
1852 return true;
1855 int id;
1856 QString systag;
1857 QString func, file;
1858 int lineNo;
1859 DbgAddr address;
1861 const char* p = output;
1862 while (*p != '\0') {
1863 // seach look for thread id, watching out for the focus indicator
1864 bool hasFocus = false;
1865 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1866 p++;
1867 if (*p == '*') {
1868 hasFocus = true;
1869 p++;
1870 // there follows only whitespace
1872 char* end;
1873 id = strtol(p, &end, 10);
1874 if (p == end) {
1875 // syntax error: no number found; bail out
1876 return true;
1878 p = end;
1880 // skip space
1881 while (isspace(*p))
1882 p++;
1885 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1887 end = strstr(p, " ");
1888 if (end == 0) {
1889 // syntax error; bail out
1890 return true;
1892 systag = QString::fromLatin1(p, end-p);
1893 p = end+2;
1896 * Now follows a standard stack frame. Sometimes, however, gdb
1897 * catches a thread at an instant where it doesn't have a stack.
1899 if (strncmp(p, "[No stack.]", 11) != 0) {
1900 ::parseFrameInfo(p, func, file, lineNo, address);
1901 } else {
1902 func = "[No stack]";
1903 file = QString();
1904 lineNo = -1;
1905 address = QString();
1906 p += 11; /* \n is skipped above */
1909 ThreadInfo* thr = new ThreadInfo;
1910 thr->id = id;
1911 thr->threadName = systag;
1912 thr->hasFocus = hasFocus;
1913 thr->function = func;
1914 thr->fileName = file;
1915 thr->lineNo = lineNo;
1916 thr->address = address;
1917 threads.append(thr);
1919 return true;
1922 static bool parseNewBreakpoint(const char* o, int& id,
1923 QString& file, int& lineNo, QString& address);
1924 static bool parseNewWatchpoint(const char* o, int& id,
1925 QString& expr);
1927 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1928 QString& file, int& lineNo, QString& address)
1930 const char* o = output;
1931 // skip lines of that begin with "(Cannot find"
1932 while (strncmp(o, "(Cannot find", 12) == 0) {
1933 o = strchr(o, '\n');
1934 if (o == 0)
1935 return false;
1936 o++; /* skip newline */
1939 if (strncmp(o, "Breakpoint ", 11) == 0) {
1940 output += 11; /* skip "Breakpoint " */
1941 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1942 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1943 output += 20;
1944 return ::parseNewWatchpoint(output, id, address);
1945 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1946 output += 11;
1947 return ::parseNewWatchpoint(output, id, address);
1949 return false;
1952 static bool parseNewBreakpoint(const char* o, int& id,
1953 QString& file, int& lineNo, QString& address)
1955 // breakpoint id
1956 char* p;
1957 id = strtoul(o, &p, 10);
1958 if (p == o)
1959 return false;
1961 // check for the address
1962 if (strncmp(p, " at 0x", 6) == 0) {
1963 char* start = p+4; /* skip " at ", but not 0x */
1964 p += 6;
1965 while (isxdigit(*p))
1966 ++p;
1967 address = QString::fromLatin1(start, p-start);
1970 // file name
1971 char* fileStart = strstr(p, "file ");
1972 if (fileStart == 0)
1973 return !address.isEmpty(); /* parse error only if there's no address */
1974 fileStart += 5;
1976 // line number
1977 char* numStart = strstr(fileStart, ", line ");
1978 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1979 numStart += 7;
1980 int line = strtoul(numStart, &p, 10);
1981 if (numStart == p)
1982 return false;
1984 file = fileName;
1985 lineNo = line-1; /* zero-based! */
1986 return true;
1989 static bool parseNewWatchpoint(const char* o, int& id,
1990 QString& expr)
1992 // watchpoint id
1993 char* p;
1994 id = strtoul(o, &p, 10);
1995 if (p == o)
1996 return false;
1998 if (strncmp(p, ": ", 2) != 0)
1999 return false;
2000 p += 2;
2002 // all the rest on the line is the expression
2003 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
2004 return true;
2007 void GdbDriver::parseLocals(const char* output, QList<ExprValue>& newVars)
2009 // check for possible error conditions
2010 if (strncmp(output, "No symbol table", 15) == 0)
2012 return;
2015 while (*output != '\0') {
2016 while (isspace(*output))
2017 output++;
2018 if (*output == '\0')
2019 break;
2020 // skip occurrences of "No locals" and "No args"
2021 if (strncmp(output, "No locals", 9) == 0 ||
2022 strncmp(output, "No arguments", 12) == 0)
2024 output = strchr(output, '\n');
2025 if (output == 0) {
2026 break;
2028 continue;
2031 ExprValue* variable = parseVar(output);
2032 if (variable == 0) {
2033 break;
2035 // do not add duplicates
2036 for (ExprValue* o = newVars.first(); o != 0; o = newVars.next()) {
2037 if (o->m_name == variable->m_name) {
2038 delete variable;
2039 goto skipDuplicate;
2042 newVars.append(variable);
2043 skipDuplicate:;
2047 ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2049 ExprValue* var = 0;
2050 // check for error conditions
2051 if (!parseErrorMessage(output, var, wantErrorValue))
2053 // parse the variable
2054 var = parseVar(output);
2056 return var;
2059 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2061 bool isGood = false;
2062 message = QString(output).simplifyWhiteSpace();
2063 if (message.isEmpty()) {
2064 message = i18n("New working directory: ") + m_programWD;
2065 isGood = true;
2067 return isGood;
2070 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2072 message = output;
2074 m_haveCoreFile = false;
2077 * Lines starting with the following do not indicate errors:
2078 * Using host libthread_db
2079 * (no debugging symbols found)
2081 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2082 strncmp(output, "(no debugging symbols found)", 28) == 0)
2084 // this line is good, go to the next one
2085 const char* end = strchr(output, '\n');
2086 if (end == 0)
2087 output += strlen(output);
2088 else
2089 output = end+1;
2093 * If we've parsed all lines, there was no error.
2095 return output[0] == '\0';
2098 bool GdbDriver::parseCoreFile(const char* output)
2100 // if command succeeded, gdb emits a line starting with "#0 "
2101 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2102 return m_haveCoreFile;
2105 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2107 // optionally: "program changed, rereading symbols",
2108 // followed by:
2109 // "Program exited normally"
2110 // "Program terminated with wignal SIGSEGV"
2111 // "Program received signal SIGINT" or other signal
2112 // "Breakpoint..."
2114 // go through the output, line by line, checking what we have
2115 const char* start = output - 1;
2116 uint flags = SFprogramActive;
2117 message = QString();
2118 do {
2119 start++; /* skip '\n' */
2121 if (strncmp(start, "Program ", 8) == 0 ||
2122 strncmp(start, "ptrace: ", 8) == 0) {
2124 * When we receive a signal, the program remains active.
2126 * Special: If we "stopped" in a corefile, the string "Program
2127 * terminated with signal"... is displayed. (Normally, we see
2128 * "Program received signal"... when a signal happens.)
2130 if (strncmp(start, "Program exited", 14) == 0 ||
2131 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2132 strncmp(start, "ptrace: ", 8) == 0)
2134 flags &= ~SFprogramActive;
2137 // set message
2138 const char* endOfMessage = strchr(start, '\n');
2139 if (endOfMessage == 0)
2140 endOfMessage = start + strlen(start);
2141 message = QString::fromLatin1(start, endOfMessage-start);
2142 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2144 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2145 * that it stopped at a temporary breakpoint).
2147 flags |= SFrefreshBreak;
2148 } else if (strstr(start, "re-reading symbols.") != 0) {
2149 flags |= SFrefreshSource;
2152 // next line, please
2153 start = strchr(start, '\n');
2154 } while (start != 0);
2157 * Gdb only notices when new threads have appeared, but not when a
2158 * thread finishes. So we always have to assume that the list of
2159 * threads has changed.
2161 flags |= SFrefreshThreads;
2163 return flags;
2166 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2168 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2169 return;
2171 // parse the table of shared libraries
2173 // strip off head line
2174 output = strchr(output, '\n');
2175 if (output == 0)
2176 return;
2177 output++; /* skip '\n' */
2178 QString shlibName;
2179 while (*output != '\0') {
2180 // format of a line is
2181 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2182 // 3 blocks of non-space followed by space
2183 for (int i = 0; *output != '\0' && i < 3; i++) {
2184 while (*output != '\0' && !isspace(*output)) { /* non-space */
2185 output++;
2187 while (isspace(*output)) { /* space */
2188 output++;
2191 if (*output == '\0')
2192 return;
2193 const char* start = output;
2194 output = strchr(output, '\n');
2195 if (output == 0)
2196 output = start + strlen(start);
2197 shlibName = QString::fromLatin1(start, output-start);
2198 if (*output != '\0')
2199 output++;
2200 shlibs.append(shlibName);
2201 TRACE("found shared lib " + shlibName);
2205 bool GdbDriver::parseFindType(const char* output, QString& type)
2207 if (strncmp(output, "type = ", 7) != 0)
2208 return false;
2211 * Everything else is the type. We strip off any leading "const" and any
2212 * trailing "&" on the grounds that neither affects the decoding of the
2213 * object. We also strip off all white-space from the type.
2215 output += 7;
2216 if (strncmp(output, "const ", 6) == 0)
2217 output += 6;
2218 type = output;
2219 type.replace(QRegExp("\\s+"), "");
2220 if (type.endsWith("&"))
2221 type.truncate(type.length() - 1);
2222 return true;
2225 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2227 if (strncmp(output, "The program has no registers now", 32) == 0) {
2228 return;
2231 QString regName;
2232 QString value;
2234 // parse register values
2235 while (*output != '\0')
2237 // skip space at the start of the line
2238 while (isspace(*output))
2239 output++;
2241 // register name
2242 const char* start = output;
2243 while (*output != '\0' && !isspace(*output))
2244 output++;
2245 if (*output == '\0')
2246 break;
2247 regName = QString::fromLatin1(start, output-start);
2249 // skip space
2250 while (isspace(*output))
2251 output++;
2253 RegisterInfo* reg = new RegisterInfo;
2254 reg->regName = regName;
2257 * If we find a brace now, this is a vector register. We look for
2258 * the closing brace and treat the result as cooked value.
2260 if (*output == '{')
2262 start = output;
2263 skipNested(output, '{', '}');
2264 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2265 // skip space, but not the end of line
2266 while (isspace(*output) && *output != '\n')
2267 output++;
2268 // get rid of the braces at the begining and the end
2269 value.remove(0, 1);
2270 if (value[value.length()-1] == '}') {
2271 value = value.left(value.length()-1);
2273 // gdb 5.3 doesn't print a separate set of raw values
2274 if (*output == '{') {
2275 // another set of vector follows
2276 // what we have so far is the raw value
2277 reg->rawValue = value;
2279 start = output;
2280 skipNested(output, '{', '}');
2281 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2282 } else {
2283 // for gdb 5.3
2284 // find first type that does not have an array, this is the RAW value
2285 const char* end=start;
2286 findEnd(end);
2287 const char* cur=start;
2288 while (cur<end) {
2289 while (*cur != '=' && cur<end)
2290 cur++;
2291 cur++;
2292 while (isspace(*cur) && cur<end)
2293 cur++;
2294 if (isNumberish(*cur)) {
2295 end=cur;
2296 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2297 end++;
2298 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2299 reg->rawValue = rawValue;
2301 if (rawValue.left(2)=="0x") {
2302 // ok we have a raw value, now get it's type
2303 end=cur-1;
2304 while (isspace(*end) || *end=='=') end--;
2305 end++;
2306 cur=end-1;
2307 while (*cur!='{' && *cur!=' ')
2308 cur--;
2309 cur++;
2310 reg->type=QString::fromLatin1(cur, end-cur);
2313 // end while loop
2314 cur=end;
2317 // skip to the end of line
2318 while (*output != '\0' && *output != '\n')
2319 output++;
2320 // get rid of the braces at the begining and the end
2321 value.remove(0, 1);
2322 if (value[value.length()-1] == '}') {
2323 value = value.left(value.length()-1);
2326 reg->cookedValue = value;
2328 else
2330 // the rest of the line is the register value
2331 start = output;
2332 output = strchr(output,'\n');
2333 if (output == 0)
2334 output = start + strlen(start);
2335 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2338 * We split the raw from the cooked values.
2339 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2340 * Here, the cooked value comes first, and the raw value is in
2341 * the second part.
2343 int pos = value.find(" (raw ");
2344 if (pos >= 0)
2346 reg->cookedValue = value.left(pos);
2347 reg->rawValue = value.mid(pos+6);
2348 if (reg->rawValue.right(1) == ")") // remove closing bracket
2349 reg->rawValue.truncate(reg->rawValue.length()-1);
2351 else
2354 * In other cases we split off the first token (separated by
2355 * whitespace). It is the raw value. The remainder of the line
2356 * is the cooked value.
2358 int pos = value.find(' ');
2359 if (pos < 0) {
2360 reg->rawValue = value;
2361 reg->cookedValue = QString();
2362 } else {
2363 reg->rawValue = value.left(pos);
2364 reg->cookedValue = value.mid(pos+1);
2368 if (*output != '\0')
2369 output++; /* skip '\n' */
2371 regs.append(reg);
2375 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2377 // "is at address" or "starts at address"
2378 const char* start = strstr(output, "s at address ");
2379 if (start == 0)
2380 return false;
2382 start += 13;
2383 const char* p = start;
2384 while (*p != '\0' && !isspace(*p))
2385 p++;
2386 addrFrom = QString::fromLatin1(start, p-start);
2388 start = strstr(p, "and ends at ");
2389 if (start == 0) {
2390 addrTo = addrFrom;
2391 return true;
2394 start += 12;
2395 p = start;
2396 while (*p != '\0' && !isspace(*p))
2397 p++;
2398 addrTo = QString::fromLatin1(start, p-start);
2400 return true;
2403 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2405 code.clear();
2407 if (strncmp(output, "Dump of assembler", 17) != 0) {
2408 // error message?
2409 DisassembledCode c;
2410 c.code = output;
2411 code.append(new DisassembledCode(c));
2412 return;
2415 // remove first line
2416 const char* p = strchr(output, '\n');
2417 if (p == 0)
2418 return; /* not a regular output */
2420 p++;
2422 // remove last line
2423 const char* end = strstr(output, "End of assembler");
2424 if (end == 0)
2425 end = p + strlen(p);
2427 DbgAddr address;
2429 // remove function offsets from the lines
2430 while (p != end)
2432 const char* start = p;
2433 // address
2434 while (p != end && !isspace(*p))
2435 p++;
2436 address = QString::fromLatin1(start, p-start);
2438 // function name (enclosed in '<>', followed by ':')
2439 while (p != end && *p != '<')
2440 p++;
2441 if (*p == '<')
2442 skipNestedAngles(p);
2443 if (*p == ':')
2444 p++;
2446 // space until code
2447 while (p != end && isspace(*p))
2448 p++;
2450 // code until end of line
2451 start = p;
2452 while (p != end && *p != '\n')
2453 p++;
2454 if (p != end) /* include '\n' */
2455 p++;
2457 DisassembledCode* c = new DisassembledCode;
2458 c->address = address;
2459 c->code = QString::fromLatin1(start, p-start);
2460 code.append(c);
2464 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2466 if (isErrorExpr(output)) {
2467 // error; strip space
2468 QString msg = output;
2469 return msg.stripWhiteSpace();
2472 const char* p = output; /* save typing */
2473 DbgAddr addr;
2474 QString dump;
2476 // the address
2477 while (*p != 0) {
2478 const char* start = p;
2479 while (*p != '\0' && *p != ':' && !isspace(*p))
2480 p++;
2481 addr = QString::fromLatin1(start, p-start);
2482 if (*p != ':') {
2483 // parse function offset
2484 while (isspace(*p))
2485 p++;
2486 start = p;
2487 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2488 p++;
2489 addr.fnoffs = QString::fromLatin1(start, p-start);
2491 if (*p == ':')
2492 p++;
2493 // skip space; this may skip a new-line char!
2494 while (isspace(*p))
2495 p++;
2496 // everything to the end of the line is the memory dump
2497 const char* end = strchr(p, '\n');
2498 if (end != 0) {
2499 dump = QString::fromLatin1(p, end-p);
2500 p = end+1;
2501 } else {
2502 dump = QString::fromLatin1(p, strlen(p));
2503 p += strlen(p);
2505 MemoryDump* md = new MemoryDump;
2506 md->address = addr;
2507 md->dump = dump;
2508 memdump.append(md);
2511 return QString();
2514 QString GdbDriver::editableValue(VarTree* value)
2516 const char* s = value->value().latin1();
2518 // if the variable is a pointer value that contains a cast,
2519 // remove the cast
2520 if (*s == '(') {
2521 skipNested(s, '(', ')');
2522 // skip space
2523 while (isspace(*s))
2524 ++s;
2527 repeat:
2528 const char* start = s;
2530 if (strncmp(s, "0x", 2) == 0)
2532 s += 2;
2533 while (isxdigit(*s))
2534 ++s;
2537 * What we saw so far might have been a reference. If so, edit the
2538 * referenced value. Otherwise, edit the pointer.
2540 if (*s == ':') {
2541 // a reference
2542 ++s;
2543 goto repeat;
2545 // a pointer
2546 // if it's a pointer to a string, remove the string
2547 const char* end = s;
2548 while (isspace(*s))
2549 ++s;
2550 if (*s == '"') {
2551 // a string
2552 return QString::fromLatin1(start, end-start);
2553 } else {
2554 // other pointer
2555 return QString::fromLatin1(start, strlen(start));
2559 // else leave it unchanged (or stripped of the reference preamble)
2560 return s;
2563 QString GdbDriver::parseSetVariable(const char* output)
2565 // if there is any output, it is an error message
2566 QString msg = output;
2567 return msg.stripWhiteSpace();
2571 #include "gdbdriver.moc"