Remember the width of column 1 of Locals and Watch window across sessions.
[kdbg.git] / kdbg / gdbdriver.cpp
blob627d81ec0ed96f1d32aa2f0e697c0edc05ea770e
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
1539 * and is preceded by a space.
1541 while (*p != '\0')
1543 if (*p == '<') {
1544 // check for operator<< and operator<
1545 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1547 p++;
1548 if (*p == '<')
1549 p++;
1551 else
1553 // skip template parameter list
1554 skipNestedAngles(p);
1556 } else if (*p == '(') {
1557 // this skips "(anonymous namespace)" as well as the formal
1558 // parameter list of the containing function if this is a member
1559 // of a nested class
1560 skipNestedWithString(p, '(', ')');
1561 } else if (*p == ' ') {
1562 ++p;
1563 if (*p == '(')
1564 break; // parameter list found
1565 } else {
1566 p++;
1570 if (*p == '\0') {
1571 func = start;
1572 file = QString();
1573 lineNo = -1;
1574 s = p;
1575 return;
1578 * Skip parameters. But notice that for complicated conversion
1579 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1580 * pointer to function) as well as operator()(...) we have to skip
1581 * additional pairs of parentheses. Furthermore, recent gdbs write the
1582 * demangled name followed by the arguments in a pair of parentheses,
1583 * where the demangled name can end in "const".
1585 do {
1586 skipNestedWithString(p, '(', ')');
1587 while (isspace(*p))
1588 p++;
1589 // skip "const"
1590 if (strncmp(p, "const", 5) == 0) {
1591 p += 5;
1592 while (isspace(*p))
1593 p++;
1595 } while (*p == '(');
1597 // check for file position
1598 if (strncmp(p, "at ", 3) == 0) {
1599 p += 3;
1600 const char* fileStart = p;
1601 // go for the end of the line
1602 while (*p != '\0' && *p != '\n')
1603 p++;
1604 // search back for colon
1605 const char* colon = p;
1606 do {
1607 --colon;
1608 } while (*colon != ':');
1609 file = QString::fromLatin1(fileStart, colon-fileStart);
1610 lineNo = atoi(colon+1)-1;
1611 // skip new-line
1612 if (*p != '\0')
1613 p++;
1614 } else {
1615 // check for "from shared lib"
1616 if (strncmp(p, "from ", 5) == 0) {
1617 p += 5;
1618 // go for the end of the line
1619 while (*p != '\0' && *p != '\n')
1620 p++;
1621 // skip new-line
1622 if (*p != '\0')
1623 p++;
1625 file = "";
1626 lineNo = -1;
1628 // construct the function name (including file info)
1629 if (*p == '\0') {
1630 func = start;
1631 } else {
1632 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1634 s = p;
1637 * Replace \n (and whitespace around it) in func by a blank. We cannot
1638 * use QString::simplifyWhiteSpace() for this because this would also
1639 * simplify space that belongs to a string arguments that gdb sometimes
1640 * prints in the argument lists of the function.
1642 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1643 int nl = 0;
1644 while ((nl = func.find('\n', nl)) >= 0) {
1645 // search back to the beginning of the whitespace
1646 int startWhite = nl;
1647 do {
1648 --startWhite;
1649 } while (isspace(func[startWhite].latin1()));
1650 startWhite++;
1651 // search forward to the end of the whitespace
1652 do {
1653 nl++;
1654 } while (isspace(func[nl].latin1()));
1655 // replace
1656 func.replace(startWhite, nl-startWhite, " ");
1657 /* continue searching for more \n's at this place: */
1658 nl = startWhite+1;
1664 * Parses a stack frame including its frame number
1666 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1667 QString& file, int& lineNo, DbgAddr& address)
1669 // Example:
1670 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1671 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1673 // must start with a hash mark followed by number
1674 // or with "Breakpoint " followed by number and comma
1675 if (s[0] == '#') {
1676 if (!isdigit(s[1]))
1677 return false;
1678 s++; /* skip the hash mark */
1679 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1680 if (!isdigit(s[11]))
1681 return false;
1682 s += 11; /* skip "Breakpoint" */
1683 } else
1684 return false;
1686 // frame number
1687 frameNo = atoi(s);
1688 while (isdigit(*s))
1689 s++;
1690 // space and comma
1691 while (isspace(*s) || *s == ',')
1692 s++;
1693 parseFrameInfo(s, func, file, lineNo, address);
1694 return true;
1697 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1699 QString func, file;
1700 int lineNo, frameNo;
1701 DbgAddr address;
1703 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1704 StackFrame* frm = new StackFrame;
1705 frm->frameNo = frameNo;
1706 frm->fileName = file;
1707 frm->lineNo = lineNo;
1708 frm->address = address;
1709 frm->var = new ExprValue(func, VarTree::NKplain);
1710 stack.append(frm);
1714 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1715 QString& file, int& lineNo, DbgAddr& address)
1717 QString func;
1718 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1722 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1724 // skip first line, which is the headline
1725 const char* p = strchr(output, '\n');
1726 if (p == 0)
1727 return false;
1728 p++;
1729 if (*p == '\0')
1730 return false;
1732 // split up a line
1733 QString location;
1734 QString address;
1735 int hits = 0;
1736 uint ignoreCount = 0;
1737 QString condition;
1738 const char* end;
1739 char* dummy;
1740 while (*p != '\0') {
1741 // get Num
1742 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1743 p = dummy;
1744 // get Type
1745 while (isspace(*p))
1746 p++;
1747 Breakpoint::Type bpType = Breakpoint::breakpoint;
1748 if (strncmp(p, "breakpoint", 10) == 0) {
1749 p += 10;
1750 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1751 bpType = Breakpoint::watchpoint;
1752 p += 13;
1753 } else if (strncmp(p, "watchpoint", 10) == 0) {
1754 bpType = Breakpoint::watchpoint;
1755 p += 10;
1757 while (isspace(*p))
1758 p++;
1759 if (*p == '\0')
1760 break;
1761 // get Disp
1762 char disp = *p++;
1763 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1764 p++;
1765 while (isspace(*p))
1766 p++;
1767 if (*p == '\0')
1768 break;
1769 // get Enb
1770 char enable = *p++;
1771 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1772 p++;
1773 while (isspace(*p))
1774 p++;
1775 if (*p == '\0')
1776 break;
1777 // the address, if present
1778 if (bpType == Breakpoint::breakpoint &&
1779 strncmp(p, "0x", 2) == 0)
1781 const char* start = p;
1782 while (*p != '\0' && !isspace(*p))
1783 p++;
1784 address = QString::fromLatin1(start, p-start);
1785 while (isspace(*p) && *p != '\n')
1786 p++;
1787 if (*p == '\0')
1788 break;
1789 } else {
1790 address = QString();
1792 // remainder is location, hit and ignore count, condition
1793 hits = 0;
1794 ignoreCount = 0;
1795 condition = QString();
1796 end = strchr(p, '\n');
1797 if (end == 0) {
1798 location = p;
1799 p += location.length();
1800 } else {
1801 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1802 p = end+1; /* skip over \n */
1805 // may be continued in next line
1806 while (isspace(*p)) { /* p points to beginning of line */
1807 // skip white space at beginning of line
1808 while (isspace(*p))
1809 p++;
1811 // seek end of line
1812 end = strchr(p, '\n');
1813 if (end == 0)
1814 end = p+strlen(p);
1816 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1817 // extract the hit count
1818 p += 22;
1819 hits = strtol(p, &dummy, 10);
1820 TRACE(QString().sprintf("hit count %d", hits));
1821 } else if (strncmp(p, "stop only if ", 13) == 0) {
1822 // extract condition
1823 p += 13;
1824 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1825 TRACE("condition: "+condition);
1826 } else if (strncmp(p, "ignore next ", 12) == 0) {
1827 // extract ignore count
1828 p += 12;
1829 ignoreCount = strtol(p, &dummy, 10);
1830 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1831 } else {
1832 // indeed a continuation
1833 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1835 p = end;
1836 if (*p != '\0')
1837 p++; /* skip '\n' */
1839 Breakpoint* bp = new Breakpoint;
1840 bp->id = bpNum;
1841 bp->type = bpType;
1842 bp->temporary = disp == 'd';
1843 bp->enabled = enable == 'y';
1844 bp->location = location;
1845 bp->address = address;
1846 bp->hitCount = hits;
1847 bp->ignoreCount = ignoreCount;
1848 bp->condition = condition;
1849 brks.append(bp);
1851 return true;
1854 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1856 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1857 // no threads
1858 return true;
1861 int id;
1862 QString systag;
1863 QString func, file;
1864 int lineNo;
1865 DbgAddr address;
1867 const char* p = output;
1868 while (*p != '\0') {
1869 // seach look for thread id, watching out for the focus indicator
1870 bool hasFocus = false;
1871 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1872 p++;
1873 if (*p == '*') {
1874 hasFocus = true;
1875 p++;
1876 // there follows only whitespace
1878 char* end;
1879 id = strtol(p, &end, 10);
1880 if (p == end) {
1881 // syntax error: no number found; bail out
1882 return true;
1884 p = end;
1886 // skip space
1887 while (isspace(*p))
1888 p++;
1891 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1893 end = strstr(p, " ");
1894 if (end == 0) {
1895 // syntax error; bail out
1896 return true;
1898 systag = QString::fromLatin1(p, end-p);
1899 p = end+2;
1902 * Now follows a standard stack frame. Sometimes, however, gdb
1903 * catches a thread at an instant where it doesn't have a stack.
1905 if (strncmp(p, "[No stack.]", 11) != 0) {
1906 ::parseFrameInfo(p, func, file, lineNo, address);
1907 } else {
1908 func = "[No stack]";
1909 file = QString();
1910 lineNo = -1;
1911 address = QString();
1912 p += 11; /* \n is skipped above */
1915 ThreadInfo* thr = new ThreadInfo;
1916 thr->id = id;
1917 thr->threadName = systag;
1918 thr->hasFocus = hasFocus;
1919 thr->function = func;
1920 thr->fileName = file;
1921 thr->lineNo = lineNo;
1922 thr->address = address;
1923 threads.append(thr);
1925 return true;
1928 static bool parseNewBreakpoint(const char* o, int& id,
1929 QString& file, int& lineNo, QString& address);
1930 static bool parseNewWatchpoint(const char* o, int& id,
1931 QString& expr);
1933 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1934 QString& file, int& lineNo, QString& address)
1936 const char* o = output;
1937 // skip lines of that begin with "(Cannot find"
1938 while (strncmp(o, "(Cannot find", 12) == 0) {
1939 o = strchr(o, '\n');
1940 if (o == 0)
1941 return false;
1942 o++; /* skip newline */
1945 if (strncmp(o, "Breakpoint ", 11) == 0) {
1946 output += 11; /* skip "Breakpoint " */
1947 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1948 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1949 output += 20;
1950 return ::parseNewWatchpoint(output, id, address);
1951 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1952 output += 11;
1953 return ::parseNewWatchpoint(output, id, address);
1955 return false;
1958 static bool parseNewBreakpoint(const char* o, int& id,
1959 QString& file, int& lineNo, QString& address)
1961 // breakpoint id
1962 char* p;
1963 id = strtoul(o, &p, 10);
1964 if (p == o)
1965 return false;
1967 // check for the address
1968 if (strncmp(p, " at 0x", 6) == 0) {
1969 char* start = p+4; /* skip " at ", but not 0x */
1970 p += 6;
1971 while (isxdigit(*p))
1972 ++p;
1973 address = QString::fromLatin1(start, p-start);
1976 // file name
1977 char* fileStart = strstr(p, "file ");
1978 if (fileStart == 0)
1979 return !address.isEmpty(); /* parse error only if there's no address */
1980 fileStart += 5;
1982 // line number
1983 char* numStart = strstr(fileStart, ", line ");
1984 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1985 numStart += 7;
1986 int line = strtoul(numStart, &p, 10);
1987 if (numStart == p)
1988 return false;
1990 file = fileName;
1991 lineNo = line-1; /* zero-based! */
1992 return true;
1995 static bool parseNewWatchpoint(const char* o, int& id,
1996 QString& expr)
1998 // watchpoint id
1999 char* p;
2000 id = strtoul(o, &p, 10);
2001 if (p == o)
2002 return false;
2004 if (strncmp(p, ": ", 2) != 0)
2005 return false;
2006 p += 2;
2008 // all the rest on the line is the expression
2009 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
2010 return true;
2013 void GdbDriver::parseLocals(const char* output, QList<ExprValue>& newVars)
2015 // check for possible error conditions
2016 if (strncmp(output, "No symbol table", 15) == 0)
2018 return;
2021 while (*output != '\0') {
2022 while (isspace(*output))
2023 output++;
2024 if (*output == '\0')
2025 break;
2026 // skip occurrences of "No locals" and "No args"
2027 if (strncmp(output, "No locals", 9) == 0 ||
2028 strncmp(output, "No arguments", 12) == 0)
2030 output = strchr(output, '\n');
2031 if (output == 0) {
2032 break;
2034 continue;
2037 ExprValue* variable = parseVar(output);
2038 if (variable == 0) {
2039 break;
2041 // do not add duplicates
2042 for (ExprValue* o = newVars.first(); o != 0; o = newVars.next()) {
2043 if (o->m_name == variable->m_name) {
2044 delete variable;
2045 goto skipDuplicate;
2048 newVars.append(variable);
2049 skipDuplicate:;
2053 ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2055 ExprValue* var = 0;
2056 // check for error conditions
2057 if (!parseErrorMessage(output, var, wantErrorValue))
2059 // parse the variable
2060 var = parseVar(output);
2062 return var;
2065 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2067 bool isGood = false;
2068 message = QString(output).simplifyWhiteSpace();
2069 if (message.isEmpty()) {
2070 message = i18n("New working directory: ") + m_programWD;
2071 isGood = true;
2073 return isGood;
2076 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2078 message = output;
2080 m_haveCoreFile = false;
2083 * Lines starting with the following do not indicate errors:
2084 * Using host libthread_db
2085 * (no debugging symbols found)
2087 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2088 strncmp(output, "(no debugging symbols found)", 28) == 0)
2090 // this line is good, go to the next one
2091 const char* end = strchr(output, '\n');
2092 if (end == 0)
2093 output += strlen(output);
2094 else
2095 output = end+1;
2099 * If we've parsed all lines, there was no error.
2101 return output[0] == '\0';
2104 bool GdbDriver::parseCoreFile(const char* output)
2106 // if command succeeded, gdb emits a line starting with "#0 "
2107 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2108 return m_haveCoreFile;
2111 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2113 // optionally: "program changed, rereading symbols",
2114 // followed by:
2115 // "Program exited normally"
2116 // "Program terminated with wignal SIGSEGV"
2117 // "Program received signal SIGINT" or other signal
2118 // "Breakpoint..."
2120 // go through the output, line by line, checking what we have
2121 const char* start = output - 1;
2122 uint flags = SFprogramActive;
2123 message = QString();
2124 do {
2125 start++; /* skip '\n' */
2127 if (strncmp(start, "Program ", 8) == 0 ||
2128 strncmp(start, "ptrace: ", 8) == 0) {
2130 * When we receive a signal, the program remains active.
2132 * Special: If we "stopped" in a corefile, the string "Program
2133 * terminated with signal"... is displayed. (Normally, we see
2134 * "Program received signal"... when a signal happens.)
2136 if (strncmp(start, "Program exited", 14) == 0 ||
2137 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2138 strncmp(start, "ptrace: ", 8) == 0)
2140 flags &= ~SFprogramActive;
2143 // set message
2144 const char* endOfMessage = strchr(start, '\n');
2145 if (endOfMessage == 0)
2146 endOfMessage = start + strlen(start);
2147 message = QString::fromLatin1(start, endOfMessage-start);
2148 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2150 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2151 * that it stopped at a temporary breakpoint).
2153 flags |= SFrefreshBreak;
2154 } else if (strstr(start, "re-reading symbols.") != 0) {
2155 flags |= SFrefreshSource;
2158 // next line, please
2159 start = strchr(start, '\n');
2160 } while (start != 0);
2163 * Gdb only notices when new threads have appeared, but not when a
2164 * thread finishes. So we always have to assume that the list of
2165 * threads has changed.
2167 flags |= SFrefreshThreads;
2169 return flags;
2172 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2174 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2175 return;
2177 // parse the table of shared libraries
2179 // strip off head line
2180 output = strchr(output, '\n');
2181 if (output == 0)
2182 return;
2183 output++; /* skip '\n' */
2184 QString shlibName;
2185 while (*output != '\0') {
2186 // format of a line is
2187 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2188 // 3 blocks of non-space followed by space
2189 for (int i = 0; *output != '\0' && i < 3; i++) {
2190 while (*output != '\0' && !isspace(*output)) { /* non-space */
2191 output++;
2193 while (isspace(*output)) { /* space */
2194 output++;
2197 if (*output == '\0')
2198 return;
2199 const char* start = output;
2200 output = strchr(output, '\n');
2201 if (output == 0)
2202 output = start + strlen(start);
2203 shlibName = QString::fromLatin1(start, output-start);
2204 if (*output != '\0')
2205 output++;
2206 shlibs.append(shlibName);
2207 TRACE("found shared lib " + shlibName);
2211 bool GdbDriver::parseFindType(const char* output, QString& type)
2213 if (strncmp(output, "type = ", 7) != 0)
2214 return false;
2217 * Everything else is the type. We strip off any leading "const" and any
2218 * trailing "&" on the grounds that neither affects the decoding of the
2219 * object. We also strip off all white-space from the type.
2221 output += 7;
2222 if (strncmp(output, "const ", 6) == 0)
2223 output += 6;
2224 type = output;
2225 type.replace(QRegExp("\\s+"), "");
2226 if (type.endsWith("&"))
2227 type.truncate(type.length() - 1);
2228 return true;
2231 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2233 if (strncmp(output, "The program has no registers now", 32) == 0) {
2234 return;
2237 QString regName;
2238 QString value;
2240 // parse register values
2241 while (*output != '\0')
2243 // skip space at the start of the line
2244 while (isspace(*output))
2245 output++;
2247 // register name
2248 const char* start = output;
2249 while (*output != '\0' && !isspace(*output))
2250 output++;
2251 if (*output == '\0')
2252 break;
2253 regName = QString::fromLatin1(start, output-start);
2255 // skip space
2256 while (isspace(*output))
2257 output++;
2259 RegisterInfo* reg = new RegisterInfo;
2260 reg->regName = regName;
2263 * If we find a brace now, this is a vector register. We look for
2264 * the closing brace and treat the result as cooked value.
2266 if (*output == '{')
2268 start = output;
2269 skipNested(output, '{', '}');
2270 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2271 // skip space, but not the end of line
2272 while (isspace(*output) && *output != '\n')
2273 output++;
2274 // get rid of the braces at the begining and the end
2275 value.remove(0, 1);
2276 if (value[value.length()-1] == '}') {
2277 value = value.left(value.length()-1);
2279 // gdb 5.3 doesn't print a separate set of raw values
2280 if (*output == '{') {
2281 // another set of vector follows
2282 // what we have so far is the raw value
2283 reg->rawValue = value;
2285 start = output;
2286 skipNested(output, '{', '}');
2287 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2288 } else {
2289 // for gdb 5.3
2290 // find first type that does not have an array, this is the RAW value
2291 const char* end=start;
2292 findEnd(end);
2293 const char* cur=start;
2294 while (cur<end) {
2295 while (*cur != '=' && cur<end)
2296 cur++;
2297 cur++;
2298 while (isspace(*cur) && cur<end)
2299 cur++;
2300 if (isNumberish(*cur)) {
2301 end=cur;
2302 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2303 end++;
2304 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2305 reg->rawValue = rawValue;
2307 if (rawValue.left(2)=="0x") {
2308 // ok we have a raw value, now get it's type
2309 end=cur-1;
2310 while (isspace(*end) || *end=='=') end--;
2311 end++;
2312 cur=end-1;
2313 while (*cur!='{' && *cur!=' ')
2314 cur--;
2315 cur++;
2316 reg->type=QString::fromLatin1(cur, end-cur);
2319 // end while loop
2320 cur=end;
2323 // skip to the end of line
2324 while (*output != '\0' && *output != '\n')
2325 output++;
2326 // get rid of the braces at the begining and the end
2327 value.remove(0, 1);
2328 if (value[value.length()-1] == '}') {
2329 value = value.left(value.length()-1);
2332 reg->cookedValue = value;
2334 else
2336 // the rest of the line is the register value
2337 start = output;
2338 output = strchr(output,'\n');
2339 if (output == 0)
2340 output = start + strlen(start);
2341 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2344 * We split the raw from the cooked values.
2345 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2346 * Here, the cooked value comes first, and the raw value is in
2347 * the second part.
2349 int pos = value.find(" (raw ");
2350 if (pos >= 0)
2352 reg->cookedValue = value.left(pos);
2353 reg->rawValue = value.mid(pos+6);
2354 if (reg->rawValue.right(1) == ")") // remove closing bracket
2355 reg->rawValue.truncate(reg->rawValue.length()-1);
2357 else
2360 * In other cases we split off the first token (separated by
2361 * whitespace). It is the raw value. The remainder of the line
2362 * is the cooked value.
2364 int pos = value.find(' ');
2365 if (pos < 0) {
2366 reg->rawValue = value;
2367 reg->cookedValue = QString();
2368 } else {
2369 reg->rawValue = value.left(pos);
2370 reg->cookedValue = value.mid(pos+1);
2374 if (*output != '\0')
2375 output++; /* skip '\n' */
2377 regs.append(reg);
2381 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2383 // "is at address" or "starts at address"
2384 const char* start = strstr(output, "s at address ");
2385 if (start == 0)
2386 return false;
2388 start += 13;
2389 const char* p = start;
2390 while (*p != '\0' && !isspace(*p))
2391 p++;
2392 addrFrom = QString::fromLatin1(start, p-start);
2394 start = strstr(p, "and ends at ");
2395 if (start == 0) {
2396 addrTo = addrFrom;
2397 return true;
2400 start += 12;
2401 p = start;
2402 while (*p != '\0' && !isspace(*p))
2403 p++;
2404 addrTo = QString::fromLatin1(start, p-start);
2406 return true;
2409 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2411 code.clear();
2413 if (strncmp(output, "Dump of assembler", 17) != 0) {
2414 // error message?
2415 DisassembledCode c;
2416 c.code = output;
2417 code.append(new DisassembledCode(c));
2418 return;
2421 // remove first line
2422 const char* p = strchr(output, '\n');
2423 if (p == 0)
2424 return; /* not a regular output */
2426 p++;
2428 // remove last line
2429 const char* end = strstr(output, "End of assembler");
2430 if (end == 0)
2431 end = p + strlen(p);
2433 DbgAddr address;
2435 // remove function offsets from the lines
2436 while (p != end)
2438 const char* start = p;
2439 // address
2440 while (p != end && !isspace(*p))
2441 p++;
2442 address = QString::fromLatin1(start, p-start);
2444 // function name (enclosed in '<>', followed by ':')
2445 while (p != end && *p != '<')
2446 p++;
2447 if (*p == '<')
2448 skipNestedAngles(p);
2449 if (*p == ':')
2450 p++;
2452 // space until code
2453 while (p != end && isspace(*p))
2454 p++;
2456 // code until end of line
2457 start = p;
2458 while (p != end && *p != '\n')
2459 p++;
2460 if (p != end) /* include '\n' */
2461 p++;
2463 DisassembledCode* c = new DisassembledCode;
2464 c->address = address;
2465 c->code = QString::fromLatin1(start, p-start);
2466 code.append(c);
2470 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2472 if (isErrorExpr(output)) {
2473 // error; strip space
2474 QString msg = output;
2475 return msg.stripWhiteSpace();
2478 const char* p = output; /* save typing */
2479 DbgAddr addr;
2480 QString dump;
2482 // the address
2483 while (*p != 0) {
2484 const char* start = p;
2485 while (*p != '\0' && *p != ':' && !isspace(*p))
2486 p++;
2487 addr = QString::fromLatin1(start, p-start);
2488 if (*p != ':') {
2489 // parse function offset
2490 while (isspace(*p))
2491 p++;
2492 start = p;
2493 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2494 p++;
2495 addr.fnoffs = QString::fromLatin1(start, p-start);
2497 if (*p == ':')
2498 p++;
2499 // skip space; this may skip a new-line char!
2500 while (isspace(*p))
2501 p++;
2502 // everything to the end of the line is the memory dump
2503 const char* end = strchr(p, '\n');
2504 if (end != 0) {
2505 dump = QString::fromLatin1(p, end-p);
2506 p = end+1;
2507 } else {
2508 dump = QString::fromLatin1(p, strlen(p));
2509 p += strlen(p);
2511 MemoryDump* md = new MemoryDump;
2512 md->address = addr;
2513 md->dump = dump;
2514 memdump.append(md);
2517 return QString();
2520 QString GdbDriver::editableValue(VarTree* value)
2522 const char* s = value->value().latin1();
2524 // if the variable is a pointer value that contains a cast,
2525 // remove the cast
2526 if (*s == '(') {
2527 skipNested(s, '(', ')');
2528 // skip space
2529 while (isspace(*s))
2530 ++s;
2533 repeat:
2534 const char* start = s;
2536 if (strncmp(s, "0x", 2) == 0)
2538 s += 2;
2539 while (isxdigit(*s))
2540 ++s;
2543 * What we saw so far might have been a reference. If so, edit the
2544 * referenced value. Otherwise, edit the pointer.
2546 if (*s == ':') {
2547 // a reference
2548 ++s;
2549 goto repeat;
2551 // a pointer
2552 // if it's a pointer to a string, remove the string
2553 const char* end = s;
2554 while (isspace(*s))
2555 ++s;
2556 if (*s == '"') {
2557 // a string
2558 return QString::fromLatin1(start, end-start);
2559 } else {
2560 // other pointer
2561 return QString::fromLatin1(start, strlen(start));
2565 // else leave it unchanged (or stripped of the reference preamble)
2566 return s;
2569 QString GdbDriver::parseSetVariable(const char* output)
2571 // if there is any output, it is an error message
2572 QString msg = output;
2573 return msg.stripWhiteSpace();
2577 #include "gdbdriver.moc"