Don't start with a tiny window when started for the first time.
[kdbg.git] / kdbg / gdbdriver.cpp
blob2a5c5d257b46c200e322e4ec77e75159dae49276
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) {
401 // attach saved redirection
402 strArg += m_redirect;
405 SIZED_QString(cmdString, MAX_FMTLEN+strArg.length());
406 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
407 return cmdString;
410 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
412 assert(cmd >= 0 && cmd < NUM_CMDS);
413 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
415 SIZED_QString(cmdString, MAX_FMTLEN+30);
417 cmdString.sprintf(cmds[cmd].fmt, intArg);
418 return cmdString;
421 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
423 assert(cmd >= 0 && cmd < NUM_CMDS);
424 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
425 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
426 cmd == DCexamine ||
427 cmd == DCtty);
429 normalizeStringArg(strArg);
431 SIZED_QString(cmdString, MAX_FMTLEN+30+strArg.length());
433 if (cmd == DCtty)
436 * intArg specifies which channels should be redirected to
437 * /dev/null. It is a value or'ed together from RDNstdin,
438 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
439 * command.
441 * Note: We rely on that after the DCtty a DCsetargs will follow,
442 * which will ultimately apply the redirection.
444 static const char* const runRedir[8] = {
446 " </dev/null",
447 " >/dev/null",
448 " </dev/null >/dev/null",
449 " 2>/dev/null",
450 " </dev/null 2>/dev/null",
451 " >/dev/null 2>&1",
452 " </dev/null >/dev/null 2>&1"
454 if (strArg.isEmpty())
455 intArg = 7; /* failsafe if no tty */
456 m_redirect = runRedir[intArg & 7];
458 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
461 if (cmd == DCexamine) {
462 // make a format specifier from the intArg
463 static const char size[16] = {
464 '\0', 'b', 'h', 'w', 'g'
466 static const char format[16] = {
467 '\0', 'x', 'd', 'u', 'o', 't',
468 'a', 'c', 'f', 's', 'i'
470 assert(MDTsizemask == 0xf); /* lowest 4 bits */
471 assert(MDTformatmask == 0xf0); /* next 4 bits */
472 int count = 16; /* number of entities to print */
473 char sizeSpec = size[intArg & MDTsizemask];
474 char formatSpec = format[(intArg & MDTformatmask) >> 4];
475 assert(sizeSpec != '\0');
476 assert(formatSpec != '\0');
477 // adjust count such that 16 lines are printed
478 switch (intArg & MDTformatmask) {
479 case MDTstring: case MDTinsn:
480 break; /* no modification needed */
481 default:
482 // all cases drop through:
483 switch (intArg & MDTsizemask) {
484 case MDTbyte:
485 case MDThalfword:
486 count *= 2;
487 case MDTword:
488 count *= 2;
489 case MDTgiantword:
490 count *= 2;
492 break;
494 QString spec;
495 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
497 return makeCmdString(DCexamine, spec, strArg);
500 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
502 // line numbers are zero-based
503 if (cmd == DCuntil || cmd == DCbreakline ||
504 cmd == DCtbreakline || cmd == DCinfoline)
506 intArg++;
508 if (cmd == DCinfoline)
510 // must split off file name part
511 int slash = strArg.findRev('/');
512 if (slash >= 0)
513 strArg = strArg.right(strArg.length()-slash-1);
515 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
517 else
519 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
521 return cmdString;
524 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
526 assert(cmd >= 0 && cmd < NUM_CMDS);
527 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
529 normalizeStringArg(strArg1);
530 normalizeStringArg(strArg2);
532 SIZED_QString(cmdString, MAX_FMTLEN+strArg1.length()+strArg2.length());
533 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
534 return cmdString;
537 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
539 assert(cmd >= 0 && cmd < NUM_CMDS);
540 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
542 SIZED_QString(cmdString, MAX_FMTLEN+60);
543 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
544 return cmdString;
547 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
549 assert(cmd >= 0 && cmd < NUM_CMDS);
550 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
552 if (cmd == DCrun) {
553 m_haveCoreFile = false;
556 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
559 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
560 bool clearLow)
562 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
565 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
566 bool clearLow)
569 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
572 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
573 bool clearLow)
575 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
578 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
579 bool clearLow)
581 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
584 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
585 bool clearLow)
587 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
590 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
592 return queueCmdString(cmd, cmds[cmd].fmt, mode);
595 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
596 QueueMode mode)
598 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
601 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
602 QueueMode mode)
604 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
607 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
608 QueueMode mode)
610 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
613 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
614 QueueMode mode)
616 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
619 void GdbDriver::terminate()
621 kill(SIGTERM);
622 m_state = DSidle;
625 void GdbDriver::detachAndTerminate()
627 kill(SIGINT);
628 flushCommands();
629 executeCmdString(DCinitialize, "detach\nquit\n", true);
632 void GdbDriver::interruptInferior()
634 kill(SIGINT);
635 // remove accidentally queued commands
636 flushHiPriQueue();
639 static bool isErrorExpr(const char* output)
641 return
642 strncmp(output, "Cannot access memory at", 23) == 0 ||
643 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
644 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
645 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
646 strncmp(output, "There is no member or method named", 34) == 0 ||
647 strncmp(output, "A parse error in expression", 27) == 0 ||
648 strncmp(output, "No symbol \"", 11) == 0 ||
649 strncmp(output, "Internal error: ", 16) == 0;
653 * Returns true if the output is an error message. If wantErrorValue is
654 * true, a new VarTree object is created and filled with the error message.
655 * If there are warnings, they are skipped and output points past the warnings
656 * on return (even if there \e are errors).
658 static bool parseErrorMessage(const char*& output,
659 VarTree*& variable, bool wantErrorValue)
661 // skip warnings
662 while (strncmp(output, "warning:", 8) == 0)
664 char* end = strchr(output+8, '\n');
665 if (end == 0)
666 output += strlen(output);
667 else
668 output = end+1;
671 if (isErrorExpr(output))
673 if (wantErrorValue) {
674 // put the error message as value in the variable
675 variable = new VarTree(QString(), VarTree::NKplain);
676 const char* endMsg = strchr(output, '\n');
677 if (endMsg == 0)
678 endMsg = output + strlen(output);
679 variable->m_value = FROM_LATIN1(output, endMsg-output);
680 } else {
681 variable = 0;
683 return true;
685 return false;
688 #if QT_VERSION >= 300
689 union Qt2QChar {
690 short s;
691 struct {
692 uchar row;
693 uchar cell;
694 } qch;
696 #endif
698 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
700 // don't accept the command if it is empty
701 if (cmd == 0 || *cmd == '\0')
702 return;
703 assert(strlen(cmd) <= MAX_FMTLEN);
704 cmds[DCprintQStringStruct].fmt = cmd;
707 VarTree* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
709 VarTree* variable = 0;
712 * Parse off white space. gdb sometimes prints white space first if the
713 * printed array leaded to an error.
715 while (isspace(*output))
716 output++;
718 // special case: empty string (0 repetitions)
719 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
721 variable = new VarTree(QString(), VarTree::NKplain);
722 variable->m_value = "\"\"";
723 return variable;
726 // check for error conditions
727 if (parseErrorMessage(output, variable, wantErrorValue))
728 return variable;
730 // parse the array
732 // find '='
733 const char* p = output;
734 p = strchr(p, '=');
735 if (p == 0) {
736 goto error;
738 // skip white space
739 do {
740 p++;
741 } while (isspace(*p));
743 if (*p == '{')
745 // this is the real data
746 p++; /* skip '{' */
748 // parse the array
749 QString result;
750 QString repeatCount;
751 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
753 * A matrix for separators between the individual "things"
754 * that are added to the string. The first index is a bool,
755 * the second index is from the enum above.
757 static const char* separator[2][3] = {
758 { "\"", 0, ", \"" }, /* normal char is added */
759 { "'", "\", '", ", '" } /* repeated char is added */
762 while (isdigit(*p)) {
763 // parse a number
764 char* end;
765 unsigned short value = (unsigned short) strtoul(p, &end, 0);
766 if (end == p)
767 goto error; /* huh? no valid digits */
768 // skip separator and search for a repeat count
769 p = end;
770 while (isspace(*p) || *p == ',')
771 p++;
772 bool repeats = strncmp(p, "<repeats ", 9) == 0;
773 if (repeats) {
774 const char* start = p;
775 p = strchr(p+9, '>'); /* search end and advance */
776 if (p == 0)
777 goto error;
778 p++; /* skip '>' */
779 repeatCount = FROM_LATIN1(start, p-start);
780 while (isspace(*p) || *p == ',')
781 p++;
783 // p is now at the next char (or the end)
785 // interpret the value as a QChar
786 // TODO: make cross-architecture compatible
787 QChar ch;
788 if (qt3like) {
789 ch = QChar(value);
790 } else {
791 #if QT_VERSION < 300
792 (unsigned short&)ch = value;
793 #else
794 Qt2QChar c;
795 c.s = value;
796 ch.setRow(c.qch.row);
797 ch.setCell(c.qch.cell);
798 #endif
801 // escape a few frequently used characters
802 char escapeCode = '\0';
803 switch (ch.latin1()) {
804 case '\n': escapeCode = 'n'; break;
805 case '\r': escapeCode = 'r'; break;
806 case '\t': escapeCode = 't'; break;
807 case '\b': escapeCode = 'b'; break;
808 case '\"': escapeCode = '\"'; break;
809 case '\\': escapeCode = '\\'; break;
810 case '\0': if (value == 0) { escapeCode = '0'; } break;
813 // add separator
814 result += separator[repeats][lastThing];
815 // add char
816 if (escapeCode != '\0') {
817 result += '\\';
818 ch = escapeCode;
820 result += ch;
822 // fixup repeat count and lastThing
823 if (repeats) {
824 result += "' ";
825 result += repeatCount;
826 lastThing = wasRepeat;
827 } else {
828 lastThing = wasChar;
831 if (*p != '}')
832 goto error;
834 // closing quote
835 if (lastThing == wasChar)
836 result += "\"";
838 // assign the value
839 variable = new VarTree(QString(), VarTree::NKplain);
840 variable->m_value = result;
842 else if (strncmp(p, "true", 4) == 0)
844 variable = new VarTree(QString(), VarTree::NKplain);
845 variable->m_value = "QString::null";
847 else if (strncmp(p, "false", 5) == 0)
849 variable = new VarTree(QString(), VarTree::NKplain);
850 variable->m_value = "(null)";
852 else
853 goto error;
854 return variable;
856 error:
857 if (wantErrorValue) {
858 variable = new VarTree(QString(), VarTree::NKplain);
859 variable->m_value = "internal parse error";
861 return variable;
864 static VarTree* parseVar(const char*& s)
866 const char* p = s;
868 // skip whitespace
869 while (isspace(*p))
870 p++;
872 QString name;
873 VarTree::NameKind kind;
874 if (!parseName(p, name, kind)) {
875 return 0;
878 // go for '='
879 while (isspace(*p))
880 p++;
881 if (*p != '=') {
882 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
883 return 0;
885 // skip the '=' and more whitespace
886 p++;
887 while (isspace(*p))
888 p++;
890 VarTree* variable = new VarTree(name, kind);
891 variable->setDeleteChildren(true);
893 if (!parseValue(p, variable)) {
894 delete variable;
895 return 0;
897 s = p;
898 return variable;
901 static void skipNested(const char*& s, char opening, char closing)
903 const char* p = s;
905 // parse a nested type
906 int nest = 1;
907 p++;
909 * Search for next matching `closing' char, skipping nested pairs of
910 * `opening' and `closing'.
912 while (*p && nest > 0) {
913 if (*p == opening) {
914 nest++;
915 } else if (*p == closing) {
916 nest--;
918 p++;
920 if (nest != 0) {
921 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
923 s = p;
927 * This function skips text that is delimited by nested angle bracktes, '<>'.
928 * A complication arises because the delimited text can contain the names of
929 * operator<<, operator>>, operator<, and operator>, which have to be treated
930 * specially so that they do not count towards the nesting of '<>'.
931 * This function assumes that the delimited text does not contain strings.
933 static void skipNestedAngles(const char*& s)
935 const char* p = s;
937 int nest = 1;
938 p++; // skip the initial '<'
939 while (*p && nest > 0)
941 // Below we can check for p-s >= 9 instead of 8 because
942 // *s is '<' and cannot be part of "operator".
943 if (*p == '<')
945 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
946 if (p[1] == '<')
947 p++;
948 } else {
949 nest++;
952 else if (*p == '>')
954 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
955 if (p[1] == '>')
956 p++;
957 } else {
958 nest--;
961 p++;
963 if (nest != 0) {
964 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
966 s = p;
970 * Find the end of line that is not inside braces
972 static void findEnd(const char*& s)
974 const char* p = s;
975 while (*p && *p!='\n') {
976 while (*p && *p!='\n' && *p!='{')
977 p++;
978 if (*p=='{') {
979 p++;
980 skipNested(p, '{', '}'); p--;
983 s = p;
986 static bool isNumberish(const char ch)
988 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
991 void skipString(const char*& p)
993 moreStrings:
994 // opening quote
995 char quote = *p++;
996 while (*p != quote) {
997 if (*p == '\\') {
998 // skip escaped character
999 // no special treatment for octal values necessary
1000 p++;
1002 // simply return if no more characters
1003 if (*p == '\0')
1004 return;
1005 p++;
1007 // closing quote
1008 p++;
1010 * Strings can consist of several parts, some of which contain repeated
1011 * characters.
1013 if (quote == '\'') {
1014 // look ahaead for <repeats 123 times>
1015 const char* q = p+1;
1016 while (isspace(*q))
1017 q++;
1018 if (strncmp(q, "<repeats ", 9) == 0) {
1019 p = q+9;
1020 while (*p != '\0' && *p != '>')
1021 p++;
1022 if (*p != '\0') {
1023 p++; /* skip the '>' */
1027 // is the string continued?
1028 if (*p == ',') {
1029 // look ahead for another quote
1030 const char* q = p+1;
1031 while (isspace(*q))
1032 q++;
1033 if (*q == '"' || *q == '\'') {
1034 // yes!
1035 p = q;
1036 goto moreStrings;
1039 /* very long strings are followed by `...' */
1040 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1041 p += 3;
1045 static void skipNestedWithString(const char*& s, char opening, char closing)
1047 const char* p = s;
1049 // parse a nested expression
1050 int nest = 1;
1051 p++;
1053 * Search for next matching `closing' char, skipping nested pairs of
1054 * `opening' and `closing' as well as strings.
1056 while (*p && nest > 0) {
1057 if (*p == opening) {
1058 nest++;
1059 } else if (*p == closing) {
1060 nest--;
1061 } else if (*p == '\'' || *p == '\"') {
1062 skipString(p);
1063 continue;
1065 p++;
1067 if (nest > 0) {
1068 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1070 s = p;
1073 inline void skipName(const char*& p)
1075 // allow : (for enumeration values) and $ and . (for _vtbl.)
1076 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1077 p++;
1080 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1082 kind = VarTree::NKplain;
1084 const char* p = s;
1085 // examples of names:
1086 // name
1087 // <Object>
1088 // <string<a,b<c>,7> >
1090 if (*p == '<') {
1091 skipNestedAngles(p);
1092 name = FROM_LATIN1(s, p - s);
1093 kind = VarTree::NKtype;
1095 else
1097 // name, which might be "static"; allow dot for "_vtbl."
1098 skipName(p);
1099 if (p == s) {
1100 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1101 return false;
1103 int len = p - s;
1104 if (len == 6 && strncmp(s, "static", 6) == 0) {
1105 kind = VarTree::NKstatic;
1107 // its a static variable, name comes now
1108 while (isspace(*p))
1109 p++;
1110 s = p;
1111 skipName(p);
1112 if (p == s) {
1113 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1114 return false;
1116 len = p - s;
1118 name = FROM_LATIN1(s, len);
1120 // return the new position
1121 s = p;
1122 return true;
1125 static bool parseValue(const char*& s, VarTree* variable)
1127 variable->m_value = "";
1129 repeat:
1130 if (*s == '{') {
1131 // Sometimes we find the following output:
1132 // {<text variable, no debug info>} 0x40012000 <access>
1133 // {<data variable, no debug info>}
1134 // {<variable (not text or data), no debug info>}
1135 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1136 strncmp(s, "{<data variable, ", 17) == 0 ||
1137 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1139 const char* start = s;
1140 skipNested(s, '{', '}');
1141 variable->m_value = FROM_LATIN1(start, s-start);
1142 variable->m_value += ' '; // add only a single space
1143 while (isspace(*s))
1144 s++;
1145 goto repeat;
1147 else
1149 s++;
1150 if (!parseNested(s, variable)) {
1151 return false;
1153 // must be the closing brace
1154 if (*s != '}') {
1155 TRACE("parse error: missing } of " + variable->getText());
1156 return false;
1158 s++;
1159 // final white space
1160 while (isspace(*s))
1161 s++;
1163 } else {
1164 // examples of leaf values (cannot be the empty string):
1165 // 123
1166 // -123
1167 // 23.575e+37
1168 // 0x32a45
1169 // @0x012ab4
1170 // (DwContentType&) @0x8123456: {...}
1171 // 0x32a45 "text"
1172 // 10 '\n'
1173 // <optimized out>
1174 // 0x823abc <Array<int> virtual table>
1175 // (void (*)()) 0x8048480 <f(E *, char)>
1176 // (E *) 0xbffff450
1177 // red
1178 // &parseP (HTMLClueV *, char *)
1179 // Variable "x" is not available.
1180 // The value of variable 'x' is distributed...
1181 // -nan(0xfffff081defa0)
1183 const char*p = s;
1185 // check for type
1186 QString type;
1187 if (*p == '(') {
1188 skipNested(p, '(', ')');
1190 while (isspace(*p))
1191 p++;
1192 variable->m_value = FROM_LATIN1(s, p - s);
1195 bool reference = false;
1196 if (*p == '@') {
1197 // skip reference marker
1198 p++;
1199 reference = true;
1201 const char* start = p;
1202 if (*p == '-')
1203 p++;
1205 // some values consist of more than one token
1206 bool checkMultiPart = false;
1208 if (p[0] == '0' && p[1] == 'x') {
1209 // parse hex number
1210 p += 2;
1211 while (isxdigit(*p))
1212 p++;
1215 * Assume this is a pointer, but only if it's not a reference, since
1216 * references can't be expanded.
1218 if (!reference) {
1219 variable->m_varKind = VarTree::VKpointer;
1220 } else {
1222 * References are followed by a colon, in which case we'll
1223 * find the value following the reference address.
1225 if (*p == ':') {
1226 p++;
1227 } else {
1228 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1229 reference = false;
1232 checkMultiPart = true;
1233 } else if (isdigit(*p)) {
1234 // parse decimal number, possibly a float
1235 while (isdigit(*p))
1236 p++;
1237 if (*p == '.') { /* TODO: obey i18n? */
1238 // In long arrays an integer may be followed by '...'.
1239 // We test for this situation and don't gobble the '...'.
1240 if (p[1] != '.' || p[0] != '.') {
1241 // fractional part
1242 p++;
1243 while (isdigit(*p))
1244 p++;
1247 if (*p == 'e' || *p == 'E') {
1248 p++;
1249 // exponent
1250 if (*p == '-' || *p == '+')
1251 p++;
1252 while (isdigit(*p))
1253 p++;
1256 // for char variables there is the char, eg. 10 '\n'
1257 checkMultiPart = true;
1258 } else if (*p == '<') {
1259 // e.g. <optimized out>
1260 skipNestedAngles(p);
1261 } else if (*p == '"' || *p == '\'') {
1262 // character may have multipart: '\000' <repeats 11 times>
1263 checkMultiPart = *p == '\'';
1264 // found a string
1265 skipString(p);
1266 } else if (*p == '&') {
1267 // function pointer
1268 p++;
1269 skipName(p);
1270 while (isspace(*p)) {
1271 p++;
1273 if (*p == '(') {
1274 skipNested(p, '(', ')');
1276 } else if (strncmp(p, "Variable \"", 10) == 0) {
1277 // Variable "x" is not available.
1278 p += 10; // skip to "
1279 skipName(p);
1280 if (strncmp(p, "\" is not available.", 19) == 0) {
1281 p += 19;
1283 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1284 p += 23;
1285 skipName(p);
1286 const char* e = strchr(p, '.');
1287 if (e == 0) {
1288 p += strlen(p);
1289 } else {
1290 p = e+1;
1292 } else {
1293 // must be an enumeration value
1294 skipName(p);
1295 // hmm, not necessarily: nan (floating point Not a Number)
1296 // is followed by a number in ()
1297 if (*p == '(')
1298 skipNested(p, '(', ')');
1300 variable->m_value += FROM_LATIN1(start, p - start);
1302 // remove line breaks from the value; this is ok since
1303 // string values never contain a literal line break
1304 variable->m_value.replace('\n', ' ');
1306 if (checkMultiPart) {
1307 // white space
1308 while (isspace(*p))
1309 p++;
1310 // may be followed by a string or <...>
1311 start = p;
1313 if (*p == '"' || *p == '\'') {
1314 skipString(p);
1315 } else if (*p == '<') {
1316 // if this value is part of an array, it might be followed
1317 // by <repeats 15 times>, which we don't skip here
1318 if (strncmp(p, "<repeats ", 9) != 0)
1319 skipNestedAngles(p);
1321 if (p != start) {
1322 // there is always a blank before the string,
1323 // which we will include in the final string value
1324 variable->m_value += FROM_LATIN1(start-1, (p - start)+1);
1325 // if this was a pointer, reset that flag since we
1326 // now got the value
1327 variable->m_varKind = VarTree::VKsimple;
1331 if (variable->m_value.length() == 0) {
1332 TRACE("parse error: no value for " + variable->getText());
1333 return false;
1336 // final white space
1337 while (isspace(*p))
1338 p++;
1339 s = p;
1342 * If this was a reference, the value follows. It might even be a
1343 * composite variable!
1345 if (reference) {
1346 goto repeat;
1349 if (variable->m_varKind == VarTree::VKpointer) {
1350 variable->setDelayedExpanding(true);
1354 return true;
1357 static bool parseNested(const char*& s, VarTree* variable)
1359 // could be a structure or an array
1360 while (isspace(*s))
1361 s++;
1363 const char* p = s;
1364 bool isStruct = false;
1366 * If there is a name followed by an = or an < -- which starts a type
1367 * name -- or "static", it is a structure
1369 if (*p == '<' || *p == '}') {
1370 isStruct = true;
1371 } else if (strncmp(p, "static ", 7) == 0) {
1372 isStruct = true;
1373 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1374 // look ahead for a comma after the name
1375 skipName(p);
1376 while (isspace(*p))
1377 p++;
1378 if (*p == '=') {
1379 isStruct = true;
1381 p = s; /* rescan the name */
1383 if (isStruct) {
1384 if (!parseVarSeq(p, variable)) {
1385 return false;
1387 variable->m_varKind = VarTree::VKstruct;
1388 } else {
1389 if (!parseValueSeq(p, variable)) {
1390 return false;
1392 variable->m_varKind = VarTree::VKarray;
1394 s = p;
1395 return true;
1398 static bool parseVarSeq(const char*& s, VarTree* variable)
1400 // parse a comma-separated sequence of variables
1401 VarTree* var = variable; /* var != 0 to indicate success if empty seq */
1402 for (;;) {
1403 if (*s == '}')
1404 break;
1405 if (strncmp(s, "<No data fields>}", 17) == 0)
1407 // no member variables, so break out immediately
1408 s += 16; /* go to the closing brace */
1409 break;
1411 var = parseVar(s);
1412 if (var == 0)
1413 break; /* syntax error */
1414 variable->appendChild(var);
1415 if (*s != ',')
1416 break;
1417 // skip the comma and whitespace
1418 s++;
1419 while (isspace(*s))
1420 s++;
1422 return var != 0;
1425 static bool parseValueSeq(const char*& s, VarTree* variable)
1427 // parse a comma-separated sequence of variables
1428 int index = 0;
1429 bool good;
1430 for (;;) {
1431 QString name;
1432 name.sprintf("[%d]", index);
1433 VarTree* var = new VarTree(name, VarTree::NKplain);
1434 var->setDeleteChildren(true);
1435 good = parseValue(s, var);
1436 if (!good) {
1437 delete var;
1438 return false;
1440 // a value may be followed by "<repeats 45 times>"
1441 if (strncmp(s, "<repeats ", 9) == 0) {
1442 s += 9;
1443 char* end;
1444 int l = strtol(s, &end, 10);
1445 if (end == s || strncmp(end, " times>", 7) != 0) {
1446 // should not happen
1447 delete var;
1448 return false;
1450 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1451 // replace name and advance index
1452 name.sprintf("[%d .. %d]", index, index+l-1);
1453 var->setText(name);
1454 index += l;
1455 // skip " times>" and space
1456 s = end+7;
1457 // possible final space
1458 while (isspace(*s))
1459 s++;
1460 } else {
1461 index++;
1463 variable->appendChild(var);
1464 // long arrays may be terminated by '...'
1465 if (strncmp(s, "...", 3) == 0) {
1466 s += 3;
1467 VarTree* var = new VarTree("...", VarTree::NKplain);
1468 var->setDeleteChildren(true);
1469 var->m_value = i18n("<additional entries of the array suppressed>");
1470 variable->appendChild(var);
1471 break;
1473 if (*s != ',') {
1474 break;
1476 // skip the comma and whitespace
1477 s++;
1478 while (isspace(*s))
1479 s++;
1480 // sometimes there is a closing brace after a comma
1481 // if (*s == '}')
1482 // break;
1484 return true;
1488 * Parses a stack frame.
1490 static void parseFrameInfo(const char*& s, QString& func,
1491 QString& file, int& lineNo, DbgAddr& address)
1493 const char* p = s;
1495 // next may be a hexadecimal address
1496 if (*p == '0') {
1497 const char* start = p;
1498 p++;
1499 if (*p == 'x')
1500 p++;
1501 while (isxdigit(*p))
1502 p++;
1503 address = FROM_LATIN1(start, p-start);
1504 if (strncmp(p, " in ", 4) == 0)
1505 p += 4;
1506 } else {
1507 address = DbgAddr();
1509 const char* start = p;
1510 // check for special signal handler frame
1511 if (strncmp(p, "<signal handler called>", 23) == 0) {
1512 func = FROM_LATIN1(start, 23);
1513 file = QString();
1514 lineNo = -1;
1515 s = p+23;
1516 if (*s == '\n')
1517 s++;
1518 return;
1522 * Skip the function name. It is terminated by a left parenthesis
1523 * which does not delimit "(anonymous namespace)" and which is
1524 * outside the angle brackets <> of template parameter lists.
1526 while (*p != '\0')
1528 if (*p == '<') {
1529 // check for operator<< and operator<
1530 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1532 p++;
1533 if (*p == '<')
1534 p++;
1536 else
1538 // skip template parameter list
1539 skipNestedAngles(p);
1541 } else if (*p == '(') {
1542 if (strncmp(p, "(anonymous namespace)", 21) != 0)
1543 break; // parameter list found
1544 p += 21;
1545 } else {
1546 p++;
1550 if (*p == '\0') {
1551 func = start;
1552 file = QString();
1553 lineNo = -1;
1554 s = p;
1555 return;
1558 * Skip parameters. But notice that for complicated conversion
1559 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1560 * pointer to function) as well as operator()(...) we have to skip
1561 * additional pairs of parentheses. Furthermore, recent gdbs write the
1562 * demangled name followed by the arguments in a pair of parentheses,
1563 * where the demangled name can end in "const".
1565 do {
1566 skipNestedWithString(p, '(', ')');
1567 while (isspace(*p))
1568 p++;
1569 // skip "const"
1570 if (strncmp(p, "const", 5) == 0) {
1571 p += 5;
1572 while (isspace(*p))
1573 p++;
1575 } while (*p == '(');
1577 // check for file position
1578 if (strncmp(p, "at ", 3) == 0) {
1579 p += 3;
1580 const char* fileStart = p;
1581 // go for the end of the line
1582 while (*p != '\0' && *p != '\n')
1583 p++;
1584 // search back for colon
1585 const char* colon = p;
1586 do {
1587 --colon;
1588 } while (*colon != ':');
1589 file = FROM_LATIN1(fileStart, colon-fileStart);
1590 lineNo = atoi(colon+1)-1;
1591 // skip new-line
1592 if (*p != '\0')
1593 p++;
1594 } else {
1595 // check for "from shared lib"
1596 if (strncmp(p, "from ", 5) == 0) {
1597 p += 5;
1598 // go for the end of the line
1599 while (*p != '\0' && *p != '\n')
1600 p++;
1601 // skip new-line
1602 if (*p != '\0')
1603 p++;
1605 file = "";
1606 lineNo = -1;
1608 // construct the function name (including file info)
1609 if (*p == '\0') {
1610 func = start;
1611 } else {
1612 func = FROM_LATIN1(start, p-start-1); /* don't include \n */
1614 s = p;
1617 * Replace \n (and whitespace around it) in func by a blank. We cannot
1618 * use QString::simplifyWhiteSpace() for this because this would also
1619 * simplify space that belongs to a string arguments that gdb sometimes
1620 * prints in the argument lists of the function.
1622 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1623 int nl = 0;
1624 while ((nl = func.find('\n', nl)) >= 0) {
1625 // search back to the beginning of the whitespace
1626 int startWhite = nl;
1627 do {
1628 --startWhite;
1629 } while (isspace(func[startWhite].latin1()));
1630 startWhite++;
1631 // search forward to the end of the whitespace
1632 do {
1633 nl++;
1634 } while (isspace(func[nl].latin1()));
1635 // replace
1636 func.replace(startWhite, nl-startWhite, " ");
1637 /* continue searching for more \n's at this place: */
1638 nl = startWhite+1;
1644 * Parses a stack frame including its frame number
1646 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1647 QString& file, int& lineNo, DbgAddr& address)
1649 // Example:
1650 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1651 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1653 // must start with a hash mark followed by number
1654 // or with "Breakpoint " followed by number and comma
1655 if (s[0] == '#') {
1656 if (!isdigit(s[1]))
1657 return false;
1658 s++; /* skip the hash mark */
1659 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1660 if (!isdigit(s[11]))
1661 return false;
1662 s += 11; /* skip "Breakpoint" */
1663 } else
1664 return false;
1666 // frame number
1667 frameNo = atoi(s);
1668 while (isdigit(*s))
1669 s++;
1670 // space and comma
1671 while (isspace(*s) || *s == ',')
1672 s++;
1673 parseFrameInfo(s, func, file, lineNo, address);
1674 return true;
1677 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1679 QString func, file;
1680 int lineNo, frameNo;
1681 DbgAddr address;
1683 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1684 StackFrame* frm = new StackFrame;
1685 frm->frameNo = frameNo;
1686 frm->fileName = file;
1687 frm->lineNo = lineNo;
1688 frm->address = address;
1689 frm->var = new VarTree(func, VarTree::NKplain);
1690 stack.append(frm);
1694 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1695 QString& file, int& lineNo, DbgAddr& address)
1697 QString func;
1698 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1702 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1704 // skip first line, which is the headline
1705 const char* p = strchr(output, '\n');
1706 if (p == 0)
1707 return false;
1708 p++;
1709 if (*p == '\0')
1710 return false;
1712 // split up a line
1713 QString location;
1714 QString address;
1715 int hits = 0;
1716 uint ignoreCount = 0;
1717 QString condition;
1718 const char* end;
1719 char* dummy;
1720 while (*p != '\0') {
1721 // get Num
1722 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1723 p = dummy;
1724 // get Type
1725 while (isspace(*p))
1726 p++;
1727 Breakpoint::Type bpType = Breakpoint::breakpoint;
1728 if (strncmp(p, "breakpoint", 10) == 0) {
1729 p += 10;
1730 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1731 bpType = Breakpoint::watchpoint;
1732 p += 13;
1733 } else if (strncmp(p, "watchpoint", 10) == 0) {
1734 bpType = Breakpoint::watchpoint;
1735 p += 10;
1737 while (isspace(*p))
1738 p++;
1739 if (*p == '\0')
1740 break;
1741 // get Disp
1742 char disp = *p++;
1743 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1744 p++;
1745 while (isspace(*p))
1746 p++;
1747 if (*p == '\0')
1748 break;
1749 // get Enb
1750 char enable = *p++;
1751 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1752 p++;
1753 while (isspace(*p))
1754 p++;
1755 if (*p == '\0')
1756 break;
1757 // the address, if present
1758 if (bpType == Breakpoint::breakpoint &&
1759 strncmp(p, "0x", 2) == 0)
1761 const char* start = p;
1762 while (*p != '\0' && !isspace(*p))
1763 p++;
1764 address = FROM_LATIN1(start, p-start);
1765 while (isspace(*p) && *p != '\n')
1766 p++;
1767 if (*p == '\0')
1768 break;
1769 } else {
1770 address = QString();
1772 // remainder is location, hit and ignore count, condition
1773 hits = 0;
1774 ignoreCount = 0;
1775 condition = QString();
1776 end = strchr(p, '\n');
1777 if (end == 0) {
1778 location = p;
1779 p += location.length();
1780 } else {
1781 location = FROM_LATIN1(p, end-p).stripWhiteSpace();
1782 p = end+1; /* skip over \n */
1785 // may be continued in next line
1786 while (isspace(*p)) { /* p points to beginning of line */
1787 // skip white space at beginning of line
1788 while (isspace(*p))
1789 p++;
1791 // seek end of line
1792 end = strchr(p, '\n');
1793 if (end == 0)
1794 end = p+strlen(p);
1796 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1797 // extract the hit count
1798 p += 22;
1799 hits = strtol(p, &dummy, 10);
1800 TRACE(QString().sprintf("hit count %d", hits));
1801 } else if (strncmp(p, "stop only if ", 13) == 0) {
1802 // extract condition
1803 p += 13;
1804 condition = FROM_LATIN1(p, end-p).stripWhiteSpace();
1805 TRACE("condition: "+condition);
1806 } else if (strncmp(p, "ignore next ", 12) == 0) {
1807 // extract ignore count
1808 p += 12;
1809 ignoreCount = strtol(p, &dummy, 10);
1810 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1811 } else {
1812 // indeed a continuation
1813 location += " " + FROM_LATIN1(p, end-p).stripWhiteSpace();
1815 p = end;
1816 if (*p != '\0')
1817 p++; /* skip '\n' */
1819 Breakpoint* bp = new Breakpoint;
1820 bp->id = bpNum;
1821 bp->type = bpType;
1822 bp->temporary = disp == 'd';
1823 bp->enabled = enable == 'y';
1824 bp->location = location;
1825 bp->address = address;
1826 bp->hitCount = hits;
1827 bp->ignoreCount = ignoreCount;
1828 bp->condition = condition;
1829 brks.append(bp);
1831 return true;
1834 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1836 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1837 // no threads
1838 return true;
1841 int id;
1842 QString systag;
1843 QString func, file;
1844 int lineNo;
1845 DbgAddr address;
1847 const char* p = output;
1848 while (*p != '\0') {
1849 // seach look for thread id, watching out for the focus indicator
1850 bool hasFocus = false;
1851 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1852 p++;
1853 if (*p == '*') {
1854 hasFocus = true;
1855 p++;
1856 // there follows only whitespace
1858 char* end;
1859 id = strtol(p, &end, 10);
1860 if (p == end) {
1861 // syntax error: no number found; bail out
1862 return true;
1864 p = end;
1866 // skip space
1867 while (isspace(*p))
1868 p++;
1871 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1873 end = strstr(p, " ");
1874 if (end == 0) {
1875 // syntax error; bail out
1876 return true;
1878 systag = FROM_LATIN1(p, end-p);
1879 p = end+2;
1882 * Now follows a standard stack frame. Sometimes, however, gdb
1883 * catches a thread at an instant where it doesn't have a stack.
1885 if (strncmp(p, "[No stack.]", 11) != 0) {
1886 ::parseFrameInfo(p, func, file, lineNo, address);
1887 } else {
1888 func = "[No stack]";
1889 file = QString();
1890 lineNo = -1;
1891 address = QString();
1892 p += 11; /* \n is skipped above */
1895 ThreadInfo* thr = new ThreadInfo;
1896 thr->id = id;
1897 thr->threadName = systag;
1898 thr->hasFocus = hasFocus;
1899 thr->function = func;
1900 thr->fileName = file;
1901 thr->lineNo = lineNo;
1902 thr->address = address;
1903 threads.append(thr);
1905 return true;
1908 static bool parseNewBreakpoint(const char* o, int& id,
1909 QString& file, int& lineNo, QString& address);
1910 static bool parseNewWatchpoint(const char* o, int& id,
1911 QString& expr);
1913 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1914 QString& file, int& lineNo, QString& address)
1916 const char* o = output;
1917 // skip lines of that begin with "(Cannot find"
1918 while (strncmp(o, "(Cannot find", 12) == 0) {
1919 o = strchr(o, '\n');
1920 if (o == 0)
1921 return false;
1922 o++; /* skip newline */
1925 if (strncmp(o, "Breakpoint ", 11) == 0) {
1926 output += 11; /* skip "Breakpoint " */
1927 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1928 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1929 output += 20;
1930 return ::parseNewWatchpoint(output, id, address);
1931 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1932 output += 11;
1933 return ::parseNewWatchpoint(output, id, address);
1935 return false;
1938 static bool parseNewBreakpoint(const char* o, int& id,
1939 QString& file, int& lineNo, QString& address)
1941 // breakpoint id
1942 char* p;
1943 id = strtoul(o, &p, 10);
1944 if (p == o)
1945 return false;
1947 // check for the address
1948 if (strncmp(p, " at 0x", 6) == 0) {
1949 char* start = p+4; /* skip " at ", but not 0x */
1950 p += 6;
1951 while (isxdigit(*p))
1952 ++p;
1953 address = FROM_LATIN1(start, p-start);
1956 // file name
1957 char* fileStart = strstr(p, "file ");
1958 if (fileStart == 0)
1959 return !address.isEmpty(); /* parse error only if there's no address */
1960 fileStart += 5;
1962 // line number
1963 char* numStart = strstr(fileStart, ", line ");
1964 QString fileName = FROM_LATIN1(fileStart, numStart-fileStart);
1965 numStart += 7;
1966 int line = strtoul(numStart, &p, 10);
1967 if (numStart == p)
1968 return false;
1970 file = fileName;
1971 lineNo = line-1; /* zero-based! */
1972 return true;
1975 static bool parseNewWatchpoint(const char* o, int& id,
1976 QString& expr)
1978 // watchpoint id
1979 char* p;
1980 id = strtoul(o, &p, 10);
1981 if (p == o)
1982 return false;
1984 if (strncmp(p, ": ", 2) != 0)
1985 return false;
1986 p += 2;
1988 // all the rest on the line is the expression
1989 expr = FROM_LATIN1(p, strlen(p)).stripWhiteSpace();
1990 return true;
1993 void GdbDriver::parseLocals(const char* output, QList<VarTree>& newVars)
1995 // check for possible error conditions
1996 if (strncmp(output, "No symbol table", 15) == 0)
1998 return;
2001 while (*output != '\0') {
2002 while (isspace(*output))
2003 output++;
2004 if (*output == '\0')
2005 break;
2006 // skip occurrences of "No locals" and "No args"
2007 if (strncmp(output, "No locals", 9) == 0 ||
2008 strncmp(output, "No arguments", 12) == 0)
2010 output = strchr(output, '\n');
2011 if (output == 0) {
2012 break;
2014 continue;
2017 VarTree* variable = parseVar(output);
2018 if (variable == 0) {
2019 break;
2021 // do not add duplicates
2022 for (VarTree* o = newVars.first(); o != 0; o = newVars.next()) {
2023 if (o->getText() == variable->getText()) {
2024 delete variable;
2025 goto skipDuplicate;
2028 newVars.append(variable);
2029 skipDuplicate:;
2033 bool GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue, VarTree*& var)
2035 // check for error conditions
2036 if (parseErrorMessage(output, var, wantErrorValue))
2038 return false;
2039 } else {
2040 // parse the variable
2041 var = parseVar(output);
2042 return true;
2046 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2048 bool isGood = false;
2049 message = QString(output).simplifyWhiteSpace();
2050 if (message.isEmpty()) {
2051 message = i18n("New working directory: ") + m_programWD;
2052 isGood = true;
2054 return isGood;
2057 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2059 message = output;
2061 m_haveCoreFile = false;
2064 * Lines starting with the following do not indicate errors:
2065 * Using host libthread_db
2066 * (no debugging symbols found)
2068 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2069 strncmp(output, "(no debugging symbols found)", 28) == 0)
2071 // this line is good, go to the next one
2072 const char* end = strchr(output, '\n');
2073 if (end == 0)
2074 output += strlen(output);
2075 else
2076 output = end+1;
2080 * If we've parsed all lines, there was no error.
2082 return output[0] == '\0';
2085 bool GdbDriver::parseCoreFile(const char* output)
2087 // if command succeeded, gdb emits a line starting with "#0 "
2088 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2089 return m_haveCoreFile;
2092 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2094 // optionally: "program changed, rereading symbols",
2095 // followed by:
2096 // "Program exited normally"
2097 // "Program terminated with wignal SIGSEGV"
2098 // "Program received signal SIGINT" or other signal
2099 // "Breakpoint..."
2101 // go through the output, line by line, checking what we have
2102 const char* start = output - 1;
2103 uint flags = SFprogramActive;
2104 message = QString();
2105 do {
2106 start++; /* skip '\n' */
2108 if (strncmp(start, "Program ", 8) == 0 ||
2109 strncmp(start, "ptrace: ", 8) == 0) {
2111 * When we receive a signal, the program remains active.
2113 * Special: If we "stopped" in a corefile, the string "Program
2114 * terminated with signal"... is displayed. (Normally, we see
2115 * "Program received signal"... when a signal happens.)
2117 if (strncmp(start, "Program exited", 14) == 0 ||
2118 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2119 strncmp(start, "ptrace: ", 8) == 0)
2121 flags &= ~SFprogramActive;
2124 // set message
2125 const char* endOfMessage = strchr(start, '\n');
2126 if (endOfMessage == 0)
2127 endOfMessage = start + strlen(start);
2128 message = FROM_LATIN1(start, endOfMessage-start);
2129 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2131 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2132 * that it stopped at a temporary breakpoint).
2134 flags |= SFrefreshBreak;
2135 } else if (strstr(start, "re-reading symbols.") != 0) {
2136 flags |= SFrefreshSource;
2139 // next line, please
2140 start = strchr(start, '\n');
2141 } while (start != 0);
2144 * Gdb only notices when new threads have appeared, but not when a
2145 * thread finishes. So we always have to assume that the list of
2146 * threads has changed.
2148 flags |= SFrefreshThreads;
2150 return flags;
2153 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2155 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2156 return;
2158 // parse the table of shared libraries
2160 // strip off head line
2161 output = strchr(output, '\n');
2162 if (output == 0)
2163 return;
2164 output++; /* skip '\n' */
2165 QString shlibName;
2166 while (*output != '\0') {
2167 // format of a line is
2168 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2169 // 3 blocks of non-space followed by space
2170 for (int i = 0; *output != '\0' && i < 3; i++) {
2171 while (*output != '\0' && !isspace(*output)) { /* non-space */
2172 output++;
2174 while (isspace(*output)) { /* space */
2175 output++;
2178 if (*output == '\0')
2179 return;
2180 const char* start = output;
2181 output = strchr(output, '\n');
2182 if (output == 0)
2183 output = start + strlen(start);
2184 shlibName = FROM_LATIN1(start, output-start);
2185 if (*output != '\0')
2186 output++;
2187 shlibs.append(shlibName);
2188 TRACE("found shared lib " + shlibName);
2192 bool GdbDriver::parseFindType(const char* output, QString& type)
2194 if (strncmp(output, "type = ", 7) != 0)
2195 return false;
2198 * Everything else is the type. We strip off any leading "const" and any
2199 * trailing "&" on the grounds that neither affects the decoding of the
2200 * object. We also strip off all white-space from the type.
2202 output += 7;
2203 if (strncmp(output, "const ", 6) == 0)
2204 output += 6;
2205 type = output;
2206 type.replace(QRegExp("\\s+"), "");
2207 if (type.endsWith("&"))
2208 type.truncate(type.length() - 1);
2209 return true;
2212 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2214 if (strncmp(output, "The program has no registers now", 32) == 0) {
2215 return;
2218 QString regName;
2219 QString value;
2221 // parse register values
2222 while (*output != '\0')
2224 // skip space at the start of the line
2225 while (isspace(*output))
2226 output++;
2228 // register name
2229 const char* start = output;
2230 while (*output != '\0' && !isspace(*output))
2231 output++;
2232 if (*output == '\0')
2233 break;
2234 regName = FROM_LATIN1(start, output-start);
2236 // skip space
2237 while (isspace(*output))
2238 output++;
2240 RegisterInfo* reg = new RegisterInfo;
2241 reg->regName = regName;
2244 * If we find a brace now, this is a vector register. We look for
2245 * the closing brace and treat the result as cooked value.
2247 if (*output == '{')
2249 start = output;
2250 skipNested(output, '{', '}');
2251 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2252 // skip space, but not the end of line
2253 while (isspace(*output) && *output != '\n')
2254 output++;
2255 // get rid of the braces at the begining and the end
2256 value.remove(0, 1);
2257 if (value[value.length()-1] == '}') {
2258 value = value.left(value.length()-1);
2260 // gdb 5.3 doesn't print a separate set of raw values
2261 if (*output == '{') {
2262 // another set of vector follows
2263 // what we have so far is the raw value
2264 reg->rawValue = value;
2266 start = output;
2267 skipNested(output, '{', '}');
2268 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2269 } else {
2270 // for gdb 5.3
2271 // find first type that does not have an array, this is the RAW value
2272 const char* end=start;
2273 findEnd(end);
2274 const char* cur=start;
2275 while (cur<end) {
2276 while (*cur != '=' && cur<end)
2277 cur++;
2278 cur++;
2279 while (isspace(*cur) && cur<end)
2280 cur++;
2281 if (isNumberish(*cur)) {
2282 end=cur;
2283 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2284 end++;
2285 QString rawValue = FROM_LATIN1(cur, end-cur).simplifyWhiteSpace();
2286 reg->rawValue = rawValue;
2288 if (rawValue.left(2)=="0x") {
2289 // ok we have a raw value, now get it's type
2290 end=cur-1;
2291 while (isspace(*end) || *end=='=') end--;
2292 end++;
2293 cur=end-1;
2294 while (*cur!='{' && *cur!=' ')
2295 cur--;
2296 cur++;
2297 reg->type=FROM_LATIN1(cur, end-cur);
2300 // end while loop
2301 cur=end;
2304 // skip to the end of line
2305 while (*output != '\0' && *output != '\n')
2306 output++;
2307 // get rid of the braces at the begining and the end
2308 value.remove(0, 1);
2309 if (value[value.length()-1] == '}') {
2310 value = value.left(value.length()-1);
2313 reg->cookedValue = value;
2315 else
2317 // the rest of the line is the register value
2318 start = output;
2319 output = strchr(output,'\n');
2320 if (output == 0)
2321 output = start + strlen(start);
2322 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2325 * We split the raw from the cooked values.
2326 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2327 * Here, the cooked value comes first, and the raw value is in
2328 * the second part.
2330 int pos = value.find(" (raw ");
2331 if (pos >= 0)
2333 reg->cookedValue = value.left(pos);
2334 reg->rawValue = value.mid(pos+6);
2335 if (reg->rawValue.right(1) == ")") // remove closing bracket
2336 reg->rawValue.truncate(reg->rawValue.length()-1);
2338 else
2341 * In other cases we split off the first token (separated by
2342 * whitespace). It is the raw value. The remainder of the line
2343 * is the cooked value.
2345 int pos = value.find(' ');
2346 if (pos < 0) {
2347 reg->rawValue = value;
2348 reg->cookedValue = QString();
2349 } else {
2350 reg->rawValue = value.left(pos);
2351 reg->cookedValue = value.mid(pos+1);
2355 if (*output != '\0')
2356 output++; /* skip '\n' */
2358 regs.append(reg);
2362 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2364 // "is at address" or "starts at address"
2365 const char* start = strstr(output, "s at address ");
2366 if (start == 0)
2367 return false;
2369 start += 13;
2370 const char* p = start;
2371 while (*p != '\0' && !isspace(*p))
2372 p++;
2373 addrFrom = FROM_LATIN1(start, p-start);
2375 start = strstr(p, "and ends at ");
2376 if (start == 0) {
2377 addrTo = addrFrom;
2378 return true;
2381 start += 12;
2382 p = start;
2383 while (*p != '\0' && !isspace(*p))
2384 p++;
2385 addrTo = FROM_LATIN1(start, p-start);
2387 return true;
2390 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2392 code.clear();
2394 if (strncmp(output, "Dump of assembler", 17) != 0) {
2395 // error message?
2396 DisassembledCode c;
2397 c.code = output;
2398 code.append(new DisassembledCode(c));
2399 return;
2402 // remove first line
2403 const char* p = strchr(output, '\n');
2404 if (p == 0)
2405 return; /* not a regular output */
2407 p++;
2409 // remove last line
2410 const char* end = strstr(output, "End of assembler");
2411 if (end == 0)
2412 end = p + strlen(p);
2414 DbgAddr address;
2416 // remove function offsets from the lines
2417 while (p != end)
2419 const char* start = p;
2420 // address
2421 while (p != end && !isspace(*p))
2422 p++;
2423 address = FROM_LATIN1(start, p-start);
2425 // function name (enclosed in '<>', followed by ':')
2426 while (p != end && *p != '<')
2427 p++;
2428 if (*p == '<')
2429 skipNestedAngles(p);
2430 if (*p == ':')
2431 p++;
2433 // space until code
2434 while (p != end && isspace(*p))
2435 p++;
2437 // code until end of line
2438 start = p;
2439 while (p != end && *p != '\n')
2440 p++;
2441 if (p != end) /* include '\n' */
2442 p++;
2444 DisassembledCode* c = new DisassembledCode;
2445 c->address = address;
2446 c->code = FROM_LATIN1(start, p-start);
2447 code.append(c);
2451 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2453 if (isErrorExpr(output)) {
2454 // error; strip space
2455 QString msg = output;
2456 return msg.stripWhiteSpace();
2459 const char* p = output; /* save typing */
2460 DbgAddr addr;
2461 QString dump;
2463 // the address
2464 while (*p != 0) {
2465 const char* start = p;
2466 while (*p != '\0' && *p != ':' && !isspace(*p))
2467 p++;
2468 addr = FROM_LATIN1(start, p-start);
2469 if (*p != ':') {
2470 // parse function offset
2471 while (isspace(*p))
2472 p++;
2473 start = p;
2474 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2475 p++;
2476 addr.fnoffs = FROM_LATIN1(start, p-start);
2478 if (*p == ':')
2479 p++;
2480 // skip space; this may skip a new-line char!
2481 while (isspace(*p))
2482 p++;
2483 // everything to the end of the line is the memory dump
2484 const char* end = strchr(p, '\n');
2485 if (end != 0) {
2486 dump = FROM_LATIN1(p, end-p);
2487 p = end+1;
2488 } else {
2489 dump = FROM_LATIN1(p, strlen(p));
2490 p += strlen(p);
2492 MemoryDump* md = new MemoryDump;
2493 md->address = addr;
2494 md->dump = dump;
2495 memdump.append(md);
2498 return QString();
2501 QString GdbDriver::editableValue(VarTree* value)
2503 const char* s = value->m_value.latin1();
2505 // if the variable is a pointer value that contains a cast,
2506 // remove the cast
2507 if (*s == '(') {
2508 skipNested(s, '(', ')');
2509 // skip space
2510 while (isspace(*s))
2511 ++s;
2514 repeat:
2515 const char* start = s;
2517 if (strncmp(s, "0x", 2) == 0)
2519 s += 2;
2520 while (isxdigit(*s))
2521 ++s;
2524 * What we saw so far might have been a reference. If so, edit the
2525 * referenced value. Otherwise, edit the pointer.
2527 if (*s == ':') {
2528 // a reference
2529 ++s;
2530 goto repeat;
2532 // a pointer
2533 // if it's a pointer to a string, remove the string
2534 const char* end = s;
2535 while (isspace(*s))
2536 ++s;
2537 if (*s == '"') {
2538 // a string
2539 return FROM_LATIN1(start, end-start);
2540 } else {
2541 // other pointer
2542 return FROM_LATIN1(start, strlen(start));
2546 // else leave it unchanged (or stripped of the reference preamble)
2547 return s;
2550 QString GdbDriver::parseSetVariable(const char* output)
2552 // if there is any output, it is an error message
2553 QString msg = output;
2554 return msg.stripWhiteSpace();
2558 #include "gdbdriver.moc"