Refine "View Code" command.
[kdbg.git] / kdbg / gdbdriver.cpp
blob07b37247c66e9c9eca6b5caacd5fc841b591da08
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include "gdbdriver.h"
8 #include "exprwnd.h"
9 #include <qregexp.h>
10 #include <qstringlist.h>
11 #include <klocale.h> /* i18n */
12 #include <ctype.h>
13 #include <stdlib.h> /* strtol, atoi */
14 #include <string.h> /* strcpy */
16 #include "assert.h"
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 #include "mydebug.h"
22 static void skipString(const char*& p);
23 static void skipNested(const char*& s, char opening, char closing);
24 static ExprValue* parseVar(const char*& s);
25 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind);
26 static bool parseValue(const char*& s, ExprValue* variable);
27 static bool parseNested(const char*& s, ExprValue* variable);
28 static bool parseVarSeq(const char*& s, ExprValue* variable);
29 static bool parseValueSeq(const char*& s, ExprValue* variable);
31 #define PROMPT "(kdbg)"
32 #define PROMPT_LEN 6
33 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
36 // TODO: make this cmd info stuff non-static to allow multiple
37 // simultaneous gdbs to run!
39 struct GdbCmdInfo {
40 DbgCommand cmd;
41 const char* fmt; /* format string */
42 enum Args {
43 argNone, argString, argNum,
44 argStringNum, argNumString,
45 argString2, argNum2
46 } argsNeeded;
49 #if 0
50 // This is how the QString data print statement generally looks like.
51 // It is set by KDebugger via setPrintQStringDataCmd().
53 static const char printQStringStructFmt[] =
54 // if the string data is junk, fail early
55 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
56 // print an array of shorts
57 "(*(unsigned short*)$qstrunicode)@"
58 // limit the length
59 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
60 // if unicode data is 0, report a special value
61 ":1==0\n";
62 #endif
63 static const char printQStringStructFmt[] = "print (0?\"%s\":$kdbgundef)\n";
66 * The following array of commands must be sorted by the DC* values,
67 * because they are used as indices.
69 static GdbCmdInfo cmds[] = {
70 { DCinitialize, "", GdbCmdInfo::argNone },
71 { DCtty, "tty %s\n", GdbCmdInfo::argString },
72 { DCexecutable, "file \"%s\"\n", GdbCmdInfo::argString },
73 { DCtargetremote, "target remote %s\n", GdbCmdInfo::argString },
74 { DCcorefile, "core-file %s\n", GdbCmdInfo::argString },
75 { DCattach, "attach %s\n", GdbCmdInfo::argString },
76 { DCinfolinemain, "kdbg_infolinemain\n", GdbCmdInfo::argNone },
77 { DCinfolocals, "kdbg__alllocals\n", GdbCmdInfo::argNone },
78 { DCinforegisters, "info all-registers\n", GdbCmdInfo::argNone},
79 { DCexamine, "x %s %s\n", GdbCmdInfo::argString2 },
80 { DCinfoline, "info line %s:%d\n", GdbCmdInfo::argStringNum },
81 { DCdisassemble, "disassemble %s %s\n", GdbCmdInfo::argString2 },
82 { DCsetargs, "set args %s\n", GdbCmdInfo::argString },
83 { DCsetenv, "set env %s %s\n", GdbCmdInfo::argString2 },
84 { DCunsetenv, "unset env %s\n", GdbCmdInfo::argString },
85 { DCsetoption, "setoption %s %d\n", GdbCmdInfo::argStringNum},
86 { DCcd, "cd %s\n", GdbCmdInfo::argString },
87 { DCbt, "bt\n", GdbCmdInfo::argNone },
88 { DCrun, "run\n", GdbCmdInfo::argNone },
89 { DCcont, "cont\n", GdbCmdInfo::argNone },
90 { DCstep, "step\n", GdbCmdInfo::argNone },
91 { DCstepi, "stepi\n", GdbCmdInfo::argNone },
92 { DCnext, "next\n", GdbCmdInfo::argNone },
93 { DCnexti, "nexti\n", GdbCmdInfo::argNone },
94 { DCfinish, "finish\n", GdbCmdInfo::argNone },
95 { DCuntil, "until %s:%d\n", GdbCmdInfo::argStringNum },
96 { DCkill, "kill\n", GdbCmdInfo::argNone },
97 { DCbreaktext, "break %s\n", GdbCmdInfo::argString },
98 { DCbreakline, "break %s:%d\n", GdbCmdInfo::argStringNum },
99 { DCtbreakline, "tbreak %s:%d\n", GdbCmdInfo::argStringNum },
100 { DCbreakaddr, "break *%s\n", GdbCmdInfo::argString },
101 { DCtbreakaddr, "tbreak *%s\n", GdbCmdInfo::argString },
102 { DCwatchpoint, "watch %s\n", GdbCmdInfo::argString },
103 { DCdelete, "delete %d\n", GdbCmdInfo::argNum },
104 { DCenable, "enable %d\n", GdbCmdInfo::argNum },
105 { DCdisable, "disable %d\n", GdbCmdInfo::argNum },
106 { DCprint, "print %s\n", GdbCmdInfo::argString },
107 { DCprintDeref, "print *(%s)\n", GdbCmdInfo::argString },
108 { DCprintStruct, "print %s\n", GdbCmdInfo::argString },
109 { DCprintQStringStruct, printQStringStructFmt, GdbCmdInfo::argString},
110 { DCframe, "frame %d\n", GdbCmdInfo::argNum },
111 { DCfindType, "whatis %s\n", GdbCmdInfo::argString },
112 { DCinfosharedlib, "info sharedlibrary\n", GdbCmdInfo::argNone },
113 { DCthread, "thread %d\n", GdbCmdInfo::argNum },
114 { DCinfothreads, "info threads\n", GdbCmdInfo::argNone },
115 { DCinfobreak, "info breakpoints\n", GdbCmdInfo::argNone },
116 { DCcondition, "condition %d %s\n", GdbCmdInfo::argNumString},
117 { DCsetpc, "set variable $pc=%s\n", GdbCmdInfo::argString },
118 { DCignore, "ignore %d %d\n", GdbCmdInfo::argNum2},
119 { DCprintWChar, "print ($s=%s)?*$s@wcslen($s):0x0\n", GdbCmdInfo::argString },
120 { DCsetvariable, "set variable %s=%s\n", GdbCmdInfo::argString2 },
123 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
124 #define MAX_FMTLEN 200
126 GdbDriver::GdbDriver() :
127 DebuggerDriver(),
128 m_gdbMajor(4), m_gdbMinor(16)
130 strcpy(m_prompt, PROMPT);
131 m_promptMinLen = PROMPT_LEN;
132 m_promptLastChar = PROMPT_LAST_CHAR;
134 #ifndef NDEBUG
135 // check command info array
136 char* perc;
137 for (int i = 0; i < NUM_CMDS; i++) {
138 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
139 assert(i == cmds[i].cmd);
140 // a format string must be associated
141 assert(cmds[i].fmt != 0);
142 assert(strlen(cmds[i].fmt) <= MAX_FMTLEN);
143 // format string must match arg specification
144 switch (cmds[i].argsNeeded) {
145 case GdbCmdInfo::argNone:
146 assert(strchr(cmds[i].fmt, '%') == 0);
147 break;
148 case GdbCmdInfo::argString:
149 perc = strchr(cmds[i].fmt, '%');
150 assert(perc != 0 && perc[1] == 's');
151 assert(strchr(perc+2, '%') == 0);
152 break;
153 case GdbCmdInfo::argNum:
154 perc = strchr(cmds[i].fmt, '%');
155 assert(perc != 0 && perc[1] == 'd');
156 assert(strchr(perc+2, '%') == 0);
157 break;
158 case GdbCmdInfo::argStringNum:
159 perc = strchr(cmds[i].fmt, '%');
160 assert(perc != 0 && perc[1] == 's');
161 perc = strchr(perc+2, '%');
162 assert(perc != 0 && perc[1] == 'd');
163 assert(strchr(perc+2, '%') == 0);
164 break;
165 case GdbCmdInfo::argNumString:
166 perc = strchr(cmds[i].fmt, '%');
167 assert(perc != 0 && perc[1] == 'd');
168 perc = strchr(perc+2, '%');
169 assert(perc != 0 && perc[1] == 's');
170 assert(strchr(perc+2, '%') == 0);
171 break;
172 case GdbCmdInfo::argString2:
173 perc = strchr(cmds[i].fmt, '%');
174 assert(perc != 0 && perc[1] == 's');
175 perc = strchr(perc+2, '%');
176 assert(perc != 0 && perc[1] == 's');
177 assert(strchr(perc+2, '%') == 0);
178 break;
179 case GdbCmdInfo::argNum2:
180 perc = strchr(cmds[i].fmt, '%');
181 assert(perc != 0 && perc[1] == 'd');
182 perc = strchr(perc+2, '%');
183 assert(perc != 0 && perc[1] == 'd');
184 assert(strchr(perc+2, '%') == 0);
185 break;
188 assert(strlen(printQStringStructFmt) <= MAX_FMTLEN);
189 #endif
192 GdbDriver::~GdbDriver()
197 QString GdbDriver::driverName() const
199 return "GDB";
202 QString GdbDriver::defaultGdb()
204 return
205 "gdb"
206 " --fullname" /* to get standard file names each time the prog stops */
207 " --nx"; /* do not execute initialization files */
210 QString GdbDriver::defaultInvocation() const
212 if (m_defaultCmd.isEmpty()) {
213 return defaultGdb();
214 } else {
215 return m_defaultCmd;
219 QStringList GdbDriver::boolOptionList() const
221 // no options
222 return QStringList();
225 bool GdbDriver::startup(QString cmdStr)
227 if (!DebuggerDriver::startup(cmdStr))
228 return false;
230 static const char gdbInitialize[] =
232 * Work around buggy gdbs that do command line editing even if they
233 * are not on a tty. The readline library echos every command back
234 * in this case, which is confusing for us.
236 "set editing off\n"
237 "set confirm off\n"
238 "set print static-members off\n"
239 "set print asm-demangle on\n"
241 * Don't assume that program functions invoked from a watch expression
242 * always succeed.
244 "set unwindonsignal on\n"
246 * Write a short macro that prints all locals: local variables and
247 * function arguments.
249 "define kdbg__alllocals\n"
250 "info locals\n" /* local vars supersede args with same name */
251 "info args\n" /* therefore, arguments must come last */
252 "end\n"
254 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
256 "define kdbg_infolinemain\n"
257 "list\n"
258 "info line\n"
259 "end\n"
260 // change prompt string and synchronize with gdb
261 "set prompt " PROMPT "\n"
264 executeCmdString(DCinitialize, gdbInitialize, false);
266 // assume that QString::null is ok
267 cmds[DCprintQStringStruct].fmt = printQStringStructFmt;
269 return true;
272 void GdbDriver::commandFinished(CmdQueueItem* cmd)
274 // command string must be committed
275 if (!cmd->m_committed) {
276 // not commited!
277 TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
278 cmd->m_cmdString)));
279 return;
282 switch (cmd->m_cmd) {
283 case DCinitialize:
284 // get version number from preamble
286 int len;
287 QRegExp GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
288 int offset = GDBVersion.match(m_output, 0, &len);
289 if (offset >= 0) {
290 char* start = m_output + offset + 5; // skip "\nGDB "
291 char* end;
292 m_gdbMajor = strtol(start, &end, 10);
293 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
294 if (start == end) {
295 // nothing was parsed
296 m_gdbMajor = 4;
297 m_gdbMinor = 16;
299 } else {
300 // assume some default version (what would make sense?)
301 m_gdbMajor = 4;
302 m_gdbMinor = 16;
304 // use a feasible core-file command
305 if (m_gdbMajor > 4 || (m_gdbMajor == 4 && m_gdbMinor >= 16)) {
306 #ifdef __FreeBSD__
307 cmds[DCcorefile].fmt = "target FreeBSD-core %s\n";
308 #else
309 cmds[DCcorefile].fmt = "target core %s\n";
310 #endif
311 } else {
312 cmds[DCcorefile].fmt = "core-file %s\n";
315 break;
316 default:;
319 /* ok, the command is ready */
320 emit commandReceived(cmd, m_output);
322 switch (cmd->m_cmd) {
323 case DCcorefile:
324 case DCinfolinemain:
325 case DCinfoline:
326 case DCframe:
327 case DCattach:
328 case DCrun:
329 case DCcont:
330 case DCstep:
331 case DCstepi:
332 case DCnext:
333 case DCnexti:
334 case DCfinish:
335 case DCuntil:
336 parseMarker(cmd);
337 default:;
342 * The --fullname option makes gdb send a special normalized sequence print
343 * each time the program stops and at some other points. The sequence has
344 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
346 void GdbDriver::parseMarker(CmdQueueItem* cmd)
348 char* startMarker = strstr(m_output, "\032\032");
349 if (startMarker == 0)
350 return;
352 // extract the marker
353 startMarker += 2;
354 TRACE(QString("found marker: ") + startMarker);
355 char* endMarker = strchr(startMarker, '\n');
356 if (endMarker == 0)
357 return;
359 *endMarker = '\0';
361 // extract filename and line number
362 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
364 int len;
365 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
366 if (lineNoStart >= 0) {
367 int lineNo = atoi(startMarker + lineNoStart+1);
369 // get address unless there is one in cmd
370 DbgAddr address = cmd->m_addr;
371 if (address.isEmpty()) {
372 const char* addrStart = startMarker + lineNoStart + len - 2;
373 address = QString(addrStart).stripWhiteSpace();
376 // now show the window
377 startMarker[lineNoStart] = '\0'; /* split off file name */
378 emit activateFileLine(startMarker, lineNo-1, address);
384 * Escapes characters that might lead to problems when they appear on gdb's
385 * command line.
387 static void normalizeStringArg(QString& arg)
390 * Remove trailing backslashes. This approach is a little simplistic,
391 * but we know that there is at the moment no case where a trailing
392 * backslash would make sense.
394 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
395 arg = arg.left(arg.length()-1);
400 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
402 assert(cmd >= 0 && cmd < NUM_CMDS);
403 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
405 normalizeStringArg(strArg);
407 if (cmd == DCcd) {
408 // need the working directory when parsing the output
409 m_programWD = strArg;
410 } else if (cmd == DCsetargs && !m_redirect.isEmpty()) {
412 * Use saved redirection. We prepend it in front of the user's
413 * arguments so that the user can override the redirections.
415 strArg = m_redirect + " " + strArg;
418 QString cmdString;
419 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
420 return cmdString;
423 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
425 assert(cmd >= 0 && cmd < NUM_CMDS);
426 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
428 QString cmdString;
429 cmdString.sprintf(cmds[cmd].fmt, intArg);
430 return cmdString;
433 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
435 assert(cmd >= 0 && cmd < NUM_CMDS);
436 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
437 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
438 cmd == DCexamine ||
439 cmd == DCtty);
441 normalizeStringArg(strArg);
443 QString cmdString;
445 if (cmd == DCtty)
448 * intArg specifies which channels should be redirected to
449 * /dev/null. It is a value or'ed together from RDNstdin,
450 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
451 * command.
453 * Note: We rely on that after the DCtty a DCsetargs will follow,
454 * which will ultimately apply the redirection.
456 static const char* const runRedir[8] = {
458 "</dev/null",
459 ">/dev/null",
460 "</dev/null >/dev/null",
461 "2>/dev/null",
462 "</dev/null 2>/dev/null",
463 ">/dev/null 2>&1",
464 "</dev/null >/dev/null 2>&1"
466 if (strArg.isEmpty())
467 intArg = 7; /* failsafe if no tty */
468 m_redirect = runRedir[intArg & 7];
470 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
473 if (cmd == DCexamine) {
474 // make a format specifier from the intArg
475 static const char size[16] = {
476 '\0', 'b', 'h', 'w', 'g'
478 static const char format[16] = {
479 '\0', 'x', 'd', 'u', 'o', 't',
480 'a', 'c', 'f', 's', 'i'
482 assert(MDTsizemask == 0xf); /* lowest 4 bits */
483 assert(MDTformatmask == 0xf0); /* next 4 bits */
484 int count = 16; /* number of entities to print */
485 char sizeSpec = size[intArg & MDTsizemask];
486 char formatSpec = format[(intArg & MDTformatmask) >> 4];
487 assert(sizeSpec != '\0');
488 assert(formatSpec != '\0');
489 // adjust count such that 16 lines are printed
490 switch (intArg & MDTformatmask) {
491 case MDTstring: case MDTinsn:
492 break; /* no modification needed */
493 default:
494 // all cases drop through:
495 switch (intArg & MDTsizemask) {
496 case MDTbyte:
497 case MDThalfword:
498 count *= 2;
499 case MDTword:
500 count *= 2;
501 case MDTgiantword:
502 count *= 2;
504 break;
506 QString spec;
507 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
509 return makeCmdString(DCexamine, spec, strArg);
512 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
514 // line numbers are zero-based
515 if (cmd == DCuntil || cmd == DCbreakline ||
516 cmd == DCtbreakline || cmd == DCinfoline)
518 intArg++;
520 if (cmd == DCinfoline)
522 // must split off file name part
523 int slash = strArg.findRev('/');
524 if (slash >= 0)
525 strArg = strArg.right(strArg.length()-slash-1);
527 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
529 else
531 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
533 return cmdString;
536 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
538 assert(cmd >= 0 && cmd < NUM_CMDS);
539 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
541 normalizeStringArg(strArg1);
542 normalizeStringArg(strArg2);
544 QString cmdString;
545 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
546 return cmdString;
549 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
551 assert(cmd >= 0 && cmd < NUM_CMDS);
552 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
554 QString cmdString;
555 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
556 return cmdString;
559 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
561 assert(cmd >= 0 && cmd < NUM_CMDS);
562 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
564 if (cmd == DCrun) {
565 m_haveCoreFile = false;
568 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
571 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
572 bool clearLow)
574 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
577 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
578 bool clearLow)
581 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
584 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
585 bool clearLow)
587 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
590 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
591 bool clearLow)
593 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
596 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
597 bool clearLow)
599 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
602 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
604 return queueCmdString(cmd, cmds[cmd].fmt, mode);
607 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
608 QueueMode mode)
610 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
613 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
614 QueueMode mode)
616 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
619 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
620 QueueMode mode)
622 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
625 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
626 QueueMode mode)
628 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
631 void GdbDriver::terminate()
633 kill(SIGTERM);
634 m_state = DSidle;
637 void GdbDriver::detachAndTerminate()
639 kill(SIGINT);
640 flushCommands();
641 executeCmdString(DCinitialize, "detach\nquit\n", true);
644 void GdbDriver::interruptInferior()
646 kill(SIGINT);
647 // remove accidentally queued commands
648 flushHiPriQueue();
651 static bool isErrorExpr(const char* output)
653 return
654 strncmp(output, "Cannot access memory at", 23) == 0 ||
655 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
656 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
657 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
658 strncmp(output, "There is no member or method named", 34) == 0 ||
659 strncmp(output, "A parse error in expression", 27) == 0 ||
660 strncmp(output, "No symbol \"", 11) == 0 ||
661 strncmp(output, "Internal error: ", 16) == 0;
665 * Returns true if the output is an error message. If wantErrorValue is
666 * true, a new ExprValue object is created and filled with the error message.
667 * If there are warnings, they are skipped and output points past the warnings
668 * on return (even if there \e are errors).
670 static bool parseErrorMessage(const char*& output,
671 ExprValue*& variable, bool wantErrorValue)
673 // skip warnings
674 while (strncmp(output, "warning:", 8) == 0)
676 char* end = strchr(output+8, '\n');
677 if (end == 0)
678 output += strlen(output);
679 else
680 output = end+1;
683 if (isErrorExpr(output))
685 if (wantErrorValue) {
686 // put the error message as value in the variable
687 variable = new ExprValue(QString(), VarTree::NKplain);
688 const char* endMsg = strchr(output, '\n');
689 if (endMsg == 0)
690 endMsg = output + strlen(output);
691 variable->m_value = QString::fromLatin1(output, endMsg-output);
692 } else {
693 variable = 0;
695 return true;
697 return false;
700 #if QT_VERSION >= 300
701 union Qt2QChar {
702 short s;
703 struct {
704 uchar row;
705 uchar cell;
706 } qch;
708 #endif
710 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
712 // don't accept the command if it is empty
713 if (cmd == 0 || *cmd == '\0')
714 return;
715 assert(strlen(cmd) <= MAX_FMTLEN);
716 cmds[DCprintQStringStruct].fmt = cmd;
719 ExprValue* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
721 ExprValue* variable = 0;
724 * Parse off white space. gdb sometimes prints white space first if the
725 * printed array leaded to an error.
727 while (isspace(*output))
728 output++;
730 // special case: empty string (0 repetitions)
731 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
733 variable = new ExprValue(QString(), VarTree::NKplain);
734 variable->m_value = "\"\"";
735 return variable;
738 // check for error conditions
739 if (parseErrorMessage(output, variable, wantErrorValue))
740 return variable;
742 // parse the array
744 // find '='
745 const char* p = output;
746 p = strchr(p, '=');
747 if (p == 0) {
748 goto error;
750 // skip white space
751 do {
752 p++;
753 } while (isspace(*p));
755 if (*p == '{')
757 // this is the real data
758 p++; /* skip '{' */
760 // parse the array
761 QString result;
762 QString repeatCount;
763 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
765 * A matrix for separators between the individual "things"
766 * that are added to the string. The first index is a bool,
767 * the second index is from the enum above.
769 static const char* separator[2][3] = {
770 { "\"", 0, ", \"" }, /* normal char is added */
771 { "'", "\", '", ", '" } /* repeated char is added */
774 while (isdigit(*p)) {
775 // parse a number
776 char* end;
777 unsigned short value = (unsigned short) strtoul(p, &end, 0);
778 if (end == p)
779 goto error; /* huh? no valid digits */
780 // skip separator and search for a repeat count
781 p = end;
782 while (isspace(*p) || *p == ',')
783 p++;
784 bool repeats = strncmp(p, "<repeats ", 9) == 0;
785 if (repeats) {
786 const char* start = p;
787 p = strchr(p+9, '>'); /* search end and advance */
788 if (p == 0)
789 goto error;
790 p++; /* skip '>' */
791 repeatCount = QString::fromLatin1(start, p-start);
792 while (isspace(*p) || *p == ',')
793 p++;
795 // p is now at the next char (or the end)
797 // interpret the value as a QChar
798 // TODO: make cross-architecture compatible
799 QChar ch;
800 if (qt3like) {
801 ch = QChar(value);
802 } else {
803 #if QT_VERSION < 300
804 (unsigned short&)ch = value;
805 #else
806 Qt2QChar c;
807 c.s = value;
808 ch.setRow(c.qch.row);
809 ch.setCell(c.qch.cell);
810 #endif
813 // escape a few frequently used characters
814 char escapeCode = '\0';
815 switch (ch.latin1()) {
816 case '\n': escapeCode = 'n'; break;
817 case '\r': escapeCode = 'r'; break;
818 case '\t': escapeCode = 't'; break;
819 case '\b': escapeCode = 'b'; break;
820 case '\"': escapeCode = '\"'; break;
821 case '\\': escapeCode = '\\'; break;
822 case '\0': if (value == 0) { escapeCode = '0'; } break;
825 // add separator
826 result += separator[repeats][lastThing];
827 // add char
828 if (escapeCode != '\0') {
829 result += '\\';
830 ch = escapeCode;
832 result += ch;
834 // fixup repeat count and lastThing
835 if (repeats) {
836 result += "' ";
837 result += repeatCount;
838 lastThing = wasRepeat;
839 } else {
840 lastThing = wasChar;
843 if (*p != '}')
844 goto error;
846 // closing quote
847 if (lastThing == wasChar)
848 result += "\"";
850 // assign the value
851 variable = new ExprValue(QString(), VarTree::NKplain);
852 variable->m_value = result;
854 else if (strncmp(p, "true", 4) == 0)
856 variable = new ExprValue(QString(), VarTree::NKplain);
857 variable->m_value = "QString::null";
859 else if (strncmp(p, "false", 5) == 0)
861 variable = new ExprValue(QString(), VarTree::NKplain);
862 variable->m_value = "(null)";
864 else
865 goto error;
866 return variable;
868 error:
869 if (wantErrorValue) {
870 variable = new ExprValue(QString(), VarTree::NKplain);
871 variable->m_value = "internal parse error";
873 return variable;
876 static ExprValue* parseVar(const char*& s)
878 const char* p = s;
880 // skip whitespace
881 while (isspace(*p))
882 p++;
884 QString name;
885 VarTree::NameKind kind;
887 * Detect anonymouse struct values: The 'name =' part is missing:
888 * s = { a = 1, { b = 2 }}
889 * Note that this detection works only inside structs when the anonymous
890 * struct is not the first member:
891 * s = {{ a = 1 }, b = 2}
892 * This is misparsed (by parseNested()) because it is mistakenly
893 * interprets the second opening brace as the first element of an array
894 * of structs.
896 if (*p == '{')
898 name = i18n("<anonymous struct or union>");
899 kind = VarTree::NKanonymous;
901 else
903 if (!parseName(p, name, kind)) {
904 return 0;
907 // go for '='
908 while (isspace(*p))
909 p++;
910 if (*p != '=') {
911 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
912 return 0;
914 // skip the '=' and more whitespace
915 p++;
916 while (isspace(*p))
917 p++;
920 ExprValue* variable = new ExprValue(name, kind);
922 if (!parseValue(p, variable)) {
923 delete variable;
924 return 0;
926 s = p;
927 return variable;
930 static void skipNested(const char*& s, char opening, char closing)
932 const char* p = s;
934 // parse a nested type
935 int nest = 1;
936 p++;
938 * Search for next matching `closing' char, skipping nested pairs of
939 * `opening' and `closing'.
941 while (*p && nest > 0) {
942 if (*p == opening) {
943 nest++;
944 } else if (*p == closing) {
945 nest--;
947 p++;
949 if (nest != 0) {
950 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
952 s = p;
956 * This function skips text that is delimited by nested angle bracktes, '<>'.
957 * A complication arises because the delimited text can contain the names of
958 * operator<<, operator>>, operator<, and operator>, which have to be treated
959 * specially so that they do not count towards the nesting of '<>'.
960 * This function assumes that the delimited text does not contain strings.
962 static void skipNestedAngles(const char*& s)
964 const char* p = s;
966 int nest = 1;
967 p++; // skip the initial '<'
968 while (*p && nest > 0)
970 // Below we can check for p-s >= 9 instead of 8 because
971 // *s is '<' and cannot be part of "operator".
972 if (*p == '<')
974 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
975 if (p[1] == '<')
976 p++;
977 } else {
978 nest++;
981 else if (*p == '>')
983 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
984 if (p[1] == '>')
985 p++;
986 } else {
987 nest--;
990 p++;
992 if (nest != 0) {
993 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
995 s = p;
999 * Find the end of line that is not inside braces
1001 static void findEnd(const char*& s)
1003 const char* p = s;
1004 while (*p && *p!='\n') {
1005 while (*p && *p!='\n' && *p!='{')
1006 p++;
1007 if (*p=='{') {
1008 p++;
1009 skipNested(p, '{', '}'); p--;
1012 s = p;
1015 static bool isNumberish(const char ch)
1017 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
1020 void skipString(const char*& p)
1022 moreStrings:
1023 // opening quote
1024 char quote = *p++;
1025 while (*p != quote) {
1026 if (*p == '\\') {
1027 // skip escaped character
1028 // no special treatment for octal values necessary
1029 p++;
1031 // simply return if no more characters
1032 if (*p == '\0')
1033 return;
1034 p++;
1036 // closing quote
1037 p++;
1039 * Strings can consist of several parts, some of which contain repeated
1040 * characters.
1042 if (quote == '\'') {
1043 // look ahaead for <repeats 123 times>
1044 const char* q = p+1;
1045 while (isspace(*q))
1046 q++;
1047 if (strncmp(q, "<repeats ", 9) == 0) {
1048 p = q+9;
1049 while (*p != '\0' && *p != '>')
1050 p++;
1051 if (*p != '\0') {
1052 p++; /* skip the '>' */
1056 // is the string continued?
1057 if (*p == ',') {
1058 // look ahead for another quote
1059 const char* q = p+1;
1060 while (isspace(*q))
1061 q++;
1062 if (*q == '"' || *q == '\'') {
1063 // yes!
1064 p = q;
1065 goto moreStrings;
1068 /* very long strings are followed by `...' */
1069 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1070 p += 3;
1074 static void skipNestedWithString(const char*& s, char opening, char closing)
1076 const char* p = s;
1078 // parse a nested expression
1079 int nest = 1;
1080 p++;
1082 * Search for next matching `closing' char, skipping nested pairs of
1083 * `opening' and `closing' as well as strings.
1085 while (*p && nest > 0) {
1086 if (*p == opening) {
1087 nest++;
1088 } else if (*p == closing) {
1089 nest--;
1090 } else if (*p == '\'' || *p == '\"') {
1091 skipString(p);
1092 continue;
1094 p++;
1096 if (nest > 0) {
1097 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1099 s = p;
1102 inline void skipName(const char*& p)
1104 // allow : (for enumeration values) and $ and . (for _vtbl.)
1105 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1106 p++;
1109 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1111 kind = VarTree::NKplain;
1113 const char* p = s;
1114 // examples of names:
1115 // name
1116 // <Object>
1117 // <string<a,b<c>,7> >
1119 if (*p == '<') {
1120 skipNestedAngles(p);
1121 name = QString::fromLatin1(s, p - s);
1122 kind = VarTree::NKtype;
1124 else
1126 // name, which might be "static"; allow dot for "_vtbl."
1127 skipName(p);
1128 if (p == s) {
1129 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1130 return false;
1132 int len = p - s;
1133 if (len == 6 && strncmp(s, "static", 6) == 0) {
1134 kind = VarTree::NKstatic;
1136 // its a static variable, name comes now
1137 while (isspace(*p))
1138 p++;
1139 s = p;
1140 skipName(p);
1141 if (p == s) {
1142 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1143 return false;
1145 len = p - s;
1147 name = QString::fromLatin1(s, len);
1149 // return the new position
1150 s = p;
1151 return true;
1154 static bool parseValue(const char*& s, ExprValue* variable)
1156 variable->m_value = "";
1158 repeat:
1159 if (*s == '{') {
1160 // Sometimes we find the following output:
1161 // {<text variable, no debug info>} 0x40012000 <access>
1162 // {<data variable, no debug info>}
1163 // {<variable (not text or data), no debug info>}
1164 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1165 strncmp(s, "{<data variable, ", 17) == 0 ||
1166 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1168 const char* start = s;
1169 skipNested(s, '{', '}');
1170 variable->m_value = QString::fromLatin1(start, s-start);
1171 variable->m_value += ' '; // add only a single space
1172 while (isspace(*s))
1173 s++;
1174 goto repeat;
1176 else
1178 s++;
1179 if (!parseNested(s, variable)) {
1180 return false;
1182 // must be the closing brace
1183 if (*s != '}') {
1184 TRACE("parse error: missing } of " + variable->m_name);
1185 return false;
1187 s++;
1188 // final white space
1189 while (isspace(*s))
1190 s++;
1192 } else {
1193 // examples of leaf values (cannot be the empty string):
1194 // 123
1195 // -123
1196 // 23.575e+37
1197 // 0x32a45
1198 // @0x012ab4
1199 // (DwContentType&) @0x8123456: {...}
1200 // 0x32a45 "text"
1201 // 10 '\n'
1202 // <optimized out>
1203 // 0x823abc <Array<int> virtual table>
1204 // (void (*)()) 0x8048480 <f(E *, char)>
1205 // (E *) 0xbffff450
1206 // red
1207 // &parseP (HTMLClueV *, char *)
1208 // Variable "x" is not available.
1209 // The value of variable 'x' is distributed...
1210 // -nan(0xfffff081defa0)
1212 const char*p = s;
1214 // check for type
1215 QString type;
1216 if (*p == '(') {
1217 skipNested(p, '(', ')');
1219 while (isspace(*p))
1220 p++;
1221 variable->m_value = QString::fromLatin1(s, p - s);
1224 bool reference = false;
1225 if (*p == '@') {
1226 // skip reference marker
1227 p++;
1228 reference = true;
1230 const char* start = p;
1231 if (*p == '-')
1232 p++;
1234 // some values consist of more than one token
1235 bool checkMultiPart = false;
1237 if (p[0] == '0' && p[1] == 'x') {
1238 // parse hex number
1239 p += 2;
1240 while (isxdigit(*p))
1241 p++;
1244 * Assume this is a pointer, but only if it's not a reference, since
1245 * references can't be expanded.
1247 if (!reference) {
1248 variable->m_varKind = VarTree::VKpointer;
1249 } else {
1251 * References are followed by a colon, in which case we'll
1252 * find the value following the reference address.
1254 if (*p == ':') {
1255 p++;
1256 } else {
1257 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1258 reference = false;
1261 checkMultiPart = true;
1262 } else if (isdigit(*p)) {
1263 // parse decimal number, possibly a float
1264 while (isdigit(*p))
1265 p++;
1266 if (*p == '.') { /* TODO: obey i18n? */
1267 // In long arrays an integer may be followed by '...'.
1268 // We test for this situation and don't gobble the '...'.
1269 if (p[1] != '.' || p[0] != '.') {
1270 // fractional part
1271 p++;
1272 while (isdigit(*p))
1273 p++;
1276 if (*p == 'e' || *p == 'E') {
1277 p++;
1278 // exponent
1279 if (*p == '-' || *p == '+')
1280 p++;
1281 while (isdigit(*p))
1282 p++;
1285 // for char variables there is the char, eg. 10 '\n'
1286 checkMultiPart = true;
1287 } else if (*p == '<') {
1288 // e.g. <optimized out>
1289 skipNestedAngles(p);
1290 } else if (*p == '"' || *p == '\'') {
1291 // character may have multipart: '\000' <repeats 11 times>
1292 checkMultiPart = *p == '\'';
1293 // found a string
1294 skipString(p);
1295 } else if (*p == '&') {
1296 // function pointer
1297 p++;
1298 skipName(p);
1299 while (isspace(*p)) {
1300 p++;
1302 if (*p == '(') {
1303 skipNested(p, '(', ')');
1305 } else if (strncmp(p, "Variable \"", 10) == 0) {
1306 // Variable "x" is not available.
1307 p += 10; // skip to "
1308 skipName(p);
1309 if (strncmp(p, "\" is not available.", 19) == 0) {
1310 p += 19;
1312 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1313 p += 23;
1314 skipName(p);
1315 const char* e = strchr(p, '.');
1316 if (e == 0) {
1317 p += strlen(p);
1318 } else {
1319 p = e+1;
1321 } else {
1322 // must be an enumeration value
1323 skipName(p);
1324 // hmm, not necessarily: nan (floating point Not a Number)
1325 // is followed by a number in ()
1326 if (*p == '(')
1327 skipNested(p, '(', ')');
1329 variable->m_value += QString::fromLatin1(start, p - start);
1331 // remove line breaks from the value; this is ok since
1332 // string values never contain a literal line break
1333 variable->m_value.replace('\n', ' ');
1335 if (checkMultiPart) {
1336 // white space
1337 while (isspace(*p))
1338 p++;
1339 // may be followed by a string or <...>
1340 start = p;
1342 if (*p == '"' || *p == '\'') {
1343 skipString(p);
1344 } else if (*p == '<') {
1345 // if this value is part of an array, it might be followed
1346 // by <repeats 15 times>, which we don't skip here
1347 if (strncmp(p, "<repeats ", 9) != 0)
1348 skipNestedAngles(p);
1350 if (p != start) {
1351 // there is always a blank before the string,
1352 // which we will include in the final string value
1353 variable->m_value += QString::fromLatin1(start-1, (p - start)+1);
1354 // if this was a pointer, reset that flag since we
1355 // now got the value
1356 variable->m_varKind = VarTree::VKsimple;
1360 if (variable->m_value.length() == 0) {
1361 TRACE("parse error: no value for " + variable->m_name);
1362 return false;
1365 // final white space
1366 while (isspace(*p))
1367 p++;
1368 s = p;
1371 * If this was a reference, the value follows. It might even be a
1372 * composite variable!
1374 if (reference) {
1375 goto repeat;
1379 return true;
1382 static bool parseNested(const char*& s, ExprValue* variable)
1384 // could be a structure or an array
1385 while (isspace(*s))
1386 s++;
1388 const char* p = s;
1389 bool isStruct = false;
1391 * If there is a name followed by an = or an < -- which starts a type
1392 * name -- or "static", it is a structure
1394 if (*p == '<' || *p == '}') {
1395 isStruct = true;
1396 } else if (strncmp(p, "static ", 7) == 0) {
1397 isStruct = true;
1398 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1399 // look ahead for a comma after the name
1400 skipName(p);
1401 while (isspace(*p))
1402 p++;
1403 if (*p == '=') {
1404 isStruct = true;
1406 p = s; /* rescan the name */
1408 if (isStruct) {
1409 if (!parseVarSeq(p, variable)) {
1410 return false;
1412 variable->m_varKind = VarTree::VKstruct;
1413 } else {
1414 if (!parseValueSeq(p, variable)) {
1415 return false;
1417 variable->m_varKind = VarTree::VKarray;
1419 s = p;
1420 return true;
1423 static bool parseVarSeq(const char*& s, ExprValue* variable)
1425 // parse a comma-separated sequence of variables
1426 ExprValue* var = variable; /* var != 0 to indicate success if empty seq */
1427 for (;;) {
1428 if (*s == '}')
1429 break;
1430 if (strncmp(s, "<No data fields>}", 17) == 0)
1432 // no member variables, so break out immediately
1433 s += 16; /* go to the closing brace */
1434 break;
1436 var = parseVar(s);
1437 if (var == 0)
1438 break; /* syntax error */
1439 variable->appendChild(var);
1440 if (*s != ',')
1441 break;
1442 // skip the comma and whitespace
1443 s++;
1444 while (isspace(*s))
1445 s++;
1447 return var != 0;
1450 static bool parseValueSeq(const char*& s, ExprValue* variable)
1452 // parse a comma-separated sequence of variables
1453 int index = 0;
1454 bool good;
1455 for (;;) {
1456 QString name;
1457 name.sprintf("[%d]", index);
1458 ExprValue* var = new ExprValue(name, VarTree::NKplain);
1459 good = parseValue(s, var);
1460 if (!good) {
1461 delete var;
1462 return false;
1464 // a value may be followed by "<repeats 45 times>"
1465 if (strncmp(s, "<repeats ", 9) == 0) {
1466 s += 9;
1467 char* end;
1468 int l = strtol(s, &end, 10);
1469 if (end == s || strncmp(end, " times>", 7) != 0) {
1470 // should not happen
1471 delete var;
1472 return false;
1474 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1475 // replace name and advance index
1476 name.sprintf("[%d .. %d]", index, index+l-1);
1477 var->m_name = name;
1478 index += l;
1479 // skip " times>" and space
1480 s = end+7;
1481 // possible final space
1482 while (isspace(*s))
1483 s++;
1484 } else {
1485 index++;
1487 variable->appendChild(var);
1488 // long arrays may be terminated by '...'
1489 if (strncmp(s, "...", 3) == 0) {
1490 s += 3;
1491 ExprValue* var = new ExprValue("...", VarTree::NKplain);
1492 var->m_value = i18n("<additional entries of the array suppressed>");
1493 variable->appendChild(var);
1494 break;
1496 if (*s != ',') {
1497 break;
1499 // skip the comma and whitespace
1500 s++;
1501 while (isspace(*s))
1502 s++;
1503 // sometimes there is a closing brace after a comma
1504 // if (*s == '}')
1505 // break;
1507 return true;
1511 * Parses a stack frame.
1513 static void parseFrameInfo(const char*& s, QString& func,
1514 QString& file, int& lineNo, DbgAddr& address)
1516 const char* p = s;
1518 // next may be a hexadecimal address
1519 if (*p == '0') {
1520 const char* start = p;
1521 p++;
1522 if (*p == 'x')
1523 p++;
1524 while (isxdigit(*p))
1525 p++;
1526 address = QString::fromLatin1(start, p-start);
1527 if (strncmp(p, " in ", 4) == 0)
1528 p += 4;
1529 } else {
1530 address = DbgAddr();
1532 const char* start = p;
1533 // check for special signal handler frame
1534 if (strncmp(p, "<signal handler called>", 23) == 0) {
1535 func = QString::fromLatin1(start, 23);
1536 file = QString();
1537 lineNo = -1;
1538 s = p+23;
1539 if (*s == '\n')
1540 s++;
1541 return;
1545 * Skip the function name. It is terminated by a left parenthesis
1546 * which does not delimit "(anonymous namespace)" and which is
1547 * outside the angle brackets <> of template parameter lists
1548 * and is preceded by a space.
1550 while (*p != '\0')
1552 if (*p == '<') {
1553 // check for operator<< and operator<
1554 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1556 p++;
1557 if (*p == '<')
1558 p++;
1560 else
1562 // skip template parameter list
1563 skipNestedAngles(p);
1565 } else if (*p == '(') {
1566 // this skips "(anonymous namespace)" as well as the formal
1567 // parameter list of the containing function if this is a member
1568 // of a nested class
1569 skipNestedWithString(p, '(', ')');
1570 } else if (*p == ' ') {
1571 ++p;
1572 if (*p == '(')
1573 break; // parameter list found
1574 } else {
1575 p++;
1579 if (*p == '\0') {
1580 func = start;
1581 file = QString();
1582 lineNo = -1;
1583 s = p;
1584 return;
1587 * Skip parameters. But notice that for complicated conversion
1588 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1589 * pointer to function) as well as operator()(...) we have to skip
1590 * additional pairs of parentheses. Furthermore, recent gdbs write the
1591 * demangled name followed by the arguments in a pair of parentheses,
1592 * where the demangled name can end in "const".
1594 do {
1595 skipNestedWithString(p, '(', ')');
1596 while (isspace(*p))
1597 p++;
1598 // skip "const"
1599 if (strncmp(p, "const", 5) == 0) {
1600 p += 5;
1601 while (isspace(*p))
1602 p++;
1604 } while (*p == '(');
1606 // check for file position
1607 if (strncmp(p, "at ", 3) == 0) {
1608 p += 3;
1609 const char* fileStart = p;
1610 // go for the end of the line
1611 while (*p != '\0' && *p != '\n')
1612 p++;
1613 // search back for colon
1614 const char* colon = p;
1615 do {
1616 --colon;
1617 } while (*colon != ':');
1618 file = QString::fromLatin1(fileStart, colon-fileStart);
1619 lineNo = atoi(colon+1)-1;
1620 // skip new-line
1621 if (*p != '\0')
1622 p++;
1623 } else {
1624 // check for "from shared lib"
1625 if (strncmp(p, "from ", 5) == 0) {
1626 p += 5;
1627 // go for the end of the line
1628 while (*p != '\0' && *p != '\n')
1629 p++;
1630 // skip new-line
1631 if (*p != '\0')
1632 p++;
1634 file = "";
1635 lineNo = -1;
1637 // construct the function name (including file info)
1638 if (*p == '\0') {
1639 func = start;
1640 } else {
1641 func = QString::fromLatin1(start, p-start-1); /* don't include \n */
1643 s = p;
1646 * Replace \n (and whitespace around it) in func by a blank. We cannot
1647 * use QString::simplifyWhiteSpace() for this because this would also
1648 * simplify space that belongs to a string arguments that gdb sometimes
1649 * prints in the argument lists of the function.
1651 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1652 int nl = 0;
1653 while ((nl = func.find('\n', nl)) >= 0) {
1654 // search back to the beginning of the whitespace
1655 int startWhite = nl;
1656 do {
1657 --startWhite;
1658 } while (isspace(func[startWhite].latin1()));
1659 startWhite++;
1660 // search forward to the end of the whitespace
1661 do {
1662 nl++;
1663 } while (isspace(func[nl].latin1()));
1664 // replace
1665 func.replace(startWhite, nl-startWhite, " ");
1666 /* continue searching for more \n's at this place: */
1667 nl = startWhite+1;
1673 * Parses a stack frame including its frame number
1675 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1676 QString& file, int& lineNo, DbgAddr& address)
1678 // Example:
1679 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1680 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1682 // must start with a hash mark followed by number
1683 // or with "Breakpoint " followed by number and comma
1684 if (s[0] == '#') {
1685 if (!isdigit(s[1]))
1686 return false;
1687 s++; /* skip the hash mark */
1688 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1689 if (!isdigit(s[11]))
1690 return false;
1691 s += 11; /* skip "Breakpoint" */
1692 } else
1693 return false;
1695 // frame number
1696 frameNo = atoi(s);
1697 while (isdigit(*s))
1698 s++;
1699 // space and comma
1700 while (isspace(*s) || *s == ',')
1701 s++;
1702 parseFrameInfo(s, func, file, lineNo, address);
1703 return true;
1706 void GdbDriver::parseBackTrace(const char* output, std::list<StackFrame>& stack)
1708 QString func, file;
1709 int lineNo, frameNo;
1710 DbgAddr address;
1712 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1713 stack.push_back(StackFrame());
1714 StackFrame* frm = &stack.back();
1715 frm->frameNo = frameNo;
1716 frm->fileName = file;
1717 frm->lineNo = lineNo;
1718 frm->address = address;
1719 frm->var = new ExprValue(func, VarTree::NKplain);
1723 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1724 QString& file, int& lineNo, DbgAddr& address)
1726 QString func;
1727 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1731 bool GdbDriver::parseBreakList(const char* output, std::list<Breakpoint>& brks)
1733 // skip first line, which is the headline
1734 const char* p = strchr(output, '\n');
1735 if (p == 0)
1736 return false;
1737 p++;
1738 if (*p == '\0')
1739 return false;
1741 // split up a line
1742 const char* end;
1743 char* dummy;
1744 while (*p != '\0') {
1745 Breakpoint bp;
1746 // get Num
1747 bp.id = strtol(p, &dummy, 10); /* don't care about overflows */
1748 p = dummy;
1749 // get Type
1750 while (isspace(*p))
1751 p++;
1752 if (strncmp(p, "breakpoint", 10) == 0) {
1753 p += 10;
1754 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1755 bp.type = Breakpoint::watchpoint;
1756 p += 13;
1757 } else if (strncmp(p, "watchpoint", 10) == 0) {
1758 bp.type = Breakpoint::watchpoint;
1759 p += 10;
1761 while (isspace(*p))
1762 p++;
1763 if (*p == '\0')
1764 break;
1765 // get Disp
1766 bp.temporary = *p++ == 'd';
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 bp.enabled = *p++ == 'y';
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 (bp.type == Breakpoint::breakpoint &&
1783 strncmp(p, "0x", 2) == 0)
1785 const char* start = p;
1786 while (*p != '\0' && !isspace(*p))
1787 p++;
1788 bp.address = QString::fromLatin1(start, p-start);
1789 while (isspace(*p) && *p != '\n')
1790 p++;
1791 if (*p == '\0')
1792 break;
1794 // remainder is location, hit and ignore count, condition
1795 end = strchr(p, '\n');
1796 if (end == 0) {
1797 bp.location = p;
1798 p += bp.location.length();
1799 } else {
1800 bp.location = QString::fromLatin1(p, end-p).stripWhiteSpace();
1801 p = end+1; /* skip over \n */
1804 // may be continued in next line
1805 while (isspace(*p)) { /* p points to beginning of line */
1806 // skip white space at beginning of line
1807 while (isspace(*p))
1808 p++;
1810 // seek end of line
1811 end = strchr(p, '\n');
1812 if (end == 0)
1813 end = p+strlen(p);
1815 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1816 // extract the hit count
1817 p += 22;
1818 bp.hitCount = strtol(p, &dummy, 10);
1819 TRACE(QString("hit count %1").arg(bp.hitCount));
1820 } else if (strncmp(p, "stop only if ", 13) == 0) {
1821 // extract condition
1822 p += 13;
1823 bp.condition = QString::fromLatin1(p, end-p).stripWhiteSpace();
1824 TRACE("condition: "+bp.condition);
1825 } else if (strncmp(p, "ignore next ", 12) == 0) {
1826 // extract ignore count
1827 p += 12;
1828 bp.ignoreCount = strtol(p, &dummy, 10);
1829 TRACE(QString("ignore count %1").arg(bp.ignoreCount));
1830 } else {
1831 // indeed a continuation
1832 bp.location += " " + QString::fromLatin1(p, end-p).stripWhiteSpace();
1834 p = end;
1835 if (*p != '\0')
1836 p++; /* skip '\n' */
1838 brks.push_back(bp);
1840 return true;
1843 std::list<ThreadInfo> GdbDriver::parseThreadList(const char* output)
1845 std::list<ThreadInfo> threads;
1846 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1847 // no threads
1848 return threads;
1851 const char* p = output;
1852 while (*p != '\0') {
1853 ThreadInfo thr;
1854 // seach look for thread id, watching out for the focus indicator
1855 thr.hasFocus = false;
1856 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1857 p++;
1858 if (*p == '*') {
1859 thr.hasFocus = true;
1860 p++;
1861 // there follows only whitespace
1863 char* end;
1864 thr.id = strtol(p, &end, 10);
1865 if (p == end) {
1866 // syntax error: no number found; bail out
1867 return threads;
1869 p = end;
1871 // skip space
1872 while (isspace(*p))
1873 p++;
1876 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1878 end = strstr(p, " ");
1879 if (end == 0) {
1880 // syntax error; bail out
1881 return threads;
1883 thr.threadName = QString::fromLatin1(p, end-p);
1884 p = end+2;
1887 * Now follows a standard stack frame. Sometimes, however, gdb
1888 * catches a thread at an instant where it doesn't have a stack.
1890 if (strncmp(p, "[No stack.]", 11) != 0) {
1891 ::parseFrameInfo(p, thr.function, thr.fileName, thr.lineNo, thr.address);
1892 } else {
1893 thr.function = "[No stack]";
1894 thr.lineNo = -1;
1895 p += 11; /* \n is skipped above */
1898 threads.push_back(thr);
1900 return threads;
1903 static bool parseNewBreakpoint(const char* o, int& id,
1904 QString& file, int& lineNo, QString& address);
1905 static bool parseNewWatchpoint(const char* o, int& id,
1906 QString& expr);
1908 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1909 QString& file, int& lineNo, QString& address)
1911 const char* o = output;
1912 // skip lines of that begin with "(Cannot find"
1913 while (strncmp(o, "(Cannot find", 12) == 0) {
1914 o = strchr(o, '\n');
1915 if (o == 0)
1916 return false;
1917 o++; /* skip newline */
1920 if (strncmp(o, "Breakpoint ", 11) == 0) {
1921 output += 11; /* skip "Breakpoint " */
1922 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1923 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1924 output += 20;
1925 return ::parseNewWatchpoint(output, id, address);
1926 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1927 output += 11;
1928 return ::parseNewWatchpoint(output, id, address);
1930 return false;
1933 static bool parseNewBreakpoint(const char* o, int& id,
1934 QString& file, int& lineNo, QString& address)
1936 // breakpoint id
1937 char* p;
1938 id = strtoul(o, &p, 10);
1939 if (p == o)
1940 return false;
1942 // check for the address
1943 if (strncmp(p, " at 0x", 6) == 0) {
1944 char* start = p+4; /* skip " at ", but not 0x */
1945 p += 6;
1946 while (isxdigit(*p))
1947 ++p;
1948 address = QString::fromLatin1(start, p-start);
1951 // file name
1952 char* fileStart = strstr(p, "file ");
1953 if (fileStart == 0)
1954 return !address.isEmpty(); /* parse error only if there's no address */
1955 fileStart += 5;
1957 // line number
1958 char* numStart = strstr(fileStart, ", line ");
1959 QString fileName = QString::fromLatin1(fileStart, numStart-fileStart);
1960 numStart += 7;
1961 int line = strtoul(numStart, &p, 10);
1962 if (numStart == p)
1963 return false;
1965 file = fileName;
1966 lineNo = line-1; /* zero-based! */
1967 return true;
1970 static bool parseNewWatchpoint(const char* o, int& id,
1971 QString& expr)
1973 // watchpoint id
1974 char* p;
1975 id = strtoul(o, &p, 10);
1976 if (p == o)
1977 return false;
1979 if (strncmp(p, ": ", 2) != 0)
1980 return false;
1981 p += 2;
1983 // all the rest on the line is the expression
1984 expr = QString::fromLatin1(p, strlen(p)).stripWhiteSpace();
1985 return true;
1988 void GdbDriver::parseLocals(const char* output, std::list<ExprValue*>& newVars)
1990 // check for possible error conditions
1991 if (strncmp(output, "No symbol table", 15) == 0)
1993 return;
1996 while (*output != '\0') {
1997 while (isspace(*output))
1998 output++;
1999 if (*output == '\0')
2000 break;
2001 // skip occurrences of "No locals" and "No args"
2002 if (strncmp(output, "No locals", 9) == 0 ||
2003 strncmp(output, "No arguments", 12) == 0)
2005 output = strchr(output, '\n');
2006 if (output == 0) {
2007 break;
2009 continue;
2012 ExprValue* variable = parseVar(output);
2013 if (variable == 0) {
2014 break;
2016 // do not add duplicates
2017 for (std::list<ExprValue*>::iterator o = newVars.begin(); o != newVars.end(); ++o) {
2018 if ((*o)->m_name == variable->m_name) {
2019 delete variable;
2020 goto skipDuplicate;
2023 newVars.push_back(variable);
2024 skipDuplicate:;
2028 ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
2030 ExprValue* var = 0;
2031 // check for error conditions
2032 if (!parseErrorMessage(output, var, wantErrorValue))
2034 // parse the variable
2035 var = parseVar(output);
2037 return var;
2040 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2042 bool isGood = false;
2043 message = QString(output).simplifyWhiteSpace();
2044 if (message.isEmpty()) {
2045 message = i18n("New working directory: ") + m_programWD;
2046 isGood = true;
2048 return isGood;
2051 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2053 message = output;
2055 m_haveCoreFile = false;
2058 * Lines starting with the following do not indicate errors:
2059 * Using host libthread_db
2060 * (no debugging symbols found)
2062 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2063 strncmp(output, "(no debugging symbols found)", 28) == 0)
2065 // this line is good, go to the next one
2066 const char* end = strchr(output, '\n');
2067 if (end == 0)
2068 output += strlen(output);
2069 else
2070 output = end+1;
2074 * If we've parsed all lines, there was no error.
2076 return output[0] == '\0';
2079 bool GdbDriver::parseCoreFile(const char* output)
2081 // if command succeeded, gdb emits a line starting with "#0 "
2082 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2083 return m_haveCoreFile;
2086 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2088 // optionally: "program changed, rereading symbols",
2089 // followed by:
2090 // "Program exited normally"
2091 // "Program terminated with wignal SIGSEGV"
2092 // "Program received signal SIGINT" or other signal
2093 // "Breakpoint..."
2095 // go through the output, line by line, checking what we have
2096 const char* start = output - 1;
2097 uint flags = SFprogramActive;
2098 message = QString();
2099 do {
2100 start++; /* skip '\n' */
2102 if (strncmp(start, "Program ", 8) == 0 ||
2103 strncmp(start, "ptrace: ", 8) == 0) {
2105 * When we receive a signal, the program remains active.
2107 * Special: If we "stopped" in a corefile, the string "Program
2108 * terminated with signal"... is displayed. (Normally, we see
2109 * "Program received signal"... when a signal happens.)
2111 if (strncmp(start, "Program exited", 14) == 0 ||
2112 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2113 strncmp(start, "ptrace: ", 8) == 0)
2115 flags &= ~SFprogramActive;
2118 // set message
2119 const char* endOfMessage = strchr(start, '\n');
2120 if (endOfMessage == 0)
2121 endOfMessage = start + strlen(start);
2122 message = QString::fromLatin1(start, endOfMessage-start);
2123 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2125 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2126 * that it stopped at a temporary breakpoint).
2128 flags |= SFrefreshBreak;
2129 } else if (strstr(start, "re-reading symbols.") != 0) {
2130 flags |= SFrefreshSource;
2133 // next line, please
2134 start = strchr(start, '\n');
2135 } while (start != 0);
2138 * Gdb only notices when new threads have appeared, but not when a
2139 * thread finishes. So we always have to assume that the list of
2140 * threads has changed.
2142 flags |= SFrefreshThreads;
2144 return flags;
2147 QStringList GdbDriver::parseSharedLibs(const char* output)
2149 QStringList shlibs;
2150 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2151 return shlibs;
2153 // parse the table of shared libraries
2155 // strip off head line
2156 output = strchr(output, '\n');
2157 if (output == 0)
2158 return shlibs;
2159 output++; /* skip '\n' */
2160 QString shlibName;
2161 while (*output != '\0') {
2162 // format of a line is
2163 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2164 // 3 blocks of non-space followed by space
2165 for (int i = 0; *output != '\0' && i < 3; i++) {
2166 while (*output != '\0' && !isspace(*output)) { /* non-space */
2167 output++;
2169 while (isspace(*output)) { /* space */
2170 output++;
2173 if (*output == '\0')
2174 return shlibs;
2175 const char* start = output;
2176 output = strchr(output, '\n');
2177 if (output == 0)
2178 output = start + strlen(start);
2179 shlibName = QString::fromLatin1(start, output-start);
2180 if (*output != '\0')
2181 output++;
2182 shlibs.append(shlibName);
2183 TRACE("found shared lib " + shlibName);
2185 return shlibs;
2188 bool GdbDriver::parseFindType(const char* output, QString& type)
2190 if (strncmp(output, "type = ", 7) != 0)
2191 return false;
2194 * Everything else is the type. We strip off any leading "const" and any
2195 * trailing "&" on the grounds that neither affects the decoding of the
2196 * object. We also strip off all white-space from the type.
2198 output += 7;
2199 if (strncmp(output, "const ", 6) == 0)
2200 output += 6;
2201 type = output;
2202 type.replace(QRegExp("\\s+"), "");
2203 if (type.endsWith("&"))
2204 type.truncate(type.length() - 1);
2205 return true;
2208 std::list<RegisterInfo> GdbDriver::parseRegisters(const char* output)
2210 std::list<RegisterInfo> regs;
2211 if (strncmp(output, "The program has no registers now", 32) == 0) {
2212 return regs;
2215 QString value;
2217 // parse register values
2218 while (*output != '\0')
2220 RegisterInfo reg;
2221 // skip space at the start of the line
2222 while (isspace(*output))
2223 output++;
2225 // register name
2226 const char* start = output;
2227 while (*output != '\0' && !isspace(*output))
2228 output++;
2229 if (*output == '\0')
2230 break;
2231 reg.regName = QString::fromLatin1(start, output-start);
2233 // skip space
2234 while (isspace(*output))
2235 output++;
2238 * If we find a brace now, this is a vector register. We look for
2239 * the closing brace and treat the result as cooked value.
2241 if (*output == '{')
2243 start = output;
2244 skipNested(output, '{', '}');
2245 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2246 // skip space, but not the end of line
2247 while (isspace(*output) && *output != '\n')
2248 output++;
2249 // get rid of the braces at the begining and the end
2250 value.remove(0, 1);
2251 if (value[value.length()-1] == '}') {
2252 value = value.left(value.length()-1);
2254 // gdb 5.3 doesn't print a separate set of raw values
2255 if (*output == '{') {
2256 // another set of vector follows
2257 // what we have so far is the raw value
2258 reg.rawValue = value;
2260 start = output;
2261 skipNested(output, '{', '}');
2262 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2263 } else {
2264 // for gdb 5.3
2265 // find first type that does not have an array, this is the RAW value
2266 const char* end=start;
2267 findEnd(end);
2268 const char* cur=start;
2269 while (cur<end) {
2270 while (*cur != '=' && cur<end)
2271 cur++;
2272 cur++;
2273 while (isspace(*cur) && cur<end)
2274 cur++;
2275 if (isNumberish(*cur)) {
2276 end=cur;
2277 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2278 end++;
2279 QString rawValue = QString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
2280 reg.rawValue = rawValue;
2282 if (rawValue.left(2)=="0x") {
2283 // ok we have a raw value, now get it's type
2284 end=cur-1;
2285 while (isspace(*end) || *end=='=') end--;
2286 end++;
2287 cur=end-1;
2288 while (*cur!='{' && *cur!=' ')
2289 cur--;
2290 cur++;
2291 reg.type = QString::fromLatin1(cur, end-cur);
2294 // end while loop
2295 cur=end;
2298 // skip to the end of line
2299 while (*output != '\0' && *output != '\n')
2300 output++;
2301 // get rid of the braces at the begining and the end
2302 value.remove(0, 1);
2303 if (value[value.length()-1] == '}') {
2304 value.truncate(value.length()-1);
2307 reg.cookedValue = value;
2309 else
2311 // the rest of the line is the register value
2312 start = output;
2313 output = strchr(output,'\n');
2314 if (output == 0)
2315 output = start + strlen(start);
2316 value = QString::fromLatin1(start, output-start).simplifyWhiteSpace();
2319 * We split the raw from the cooked values.
2320 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2321 * Here, the cooked value comes first, and the raw value is in
2322 * the second part.
2324 int pos = value.find(" (raw ");
2325 if (pos >= 0)
2327 reg.cookedValue = value.left(pos);
2328 reg.rawValue = value.mid(pos+6);
2329 if (reg.rawValue.right(1) == ")") // remove closing bracket
2330 reg.rawValue.truncate(reg.rawValue.length()-1);
2332 else
2335 * In other cases we split off the first token (separated by
2336 * whitespace). It is the raw value. The remainder of the line
2337 * is the cooked value.
2339 int pos = value.find(' ');
2340 if (pos < 0) {
2341 reg.rawValue = value;
2342 reg.cookedValue = QString();
2343 } else {
2344 reg.rawValue = value.left(pos);
2345 reg.cookedValue = value.mid(pos+1);
2349 if (*output != '\0')
2350 output++; /* skip '\n' */
2352 regs.push_back(reg);
2354 return regs;
2357 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2359 // "is at address" or "starts at address"
2360 const char* start = strstr(output, "s at address ");
2361 if (start == 0)
2362 return false;
2364 start += 13;
2365 const char* p = start;
2366 while (*p != '\0' && !isspace(*p))
2367 p++;
2368 addrFrom = QString::fromLatin1(start, p-start);
2370 start = strstr(p, "and ends at ");
2371 if (start == 0) {
2372 addrTo = addrFrom;
2373 return true;
2376 start += 12;
2377 p = start;
2378 while (*p != '\0' && !isspace(*p))
2379 p++;
2380 addrTo = QString::fromLatin1(start, p-start);
2382 return true;
2385 std::list<DisassembledCode> GdbDriver::parseDisassemble(const char* output)
2387 std::list<DisassembledCode> code;
2389 if (strncmp(output, "Dump of assembler", 17) != 0) {
2390 // error message?
2391 DisassembledCode c;
2392 c.code = output;
2393 code.push_back(c);
2394 return code;
2397 // remove first line
2398 const char* p = strchr(output, '\n');
2399 if (p == 0)
2400 return code; /* not a regular output */
2402 p++;
2404 // remove last line
2405 const char* end = strstr(output, "End of assembler");
2406 if (end == 0)
2407 end = p + strlen(p);
2409 // remove function offsets from the lines
2410 while (p != end)
2412 DisassembledCode c;
2413 const char* start = p;
2414 // address
2415 while (p != end && !isspace(*p))
2416 p++;
2417 c.address = QString::fromLatin1(start, p-start);
2419 // function name (enclosed in '<>', followed by ':')
2420 while (p != end && *p != '<')
2421 p++;
2422 if (*p == '<')
2423 skipNestedAngles(p);
2424 if (*p == ':')
2425 p++;
2427 // space until code
2428 while (p != end && isspace(*p))
2429 p++;
2431 // code until end of line
2432 start = p;
2433 while (p != end && *p != '\n')
2434 p++;
2435 if (p != end) /* include '\n' */
2436 p++;
2438 c.code = QString::fromLatin1(start, p-start);
2439 code.push_back(c);
2441 return code;
2444 QString GdbDriver::parseMemoryDump(const char* output, std::list<MemoryDump>& memdump)
2446 if (isErrorExpr(output)) {
2447 // error; strip space
2448 QString msg = output;
2449 return msg.stripWhiteSpace();
2452 const char* p = output; /* save typing */
2454 // the address
2455 while (*p != 0) {
2456 MemoryDump md;
2458 const char* start = p;
2459 while (*p != '\0' && *p != ':' && !isspace(*p))
2460 p++;
2461 md.address = QString::fromLatin1(start, p-start);
2462 if (*p != ':') {
2463 // parse function offset
2464 while (isspace(*p))
2465 p++;
2466 start = p;
2467 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2468 p++;
2469 md.address.fnoffs = QString::fromLatin1(start, p-start);
2471 if (*p == ':')
2472 p++;
2473 // skip space; this may skip a new-line char!
2474 while (isspace(*p))
2475 p++;
2476 // everything to the end of the line is the memory dump
2477 const char* end = strchr(p, '\n');
2478 if (end != 0) {
2479 md.dump = QString::fromLatin1(p, end-p);
2480 p = end+1;
2481 } else {
2482 md.dump = QString::fromLatin1(p, strlen(p));
2483 p += strlen(p);
2485 memdump.push_back(md);
2488 return QString();
2491 QString GdbDriver::editableValue(VarTree* value)
2493 const char* s = value->value().latin1();
2495 // if the variable is a pointer value that contains a cast,
2496 // remove the cast
2497 if (*s == '(') {
2498 skipNested(s, '(', ')');
2499 // skip space
2500 while (isspace(*s))
2501 ++s;
2504 repeat:
2505 const char* start = s;
2507 if (strncmp(s, "0x", 2) == 0)
2509 s += 2;
2510 while (isxdigit(*s))
2511 ++s;
2514 * What we saw so far might have been a reference. If so, edit the
2515 * referenced value. Otherwise, edit the pointer.
2517 if (*s == ':') {
2518 // a reference
2519 ++s;
2520 goto repeat;
2522 // a pointer
2523 // if it's a pointer to a string, remove the string
2524 const char* end = s;
2525 while (isspace(*s))
2526 ++s;
2527 if (*s == '"') {
2528 // a string
2529 return QString::fromLatin1(start, end-start);
2530 } else {
2531 // other pointer
2532 return QString::fromLatin1(start, strlen(start));
2536 // else leave it unchanged (or stripped of the reference preamble)
2537 return s;
2540 QString GdbDriver::parseSetVariable(const char* output)
2542 // if there is any output, it is an error message
2543 QString msg = output;
2544 return msg.stripWhiteSpace();
2548 #include "gdbdriver.moc"