Merge branch 'maint'
[kdbg.git] / kdbg / gdbdriver.cpp
blobfb3958ef9f2e30a16d3fac858aa0a239b8f589fd
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 #ifdef __FreeBSD__
306 cmds[DCcorefile].fmt = "target FreeBSD-core %s\n";
307 #else
308 cmds[DCcorefile].fmt = "target core %s\n";
309 #endif
310 } else {
311 cmds[DCcorefile].fmt = "core-file %s\n";
314 break;
315 default:;
318 /* ok, the command is ready */
319 emit commandReceived(cmd, m_output);
321 switch (cmd->m_cmd) {
322 case DCcorefile:
323 case DCinfolinemain:
324 case DCframe:
325 case DCattach:
326 case DCrun:
327 case DCcont:
328 case DCstep:
329 case DCstepi:
330 case DCnext:
331 case DCnexti:
332 case DCfinish:
333 case DCuntil:
334 parseMarker();
335 default:;
340 * The --fullname option makes gdb send a special normalized sequence print
341 * each time the program stops and at some other points. The sequence has
342 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
344 void GdbDriver::parseMarker()
346 char* startMarker = strstr(m_output, "\032\032");
347 if (startMarker == 0)
348 return;
350 // extract the marker
351 startMarker += 2;
352 TRACE(QString("found marker: ") + startMarker);
353 char* endMarker = strchr(startMarker, '\n');
354 if (endMarker == 0)
355 return;
357 *endMarker = '\0';
359 // extract filename and line number
360 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
362 int len;
363 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
364 if (lineNoStart >= 0) {
365 int lineNo = atoi(startMarker + lineNoStart+1);
367 // get address
368 const char* addrStart = startMarker + lineNoStart + len - 2;
369 DbgAddr address = QString(addrStart).stripWhiteSpace();
371 // now show the window
372 startMarker[lineNoStart] = '\0'; /* split off file name */
373 emit activateFileLine(startMarker, lineNo-1, address);
379 * Escapes characters that might lead to problems when they appear on gdb's
380 * command line.
382 static void normalizeStringArg(QString& arg)
385 * Remove trailing backslashes. This approach is a little simplistic,
386 * but we know that there is at the moment no case where a trailing
387 * backslash would make sense.
389 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
390 arg = arg.left(arg.length()-1);
395 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
397 assert(cmd >= 0 && cmd < NUM_CMDS);
398 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
400 normalizeStringArg(strArg);
402 if (cmd == DCcd) {
403 // need the working directory when parsing the output
404 m_programWD = strArg;
405 } else if (cmd == DCsetargs && !m_redirect.isEmpty()) {
407 * Use saved redirection. We prepend it in front of the user's
408 * arguments so that the user can override the redirections.
410 strArg = m_redirect + " " + strArg;
413 QString cmdString;
414 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
415 return cmdString;
418 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
420 assert(cmd >= 0 && cmd < NUM_CMDS);
421 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
423 QString cmdString;
424 cmdString.sprintf(cmds[cmd].fmt, intArg);
425 return cmdString;
428 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
430 assert(cmd >= 0 && cmd < NUM_CMDS);
431 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
432 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
433 cmd == DCexamine ||
434 cmd == DCtty);
436 normalizeStringArg(strArg);
438 QString cmdString;
440 if (cmd == DCtty)
443 * intArg specifies which channels should be redirected to
444 * /dev/null. It is a value or'ed together from RDNstdin,
445 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
446 * command.
448 * Note: We rely on that after the DCtty a DCsetargs will follow,
449 * which will ultimately apply the redirection.
451 static const char* const runRedir[8] = {
453 "</dev/null",
454 ">/dev/null",
455 "</dev/null >/dev/null",
456 "2>/dev/null",
457 "</dev/null 2>/dev/null",
458 ">/dev/null 2>&1",
459 "</dev/null >/dev/null 2>&1"
461 if (strArg.isEmpty())
462 intArg = 7; /* failsafe if no tty */
463 m_redirect = runRedir[intArg & 7];
465 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
468 if (cmd == DCexamine) {
469 // make a format specifier from the intArg
470 static const char size[16] = {
471 '\0', 'b', 'h', 'w', 'g'
473 static const char format[16] = {
474 '\0', 'x', 'd', 'u', 'o', 't',
475 'a', 'c', 'f', 's', 'i'
477 assert(MDTsizemask == 0xf); /* lowest 4 bits */
478 assert(MDTformatmask == 0xf0); /* next 4 bits */
479 int count = 16; /* number of entities to print */
480 char sizeSpec = size[intArg & MDTsizemask];
481 char formatSpec = format[(intArg & MDTformatmask) >> 4];
482 assert(sizeSpec != '\0');
483 assert(formatSpec != '\0');
484 // adjust count such that 16 lines are printed
485 switch (intArg & MDTformatmask) {
486 case MDTstring: case MDTinsn:
487 break; /* no modification needed */
488 default:
489 // all cases drop through:
490 switch (intArg & MDTsizemask) {
491 case MDTbyte:
492 case MDThalfword:
493 count *= 2;
494 case MDTword:
495 count *= 2;
496 case MDTgiantword:
497 count *= 2;
499 break;
501 QString spec;
502 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
504 return makeCmdString(DCexamine, spec, strArg);
507 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
509 // line numbers are zero-based
510 if (cmd == DCuntil || cmd == DCbreakline ||
511 cmd == DCtbreakline || cmd == DCinfoline)
513 intArg++;
515 if (cmd == DCinfoline)
517 // must split off file name part
518 int slash = strArg.findRev('/');
519 if (slash >= 0)
520 strArg = strArg.right(strArg.length()-slash-1);
522 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
524 else
526 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
528 return cmdString;
531 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
533 assert(cmd >= 0 && cmd < NUM_CMDS);
534 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
536 normalizeStringArg(strArg1);
537 normalizeStringArg(strArg2);
539 QString cmdString;
540 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
541 return cmdString;
544 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
546 assert(cmd >= 0 && cmd < NUM_CMDS);
547 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
549 QString cmdString;
550 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
551 return cmdString;
554 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
556 assert(cmd >= 0 && cmd < NUM_CMDS);
557 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
559 if (cmd == DCrun) {
560 m_haveCoreFile = false;
563 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
566 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
567 bool clearLow)
569 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
572 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
573 bool clearLow)
576 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
579 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
580 bool clearLow)
582 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
585 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
586 bool clearLow)
588 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
591 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
592 bool clearLow)
594 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
597 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
599 return queueCmdString(cmd, cmds[cmd].fmt, mode);
602 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
603 QueueMode mode)
605 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
608 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
609 QueueMode mode)
611 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
614 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
615 QueueMode mode)
617 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
620 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
621 QueueMode mode)
623 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
626 void GdbDriver::terminate()
628 kill(SIGTERM);
629 m_state = DSidle;
632 void GdbDriver::detachAndTerminate()
634 kill(SIGINT);
635 flushCommands();
636 executeCmdString(DCinitialize, "detach\nquit\n", true);
639 void GdbDriver::interruptInferior()
641 kill(SIGINT);
642 // remove accidentally queued commands
643 flushHiPriQueue();
646 static bool isErrorExpr(const char* output)
648 return
649 strncmp(output, "Cannot access memory at", 23) == 0 ||
650 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
651 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
652 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
653 strncmp(output, "There is no member or method named", 34) == 0 ||
654 strncmp(output, "A parse error in expression", 27) == 0 ||
655 strncmp(output, "No symbol \"", 11) == 0 ||
656 strncmp(output, "Internal error: ", 16) == 0;
660 * Returns true if the output is an error message. If wantErrorValue is
661 * true, a new ExprValue object is created and filled with the error message.
662 * If there are warnings, they are skipped and output points past the warnings
663 * on return (even if there \e are errors).
665 static bool parseErrorMessage(const char*& output,
666 ExprValue*& variable, bool wantErrorValue)
668 // skip warnings
669 while (strncmp(output, "warning:", 8) == 0)
671 char* end = strchr(output+8, '\n');
672 if (end == 0)
673 output += strlen(output);
674 else
675 output = end+1;
678 if (isErrorExpr(output))
680 if (wantErrorValue) {
681 // put the error message as value in the variable
682 variable = new ExprValue(QString(), VarTree::NKplain);
683 const char* endMsg = strchr(output, '\n');
684 if (endMsg == 0)
685 endMsg = output + strlen(output);
686 variable->m_value = QString::fromLatin1(output, endMsg-output);
687 } else {
688 variable = 0;
690 return true;
692 return false;
695 #if QT_VERSION >= 300
696 union Qt2QChar {
697 short s;
698 struct {
699 uchar row;
700 uchar cell;
701 } qch;
703 #endif
705 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
707 // don't accept the command if it is empty
708 if (cmd == 0 || *cmd == '\0')
709 return;
710 assert(strlen(cmd) <= MAX_FMTLEN);
711 cmds[DCprintQStringStruct].fmt = cmd;
714 ExprValue* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
716 ExprValue* variable = 0;
719 * Parse off white space. gdb sometimes prints white space first if the
720 * printed array leaded to an error.
722 while (isspace(*output))
723 output++;
725 // special case: empty string (0 repetitions)
726 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
728 variable = new ExprValue(QString(), VarTree::NKplain);
729 variable->m_value = "\"\"";
730 return variable;
733 // check for error conditions
734 if (parseErrorMessage(output, variable, wantErrorValue))
735 return variable;
737 // parse the array
739 // find '='
740 const char* p = output;
741 p = strchr(p, '=');
742 if (p == 0) {
743 goto error;
745 // skip white space
746 do {
747 p++;
748 } while (isspace(*p));
750 if (*p == '{')
752 // this is the real data
753 p++; /* skip '{' */
755 // parse the array
756 QString result;
757 QString repeatCount;
758 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
760 * A matrix for separators between the individual "things"
761 * that are added to the string. The first index is a bool,
762 * the second index is from the enum above.
764 static const char* separator[2][3] = {
765 { "\"", 0, ", \"" }, /* normal char is added */
766 { "'", "\", '", ", '" } /* repeated char is added */
769 while (isdigit(*p)) {
770 // parse a number
771 char* end;
772 unsigned short value = (unsigned short) strtoul(p, &end, 0);
773 if (end == p)
774 goto error; /* huh? no valid digits */
775 // skip separator and search for a repeat count
776 p = end;
777 while (isspace(*p) || *p == ',')
778 p++;
779 bool repeats = strncmp(p, "<repeats ", 9) == 0;
780 if (repeats) {
781 const char* start = p;
782 p = strchr(p+9, '>'); /* search end and advance */
783 if (p == 0)
784 goto error;
785 p++; /* skip '>' */
786 repeatCount = QString::fromLatin1(start, p-start);
787 while (isspace(*p) || *p == ',')
788 p++;
790 // p is now at the next char (or the end)
792 // interpret the value as a QChar
793 // TODO: make cross-architecture compatible
794 QChar ch;
795 if (qt3like) {
796 ch = QChar(value);
797 } else {
798 #if QT_VERSION < 300
799 (unsigned short&)ch = value;
800 #else
801 Qt2QChar c;
802 c.s = value;
803 ch.setRow(c.qch.row);
804 ch.setCell(c.qch.cell);
805 #endif
808 // escape a few frequently used characters
809 char escapeCode = '\0';
810 switch (ch.latin1()) {
811 case '\n': escapeCode = 'n'; break;
812 case '\r': escapeCode = 'r'; break;
813 case '\t': escapeCode = 't'; break;
814 case '\b': escapeCode = 'b'; break;
815 case '\"': escapeCode = '\"'; break;
816 case '\\': escapeCode = '\\'; break;
817 case '\0': if (value == 0) { escapeCode = '0'; } break;
820 // add separator
821 result += separator[repeats][lastThing];
822 // add char
823 if (escapeCode != '\0') {
824 result += '\\';
825 ch = escapeCode;
827 result += ch;
829 // fixup repeat count and lastThing
830 if (repeats) {
831 result += "' ";
832 result += repeatCount;
833 lastThing = wasRepeat;
834 } else {
835 lastThing = wasChar;
838 if (*p != '}')
839 goto error;
841 // closing quote
842 if (lastThing == wasChar)
843 result += "\"";
845 // assign the value
846 variable = new ExprValue(QString(), VarTree::NKplain);
847 variable->m_value = result;
849 else if (strncmp(p, "true", 4) == 0)
851 variable = new ExprValue(QString(), VarTree::NKplain);
852 variable->m_value = "QString::null";
854 else if (strncmp(p, "false", 5) == 0)
856 variable = new ExprValue(QString(), VarTree::NKplain);
857 variable->m_value = "(null)";
859 else
860 goto error;
861 return variable;
863 error:
864 if (wantErrorValue) {
865 variable = new ExprValue(QString(), VarTree::NKplain);
866 variable->m_value = "internal parse error";
868 return variable;
871 static ExprValue* parseVar(const char*& s)
873 const char* p = s;
875 // skip whitespace
876 while (isspace(*p))
877 p++;
879 QString name;
880 VarTree::NameKind kind;
882 * Detect anonymouse struct values: The 'name =' part is missing:
883 * s = { a = 1, { b = 2 }}
884 * Note that this detection works only inside structs when the anonymous
885 * struct is not the first member:
886 * s = {{ a = 1 }, b = 2}
887 * This is misparsed (by parseNested()) because it is mistakenly
888 * interprets the second opening brace as the first element of an array
889 * of structs.
891 if (*p == '{')
893 name = i18n("<anonymous struct or union>");
894 kind = VarTree::NKanonymous;
896 else
898 if (!parseName(p, name, kind)) {
899 return 0;
902 // go for '='
903 while (isspace(*p))
904 p++;
905 if (*p != '=') {
906 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
907 return 0;
909 // skip the '=' and more whitespace
910 p++;
911 while (isspace(*p))
912 p++;
915 ExprValue* variable = new ExprValue(name, kind);
917 if (!parseValue(p, variable)) {
918 delete variable;
919 return 0;
921 s = p;
922 return variable;
925 static void skipNested(const char*& s, char opening, char closing)
927 const char* p = s;
929 // parse a nested type
930 int nest = 1;
931 p++;
933 * Search for next matching `closing' char, skipping nested pairs of
934 * `opening' and `closing'.
936 while (*p && nest > 0) {
937 if (*p == opening) {
938 nest++;
939 } else if (*p == closing) {
940 nest--;
942 p++;
944 if (nest != 0) {
945 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
947 s = p;
951 * This function skips text that is delimited by nested angle bracktes, '<>'.
952 * A complication arises because the delimited text can contain the names of
953 * operator<<, operator>>, operator<, and operator>, which have to be treated
954 * specially so that they do not count towards the nesting of '<>'.
955 * This function assumes that the delimited text does not contain strings.
957 static void skipNestedAngles(const char*& s)
959 const char* p = s;
961 int nest = 1;
962 p++; // skip the initial '<'
963 while (*p && nest > 0)
965 // Below we can check for p-s >= 9 instead of 8 because
966 // *s is '<' and cannot be part of "operator".
967 if (*p == '<')
969 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
970 if (p[1] == '<')
971 p++;
972 } else {
973 nest++;
976 else if (*p == '>')
978 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
979 if (p[1] == '>')
980 p++;
981 } else {
982 nest--;
985 p++;
987 if (nest != 0) {
988 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
990 s = p;
994 * Find the end of line that is not inside braces
996 static void findEnd(const char*& s)
998 const char* p = s;
999 while (*p && *p!='\n') {
1000 while (*p && *p!='\n' && *p!='{')
1001 p++;
1002 if (*p=='{') {
1003 p++;
1004 skipNested(p, '{', '}'); p--;
1007 s = p;
1010 static bool isNumberish(const char ch)
1012 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
1015 void skipString(const char*& p)
1017 moreStrings:
1018 // opening quote
1019 char quote = *p++;
1020 while (*p != quote) {
1021 if (*p == '\\') {
1022 // skip escaped character
1023 // no special treatment for octal values necessary
1024 p++;
1026 // simply return if no more characters
1027 if (*p == '\0')
1028 return;
1029 p++;
1031 // closing quote
1032 p++;
1034 * Strings can consist of several parts, some of which contain repeated
1035 * characters.
1037 if (quote == '\'') {
1038 // look ahaead for <repeats 123 times>
1039 const char* q = p+1;
1040 while (isspace(*q))
1041 q++;
1042 if (strncmp(q, "<repeats ", 9) == 0) {
1043 p = q+9;
1044 while (*p != '\0' && *p != '>')
1045 p++;
1046 if (*p != '\0') {
1047 p++; /* skip the '>' */
1051 // is the string continued?
1052 if (*p == ',') {
1053 // look ahead for another quote
1054 const char* q = p+1;
1055 while (isspace(*q))
1056 q++;
1057 if (*q == '"' || *q == '\'') {
1058 // yes!
1059 p = q;
1060 goto moreStrings;
1063 /* very long strings are followed by `...' */
1064 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1065 p += 3;
1069 static void skipNestedWithString(const char*& s, char opening, char closing)
1071 const char* p = s;
1073 // parse a nested expression
1074 int nest = 1;
1075 p++;
1077 * Search for next matching `closing' char, skipping nested pairs of
1078 * `opening' and `closing' as well as strings.
1080 while (*p && nest > 0) {
1081 if (*p == opening) {
1082 nest++;
1083 } else if (*p == closing) {
1084 nest--;
1085 } else if (*p == '\'' || *p == '\"') {
1086 skipString(p);
1087 continue;
1089 p++;
1091 if (nest > 0) {
1092 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1094 s = p;
1097 inline void skipName(const char*& p)
1099 // allow : (for enumeration values) and $ and . (for _vtbl.)
1100 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1101 p++;
1104 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1106 kind = VarTree::NKplain;
1108 const char* p = s;
1109 // examples of names:
1110 // name
1111 // <Object>
1112 // <string<a,b<c>,7> >
1114 if (*p == '<') {
1115 skipNestedAngles(p);
1116 name = QString::fromLatin1(s, p - s);
1117 kind = VarTree::NKtype;
1119 else
1121 // name, which might be "static"; allow dot for "_vtbl."
1122 skipName(p);
1123 if (p == s) {
1124 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1125 return false;
1127 int len = p - s;
1128 if (len == 6 && strncmp(s, "static", 6) == 0) {
1129 kind = VarTree::NKstatic;
1131 // its a static variable, name comes now
1132 while (isspace(*p))
1133 p++;
1134 s = p;
1135 skipName(p);
1136 if (p == s) {
1137 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1138 return false;
1140 len = p - s;
1142 name = QString::fromLatin1(s, len);
1144 // return the new position
1145 s = p;
1146 return true;
1149 static bool parseValue(const char*& s, ExprValue* variable)
1151 variable->m_value = "";
1153 repeat:
1154 if (*s == '{') {
1155 // Sometimes we find the following output:
1156 // {<text variable, no debug info>} 0x40012000 <access>
1157 // {<data variable, no debug info>}
1158 // {<variable (not text or data), no debug info>}
1159 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1160 strncmp(s, "{<data variable, ", 17) == 0 ||
1161 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1163 const char* start = s;
1164 skipNested(s, '{', '}');
1165 variable->m_value = QString::fromLatin1(start, s-start);
1166 variable->m_value += ' '; // add only a single space
1167 while (isspace(*s))
1168 s++;
1169 goto repeat;
1171 else
1173 s++;
1174 if (!parseNested(s, variable)) {
1175 return false;
1177 // must be the closing brace
1178 if (*s != '}') {
1179 TRACE("parse error: missing } of " + variable->m_name);
1180 return false;
1182 s++;
1183 // final white space
1184 while (isspace(*s))
1185 s++;
1187 } else {
1188 // examples of leaf values (cannot be the empty string):
1189 // 123
1190 // -123
1191 // 23.575e+37
1192 // 0x32a45
1193 // @0x012ab4
1194 // (DwContentType&) @0x8123456: {...}
1195 // 0x32a45 "text"
1196 // 10 '\n'
1197 // <optimized out>
1198 // 0x823abc <Array<int> virtual table>
1199 // (void (*)()) 0x8048480 <f(E *, char)>
1200 // (E *) 0xbffff450
1201 // red
1202 // &parseP (HTMLClueV *, char *)
1203 // Variable "x" is not available.
1204 // The value of variable 'x' is distributed...
1205 // -nan(0xfffff081defa0)
1207 const char*p = s;
1209 // check for type
1210 QString type;
1211 if (*p == '(') {
1212 skipNested(p, '(', ')');
1214 while (isspace(*p))
1215 p++;
1216 variable->m_value = QString::fromLatin1(s, p - s);
1219 bool reference = false;
1220 if (*p == '@') {
1221 // skip reference marker
1222 p++;
1223 reference = true;
1225 const char* start = p;
1226 if (*p == '-')
1227 p++;
1229 // some values consist of more than one token
1230 bool checkMultiPart = false;
1232 if (p[0] == '0' && p[1] == 'x') {
1233 // parse hex number
1234 p += 2;
1235 while (isxdigit(*p))
1236 p++;
1239 * Assume this is a pointer, but only if it's not a reference, since
1240 * references can't be expanded.
1242 if (!reference) {
1243 variable->m_varKind = VarTree::VKpointer;
1244 } else {
1246 * References are followed by a colon, in which case we'll
1247 * find the value following the reference address.
1249 if (*p == ':') {
1250 p++;
1251 } else {
1252 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1253 reference = false;
1256 checkMultiPart = true;
1257 } else if (isdigit(*p)) {
1258 // parse decimal number, possibly a float
1259 while (isdigit(*p))
1260 p++;
1261 if (*p == '.') { /* TODO: obey i18n? */
1262 // In long arrays an integer may be followed by '...'.
1263 // We test for this situation and don't gobble the '...'.
1264 if (p[1] != '.' || p[0] != '.') {
1265 // fractional part
1266 p++;
1267 while (isdigit(*p))
1268 p++;
1271 if (*p == 'e' || *p == 'E') {
1272 p++;
1273 // exponent
1274 if (*p == '-' || *p == '+')
1275 p++;
1276 while (isdigit(*p))
1277 p++;
1280 // for char variables there is the char, eg. 10 '\n'
1281 checkMultiPart = true;
1282 } else if (*p == '<') {
1283 // e.g. <optimized out>
1284 skipNestedAngles(p);
1285 } else if (*p == '"' || *p == '\'') {
1286 // character may have multipart: '\000' <repeats 11 times>
1287 checkMultiPart = *p == '\'';
1288 // found a string
1289 skipString(p);
1290 } else if (*p == '&') {
1291 // function pointer
1292 p++;
1293 skipName(p);
1294 while (isspace(*p)) {
1295 p++;
1297 if (*p == '(') {
1298 skipNested(p, '(', ')');
1300 } else if (strncmp(p, "Variable \"", 10) == 0) {
1301 // Variable "x" is not available.
1302 p += 10; // skip to "
1303 skipName(p);
1304 if (strncmp(p, "\" is not available.", 19) == 0) {
1305 p += 19;
1307 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1308 p += 23;
1309 skipName(p);
1310 const char* e = strchr(p, '.');
1311 if (e == 0) {
1312 p += strlen(p);
1313 } else {
1314 p = e+1;
1316 } else {
1317 // must be an enumeration value
1318 skipName(p);
1319 // hmm, not necessarily: nan (floating point Not a Number)
1320 // is followed by a number in ()
1321 if (*p == '(')
1322 skipNested(p, '(', ')');
1324 variable->m_value += QString::fromLatin1(start, p - start);
1326 // remove line breaks from the value; this is ok since
1327 // string values never contain a literal line break
1328 variable->m_value.replace('\n', ' ');
1330 if (checkMultiPart) {
1331 // white space
1332 while (isspace(*p))
1333 p++;
1334 // may be followed by a string or <...>
1335 start = p;
1337 if (*p == '"' || *p == '\'') {
1338 skipString(p);
1339 } else if (*p == '<') {
1340 // if this value is part of an array, it might be followed
1341 // by <repeats 15 times>, which we don't skip here
1342 if (strncmp(p, "<repeats ", 9) != 0)
1343 skipNestedAngles(p);
1345 if (p != start) {
1346 // there is always a blank before the string,
1347 // which we will include in the final string value
1348 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1349 // if this was a pointer, reset that flag since we
1350 // now got the value
1351 variable->m_varKind = VarTree::VKsimple;
1355 if (variable->m_value.length() == 0) {
1356 TRACE("parse error: no value for " + variable->m_name);
1357 return false;
1360 // final white space
1361 while (isspace(*p))
1362 p++;
1363 s = p;
1366 * If this was a reference, the value follows. It might even be a
1367 * composite variable!
1369 if (reference) {
1370 goto repeat;
1374 return true;
1377 static bool parseNested(const char*& s, ExprValue* variable)
1379 // could be a structure or an array
1380 while (isspace(*s))
1381 s++;
1383 const char* p = s;
1384 bool isStruct = false;
1386 * If there is a name followed by an = or an < -- which starts a type
1387 * name -- or "static", it is a structure
1389 if (*p == '<' || *p == '}') {
1390 isStruct = true;
1391 } else if (strncmp(p, "static ", 7) == 0) {
1392 isStruct = true;
1393 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1394 // look ahead for a comma after the name
1395 skipName(p);
1396 while (isspace(*p))
1397 p++;
1398 if (*p == '=') {
1399 isStruct = true;
1401 p = s; /* rescan the name */
1403 if (isStruct) {
1404 if (!parseVarSeq(p, variable)) {
1405 return false;
1407 variable->m_varKind = VarTree::VKstruct;
1408 } else {
1409 if (!parseValueSeq(p, variable)) {
1410 return false;
1412 variable->m_varKind = VarTree::VKarray;
1414 s = p;
1415 return true;
1418 static bool parseVarSeq(const char*& s, ExprValue* variable)
1420 // parse a comma-separated sequence of variables
1421 ExprValue* var = variable; /* var != 0 to indicate success if empty seq */
1422 for (;;) {
1423 if (*s == '}')
1424 break;
1425 if (strncmp(s, "<No data fields>}", 17) == 0)
1427 // no member variables, so break out immediately
1428 s += 16; /* go to the closing brace */
1429 break;
1431 var = parseVar(s);
1432 if (var == 0)
1433 break; /* syntax error */
1434 variable->appendChild(var);
1435 if (*s != ',')
1436 break;
1437 // skip the comma and whitespace
1438 s++;
1439 while (isspace(*s))
1440 s++;
1442 return var != 0;
1445 static bool parseValueSeq(const char*& s, ExprValue* variable)
1447 // parse a comma-separated sequence of variables
1448 int index = 0;
1449 bool good;
1450 for (;;) {
1451 QString name;
1452 name.sprintf("[%d]", index);
1453 ExprValue* var = new ExprValue(name, VarTree::NKplain);
1454 good = parseValue(s, var);
1455 if (!good) {
1456 delete var;
1457 return false;
1459 // a value may be followed by "<repeats 45 times>"
1460 if (strncmp(s, "<repeats ", 9) == 0) {
1461 s += 9;
1462 char* end;
1463 int l = strtol(s, &end, 10);
1464 if (end == s || strncmp(end, " times>", 7) != 0) {
1465 // should not happen
1466 delete var;
1467 return false;
1469 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1470 // replace name and advance index
1471 name.sprintf("[%d .. %d]", index, index+l-1);
1472 var->m_name = name;
1473 index += l;
1474 // skip " times>" and space
1475 s = end+7;
1476 // possible final space
1477 while (isspace(*s))
1478 s++;
1479 } else {
1480 index++;
1482 variable->appendChild(var);
1483 // long arrays may be terminated by '...'
1484 if (strncmp(s, "...", 3) == 0) {
1485 s += 3;
1486 ExprValue* var = new ExprValue("...", VarTree::NKplain);
1487 var->m_value = i18n("<additional entries of the array suppressed>");
1488 variable->appendChild(var);
1489 break;
1491 if (*s != ',') {
1492 break;
1494 // skip the comma and whitespace
1495 s++;
1496 while (isspace(*s))
1497 s++;
1498 // sometimes there is a closing brace after a comma
1499 // if (*s == '}')
1500 // break;
1502 return true;
1506 * Parses a stack frame.
1508 static void parseFrameInfo(const char*& s, QString& func,
1509 QString& file, int& lineNo, DbgAddr& address)
1511 const char* p = s;
1513 // next may be a hexadecimal address
1514 if (*p == '0') {
1515 const char* start = p;
1516 p++;
1517 if (*p == 'x')
1518 p++;
1519 while (isxdigit(*p))
1520 p++;
1521 address = QString::fromLatin1(start, p-start);
1522 if (strncmp(p, " in ", 4) == 0)
1523 p += 4;
1524 } else {
1525 address = DbgAddr();
1527 const char* start = p;
1528 // check for special signal handler frame
1529 if (strncmp(p, "<signal handler called>", 23) == 0) {
1530 func = QString::fromLatin1(start, 23);
1531 file = QString();
1532 lineNo = -1;
1533 s = p+23;
1534 if (*s == '\n')
1535 s++;
1536 return;
1540 * Skip the function name. It is terminated by a left parenthesis
1541 * which does not delimit "(anonymous namespace)" and which is
1542 * outside the angle brackets <> of template parameter lists
1543 * and is preceded by a space.
1545 while (*p != '\0')
1547 if (*p == '<') {
1548 // check for operator<< and operator<
1549 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1551 p++;
1552 if (*p == '<')
1553 p++;
1555 else
1557 // skip template parameter list
1558 skipNestedAngles(p);
1560 } else if (*p == '(') {
1561 // this skips "(anonymous namespace)" as well as the formal
1562 // parameter list of the containing function if this is a member
1563 // of a nested class
1564 skipNestedWithString(p, '(', ')');
1565 } else if (*p == ' ') {
1566 ++p;
1567 if (*p == '(')
1568 break; // parameter list found
1569 } else {
1570 p++;
1574 if (*p == '\0') {
1575 func = start;
1576 file = QString();
1577 lineNo = -1;
1578 s = p;
1579 return;
1582 * Skip parameters. But notice that for complicated conversion
1583 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1584 * pointer to function) as well as operator()(...) we have to skip
1585 * additional pairs of parentheses. Furthermore, recent gdbs write the
1586 * demangled name followed by the arguments in a pair of parentheses,
1587 * where the demangled name can end in "const".
1589 do {
1590 skipNestedWithString(p, '(', ')');
1591 while (isspace(*p))
1592 p++;
1593 // skip "const"
1594 if (strncmp(p, "const", 5) == 0) {
1595 p += 5;
1596 while (isspace(*p))
1597 p++;
1599 } while (*p == '(');
1601 // check for file position
1602 if (strncmp(p, "at ", 3) == 0) {
1603 p += 3;
1604 const char* fileStart = p;
1605 // go for the end of the line
1606 while (*p != '\0' && *p != '\n')
1607 p++;
1608 // search back for colon
1609 const char* colon = p;
1610 do {
1611 --colon;
1612 } while (*colon != ':');
1613 file = QString::fromLatin1(fileStart, colon-fileStart);
1614 lineNo = atoi(colon+1)-1;
1615 // skip new-line
1616 if (*p != '\0')
1617 p++;
1618 } else {
1619 // check for "from shared lib"
1620 if (strncmp(p, "from ", 5) == 0) {
1621 p += 5;
1622 // go for the end of the line
1623 while (*p != '\0' && *p != '\n')
1624 p++;
1625 // skip new-line
1626 if (*p != '\0')
1627 p++;
1629 file = "";
1630 lineNo = -1;
1632 // construct the function name (including file info)
1633 if (*p == '\0') {
1634 func = start;
1635 } else {
1636 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1638 s = p;
1641 * Replace \n (and whitespace around it) in func by a blank. We cannot
1642 * use QString::simplifyWhiteSpace() for this because this would also
1643 * simplify space that belongs to a string arguments that gdb sometimes
1644 * prints in the argument lists of the function.
1646 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1647 int nl = 0;
1648 while ((nl = func.find('\n', nl)) >= 0) {
1649 // search back to the beginning of the whitespace
1650 int startWhite = nl;
1651 do {
1652 --startWhite;
1653 } while (isspace(func[startWhite].latin1()));
1654 startWhite++;
1655 // search forward to the end of the whitespace
1656 do {
1657 nl++;
1658 } while (isspace(func[nl].latin1()));
1659 // replace
1660 func.replace(startWhite, nl-startWhite, " ");
1661 /* continue searching for more \n's at this place: */
1662 nl = startWhite+1;
1668 * Parses a stack frame including its frame number
1670 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1671 QString& file, int& lineNo, DbgAddr& address)
1673 // Example:
1674 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1675 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1677 // must start with a hash mark followed by number
1678 // or with "Breakpoint " followed by number and comma
1679 if (s[0] == '#') {
1680 if (!isdigit(s[1]))
1681 return false;
1682 s++; /* skip the hash mark */
1683 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1684 if (!isdigit(s[11]))
1685 return false;
1686 s += 11; /* skip "Breakpoint" */
1687 } else
1688 return false;
1690 // frame number
1691 frameNo = atoi(s);
1692 while (isdigit(*s))
1693 s++;
1694 // space and comma
1695 while (isspace(*s) || *s == ',')
1696 s++;
1697 parseFrameInfo(s, func, file, lineNo, address);
1698 return true;
1701 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1703 QString func, file;
1704 int lineNo, frameNo;
1705 DbgAddr address;
1707 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1708 StackFrame* frm = new StackFrame;
1709 frm->frameNo = frameNo;
1710 frm->fileName = file;
1711 frm->lineNo = lineNo;
1712 frm->address = address;
1713 frm->var = new ExprValue(func, VarTree::NKplain);
1714 stack.append(frm);
1718 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1719 QString& file, int& lineNo, DbgAddr& address)
1721 QString func;
1722 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1726 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1728 // skip first line, which is the headline
1729 const char* p = strchr(output, '\n');
1730 if (p == 0)
1731 return false;
1732 p++;
1733 if (*p == '\0')
1734 return false;
1736 // split up a line
1737 QString location;
1738 QString address;
1739 int hits = 0;
1740 uint ignoreCount = 0;
1741 QString condition;
1742 const char* end;
1743 char* dummy;
1744 while (*p != '\0') {
1745 // get Num
1746 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1747 p = dummy;
1748 // get Type
1749 while (isspace(*p))
1750 p++;
1751 Breakpoint::Type bpType = Breakpoint::breakpoint;
1752 if (strncmp(p, "breakpoint", 10) == 0) {
1753 p += 10;
1754 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1755 bpType = Breakpoint::watchpoint;
1756 p += 13;
1757 } else if (strncmp(p, "watchpoint", 10) == 0) {
1758 bpType = Breakpoint::watchpoint;
1759 p += 10;
1761 while (isspace(*p))
1762 p++;
1763 if (*p == '\0')
1764 break;
1765 // get Disp
1766 char disp = *p++;
1767 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1768 p++;
1769 while (isspace(*p))
1770 p++;
1771 if (*p == '\0')
1772 break;
1773 // get Enb
1774 char enable = *p++;
1775 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1776 p++;
1777 while (isspace(*p))
1778 p++;
1779 if (*p == '\0')
1780 break;
1781 // the address, if present
1782 if (bpType == Breakpoint::breakpoint &&
1783 strncmp(p, "0x", 2) == 0)
1785 const char* start = p;
1786 while (*p != '\0' && !isspace(*p))
1787 p++;
1788 address = QString::fromLatin1(start, p-start);
1789 while (isspace(*p) && *p != '\n')
1790 p++;
1791 if (*p == '\0')
1792 break;
1793 } else {
1794 address = QString();
1796 // remainder is location, hit and ignore count, condition
1797 hits = 0;
1798 ignoreCount = 0;
1799 condition = QString();
1800 end = strchr(p, '\n');
1801 if (end == 0) {
1802 location = p;
1803 p += location.length();
1804 } else {
1805 location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1806 p = end+1; /* skip over \n */
1809 // may be continued in next line
1810 while (isspace(*p)) { /* p points to beginning of line */
1811 // skip white space at beginning of line
1812 while (isspace(*p))
1813 p++;
1815 // seek end of line
1816 end = strchr(p, '\n');
1817 if (end == 0)
1818 end = p+strlen(p);
1820 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1821 // extract the hit count
1822 p += 22;
1823 hits = strtol(p, &dummy, 10);
1824 TRACE(QString().sprintf("hit count %d", hits));
1825 } else if (strncmp(p, "stop only if ", 13) == 0) {
1826 // extract condition
1827 p += 13;
1828 condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1829 TRACE("condition: "+condition);
1830 } else if (strncmp(p, "ignore next ", 12) == 0) {
1831 // extract ignore count
1832 p += 12;
1833 ignoreCount = strtol(p, &dummy, 10);
1834 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1835 } else {
1836 // indeed a continuation
1837 location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1839 p = end;
1840 if (*p != '\0')
1841 p++; /* skip '\n' */
1843 Breakpoint* bp = new Breakpoint;
1844 bp->id = bpNum;
1845 bp->type = bpType;
1846 bp->temporary = disp == 'd';
1847 bp->enabled = enable == 'y';
1848 bp->location = location;
1849 bp->address = address;
1850 bp->hitCount = hits;
1851 bp->ignoreCount = ignoreCount;
1852 bp->condition = condition;
1853 brks.append(bp);
1855 return true;
1858 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1860 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1861 // no threads
1862 return true;
1865 int id;
1866 QString systag;
1867 QString func, file;
1868 int lineNo;
1869 DbgAddr address;
1871 const char* p = output;
1872 while (*p != '\0') {
1873 // seach look for thread id, watching out for the focus indicator
1874 bool hasFocus = false;
1875 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1876 p++;
1877 if (*p == '*') {
1878 hasFocus = true;
1879 p++;
1880 // there follows only whitespace
1882 char* end;
1883 id = strtol(p, &end, 10);
1884 if (p == end) {
1885 // syntax error: no number found; bail out
1886 return true;
1888 p = end;
1890 // skip space
1891 while (isspace(*p))
1892 p++;
1895 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1897 end = strstr(p, " ");
1898 if (end == 0) {
1899 // syntax error; bail out
1900 return true;
1902 systag = QString::fromLatin1(p, end-p);
1903 p = end+2;
1906 * Now follows a standard stack frame. Sometimes, however, gdb
1907 * catches a thread at an instant where it doesn't have a stack.
1909 if (strncmp(p, "[No stack.]", 11) != 0) {
1910 ::parseFrameInfo(p, func, file, lineNo, address);
1911 } else {
1912 func = "[No stack]";
1913 file = QString();
1914 lineNo = -1;
1915 address = QString();
1916 p += 11; /* \n is skipped above */
1919 ThreadInfo* thr = new ThreadInfo;
1920 thr->id = id;
1921 thr->threadName = systag;
1922 thr->hasFocus = hasFocus;
1923 thr->function = func;
1924 thr->fileName = file;
1925 thr->lineNo = lineNo;
1926 thr->address = address;
1927 threads.append(thr);
1929 return true;
1932 static bool parseNewBreakpoint(const char* o, int& id,
1933 QString& file, int& lineNo, QString& address);
1934 static bool parseNewWatchpoint(const char* o, int& id,
1935 QString& expr);
1937 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1938 QString& file, int& lineNo, QString& address)
1940 const char* o = output;
1941 // skip lines of that begin with "(Cannot find"
1942 while (strncmp(o, "(Cannot find", 12) == 0) {
1943 o = strchr(o, '\n');
1944 if (o == 0)
1945 return false;
1946 o++; /* skip newline */
1949 if (strncmp(o, "Breakpoint ", 11) == 0) {
1950 output += 11; /* skip "Breakpoint " */
1951 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1952 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1953 output += 20;
1954 return ::parseNewWatchpoint(output, id, address);
1955 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1956 output += 11;
1957 return ::parseNewWatchpoint(output, id, address);
1959 return false;
1962 static bool parseNewBreakpoint(const char* o, int& id,
1963 QString& file, int& lineNo, QString& address)
1965 // breakpoint id
1966 char* p;
1967 id = strtoul(o, &p, 10);
1968 if (p == o)
1969 return false;
1971 // check for the address
1972 if (strncmp(p, " at 0x", 6) == 0) {
1973 char* start = p+4; /* skip " at ", but not 0x */
1974 p += 6;
1975 while (isxdigit(*p))
1976 ++p;
1977 address = QString::fromLatin1(start, p-start);
1980 // file name
1981 char* fileStart = strstr(p, "file ");
1982 if (fileStart == 0)
1983 return !address.isEmpty(); /* parse error only if there's no address */
1984 fileStart += 5;
1986 // line number
1987 char* numStart = strstr(fileStart, ", line ");
1988 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1989 numStart += 7;
1990 int line = strtoul(numStart, &p, 10);
1991 if (numStart == p)
1992 return false;
1994 file = fileName;
1995 lineNo = line-1; /* zero-based! */
1996 return true;
1999 static bool parseNewWatchpoint(const char* o, int& id,
2000 QString& expr)
2002 // watchpoint id
2003 char* p;
2004 id = strtoul(o, &p, 10);
2005 if (p == o)
2006 return false;
2008 if (strncmp(p, ": ", 2) != 0)
2009 return false;
2010 p += 2;
2012 // all the rest on the line is the expression
2013 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
2014 return true;
2017 void GdbDriver::parseLocals(const char* output, QList<ExprValue>& newVars)
2019 // check for possible error conditions
2020 if (strncmp(output, "No symbol table", 15) == 0)
2022 return;
2025 while (*output != '\0') {
2026 while (isspace(*output))
2027 output++;
2028 if (*output == '\0')
2029 break;
2030 // skip occurrences of "No locals" and "No args"
2031 if (strncmp(output, "No locals", 9) == 0 ||
2032 strncmp(output, "No arguments", 12) == 0)
2034 output = strchr(output, '\n');
2035 if (output == 0) {
2036 break;
2038 continue;
2041 ExprValue* variable = parseVar(output);
2042 if (variable == 0) {
2043 break;
2045 // do not add duplicates
2046 for (ExprValue* o = newVars.first(); o != 0; o = newVars.next()) {
2047 if (o->m_name == variable->m_name) {
2048 delete variable;
2049 goto skipDuplicate;
2052 newVars.append(variable);
2053 skipDuplicate:;
2057 ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2059 ExprValue* var = 0;
2060 // check for error conditions
2061 if (!parseErrorMessage(output, var, wantErrorValue))
2063 // parse the variable
2064 var = parseVar(output);
2066 return var;
2069 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2071 bool isGood = false;
2072 message = QString(output).simplifyWhiteSpace();
2073 if (message.isEmpty()) {
2074 message = i18n("New working directory: ") + m_programWD;
2075 isGood = true;
2077 return isGood;
2080 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2082 message = output;
2084 m_haveCoreFile = false;
2087 * Lines starting with the following do not indicate errors:
2088 * Using host libthread_db
2089 * (no debugging symbols found)
2091 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2092 strncmp(output, "(no debugging symbols found)", 28) == 0)
2094 // this line is good, go to the next one
2095 const char* end = strchr(output, '\n');
2096 if (end == 0)
2097 output += strlen(output);
2098 else
2099 output = end+1;
2103 * If we've parsed all lines, there was no error.
2105 return output[0] == '\0';
2108 bool GdbDriver::parseCoreFile(const char* output)
2110 // if command succeeded, gdb emits a line starting with "#0 "
2111 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2112 return m_haveCoreFile;
2115 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2117 // optionally: "program changed, rereading symbols",
2118 // followed by:
2119 // "Program exited normally"
2120 // "Program terminated with wignal SIGSEGV"
2121 // "Program received signal SIGINT" or other signal
2122 // "Breakpoint..."
2124 // go through the output, line by line, checking what we have
2125 const char* start = output - 1;
2126 uint flags = SFprogramActive;
2127 message = QString();
2128 do {
2129 start++; /* skip '\n' */
2131 if (strncmp(start, "Program ", 8) == 0 ||
2132 strncmp(start, "ptrace: ", 8) == 0) {
2134 * When we receive a signal, the program remains active.
2136 * Special: If we "stopped" in a corefile, the string "Program
2137 * terminated with signal"... is displayed. (Normally, we see
2138 * "Program received signal"... when a signal happens.)
2140 if (strncmp(start, "Program exited", 14) == 0 ||
2141 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2142 strncmp(start, "ptrace: ", 8) == 0)
2144 flags &= ~SFprogramActive;
2147 // set message
2148 const char* endOfMessage = strchr(start, '\n');
2149 if (endOfMessage == 0)
2150 endOfMessage = start + strlen(start);
2151 message = QString::fromLatin1(start, endOfMessage-start);
2152 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2154 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2155 * that it stopped at a temporary breakpoint).
2157 flags |= SFrefreshBreak;
2158 } else if (strstr(start, "re-reading symbols.") != 0) {
2159 flags |= SFrefreshSource;
2162 // next line, please
2163 start = strchr(start, '\n');
2164 } while (start != 0);
2167 * Gdb only notices when new threads have appeared, but not when a
2168 * thread finishes. So we always have to assume that the list of
2169 * threads has changed.
2171 flags |= SFrefreshThreads;
2173 return flags;
2176 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2178 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2179 return;
2181 // parse the table of shared libraries
2183 // strip off head line
2184 output = strchr(output, '\n');
2185 if (output == 0)
2186 return;
2187 output++; /* skip '\n' */
2188 QString shlibName;
2189 while (*output != '\0') {
2190 // format of a line is
2191 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2192 // 3 blocks of non-space followed by space
2193 for (int i = 0; *output != '\0' && i < 3; i++) {
2194 while (*output != '\0' && !isspace(*output)) { /* non-space */
2195 output++;
2197 while (isspace(*output)) { /* space */
2198 output++;
2201 if (*output == '\0')
2202 return;
2203 const char* start = output;
2204 output = strchr(output, '\n');
2205 if (output == 0)
2206 output = start + strlen(start);
2207 shlibName = QString::fromLatin1(start, output-start);
2208 if (*output != '\0')
2209 output++;
2210 shlibs.append(shlibName);
2211 TRACE("found shared lib " + shlibName);
2215 bool GdbDriver::parseFindType(const char* output, QString& type)
2217 if (strncmp(output, "type = ", 7) != 0)
2218 return false;
2221 * Everything else is the type. We strip off any leading "const" and any
2222 * trailing "&" on the grounds that neither affects the decoding of the
2223 * object. We also strip off all white-space from the type.
2225 output += 7;
2226 if (strncmp(output, "const ", 6) == 0)
2227 output += 6;
2228 type = output;
2229 type.replace(QRegExp("\\s+"), "");
2230 if (type.endsWith("&"))
2231 type.truncate(type.length() - 1);
2232 return true;
2235 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2237 if (strncmp(output, "The program has no registers now", 32) == 0) {
2238 return;
2241 QString regName;
2242 QString value;
2244 // parse register values
2245 while (*output != '\0')
2247 // skip space at the start of the line
2248 while (isspace(*output))
2249 output++;
2251 // register name
2252 const char* start = output;
2253 while (*output != '\0' && !isspace(*output))
2254 output++;
2255 if (*output == '\0')
2256 break;
2257 regName = QString::fromLatin1(start, output-start);
2259 // skip space
2260 while (isspace(*output))
2261 output++;
2263 RegisterInfo* reg = new RegisterInfo;
2264 reg->regName = regName;
2267 * If we find a brace now, this is a vector register. We look for
2268 * the closing brace and treat the result as cooked value.
2270 if (*output == '{')
2272 start = output;
2273 skipNested(output, '{', '}');
2274 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2275 // skip space, but not the end of line
2276 while (isspace(*output) && *output != '\n')
2277 output++;
2278 // get rid of the braces at the begining and the end
2279 value.remove(0, 1);
2280 if (value[value.length()-1] == '}') {
2281 value = value.left(value.length()-1);
2283 // gdb 5.3 doesn't print a separate set of raw values
2284 if (*output == '{') {
2285 // another set of vector follows
2286 // what we have so far is the raw value
2287 reg->rawValue = value;
2289 start = output;
2290 skipNested(output, '{', '}');
2291 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2292 } else {
2293 // for gdb 5.3
2294 // find first type that does not have an array, this is the RAW value
2295 const char* end=start;
2296 findEnd(end);
2297 const char* cur=start;
2298 while (cur<end) {
2299 while (*cur != '=' && cur<end)
2300 cur++;
2301 cur++;
2302 while (isspace(*cur) && cur<end)
2303 cur++;
2304 if (isNumberish(*cur)) {
2305 end=cur;
2306 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2307 end++;
2308 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2309 reg->rawValue = rawValue;
2311 if (rawValue.left(2)=="0x") {
2312 // ok we have a raw value, now get it's type
2313 end=cur-1;
2314 while (isspace(*end) || *end=='=') end--;
2315 end++;
2316 cur=end-1;
2317 while (*cur!='{' && *cur!=' ')
2318 cur--;
2319 cur++;
2320 reg->type=QString::fromLatin1(cur, end-cur);
2323 // end while loop
2324 cur=end;
2327 // skip to the end of line
2328 while (*output != '\0' && *output != '\n')
2329 output++;
2330 // get rid of the braces at the begining and the end
2331 value.remove(0, 1);
2332 if (value[value.length()-1] == '}') {
2333 value = value.left(value.length()-1);
2336 reg->cookedValue = value;
2338 else
2340 // the rest of the line is the register value
2341 start = output;
2342 output = strchr(output,'\n');
2343 if (output == 0)
2344 output = start + strlen(start);
2345 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2348 * We split the raw from the cooked values.
2349 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2350 * Here, the cooked value comes first, and the raw value is in
2351 * the second part.
2353 int pos = value.find(" (raw ");
2354 if (pos >= 0)
2356 reg->cookedValue = value.left(pos);
2357 reg->rawValue = value.mid(pos+6);
2358 if (reg->rawValue.right(1) == ")") // remove closing bracket
2359 reg->rawValue.truncate(reg->rawValue.length()-1);
2361 else
2364 * In other cases we split off the first token (separated by
2365 * whitespace). It is the raw value. The remainder of the line
2366 * is the cooked value.
2368 int pos = value.find(' ');
2369 if (pos < 0) {
2370 reg->rawValue = value;
2371 reg->cookedValue = QString();
2372 } else {
2373 reg->rawValue = value.left(pos);
2374 reg->cookedValue = value.mid(pos+1);
2378 if (*output != '\0')
2379 output++; /* skip '\n' */
2381 regs.append(reg);
2385 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2387 // "is at address" or "starts at address"
2388 const char* start = strstr(output, "s at address ");
2389 if (start == 0)
2390 return false;
2392 start += 13;
2393 const char* p = start;
2394 while (*p != '\0' && !isspace(*p))
2395 p++;
2396 addrFrom = QString::fromLatin1(start, p-start);
2398 start = strstr(p, "and ends at ");
2399 if (start == 0) {
2400 addrTo = addrFrom;
2401 return true;
2404 start += 12;
2405 p = start;
2406 while (*p != '\0' && !isspace(*p))
2407 p++;
2408 addrTo = QString::fromLatin1(start, p-start);
2410 return true;
2413 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2415 code.clear();
2417 if (strncmp(output, "Dump of assembler", 17) != 0) {
2418 // error message?
2419 DisassembledCode c;
2420 c.code = output;
2421 code.append(new DisassembledCode(c));
2422 return;
2425 // remove first line
2426 const char* p = strchr(output, '\n');
2427 if (p == 0)
2428 return; /* not a regular output */
2430 p++;
2432 // remove last line
2433 const char* end = strstr(output, "End of assembler");
2434 if (end == 0)
2435 end = p + strlen(p);
2437 DbgAddr address;
2439 // remove function offsets from the lines
2440 while (p != end)
2442 const char* start = p;
2443 // address
2444 while (p != end && !isspace(*p))
2445 p++;
2446 address = QString::fromLatin1(start, p-start);
2448 // function name (enclosed in '<>', followed by ':')
2449 while (p != end && *p != '<')
2450 p++;
2451 if (*p == '<')
2452 skipNestedAngles(p);
2453 if (*p == ':')
2454 p++;
2456 // space until code
2457 while (p != end && isspace(*p))
2458 p++;
2460 // code until end of line
2461 start = p;
2462 while (p != end && *p != '\n')
2463 p++;
2464 if (p != end) /* include '\n' */
2465 p++;
2467 DisassembledCode* c = new DisassembledCode;
2468 c->address = address;
2469 c->code = QString::fromLatin1(start, p-start);
2470 code.append(c);
2474 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2476 if (isErrorExpr(output)) {
2477 // error; strip space
2478 QString msg = output;
2479 return msg.stripWhiteSpace();
2482 const char* p = output; /* save typing */
2483 DbgAddr addr;
2484 QString dump;
2486 // the address
2487 while (*p != 0) {
2488 const char* start = p;
2489 while (*p != '\0' && *p != ':' && !isspace(*p))
2490 p++;
2491 addr = QString::fromLatin1(start, p-start);
2492 if (*p != ':') {
2493 // parse function offset
2494 while (isspace(*p))
2495 p++;
2496 start = p;
2497 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2498 p++;
2499 addr.fnoffs = QString::fromLatin1(start, p-start);
2501 if (*p == ':')
2502 p++;
2503 // skip space; this may skip a new-line char!
2504 while (isspace(*p))
2505 p++;
2506 // everything to the end of the line is the memory dump
2507 const char* end = strchr(p, '\n');
2508 if (end != 0) {
2509 dump = QString::fromLatin1(p, end-p);
2510 p = end+1;
2511 } else {
2512 dump = QString::fromLatin1(p, strlen(p));
2513 p += strlen(p);
2515 MemoryDump* md = new MemoryDump;
2516 md->address = addr;
2517 md->dump = dump;
2518 memdump.append(md);
2521 return QString();
2524 QString GdbDriver::editableValue(VarTree* value)
2526 const char* s = value->value().latin1();
2528 // if the variable is a pointer value that contains a cast,
2529 // remove the cast
2530 if (*s == '(') {
2531 skipNested(s, '(', ')');
2532 // skip space
2533 while (isspace(*s))
2534 ++s;
2537 repeat:
2538 const char* start = s;
2540 if (strncmp(s, "0x", 2) == 0)
2542 s += 2;
2543 while (isxdigit(*s))
2544 ++s;
2547 * What we saw so far might have been a reference. If so, edit the
2548 * referenced value. Otherwise, edit the pointer.
2550 if (*s == ':') {
2551 // a reference
2552 ++s;
2553 goto repeat;
2555 // a pointer
2556 // if it's a pointer to a string, remove the string
2557 const char* end = s;
2558 while (isspace(*s))
2559 ++s;
2560 if (*s == '"') {
2561 // a string
2562 return QString::fromLatin1(start, end-start);
2563 } else {
2564 // other pointer
2565 return QString::fromLatin1(start, strlen(start));
2569 // else leave it unchanged (or stripped of the reference preamble)
2570 return s;
2573 QString GdbDriver::parseSetVariable(const char* output)
2575 // if there is any output, it is an error message
2576 QString msg = output;
2577 return msg.stripWhiteSpace();
2581 #include "gdbdriver.moc"