Fix a crash when a value is edited in a floating variable display.
[kdbg.git] / kdbg / gdbdriver.cpp
blob8b0463a077b6efee044c2c12e17172a2c21d0370
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "gdbdriver.h"
7 #include "exprwnd.h"
8 #include <qregexp.h>
9 #include <qstringlist.h>
10 #include <klocale.h> /* i18n */
11 #include <ctype.h>
12 #include <stdlib.h> /* strtol, atoi */
13 #include <string.h> /* strcpy */
15 #include "assert.h"
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #include "mydebug.h"
21 static void skipString(const char*& p);
22 static void skipNested(const char*& s, char opening, char closing);
23 static VarTree* parseVar(const char*& s);
24 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind);
25 static bool parseValue(const char*& s, VarTree* variable);
26 static bool parseNested(const char*& s, VarTree* variable);
27 static bool parseVarSeq(const char*& s, VarTree* variable);
28 static bool parseValueSeq(const char*& s, VarTree* variable);
30 #define PROMPT "(kdbg)"
31 #define PROMPT_LEN 6
32 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
35 // TODO: make this cmd info stuff non-static to allow multiple
36 // simultaneous gdbs to run!
38 struct GdbCmdInfo {
39 DbgCommand cmd;
40 const char* fmt; /* format string */
41 enum Args {
42 argNone, argString, argNum,
43 argStringNum, argNumString,
44 argString2, argNum2
45 } argsNeeded;
48 #if 0
49 // This is how the QString data print statement generally looks like.
50 // It is set by KDebugger via setPrintQStringDataCmd().
52 static const char printQStringStructFmt[] =
53 // if the string data is junk, fail early
54 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
55 // print an array of shorts
56 "(*(unsigned short*)$qstrunicode)@"
57 // limit the length
58 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
59 // if unicode data is 0, report a special value
60 ":1==0\n";
61 #endif
62 static const char printQStringStructFmt[] = "print (0?\"%s\":$kdbgundef)\n";
65 * The following array of commands must be sorted by the DC* values,
66 * because they are used as indices.
68 static GdbCmdInfo cmds[] = {
69 { DCinitialize, "", GdbCmdInfo::argNone },
70 { DCtty, "tty %s\n", GdbCmdInfo::argString },
71 { DCexecutable, "file \"%s\"\n", GdbCmdInfo::argString },
72 { DCtargetremote, "target remote %s\n", GdbCmdInfo::argString },
73 { DCcorefile, "core-file %s\n", GdbCmdInfo::argString },
74 { DCattach, "attach %s\n", GdbCmdInfo::argString },
75 { DCinfolinemain, "kdbg_infolinemain\n", GdbCmdInfo::argNone },
76 { DCinfolocals, "kdbg__alllocals\n", GdbCmdInfo::argNone },
77 { DCinforegisters, "info all-registers\n", GdbCmdInfo::argNone},
78 { DCexamine, "x %s %s\n", GdbCmdInfo::argString2 },
79 { DCinfoline, "info line %s:%d\n", GdbCmdInfo::argStringNum },
80 { DCdisassemble, "disassemble %s %s\n", GdbCmdInfo::argString2 },
81 { DCsetargs, "set args %s\n", GdbCmdInfo::argString },
82 { DCsetenv, "set env %s %s\n", GdbCmdInfo::argString2 },
83 { DCunsetenv, "unset env %s\n", GdbCmdInfo::argString },
84 { DCsetoption, "setoption %s %d\n", GdbCmdInfo::argStringNum},
85 { DCcd, "cd %s\n", GdbCmdInfo::argString },
86 { DCbt, "bt\n", GdbCmdInfo::argNone },
87 { DCrun, "run\n", GdbCmdInfo::argNone },
88 { DCcont, "cont\n", GdbCmdInfo::argNone },
89 { DCstep, "step\n", GdbCmdInfo::argNone },
90 { DCstepi, "stepi\n", GdbCmdInfo::argNone },
91 { DCnext, "next\n", GdbCmdInfo::argNone },
92 { DCnexti, "nexti\n", GdbCmdInfo::argNone },
93 { DCfinish, "finish\n", GdbCmdInfo::argNone },
94 { DCuntil, "until %s:%d\n", GdbCmdInfo::argStringNum },
95 { DCkill, "kill\n", GdbCmdInfo::argNone },
96 { DCbreaktext, "break %s\n", GdbCmdInfo::argString },
97 { DCbreakline, "break %s:%d\n", GdbCmdInfo::argStringNum },
98 { DCtbreakline, "tbreak %s:%d\n", GdbCmdInfo::argStringNum },
99 { DCbreakaddr, "break *%s\n", GdbCmdInfo::argString },
100 { DCtbreakaddr, "tbreak *%s\n", GdbCmdInfo::argString },
101 { DCwatchpoint, "watch %s\n", GdbCmdInfo::argString },
102 { DCdelete, "delete %d\n", GdbCmdInfo::argNum },
103 { DCenable, "enable %d\n", GdbCmdInfo::argNum },
104 { DCdisable, "disable %d\n", GdbCmdInfo::argNum },
105 { DCprint, "print %s\n", GdbCmdInfo::argString },
106 { DCprintDeref, "print *(%s)\n", GdbCmdInfo::argString },
107 { DCprintStruct, "print %s\n", GdbCmdInfo::argString },
108 { DCprintQStringStruct, printQStringStructFmt, GdbCmdInfo::argString},
109 { DCframe, "frame %d\n", GdbCmdInfo::argNum },
110 { DCfindType, "whatis %s\n", GdbCmdInfo::argString },
111 { DCinfosharedlib, "info sharedlibrary\n", GdbCmdInfo::argNone },
112 { DCthread, "thread %d\n", GdbCmdInfo::argNum },
113 { DCinfothreads, "info threads\n", GdbCmdInfo::argNone },
114 { DCinfobreak, "info breakpoints\n", GdbCmdInfo::argNone },
115 { DCcondition, "condition %d %s\n", GdbCmdInfo::argNumString},
116 { DCsetpc, "set variable $pc=%s\n", GdbCmdInfo::argString },
117 { DCignore, "ignore %d %d\n", GdbCmdInfo::argNum2},
118 { DCsetvariable, "set variable %s=%s\n", GdbCmdInfo::argString2 },
121 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
122 #define MAX_FMTLEN 200
124 GdbDriver::GdbDriver() :
125 DebuggerDriver(),
126 m_gdbMajor(4), m_gdbMinor(16)
128 strcpy(m_prompt, PROMPT);
129 m_promptMinLen = PROMPT_LEN;
130 m_promptLastChar = PROMPT_LAST_CHAR;
132 #ifndef NDEBUG
133 // check command info array
134 char* perc;
135 for (int i = 0; i < NUM_CMDS; i++) {
136 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
137 assert(i == cmds[i].cmd);
138 // a format string must be associated
139 assert(cmds[i].fmt != 0);
140 assert(strlen(cmds[i].fmt) <= MAX_FMTLEN);
141 // format string must match arg specification
142 switch (cmds[i].argsNeeded) {
143 case GdbCmdInfo::argNone:
144 assert(strchr(cmds[i].fmt, '%') == 0);
145 break;
146 case GdbCmdInfo::argString:
147 perc = strchr(cmds[i].fmt, '%');
148 assert(perc != 0 && perc[1] == 's');
149 assert(strchr(perc+2, '%') == 0);
150 break;
151 case GdbCmdInfo::argNum:
152 perc = strchr(cmds[i].fmt, '%');
153 assert(perc != 0 && perc[1] == 'd');
154 assert(strchr(perc+2, '%') == 0);
155 break;
156 case GdbCmdInfo::argStringNum:
157 perc = strchr(cmds[i].fmt, '%');
158 assert(perc != 0 && perc[1] == 's');
159 perc = strchr(perc+2, '%');
160 assert(perc != 0 && perc[1] == 'd');
161 assert(strchr(perc+2, '%') == 0);
162 break;
163 case GdbCmdInfo::argNumString:
164 perc = strchr(cmds[i].fmt, '%');
165 assert(perc != 0 && perc[1] == 'd');
166 perc = strchr(perc+2, '%');
167 assert(perc != 0 && perc[1] == 's');
168 assert(strchr(perc+2, '%') == 0);
169 break;
170 case GdbCmdInfo::argString2:
171 perc = strchr(cmds[i].fmt, '%');
172 assert(perc != 0 && perc[1] == 's');
173 perc = strchr(perc+2, '%');
174 assert(perc != 0 && perc[1] == 's');
175 assert(strchr(perc+2, '%') == 0);
176 break;
177 case GdbCmdInfo::argNum2:
178 perc = strchr(cmds[i].fmt, '%');
179 assert(perc != 0 && perc[1] == 'd');
180 perc = strchr(perc+2, '%');
181 assert(perc != 0 && perc[1] == 'd');
182 assert(strchr(perc+2, '%') == 0);
183 break;
186 assert(strlen(printQStringStructFmt) <= MAX_FMTLEN);
187 #endif
190 GdbDriver::~GdbDriver()
195 QString GdbDriver::driverName() const
197 return "GDB";
200 QString GdbDriver::defaultGdb()
202 return
203 "gdb"
204 " --fullname" /* to get standard file names each time the prog stops */
205 " --nx"; /* do not execute initialization files */
208 QString GdbDriver::defaultInvocation() const
210 if (m_defaultCmd.isEmpty()) {
211 return defaultGdb();
212 } else {
213 return m_defaultCmd;
217 QStringList GdbDriver::boolOptionList() const
219 // no options
220 return QStringList();
223 bool GdbDriver::startup(QString cmdStr)
225 if (!DebuggerDriver::startup(cmdStr))
226 return false;
228 static const char gdbInitialize[] =
230 * Work around buggy gdbs that do command line editing even if they
231 * are not on a tty. The readline library echos every command back
232 * in this case, which is confusing for us.
234 "set editing off\n"
235 "set confirm off\n"
236 "set print static-members off\n"
237 "set print asm-demangle on\n"
239 * Don't assume that program functions invoked from a watch expression
240 * always succeed.
242 "set unwindonsignal on\n"
244 * Write a short macro that prints all locals: local variables and
245 * function arguments.
247 "define kdbg__alllocals\n"
248 "info locals\n" /* local vars supersede args with same name */
249 "info args\n" /* therefore, arguments must come last */
250 "end\n"
252 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
254 "define kdbg_infolinemain\n"
255 "list\n"
256 "info line\n"
257 "end\n"
258 // change prompt string and synchronize with gdb
259 "set prompt " PROMPT "\n"
262 executeCmdString(DCinitialize, gdbInitialize, false);
264 // assume that QString::null is ok
265 cmds[DCprintQStringStruct].fmt = printQStringStructFmt;
267 return true;
270 void GdbDriver::commandFinished(CmdQueueItem* cmd)
272 // command string must be committed
273 if (!cmd->m_committed) {
274 // not commited!
275 TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
276 cmd->m_cmdString)));
277 return;
280 switch (cmd->m_cmd) {
281 case DCinitialize:
282 // get version number from preamble
284 int len;
285 QRegExp GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
286 int offset = GDBVersion.match(m_output, 0, &len);
287 if (offset >= 0) {
288 char* start = m_output + offset + 5; // skip "\nGDB "
289 char* end;
290 m_gdbMajor = strtol(start, &end, 10);
291 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
292 if (start == end) {
293 // nothing was parsed
294 m_gdbMajor = 4;
295 m_gdbMinor = 16;
297 } else {
298 // assume some default version (what would make sense?)
299 m_gdbMajor = 4;
300 m_gdbMinor = 16;
302 // use a feasible core-file command
303 if (m_gdbMajor > 4 || (m_gdbMajor == 4 && m_gdbMinor >= 16)) {
304 cmds[DCcorefile].fmt = "target core %s\n";
305 } else {
306 cmds[DCcorefile].fmt = "core-file %s\n";
309 break;
310 default:;
313 /* ok, the command is ready */
314 emit commandReceived(cmd, m_output);
316 switch (cmd->m_cmd) {
317 case DCcorefile:
318 case DCinfolinemain:
319 case DCframe:
320 case DCattach:
321 case DCrun:
322 case DCcont:
323 case DCstep:
324 case DCstepi:
325 case DCnext:
326 case DCnexti:
327 case DCfinish:
328 case DCuntil:
329 parseMarker();
330 default:;
335 * The --fullname option makes gdb send a special normalized sequence print
336 * each time the program stops and at some other points. The sequence has
337 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
339 void GdbDriver::parseMarker()
341 char* startMarker = strstr(m_output, "\032\032");
342 if (startMarker == 0)
343 return;
345 // extract the marker
346 startMarker += 2;
347 TRACE(QString("found marker: ") + startMarker);
348 char* endMarker = strchr(startMarker, '\n');
349 if (endMarker == 0)
350 return;
352 *endMarker = '\0';
354 // extract filename and line number
355 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
357 int len;
358 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
359 if (lineNoStart >= 0) {
360 int lineNo = atoi(startMarker + lineNoStart+1);
362 // get address
363 const char* addrStart = startMarker + lineNoStart + len - 2;
364 DbgAddr address = QString(addrStart).stripWhiteSpace();
366 // now show the window
367 startMarker[lineNoStart] = '\0'; /* split off file name */
368 emit activateFileLine(startMarker, lineNo-1, address);
374 * Escapes characters that might lead to problems when they appear on gdb's
375 * command line.
377 static void normalizeStringArg(QString& arg)
380 * Remove trailing backslashes. This approach is a little simplistic,
381 * but we know that there is at the moment no case where a trailing
382 * backslash would make sense.
384 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
385 arg = arg.left(arg.length()-1);
390 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
392 assert(cmd >= 0 && cmd < NUM_CMDS);
393 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
395 normalizeStringArg(strArg);
397 if (cmd == DCcd) {
398 // need the working directory when parsing the output
399 m_programWD = strArg;
400 } else if (cmd == DCsetargs && !m_redirect.isEmpty()) {
402 * Use saved redirection. We prepend it in front of the user's
403 * arguments so that the user can override the redirections.
405 strArg = m_redirect + " " + strArg;
408 SIZED_QString(cmdString, MAX_FMTLEN+strArg.length());
409 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
410 return cmdString;
413 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
415 assert(cmd >= 0 && cmd < NUM_CMDS);
416 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
418 SIZED_QString(cmdString, MAX_FMTLEN+30);
420 cmdString.sprintf(cmds[cmd].fmt, intArg);
421 return cmdString;
424 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
426 assert(cmd >= 0 && cmd < NUM_CMDS);
427 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
428 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
429 cmd == DCexamine ||
430 cmd == DCtty);
432 normalizeStringArg(strArg);
434 SIZED_QString(cmdString, MAX_FMTLEN+30+strArg.length());
436 if (cmd == DCtty)
439 * intArg specifies which channels should be redirected to
440 * /dev/null. It is a value or'ed together from RDNstdin,
441 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
442 * command.
444 * Note: We rely on that after the DCtty a DCsetargs will follow,
445 * which will ultimately apply the redirection.
447 static const char* const runRedir[8] = {
449 "</dev/null",
450 ">/dev/null",
451 "</dev/null >/dev/null",
452 "2>/dev/null",
453 "</dev/null 2>/dev/null",
454 ">/dev/null 2>&1",
455 "</dev/null >/dev/null 2>&1"
457 if (strArg.isEmpty())
458 intArg = 7; /* failsafe if no tty */
459 m_redirect = runRedir[intArg & 7];
461 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
464 if (cmd == DCexamine) {
465 // make a format specifier from the intArg
466 static const char size[16] = {
467 '\0', 'b', 'h', 'w', 'g'
469 static const char format[16] = {
470 '\0', 'x', 'd', 'u', 'o', 't',
471 'a', 'c', 'f', 's', 'i'
473 assert(MDTsizemask == 0xf); /* lowest 4 bits */
474 assert(MDTformatmask == 0xf0); /* next 4 bits */
475 int count = 16; /* number of entities to print */
476 char sizeSpec = size[intArg & MDTsizemask];
477 char formatSpec = format[(intArg & MDTformatmask) >> 4];
478 assert(sizeSpec != '\0');
479 assert(formatSpec != '\0');
480 // adjust count such that 16 lines are printed
481 switch (intArg & MDTformatmask) {
482 case MDTstring: case MDTinsn:
483 break; /* no modification needed */
484 default:
485 // all cases drop through:
486 switch (intArg & MDTsizemask) {
487 case MDTbyte:
488 case MDThalfword:
489 count *= 2;
490 case MDTword:
491 count *= 2;
492 case MDTgiantword:
493 count *= 2;
495 break;
497 QString spec;
498 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
500 return makeCmdString(DCexamine, spec, strArg);
503 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
505 // line numbers are zero-based
506 if (cmd == DCuntil || cmd == DCbreakline ||
507 cmd == DCtbreakline || cmd == DCinfoline)
509 intArg++;
511 if (cmd == DCinfoline)
513 // must split off file name part
514 int slash = strArg.findRev('/');
515 if (slash >= 0)
516 strArg = strArg.right(strArg.length()-slash-1);
518 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
520 else
522 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
524 return cmdString;
527 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
529 assert(cmd >= 0 && cmd < NUM_CMDS);
530 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
532 normalizeStringArg(strArg1);
533 normalizeStringArg(strArg2);
535 SIZED_QString(cmdString, MAX_FMTLEN+strArg1.length()+strArg2.length());
536 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
537 return cmdString;
540 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
542 assert(cmd >= 0 && cmd < NUM_CMDS);
543 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
545 SIZED_QString(cmdString, MAX_FMTLEN+60);
546 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
547 return cmdString;
550 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
552 assert(cmd >= 0 && cmd < NUM_CMDS);
553 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
555 if (cmd == DCrun) {
556 m_haveCoreFile = false;
559 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
562 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
563 bool clearLow)
565 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
568 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
569 bool clearLow)
572 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
575 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
576 bool clearLow)
578 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
581 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
582 bool clearLow)
584 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
587 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
588 bool clearLow)
590 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
593 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
595 return queueCmdString(cmd, cmds[cmd].fmt, mode);
598 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
599 QueueMode mode)
601 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
604 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
605 QueueMode mode)
607 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
610 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
611 QueueMode mode)
613 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
616 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
617 QueueMode mode)
619 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
622 void GdbDriver::terminate()
624 kill(SIGTERM);
625 m_state = DSidle;
628 void GdbDriver::detachAndTerminate()
630 kill(SIGINT);
631 flushCommands();
632 executeCmdString(DCinitialize, "detach\nquit\n", true);
635 void GdbDriver::interruptInferior()
637 kill(SIGINT);
638 // remove accidentally queued commands
639 flushHiPriQueue();
642 static bool isErrorExpr(const char* output)
644 return
645 strncmp(output, "Cannot access memory at", 23) == 0 ||
646 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
647 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
648 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
649 strncmp(output, "There is no member or method named", 34) == 0 ||
650 strncmp(output, "A parse error in expression", 27) == 0 ||
651 strncmp(output, "No symbol \"", 11) == 0 ||
652 strncmp(output, "Internal error: ", 16) == 0;
656 * Returns true if the output is an error message. If wantErrorValue is
657 * true, a new VarTree object is created and filled with the error message.
658 * If there are warnings, they are skipped and output points past the warnings
659 * on return (even if there \e are errors).
661 static bool parseErrorMessage(const char*& output,
662 VarTree*& variable, bool wantErrorValue)
664 // skip warnings
665 while (strncmp(output, "warning:", 8) == 0)
667 char* end = strchr(output+8, '\n');
668 if (end == 0)
669 output += strlen(output);
670 else
671 output = end+1;
674 if (isErrorExpr(output))
676 if (wantErrorValue) {
677 // put the error message as value in the variable
678 variable = new VarTree(QString(), VarTree::NKplain);
679 const char* endMsg = strchr(output, '\n');
680 if (endMsg == 0)
681 endMsg = output + strlen(output);
682 variable->m_value = FROM_LATIN1(output, endMsg-output);
683 } else {
684 variable = 0;
686 return true;
688 return false;
691 #if QT_VERSION >= 300
692 union Qt2QChar {
693 short s;
694 struct {
695 uchar row;
696 uchar cell;
697 } qch;
699 #endif
701 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
703 // don't accept the command if it is empty
704 if (cmd == 0 || *cmd == '\0')
705 return;
706 assert(strlen(cmd) <= MAX_FMTLEN);
707 cmds[DCprintQStringStruct].fmt = cmd;
710 VarTree* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
712 VarTree* variable = 0;
715 * Parse off white space. gdb sometimes prints white space first if the
716 * printed array leaded to an error.
718 while (isspace(*output))
719 output++;
721 // special case: empty string (0 repetitions)
722 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
724 variable = new VarTree(QString(), VarTree::NKplain);
725 variable->m_value = "\"\"";
726 return variable;
729 // check for error conditions
730 if (parseErrorMessage(output, variable, wantErrorValue))
731 return variable;
733 // parse the array
735 // find '='
736 const char* p = output;
737 p = strchr(p, '=');
738 if (p == 0) {
739 goto error;
741 // skip white space
742 do {
743 p++;
744 } while (isspace(*p));
746 if (*p == '{')
748 // this is the real data
749 p++; /* skip '{' */
751 // parse the array
752 QString result;
753 QString repeatCount;
754 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
756 * A matrix for separators between the individual "things"
757 * that are added to the string. The first index is a bool,
758 * the second index is from the enum above.
760 static const char* separator[2][3] = {
761 { "\"", 0, ", \"" }, /* normal char is added */
762 { "'", "\", '", ", '" } /* repeated char is added */
765 while (isdigit(*p)) {
766 // parse a number
767 char* end;
768 unsigned short value = (unsigned short) strtoul(p, &end, 0);
769 if (end == p)
770 goto error; /* huh? no valid digits */
771 // skip separator and search for a repeat count
772 p = end;
773 while (isspace(*p) || *p == ',')
774 p++;
775 bool repeats = strncmp(p, "<repeats ", 9) == 0;
776 if (repeats) {
777 const char* start = p;
778 p = strchr(p+9, '>'); /* search end and advance */
779 if (p == 0)
780 goto error;
781 p++; /* skip '>' */
782 repeatCount = FROM_LATIN1(start, p-start);
783 while (isspace(*p) || *p == ',')
784 p++;
786 // p is now at the next char (or the end)
788 // interpret the value as a QChar
789 // TODO: make cross-architecture compatible
790 QChar ch;
791 if (qt3like) {
792 ch = QChar(value);
793 } else {
794 #if QT_VERSION < 300
795 (unsigned short&)ch = value;
796 #else
797 Qt2QChar c;
798 c.s = value;
799 ch.setRow(c.qch.row);
800 ch.setCell(c.qch.cell);
801 #endif
804 // escape a few frequently used characters
805 char escapeCode = '\0';
806 switch (ch.latin1()) {
807 case '\n': escapeCode = 'n'; break;
808 case '\r': escapeCode = 'r'; break;
809 case '\t': escapeCode = 't'; break;
810 case '\b': escapeCode = 'b'; break;
811 case '\"': escapeCode = '\"'; break;
812 case '\\': escapeCode = '\\'; break;
813 case '\0': if (value == 0) { escapeCode = '0'; } break;
816 // add separator
817 result += separator[repeats][lastThing];
818 // add char
819 if (escapeCode != '\0') {
820 result += '\\';
821 ch = escapeCode;
823 result += ch;
825 // fixup repeat count and lastThing
826 if (repeats) {
827 result += "' ";
828 result += repeatCount;
829 lastThing = wasRepeat;
830 } else {
831 lastThing = wasChar;
834 if (*p != '}')
835 goto error;
837 // closing quote
838 if (lastThing == wasChar)
839 result += "\"";
841 // assign the value
842 variable = new VarTree(QString(), VarTree::NKplain);
843 variable->m_value = result;
845 else if (strncmp(p, "true", 4) == 0)
847 variable = new VarTree(QString(), VarTree::NKplain);
848 variable->m_value = "QString::null";
850 else if (strncmp(p, "false", 5) == 0)
852 variable = new VarTree(QString(), VarTree::NKplain);
853 variable->m_value = "(null)";
855 else
856 goto error;
857 return variable;
859 error:
860 if (wantErrorValue) {
861 variable = new VarTree(QString(), VarTree::NKplain);
862 variable->m_value = "internal parse error";
864 return variable;
867 static VarTree* parseVar(const char*& s)
869 const char* p = s;
871 // skip whitespace
872 while (isspace(*p))
873 p++;
875 QString name;
876 VarTree::NameKind kind;
878 * Detect anonymouse struct values: The 'name =' part is missing:
879 * s = { a = 1, { b = 2 }}
880 * Note that this detection works only inside structs when the anonymous
881 * struct is not the first member:
882 * s = {{ a = 1 }, b = 2}
883 * This is misparsed (by parseNested()) because it is mistakenly
884 * interprets the second opening brace as the first element of an array
885 * of structs.
887 if (*p == '{')
889 name = i18n("<anonymous struct or union>");
890 kind = VarTree::NKanonymous;
892 else
894 if (!parseName(p, name, kind)) {
895 return 0;
898 // go for '='
899 while (isspace(*p))
900 p++;
901 if (*p != '=') {
902 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
903 return 0;
905 // skip the '=' and more whitespace
906 p++;
907 while (isspace(*p))
908 p++;
911 VarTree* variable = new VarTree(name, kind);
912 variable->setDeleteChildren(true);
914 if (!parseValue(p, variable)) {
915 delete variable;
916 return 0;
918 s = p;
919 return variable;
922 static void skipNested(const char*& s, char opening, char closing)
924 const char* p = s;
926 // parse a nested type
927 int nest = 1;
928 p++;
930 * Search for next matching `closing' char, skipping nested pairs of
931 * `opening' and `closing'.
933 while (*p && nest > 0) {
934 if (*p == opening) {
935 nest++;
936 } else if (*p == closing) {
937 nest--;
939 p++;
941 if (nest != 0) {
942 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
944 s = p;
948 * This function skips text that is delimited by nested angle bracktes, '<>'.
949 * A complication arises because the delimited text can contain the names of
950 * operator<<, operator>>, operator<, and operator>, which have to be treated
951 * specially so that they do not count towards the nesting of '<>'.
952 * This function assumes that the delimited text does not contain strings.
954 static void skipNestedAngles(const char*& s)
956 const char* p = s;
958 int nest = 1;
959 p++; // skip the initial '<'
960 while (*p && nest > 0)
962 // Below we can check for p-s >= 9 instead of 8 because
963 // *s is '<' and cannot be part of "operator".
964 if (*p == '<')
966 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
967 if (p[1] == '<')
968 p++;
969 } else {
970 nest++;
973 else if (*p == '>')
975 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
976 if (p[1] == '>')
977 p++;
978 } else {
979 nest--;
982 p++;
984 if (nest != 0) {
985 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
987 s = p;
991 * Find the end of line that is not inside braces
993 static void findEnd(const char*& s)
995 const char* p = s;
996 while (*p && *p!='\n') {
997 while (*p && *p!='\n' && *p!='{')
998 p++;
999 if (*p=='{') {
1000 p++;
1001 skipNested(p, '{', '}'); p--;
1004 s = p;
1007 static bool isNumberish(const char ch)
1009 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
1012 void skipString(const char*& p)
1014 moreStrings:
1015 // opening quote
1016 char quote = *p++;
1017 while (*p != quote) {
1018 if (*p == '\\') {
1019 // skip escaped character
1020 // no special treatment for octal values necessary
1021 p++;
1023 // simply return if no more characters
1024 if (*p == '\0')
1025 return;
1026 p++;
1028 // closing quote
1029 p++;
1031 * Strings can consist of several parts, some of which contain repeated
1032 * characters.
1034 if (quote == '\'') {
1035 // look ahaead for <repeats 123 times>
1036 const char* q = p+1;
1037 while (isspace(*q))
1038 q++;
1039 if (strncmp(q, "<repeats ", 9) == 0) {
1040 p = q+9;
1041 while (*p != '\0' && *p != '>')
1042 p++;
1043 if (*p != '\0') {
1044 p++; /* skip the '>' */
1048 // is the string continued?
1049 if (*p == ',') {
1050 // look ahead for another quote
1051 const char* q = p+1;
1052 while (isspace(*q))
1053 q++;
1054 if (*q == '"' || *q == '\'') {
1055 // yes!
1056 p = q;
1057 goto moreStrings;
1060 /* very long strings are followed by `...' */
1061 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1062 p += 3;
1066 static void skipNestedWithString(const char*& s, char opening, char closing)
1068 const char* p = s;
1070 // parse a nested expression
1071 int nest = 1;
1072 p++;
1074 * Search for next matching `closing' char, skipping nested pairs of
1075 * `opening' and `closing' as well as strings.
1077 while (*p && nest > 0) {
1078 if (*p == opening) {
1079 nest++;
1080 } else if (*p == closing) {
1081 nest--;
1082 } else if (*p == '\'' || *p == '\"') {
1083 skipString(p);
1084 continue;
1086 p++;
1088 if (nest > 0) {
1089 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1091 s = p;
1094 inline void skipName(const char*& p)
1096 // allow : (for enumeration values) and $ and . (for _vtbl.)
1097 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1098 p++;
1101 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1103 kind = VarTree::NKplain;
1105 const char* p = s;
1106 // examples of names:
1107 // name
1108 // <Object>
1109 // <string<a,b<c>,7> >
1111 if (*p == '<') {
1112 skipNestedAngles(p);
1113 name = FROM_LATIN1(s, p - s);
1114 kind = VarTree::NKtype;
1116 else
1118 // name, which might be "static"; allow dot for "_vtbl."
1119 skipName(p);
1120 if (p == s) {
1121 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1122 return false;
1124 int len = p - s;
1125 if (len == 6 && strncmp(s, "static", 6) == 0) {
1126 kind = VarTree::NKstatic;
1128 // its a static variable, name comes now
1129 while (isspace(*p))
1130 p++;
1131 s = p;
1132 skipName(p);
1133 if (p == s) {
1134 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1135 return false;
1137 len = p - s;
1139 name = FROM_LATIN1(s, len);
1141 // return the new position
1142 s = p;
1143 return true;
1146 static bool parseValue(const char*& s, VarTree* variable)
1148 variable->m_value = "";
1150 repeat:
1151 if (*s == '{') {
1152 // Sometimes we find the following output:
1153 // {<text variable, no debug info>} 0x40012000 <access>
1154 // {<data variable, no debug info>}
1155 // {<variable (not text or data), no debug info>}
1156 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1157 strncmp(s, "{<data variable, ", 17) == 0 ||
1158 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1160 const char* start = s;
1161 skipNested(s, '{', '}');
1162 variable->m_value = FROM_LATIN1(start, s-start);
1163 variable->m_value += ' '; // add only a single space
1164 while (isspace(*s))
1165 s++;
1166 goto repeat;
1168 else
1170 s++;
1171 if (!parseNested(s, variable)) {
1172 return false;
1174 // must be the closing brace
1175 if (*s != '}') {
1176 TRACE("parse error: missing } of " + variable->getText());
1177 return false;
1179 s++;
1180 // final white space
1181 while (isspace(*s))
1182 s++;
1184 } else {
1185 // examples of leaf values (cannot be the empty string):
1186 // 123
1187 // -123
1188 // 23.575e+37
1189 // 0x32a45
1190 // @0x012ab4
1191 // (DwContentType&) @0x8123456: {...}
1192 // 0x32a45 "text"
1193 // 10 '\n'
1194 // <optimized out>
1195 // 0x823abc <Array<int> virtual table>
1196 // (void (*)()) 0x8048480 <f(E *, char)>
1197 // (E *) 0xbffff450
1198 // red
1199 // &parseP (HTMLClueV *, char *)
1200 // Variable "x" is not available.
1201 // The value of variable 'x' is distributed...
1202 // -nan(0xfffff081defa0)
1204 const char*p = s;
1206 // check for type
1207 QString type;
1208 if (*p == '(') {
1209 skipNested(p, '(', ')');
1211 while (isspace(*p))
1212 p++;
1213 variable->m_value = FROM_LATIN1(s, p - s);
1216 bool reference = false;
1217 if (*p == '@') {
1218 // skip reference marker
1219 p++;
1220 reference = true;
1222 const char* start = p;
1223 if (*p == '-')
1224 p++;
1226 // some values consist of more than one token
1227 bool checkMultiPart = false;
1229 if (p[0] == '0' && p[1] == 'x') {
1230 // parse hex number
1231 p += 2;
1232 while (isxdigit(*p))
1233 p++;
1236 * Assume this is a pointer, but only if it's not a reference, since
1237 * references can't be expanded.
1239 if (!reference) {
1240 variable->m_varKind = VarTree::VKpointer;
1241 } else {
1243 * References are followed by a colon, in which case we'll
1244 * find the value following the reference address.
1246 if (*p == ':') {
1247 p++;
1248 } else {
1249 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1250 reference = false;
1253 checkMultiPart = true;
1254 } else if (isdigit(*p)) {
1255 // parse decimal number, possibly a float
1256 while (isdigit(*p))
1257 p++;
1258 if (*p == '.') { /* TODO: obey i18n? */
1259 // In long arrays an integer may be followed by '...'.
1260 // We test for this situation and don't gobble the '...'.
1261 if (p[1] != '.' || p[0] != '.') {
1262 // fractional part
1263 p++;
1264 while (isdigit(*p))
1265 p++;
1268 if (*p == 'e' || *p == 'E') {
1269 p++;
1270 // exponent
1271 if (*p == '-' || *p == '+')
1272 p++;
1273 while (isdigit(*p))
1274 p++;
1277 // for char variables there is the char, eg. 10 '\n'
1278 checkMultiPart = true;
1279 } else if (*p == '<') {
1280 // e.g. <optimized out>
1281 skipNestedAngles(p);
1282 } else if (*p == '"' || *p == '\'') {
1283 // character may have multipart: '\000' <repeats 11 times>
1284 checkMultiPart = *p == '\'';
1285 // found a string
1286 skipString(p);
1287 } else if (*p == '&') {
1288 // function pointer
1289 p++;
1290 skipName(p);
1291 while (isspace(*p)) {
1292 p++;
1294 if (*p == '(') {
1295 skipNested(p, '(', ')');
1297 } else if (strncmp(p, "Variable \"", 10) == 0) {
1298 // Variable "x" is not available.
1299 p += 10; // skip to "
1300 skipName(p);
1301 if (strncmp(p, "\" is not available.", 19) == 0) {
1302 p += 19;
1304 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1305 p += 23;
1306 skipName(p);
1307 const char* e = strchr(p, '.');
1308 if (e == 0) {
1309 p += strlen(p);
1310 } else {
1311 p = e+1;
1313 } else {
1314 // must be an enumeration value
1315 skipName(p);
1316 // hmm, not necessarily: nan (floating point Not a Number)
1317 // is followed by a number in ()
1318 if (*p == '(')
1319 skipNested(p, '(', ')');
1321 variable->m_value += FROM_LATIN1(start, p - start);
1323 // remove line breaks from the value; this is ok since
1324 // string values never contain a literal line break
1325 variable->m_value.replace('\n', ' ');
1327 if (checkMultiPart) {
1328 // white space
1329 while (isspace(*p))
1330 p++;
1331 // may be followed by a string or <...>
1332 start = p;
1334 if (*p == '"' || *p == '\'') {
1335 skipString(p);
1336 } else if (*p == '<') {
1337 // if this value is part of an array, it might be followed
1338 // by <repeats 15 times>, which we don't skip here
1339 if (strncmp(p, "<repeats ", 9) != 0)
1340 skipNestedAngles(p);
1342 if (p != start) {
1343 // there is always a blank before the string,
1344 // which we will include in the final string value
1345 variable->m_value += FROM_LATIN1(start-1, (p - start)+1);
1346 // if this was a pointer, reset that flag since we
1347 // now got the value
1348 variable->m_varKind = VarTree::VKsimple;
1352 if (variable->m_value.length() == 0) {
1353 TRACE("parse error: no value for " + variable->getText());
1354 return false;
1357 // final white space
1358 while (isspace(*p))
1359 p++;
1360 s = p;
1363 * If this was a reference, the value follows. It might even be a
1364 * composite variable!
1366 if (reference) {
1367 goto repeat;
1370 if (variable->m_varKind == VarTree::VKpointer) {
1371 variable->setDelayedExpanding(true);
1375 return true;
1378 static bool parseNested(const char*& s, VarTree* variable)
1380 // could be a structure or an array
1381 while (isspace(*s))
1382 s++;
1384 const char* p = s;
1385 bool isStruct = false;
1387 * If there is a name followed by an = or an < -- which starts a type
1388 * name -- or "static", it is a structure
1390 if (*p == '<' || *p == '}') {
1391 isStruct = true;
1392 } else if (strncmp(p, "static ", 7) == 0) {
1393 isStruct = true;
1394 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1395 // look ahead for a comma after the name
1396 skipName(p);
1397 while (isspace(*p))
1398 p++;
1399 if (*p == '=') {
1400 isStruct = true;
1402 p = s; /* rescan the name */
1404 if (isStruct) {
1405 if (!parseVarSeq(p, variable)) {
1406 return false;
1408 variable->m_varKind = VarTree::VKstruct;
1409 } else {
1410 if (!parseValueSeq(p, variable)) {
1411 return false;
1413 variable->m_varKind = VarTree::VKarray;
1415 s = p;
1416 return true;
1419 static bool parseVarSeq(const char*& s, VarTree* variable)
1421 // parse a comma-separated sequence of variables
1422 VarTree* var = variable; /* var != 0 to indicate success if empty seq */
1423 for (;;) {
1424 if (*s == '}')
1425 break;
1426 if (strncmp(s, "<No data fields>}", 17) == 0)
1428 // no member variables, so break out immediately
1429 s += 16; /* go to the closing brace */
1430 break;
1432 var = parseVar(s);
1433 if (var == 0)
1434 break; /* syntax error */
1435 variable->appendChild(var);
1436 if (*s != ',')
1437 break;
1438 // skip the comma and whitespace
1439 s++;
1440 while (isspace(*s))
1441 s++;
1443 return var != 0;
1446 static bool parseValueSeq(const char*& s, VarTree* variable)
1448 // parse a comma-separated sequence of variables
1449 int index = 0;
1450 bool good;
1451 for (;;) {
1452 QString name;
1453 name.sprintf("[%d]", index);
1454 VarTree* var = new VarTree(name, VarTree::NKplain);
1455 var->setDeleteChildren(true);
1456 good = parseValue(s, var);
1457 if (!good) {
1458 delete var;
1459 return false;
1461 // a value may be followed by "<repeats 45 times>"
1462 if (strncmp(s, "<repeats ", 9) == 0) {
1463 s += 9;
1464 char* end;
1465 int l = strtol(s, &end, 10);
1466 if (end == s || strncmp(end, " times>", 7) != 0) {
1467 // should not happen
1468 delete var;
1469 return false;
1471 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1472 // replace name and advance index
1473 name.sprintf("[%d .. %d]", index, index+l-1);
1474 var->setText(name);
1475 index += l;
1476 // skip " times>" and space
1477 s = end+7;
1478 // possible final space
1479 while (isspace(*s))
1480 s++;
1481 } else {
1482 index++;
1484 variable->appendChild(var);
1485 // long arrays may be terminated by '...'
1486 if (strncmp(s, "...", 3) == 0) {
1487 s += 3;
1488 VarTree* var = new VarTree("...", VarTree::NKplain);
1489 var->setDeleteChildren(true);
1490 var->m_value = i18n("<additional entries of the array suppressed>");
1491 variable->appendChild(var);
1492 break;
1494 if (*s != ',') {
1495 break;
1497 // skip the comma and whitespace
1498 s++;
1499 while (isspace(*s))
1500 s++;
1501 // sometimes there is a closing brace after a comma
1502 // if (*s == '}')
1503 // break;
1505 return true;
1509 * Parses a stack frame.
1511 static void parseFrameInfo(const char*& s, QString& func,
1512 QString& file, int& lineNo, DbgAddr& address)
1514 const char* p = s;
1516 // next may be a hexadecimal address
1517 if (*p == '0') {
1518 const char* start = p;
1519 p++;
1520 if (*p == 'x')
1521 p++;
1522 while (isxdigit(*p))
1523 p++;
1524 address = FROM_LATIN1(start, p-start);
1525 if (strncmp(p, " in ", 4) == 0)
1526 p += 4;
1527 } else {
1528 address = DbgAddr();
1530 const char* start = p;
1531 // check for special signal handler frame
1532 if (strncmp(p, "<signal handler called>", 23) == 0) {
1533 func = FROM_LATIN1(start, 23);
1534 file = QString();
1535 lineNo = -1;
1536 s = p+23;
1537 if (*s == '\n')
1538 s++;
1539 return;
1543 * Skip the function name. It is terminated by a left parenthesis
1544 * which does not delimit "(anonymous namespace)" and which is
1545 * outside the angle brackets <> of template parameter lists
1546 * and is preceded by a space.
1548 while (*p != '\0')
1550 if (*p == '<') {
1551 // check for operator<< and operator<
1552 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1554 p++;
1555 if (*p == '<')
1556 p++;
1558 else
1560 // skip template parameter list
1561 skipNestedAngles(p);
1563 } else if (*p == '(') {
1564 // this skips "(anonymous namespace)" as well as the formal
1565 // parameter list of the containing function if this is a member
1566 // of a nested class
1567 skipNestedWithString(p, '(', ')');
1568 } else if (*p == ' ') {
1569 ++p;
1570 if (*p == '(')
1571 break; // parameter list found
1572 } else {
1573 p++;
1577 if (*p == '\0') {
1578 func = start;
1579 file = QString();
1580 lineNo = -1;
1581 s = p;
1582 return;
1585 * Skip parameters. But notice that for complicated conversion
1586 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1587 * pointer to function) as well as operator()(...) we have to skip
1588 * additional pairs of parentheses. Furthermore, recent gdbs write the
1589 * demangled name followed by the arguments in a pair of parentheses,
1590 * where the demangled name can end in "const".
1592 do {
1593 skipNestedWithString(p, '(', ')');
1594 while (isspace(*p))
1595 p++;
1596 // skip "const"
1597 if (strncmp(p, "const", 5) == 0) {
1598 p += 5;
1599 while (isspace(*p))
1600 p++;
1602 } while (*p == '(');
1604 // check for file position
1605 if (strncmp(p, "at ", 3) == 0) {
1606 p += 3;
1607 const char* fileStart = p;
1608 // go for the end of the line
1609 while (*p != '\0' && *p != '\n')
1610 p++;
1611 // search back for colon
1612 const char* colon = p;
1613 do {
1614 --colon;
1615 } while (*colon != ':');
1616 file = FROM_LATIN1(fileStart, colon-fileStart);
1617 lineNo = atoi(colon+1)-1;
1618 // skip new-line
1619 if (*p != '\0')
1620 p++;
1621 } else {
1622 // check for "from shared lib"
1623 if (strncmp(p, "from ", 5) == 0) {
1624 p += 5;
1625 // go for the end of the line
1626 while (*p != '\0' && *p != '\n')
1627 p++;
1628 // skip new-line
1629 if (*p != '\0')
1630 p++;
1632 file = "";
1633 lineNo = -1;
1635 // construct the function name (including file info)
1636 if (*p == '\0') {
1637 func = start;
1638 } else {
1639 func = FROM_LATIN1(start, p-start-1); /* don't include \n */
1641 s = p;
1644 * Replace \n (and whitespace around it) in func by a blank. We cannot
1645 * use QString::simplifyWhiteSpace() for this because this would also
1646 * simplify space that belongs to a string arguments that gdb sometimes
1647 * prints in the argument lists of the function.
1649 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1650 int nl = 0;
1651 while ((nl = func.find('\n', nl)) >= 0) {
1652 // search back to the beginning of the whitespace
1653 int startWhite = nl;
1654 do {
1655 --startWhite;
1656 } while (isspace(func[startWhite].latin1()));
1657 startWhite++;
1658 // search forward to the end of the whitespace
1659 do {
1660 nl++;
1661 } while (isspace(func[nl].latin1()));
1662 // replace
1663 func.replace(startWhite, nl-startWhite, " ");
1664 /* continue searching for more \n's at this place: */
1665 nl = startWhite+1;
1671 * Parses a stack frame including its frame number
1673 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1674 QString& file, int& lineNo, DbgAddr& address)
1676 // Example:
1677 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1678 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1680 // must start with a hash mark followed by number
1681 // or with "Breakpoint " followed by number and comma
1682 if (s[0] == '#') {
1683 if (!isdigit(s[1]))
1684 return false;
1685 s++; /* skip the hash mark */
1686 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1687 if (!isdigit(s[11]))
1688 return false;
1689 s += 11; /* skip "Breakpoint" */
1690 } else
1691 return false;
1693 // frame number
1694 frameNo = atoi(s);
1695 while (isdigit(*s))
1696 s++;
1697 // space and comma
1698 while (isspace(*s) || *s == ',')
1699 s++;
1700 parseFrameInfo(s, func, file, lineNo, address);
1701 return true;
1704 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1706 QString func, file;
1707 int lineNo, frameNo;
1708 DbgAddr address;
1710 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1711 StackFrame* frm = new StackFrame;
1712 frm->frameNo = frameNo;
1713 frm->fileName = file;
1714 frm->lineNo = lineNo;
1715 frm->address = address;
1716 frm->var = new VarTree(func, VarTree::NKplain);
1717 stack.append(frm);
1721 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1722 QString& file, int& lineNo, DbgAddr& address)
1724 QString func;
1725 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1729 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1731 // skip first line, which is the headline
1732 const char* p = strchr(output, '\n');
1733 if (p == 0)
1734 return false;
1735 p++;
1736 if (*p == '\0')
1737 return false;
1739 // split up a line
1740 QString location;
1741 QString address;
1742 int hits = 0;
1743 uint ignoreCount = 0;
1744 QString condition;
1745 const char* end;
1746 char* dummy;
1747 while (*p != '\0') {
1748 // get Num
1749 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1750 p = dummy;
1751 // get Type
1752 while (isspace(*p))
1753 p++;
1754 Breakpoint::Type bpType = Breakpoint::breakpoint;
1755 if (strncmp(p, "breakpoint", 10) == 0) {
1756 p += 10;
1757 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1758 bpType = Breakpoint::watchpoint;
1759 p += 13;
1760 } else if (strncmp(p, "watchpoint", 10) == 0) {
1761 bpType = Breakpoint::watchpoint;
1762 p += 10;
1764 while (isspace(*p))
1765 p++;
1766 if (*p == '\0')
1767 break;
1768 // get Disp
1769 char disp = *p++;
1770 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1771 p++;
1772 while (isspace(*p))
1773 p++;
1774 if (*p == '\0')
1775 break;
1776 // get Enb
1777 char enable = *p++;
1778 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1779 p++;
1780 while (isspace(*p))
1781 p++;
1782 if (*p == '\0')
1783 break;
1784 // the address, if present
1785 if (bpType == Breakpoint::breakpoint &&
1786 strncmp(p, "0x", 2) == 0)
1788 const char* start = p;
1789 while (*p != '\0' && !isspace(*p))
1790 p++;
1791 address = FROM_LATIN1(start, p-start);
1792 while (isspace(*p) && *p != '\n')
1793 p++;
1794 if (*p == '\0')
1795 break;
1796 } else {
1797 address = QString();
1799 // remainder is location, hit and ignore count, condition
1800 hits = 0;
1801 ignoreCount = 0;
1802 condition = QString();
1803 end = strchr(p, '\n');
1804 if (end == 0) {
1805 location = p;
1806 p += location.length();
1807 } else {
1808 location = FROM_LATIN1(p, end-p).stripWhiteSpace();
1809 p = end+1; /* skip over \n */
1812 // may be continued in next line
1813 while (isspace(*p)) { /* p points to beginning of line */
1814 // skip white space at beginning of line
1815 while (isspace(*p))
1816 p++;
1818 // seek end of line
1819 end = strchr(p, '\n');
1820 if (end == 0)
1821 end = p+strlen(p);
1823 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1824 // extract the hit count
1825 p += 22;
1826 hits = strtol(p, &dummy, 10);
1827 TRACE(QString().sprintf("hit count %d", hits));
1828 } else if (strncmp(p, "stop only if ", 13) == 0) {
1829 // extract condition
1830 p += 13;
1831 condition = FROM_LATIN1(p, end-p).stripWhiteSpace();
1832 TRACE("condition: "+condition);
1833 } else if (strncmp(p, "ignore next ", 12) == 0) {
1834 // extract ignore count
1835 p += 12;
1836 ignoreCount = strtol(p, &dummy, 10);
1837 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1838 } else {
1839 // indeed a continuation
1840 location += " " + FROM_LATIN1(p, end-p).stripWhiteSpace();
1842 p = end;
1843 if (*p != '\0')
1844 p++; /* skip '\n' */
1846 Breakpoint* bp = new Breakpoint;
1847 bp->id = bpNum;
1848 bp->type = bpType;
1849 bp->temporary = disp == 'd';
1850 bp->enabled = enable == 'y';
1851 bp->location = location;
1852 bp->address = address;
1853 bp->hitCount = hits;
1854 bp->ignoreCount = ignoreCount;
1855 bp->condition = condition;
1856 brks.append(bp);
1858 return true;
1861 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1863 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1864 // no threads
1865 return true;
1868 int id;
1869 QString systag;
1870 QString func, file;
1871 int lineNo;
1872 DbgAddr address;
1874 const char* p = output;
1875 while (*p != '\0') {
1876 // seach look for thread id, watching out for the focus indicator
1877 bool hasFocus = false;
1878 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1879 p++;
1880 if (*p == '*') {
1881 hasFocus = true;
1882 p++;
1883 // there follows only whitespace
1885 char* end;
1886 id = strtol(p, &end, 10);
1887 if (p == end) {
1888 // syntax error: no number found; bail out
1889 return true;
1891 p = end;
1893 // skip space
1894 while (isspace(*p))
1895 p++;
1898 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1900 end = strstr(p, " ");
1901 if (end == 0) {
1902 // syntax error; bail out
1903 return true;
1905 systag = FROM_LATIN1(p, end-p);
1906 p = end+2;
1909 * Now follows a standard stack frame. Sometimes, however, gdb
1910 * catches a thread at an instant where it doesn't have a stack.
1912 if (strncmp(p, "[No stack.]", 11) != 0) {
1913 ::parseFrameInfo(p, func, file, lineNo, address);
1914 } else {
1915 func = "[No stack]";
1916 file = QString();
1917 lineNo = -1;
1918 address = QString();
1919 p += 11; /* \n is skipped above */
1922 ThreadInfo* thr = new ThreadInfo;
1923 thr->id = id;
1924 thr->threadName = systag;
1925 thr->hasFocus = hasFocus;
1926 thr->function = func;
1927 thr->fileName = file;
1928 thr->lineNo = lineNo;
1929 thr->address = address;
1930 threads.append(thr);
1932 return true;
1935 static bool parseNewBreakpoint(const char* o, int& id,
1936 QString& file, int& lineNo, QString& address);
1937 static bool parseNewWatchpoint(const char* o, int& id,
1938 QString& expr);
1940 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1941 QString& file, int& lineNo, QString& address)
1943 const char* o = output;
1944 // skip lines of that begin with "(Cannot find"
1945 while (strncmp(o, "(Cannot find", 12) == 0) {
1946 o = strchr(o, '\n');
1947 if (o == 0)
1948 return false;
1949 o++; /* skip newline */
1952 if (strncmp(o, "Breakpoint ", 11) == 0) {
1953 output += 11; /* skip "Breakpoint " */
1954 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1955 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1956 output += 20;
1957 return ::parseNewWatchpoint(output, id, address);
1958 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1959 output += 11;
1960 return ::parseNewWatchpoint(output, id, address);
1962 return false;
1965 static bool parseNewBreakpoint(const char* o, int& id,
1966 QString& file, int& lineNo, QString& address)
1968 // breakpoint id
1969 char* p;
1970 id = strtoul(o, &p, 10);
1971 if (p == o)
1972 return false;
1974 // check for the address
1975 if (strncmp(p, " at 0x", 6) == 0) {
1976 char* start = p+4; /* skip " at ", but not 0x */
1977 p += 6;
1978 while (isxdigit(*p))
1979 ++p;
1980 address = FROM_LATIN1(start, p-start);
1983 // file name
1984 char* fileStart = strstr(p, "file ");
1985 if (fileStart == 0)
1986 return !address.isEmpty(); /* parse error only if there's no address */
1987 fileStart += 5;
1989 // line number
1990 char* numStart = strstr(fileStart, ", line ");
1991 QString fileName = FROM_LATIN1(fileStart, numStart-fileStart);
1992 numStart += 7;
1993 int line = strtoul(numStart, &p, 10);
1994 if (numStart == p)
1995 return false;
1997 file = fileName;
1998 lineNo = line-1; /* zero-based! */
1999 return true;
2002 static bool parseNewWatchpoint(const char* o, int& id,
2003 QString& expr)
2005 // watchpoint id
2006 char* p;
2007 id = strtoul(o, &p, 10);
2008 if (p == o)
2009 return false;
2011 if (strncmp(p, ": ", 2) != 0)
2012 return false;
2013 p += 2;
2015 // all the rest on the line is the expression
2016 expr = FROM_LATIN1(p, strlen(p)).stripWhiteSpace();
2017 return true;
2020 void GdbDriver::parseLocals(const char* output, QList<VarTree>& newVars)
2022 // check for possible error conditions
2023 if (strncmp(output, "No symbol table", 15) == 0)
2025 return;
2028 while (*output != '\0') {
2029 while (isspace(*output))
2030 output++;
2031 if (*output == '\0')
2032 break;
2033 // skip occurrences of "No locals" and "No args"
2034 if (strncmp(output, "No locals", 9) == 0 ||
2035 strncmp(output, "No arguments", 12) == 0)
2037 output = strchr(output, '\n');
2038 if (output == 0) {
2039 break;
2041 continue;
2044 VarTree* variable = parseVar(output);
2045 if (variable == 0) {
2046 break;
2048 // do not add duplicates
2049 for (VarTree* o = newVars.first(); o != 0; o = newVars.next()) {
2050 if (o->getText() == variable->getText()) {
2051 delete variable;
2052 goto skipDuplicate;
2055 newVars.append(variable);
2056 skipDuplicate:;
2060 bool GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue, VarTree*& var)
2062 // check for error conditions
2063 if (parseErrorMessage(output, var, wantErrorValue))
2065 return false;
2066 } else {
2067 // parse the variable
2068 var = parseVar(output);
2069 return true;
2073 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2075 bool isGood = false;
2076 message = QString(output).simplifyWhiteSpace();
2077 if (message.isEmpty()) {
2078 message = i18n("New working directory: ") + m_programWD;
2079 isGood = true;
2081 return isGood;
2084 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2086 message = output;
2088 m_haveCoreFile = false;
2091 * Lines starting with the following do not indicate errors:
2092 * Using host libthread_db
2093 * (no debugging symbols found)
2095 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2096 strncmp(output, "(no debugging symbols found)", 28) == 0)
2098 // this line is good, go to the next one
2099 const char* end = strchr(output, '\n');
2100 if (end == 0)
2101 output += strlen(output);
2102 else
2103 output = end+1;
2107 * If we've parsed all lines, there was no error.
2109 return output[0] == '\0';
2112 bool GdbDriver::parseCoreFile(const char* output)
2114 // if command succeeded, gdb emits a line starting with "#0 "
2115 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2116 return m_haveCoreFile;
2119 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2121 // optionally: "program changed, rereading symbols",
2122 // followed by:
2123 // "Program exited normally"
2124 // "Program terminated with wignal SIGSEGV"
2125 // "Program received signal SIGINT" or other signal
2126 // "Breakpoint..."
2128 // go through the output, line by line, checking what we have
2129 const char* start = output - 1;
2130 uint flags = SFprogramActive;
2131 message = QString();
2132 do {
2133 start++; /* skip '\n' */
2135 if (strncmp(start, "Program ", 8) == 0 ||
2136 strncmp(start, "ptrace: ", 8) == 0) {
2138 * When we receive a signal, the program remains active.
2140 * Special: If we "stopped" in a corefile, the string "Program
2141 * terminated with signal"... is displayed. (Normally, we see
2142 * "Program received signal"... when a signal happens.)
2144 if (strncmp(start, "Program exited", 14) == 0 ||
2145 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2146 strncmp(start, "ptrace: ", 8) == 0)
2148 flags &= ~SFprogramActive;
2151 // set message
2152 const char* endOfMessage = strchr(start, '\n');
2153 if (endOfMessage == 0)
2154 endOfMessage = start + strlen(start);
2155 message = FROM_LATIN1(start, endOfMessage-start);
2156 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2158 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2159 * that it stopped at a temporary breakpoint).
2161 flags |= SFrefreshBreak;
2162 } else if (strstr(start, "re-reading symbols.") != 0) {
2163 flags |= SFrefreshSource;
2166 // next line, please
2167 start = strchr(start, '\n');
2168 } while (start != 0);
2171 * Gdb only notices when new threads have appeared, but not when a
2172 * thread finishes. So we always have to assume that the list of
2173 * threads has changed.
2175 flags |= SFrefreshThreads;
2177 return flags;
2180 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2182 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2183 return;
2185 // parse the table of shared libraries
2187 // strip off head line
2188 output = strchr(output, '\n');
2189 if (output == 0)
2190 return;
2191 output++; /* skip '\n' */
2192 QString shlibName;
2193 while (*output != '\0') {
2194 // format of a line is
2195 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2196 // 3 blocks of non-space followed by space
2197 for (int i = 0; *output != '\0' && i < 3; i++) {
2198 while (*output != '\0' && !isspace(*output)) { /* non-space */
2199 output++;
2201 while (isspace(*output)) { /* space */
2202 output++;
2205 if (*output == '\0')
2206 return;
2207 const char* start = output;
2208 output = strchr(output, '\n');
2209 if (output == 0)
2210 output = start + strlen(start);
2211 shlibName = FROM_LATIN1(start, output-start);
2212 if (*output != '\0')
2213 output++;
2214 shlibs.append(shlibName);
2215 TRACE("found shared lib " + shlibName);
2219 bool GdbDriver::parseFindType(const char* output, QString& type)
2221 if (strncmp(output, "type = ", 7) != 0)
2222 return false;
2225 * Everything else is the type. We strip off any leading "const" and any
2226 * trailing "&" on the grounds that neither affects the decoding of the
2227 * object. We also strip off all white-space from the type.
2229 output += 7;
2230 if (strncmp(output, "const ", 6) == 0)
2231 output += 6;
2232 type = output;
2233 type.replace(QRegExp("\\s+"), "");
2234 if (type.endsWith("&"))
2235 type.truncate(type.length() - 1);
2236 return true;
2239 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2241 if (strncmp(output, "The program has no registers now", 32) == 0) {
2242 return;
2245 QString regName;
2246 QString value;
2248 // parse register values
2249 while (*output != '\0')
2251 // skip space at the start of the line
2252 while (isspace(*output))
2253 output++;
2255 // register name
2256 const char* start = output;
2257 while (*output != '\0' && !isspace(*output))
2258 output++;
2259 if (*output == '\0')
2260 break;
2261 regName = FROM_LATIN1(start, output-start);
2263 // skip space
2264 while (isspace(*output))
2265 output++;
2267 RegisterInfo* reg = new RegisterInfo;
2268 reg->regName = regName;
2271 * If we find a brace now, this is a vector register. We look for
2272 * the closing brace and treat the result as cooked value.
2274 if (*output == '{')
2276 start = output;
2277 skipNested(output, '{', '}');
2278 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2279 // skip space, but not the end of line
2280 while (isspace(*output) && *output != '\n')
2281 output++;
2282 // get rid of the braces at the begining and the end
2283 value.remove(0, 1);
2284 if (value[value.length()-1] == '}') {
2285 value = value.left(value.length()-1);
2287 // gdb 5.3 doesn't print a separate set of raw values
2288 if (*output == '{') {
2289 // another set of vector follows
2290 // what we have so far is the raw value
2291 reg->rawValue = value;
2293 start = output;
2294 skipNested(output, '{', '}');
2295 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2296 } else {
2297 // for gdb 5.3
2298 // find first type that does not have an array, this is the RAW value
2299 const char* end=start;
2300 findEnd(end);
2301 const char* cur=start;
2302 while (cur<end) {
2303 while (*cur != '=' && cur<end)
2304 cur++;
2305 cur++;
2306 while (isspace(*cur) && cur<end)
2307 cur++;
2308 if (isNumberish(*cur)) {
2309 end=cur;
2310 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2311 end++;
2312 QString rawValue = FROM_LATIN1(cur, end-cur).simplifyWhiteSpace();
2313 reg->rawValue = rawValue;
2315 if (rawValue.left(2)=="0x") {
2316 // ok we have a raw value, now get it's type
2317 end=cur-1;
2318 while (isspace(*end) || *end=='=') end--;
2319 end++;
2320 cur=end-1;
2321 while (*cur!='{' && *cur!=' ')
2322 cur--;
2323 cur++;
2324 reg->type=FROM_LATIN1(cur, end-cur);
2327 // end while loop
2328 cur=end;
2331 // skip to the end of line
2332 while (*output != '\0' && *output != '\n')
2333 output++;
2334 // get rid of the braces at the begining and the end
2335 value.remove(0, 1);
2336 if (value[value.length()-1] == '}') {
2337 value = value.left(value.length()-1);
2340 reg->cookedValue = value;
2342 else
2344 // the rest of the line is the register value
2345 start = output;
2346 output = strchr(output,'\n');
2347 if (output == 0)
2348 output = start + strlen(start);
2349 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2352 * We split the raw from the cooked values.
2353 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2354 * Here, the cooked value comes first, and the raw value is in
2355 * the second part.
2357 int pos = value.find(" (raw ");
2358 if (pos >= 0)
2360 reg->cookedValue = value.left(pos);
2361 reg->rawValue = value.mid(pos+6);
2362 if (reg->rawValue.right(1) == ")") // remove closing bracket
2363 reg->rawValue.truncate(reg->rawValue.length()-1);
2365 else
2368 * In other cases we split off the first token (separated by
2369 * whitespace). It is the raw value. The remainder of the line
2370 * is the cooked value.
2372 int pos = value.find(' ');
2373 if (pos < 0) {
2374 reg->rawValue = value;
2375 reg->cookedValue = QString();
2376 } else {
2377 reg->rawValue = value.left(pos);
2378 reg->cookedValue = value.mid(pos+1);
2382 if (*output != '\0')
2383 output++; /* skip '\n' */
2385 regs.append(reg);
2389 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2391 // "is at address" or "starts at address"
2392 const char* start = strstr(output, "s at address ");
2393 if (start == 0)
2394 return false;
2396 start += 13;
2397 const char* p = start;
2398 while (*p != '\0' && !isspace(*p))
2399 p++;
2400 addrFrom = FROM_LATIN1(start, p-start);
2402 start = strstr(p, "and ends at ");
2403 if (start == 0) {
2404 addrTo = addrFrom;
2405 return true;
2408 start += 12;
2409 p = start;
2410 while (*p != '\0' && !isspace(*p))
2411 p++;
2412 addrTo = FROM_LATIN1(start, p-start);
2414 return true;
2417 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2419 code.clear();
2421 if (strncmp(output, "Dump of assembler", 17) != 0) {
2422 // error message?
2423 DisassembledCode c;
2424 c.code = output;
2425 code.append(new DisassembledCode(c));
2426 return;
2429 // remove first line
2430 const char* p = strchr(output, '\n');
2431 if (p == 0)
2432 return; /* not a regular output */
2434 p++;
2436 // remove last line
2437 const char* end = strstr(output, "End of assembler");
2438 if (end == 0)
2439 end = p + strlen(p);
2441 DbgAddr address;
2443 // remove function offsets from the lines
2444 while (p != end)
2446 const char* start = p;
2447 // address
2448 while (p != end && !isspace(*p))
2449 p++;
2450 address = FROM_LATIN1(start, p-start);
2452 // function name (enclosed in '<>', followed by ':')
2453 while (p != end && *p != '<')
2454 p++;
2455 if (*p == '<')
2456 skipNestedAngles(p);
2457 if (*p == ':')
2458 p++;
2460 // space until code
2461 while (p != end && isspace(*p))
2462 p++;
2464 // code until end of line
2465 start = p;
2466 while (p != end && *p != '\n')
2467 p++;
2468 if (p != end) /* include '\n' */
2469 p++;
2471 DisassembledCode* c = new DisassembledCode;
2472 c->address = address;
2473 c->code = FROM_LATIN1(start, p-start);
2474 code.append(c);
2478 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2480 if (isErrorExpr(output)) {
2481 // error; strip space
2482 QString msg = output;
2483 return msg.stripWhiteSpace();
2486 const char* p = output; /* save typing */
2487 DbgAddr addr;
2488 QString dump;
2490 // the address
2491 while (*p != 0) {
2492 const char* start = p;
2493 while (*p != '\0' && *p != ':' && !isspace(*p))
2494 p++;
2495 addr = FROM_LATIN1(start, p-start);
2496 if (*p != ':') {
2497 // parse function offset
2498 while (isspace(*p))
2499 p++;
2500 start = p;
2501 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2502 p++;
2503 addr.fnoffs = FROM_LATIN1(start, p-start);
2505 if (*p == ':')
2506 p++;
2507 // skip space; this may skip a new-line char!
2508 while (isspace(*p))
2509 p++;
2510 // everything to the end of the line is the memory dump
2511 const char* end = strchr(p, '\n');
2512 if (end != 0) {
2513 dump = FROM_LATIN1(p, end-p);
2514 p = end+1;
2515 } else {
2516 dump = FROM_LATIN1(p, strlen(p));
2517 p += strlen(p);
2519 MemoryDump* md = new MemoryDump;
2520 md->address = addr;
2521 md->dump = dump;
2522 memdump.append(md);
2525 return QString();
2528 QString GdbDriver::editableValue(VarTree* value)
2530 const char* s = value->m_value.latin1();
2532 // if the variable is a pointer value that contains a cast,
2533 // remove the cast
2534 if (*s == '(') {
2535 skipNested(s, '(', ')');
2536 // skip space
2537 while (isspace(*s))
2538 ++s;
2541 repeat:
2542 const char* start = s;
2544 if (strncmp(s, "0x", 2) == 0)
2546 s += 2;
2547 while (isxdigit(*s))
2548 ++s;
2551 * What we saw so far might have been a reference. If so, edit the
2552 * referenced value. Otherwise, edit the pointer.
2554 if (*s == ':') {
2555 // a reference
2556 ++s;
2557 goto repeat;
2559 // a pointer
2560 // if it's a pointer to a string, remove the string
2561 const char* end = s;
2562 while (isspace(*s))
2563 ++s;
2564 if (*s == '"') {
2565 // a string
2566 return FROM_LATIN1(start, end-start);
2567 } else {
2568 // other pointer
2569 return FROM_LATIN1(start, strlen(start));
2573 // else leave it unchanged (or stripped of the reference preamble)
2574 return s;
2577 QString GdbDriver::parseSetVariable(const char* output)
2579 // if there is any output, it is an error message
2580 QString msg = output;
2581 return msg.stripWhiteSpace();
2585 #include "gdbdriver.moc"