Extend the widechar test by a structure with wchar_t* members.
[kdbg.git] / kdbg / gdbdriver.cpp
blob58016268c27c4bf8806a98f01a9cce38c6c2a2e2
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 { DCprintWChar, "print ($s=%s)?*$s@wcslen($s):0x0\n", GdbCmdInfo::argString },
119 { DCsetvariable, "set variable %s=%s\n", GdbCmdInfo::argString2 },
122 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
123 #define MAX_FMTLEN 200
125 GdbDriver::GdbDriver() :
126 DebuggerDriver(),
127 m_gdbMajor(4), m_gdbMinor(16)
129 strcpy(m_prompt, PROMPT);
130 m_promptMinLen = PROMPT_LEN;
131 m_promptLastChar = PROMPT_LAST_CHAR;
133 #ifndef NDEBUG
134 // check command info array
135 char* perc;
136 for (int i = 0; i < NUM_CMDS; i++) {
137 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
138 assert(i == cmds[i].cmd);
139 // a format string must be associated
140 assert(cmds[i].fmt != 0);
141 assert(strlen(cmds[i].fmt) <= MAX_FMTLEN);
142 // format string must match arg specification
143 switch (cmds[i].argsNeeded) {
144 case GdbCmdInfo::argNone:
145 assert(strchr(cmds[i].fmt, '%') == 0);
146 break;
147 case GdbCmdInfo::argString:
148 perc = strchr(cmds[i].fmt, '%');
149 assert(perc != 0 && perc[1] == 's');
150 assert(strchr(perc+2, '%') == 0);
151 break;
152 case GdbCmdInfo::argNum:
153 perc = strchr(cmds[i].fmt, '%');
154 assert(perc != 0 && perc[1] == 'd');
155 assert(strchr(perc+2, '%') == 0);
156 break;
157 case GdbCmdInfo::argStringNum:
158 perc = strchr(cmds[i].fmt, '%');
159 assert(perc != 0 && perc[1] == 's');
160 perc = strchr(perc+2, '%');
161 assert(perc != 0 && perc[1] == 'd');
162 assert(strchr(perc+2, '%') == 0);
163 break;
164 case GdbCmdInfo::argNumString:
165 perc = strchr(cmds[i].fmt, '%');
166 assert(perc != 0 && perc[1] == 'd');
167 perc = strchr(perc+2, '%');
168 assert(perc != 0 && perc[1] == 's');
169 assert(strchr(perc+2, '%') == 0);
170 break;
171 case GdbCmdInfo::argString2:
172 perc = strchr(cmds[i].fmt, '%');
173 assert(perc != 0 && perc[1] == 's');
174 perc = strchr(perc+2, '%');
175 assert(perc != 0 && perc[1] == 's');
176 assert(strchr(perc+2, '%') == 0);
177 break;
178 case GdbCmdInfo::argNum2:
179 perc = strchr(cmds[i].fmt, '%');
180 assert(perc != 0 && perc[1] == 'd');
181 perc = strchr(perc+2, '%');
182 assert(perc != 0 && perc[1] == 'd');
183 assert(strchr(perc+2, '%') == 0);
184 break;
187 assert(strlen(printQStringStructFmt) <= MAX_FMTLEN);
188 #endif
191 GdbDriver::~GdbDriver()
196 QString GdbDriver::driverName() const
198 return "GDB";
201 QString GdbDriver::defaultGdb()
203 return
204 "gdb"
205 " --fullname" /* to get standard file names each time the prog stops */
206 " --nx"; /* do not execute initialization files */
209 QString GdbDriver::defaultInvocation() const
211 if (m_defaultCmd.isEmpty()) {
212 return defaultGdb();
213 } else {
214 return m_defaultCmd;
218 QStringList GdbDriver::boolOptionList() const
220 // no options
221 return QStringList();
224 bool GdbDriver::startup(QString cmdStr)
226 if (!DebuggerDriver::startup(cmdStr))
227 return false;
229 static const char gdbInitialize[] =
231 * Work around buggy gdbs that do command line editing even if they
232 * are not on a tty. The readline library echos every command back
233 * in this case, which is confusing for us.
235 "set editing off\n"
236 "set confirm off\n"
237 "set print static-members off\n"
238 "set print asm-demangle on\n"
240 * Don't assume that program functions invoked from a watch expression
241 * always succeed.
243 "set unwindonsignal on\n"
245 * Write a short macro that prints all locals: local variables and
246 * function arguments.
248 "define kdbg__alllocals\n"
249 "info locals\n" /* local vars supersede args with same name */
250 "info args\n" /* therefore, arguments must come last */
251 "end\n"
253 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
255 "define kdbg_infolinemain\n"
256 "list\n"
257 "info line\n"
258 "end\n"
259 // change prompt string and synchronize with gdb
260 "set prompt " PROMPT "\n"
263 executeCmdString(DCinitialize, gdbInitialize, false);
265 // assume that QString::null is ok
266 cmds[DCprintQStringStruct].fmt = printQStringStructFmt;
268 return true;
271 void GdbDriver::commandFinished(CmdQueueItem* cmd)
273 // command string must be committed
274 if (!cmd->m_committed) {
275 // not commited!
276 TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
277 cmd->m_cmdString)));
278 return;
281 switch (cmd->m_cmd) {
282 case DCinitialize:
283 // get version number from preamble
285 int len;
286 QRegExp GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
287 int offset = GDBVersion.match(m_output, 0, &len);
288 if (offset >= 0) {
289 char* start = m_output + offset + 5; // skip "\nGDB "
290 char* end;
291 m_gdbMajor = strtol(start, &end, 10);
292 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
293 if (start == end) {
294 // nothing was parsed
295 m_gdbMajor = 4;
296 m_gdbMinor = 16;
298 } else {
299 // assume some default version (what would make sense?)
300 m_gdbMajor = 4;
301 m_gdbMinor = 16;
303 // use a feasible core-file command
304 if (m_gdbMajor > 4 || (m_gdbMajor == 4 && m_gdbMinor >= 16)) {
305 cmds[DCcorefile].fmt = "target core %s\n";
306 } else {
307 cmds[DCcorefile].fmt = "core-file %s\n";
310 break;
311 default:;
314 /* ok, the command is ready */
315 emit commandReceived(cmd, m_output);
317 switch (cmd->m_cmd) {
318 case DCcorefile:
319 case DCinfolinemain:
320 case DCframe:
321 case DCattach:
322 case DCrun:
323 case DCcont:
324 case DCstep:
325 case DCstepi:
326 case DCnext:
327 case DCnexti:
328 case DCfinish:
329 case DCuntil:
330 parseMarker();
331 default:;
336 * The --fullname option makes gdb send a special normalized sequence print
337 * each time the program stops and at some other points. The sequence has
338 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
340 void GdbDriver::parseMarker()
342 char* startMarker = strstr(m_output, "\032\032");
343 if (startMarker == 0)
344 return;
346 // extract the marker
347 startMarker += 2;
348 TRACE(QString("found marker: ") + startMarker);
349 char* endMarker = strchr(startMarker, '\n');
350 if (endMarker == 0)
351 return;
353 *endMarker = '\0';
355 // extract filename and line number
356 static QRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
358 int len;
359 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
360 if (lineNoStart >= 0) {
361 int lineNo = atoi(startMarker + lineNoStart+1);
363 // get address
364 const char* addrStart = startMarker + lineNoStart + len - 2;
365 DbgAddr address = QString(addrStart).stripWhiteSpace();
367 // now show the window
368 startMarker[lineNoStart] = '\0'; /* split off file name */
369 emit activateFileLine(startMarker, lineNo-1, address);
375 * Escapes characters that might lead to problems when they appear on gdb's
376 * command line.
378 static void normalizeStringArg(QString& arg)
381 * Remove trailing backslashes. This approach is a little simplistic,
382 * but we know that there is at the moment no case where a trailing
383 * backslash would make sense.
385 while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
386 arg = arg.left(arg.length()-1);
391 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg)
393 assert(cmd >= 0 && cmd < NUM_CMDS);
394 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);
396 normalizeStringArg(strArg);
398 if (cmd == DCcd) {
399 // need the working directory when parsing the output
400 m_programWD = strArg;
401 } else if (cmd == DCsetargs) {
402 // attach saved redirection
403 strArg += m_redirect;
406 SIZED_QString(cmdString, MAX_FMTLEN+strArg.length());
407 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
408 return cmdString;
411 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
413 assert(cmd >= 0 && cmd < NUM_CMDS);
414 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);
416 SIZED_QString(cmdString, MAX_FMTLEN+30);
418 cmdString.sprintf(cmds[cmd].fmt, intArg);
419 return cmdString;
422 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
424 assert(cmd >= 0 && cmd < NUM_CMDS);
425 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
426 cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
427 cmd == DCexamine ||
428 cmd == DCtty);
430 normalizeStringArg(strArg);
432 SIZED_QString(cmdString, MAX_FMTLEN+30+strArg.length());
434 if (cmd == DCtty)
437 * intArg specifies which channels should be redirected to
438 * /dev/null. It is a value or'ed together from RDNstdin,
439 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
440 * command.
442 * Note: We rely on that after the DCtty a DCsetargs will follow,
443 * which will ultimately apply the redirection.
445 static const char* const runRedir[8] = {
447 " </dev/null",
448 " >/dev/null",
449 " </dev/null >/dev/null",
450 " 2>/dev/null",
451 " </dev/null 2>/dev/null",
452 " >/dev/null 2>&1",
453 " </dev/null >/dev/null 2>&1"
455 if (strArg.isEmpty())
456 intArg = 7; /* failsafe if no tty */
457 m_redirect = runRedir[intArg & 7];
459 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
462 if (cmd == DCexamine) {
463 // make a format specifier from the intArg
464 static const char size[16] = {
465 '\0', 'b', 'h', 'w', 'g'
467 static const char format[16] = {
468 '\0', 'x', 'd', 'u', 'o', 't',
469 'a', 'c', 'f', 's', 'i'
471 assert(MDTsizemask == 0xf); /* lowest 4 bits */
472 assert(MDTformatmask == 0xf0); /* next 4 bits */
473 int count = 16; /* number of entities to print */
474 char sizeSpec = size[intArg & MDTsizemask];
475 char formatSpec = format[(intArg & MDTformatmask) >> 4];
476 assert(sizeSpec != '\0');
477 assert(formatSpec != '\0');
478 // adjust count such that 16 lines are printed
479 switch (intArg & MDTformatmask) {
480 case MDTstring: case MDTinsn:
481 break; /* no modification needed */
482 default:
483 // all cases drop through:
484 switch (intArg & MDTsizemask) {
485 case MDTbyte:
486 case MDThalfword:
487 count *= 2;
488 case MDTword:
489 count *= 2;
490 case MDTgiantword:
491 count *= 2;
493 break;
495 QString spec;
496 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
498 return makeCmdString(DCexamine, spec, strArg);
501 if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
503 // line numbers are zero-based
504 if (cmd == DCuntil || cmd == DCbreakline ||
505 cmd == DCtbreakline || cmd == DCinfoline)
507 intArg++;
509 if (cmd == DCinfoline)
511 // must split off file name part
512 int slash = strArg.findRev('/');
513 if (slash >= 0)
514 strArg = strArg.right(strArg.length()-slash-1);
516 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
518 else
520 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
522 return cmdString;
525 QString GdbDriver::makeCmdString(DbgCommand cmd, QString strArg1, QString strArg2)
527 assert(cmd >= 0 && cmd < NUM_CMDS);
528 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);
530 normalizeStringArg(strArg1);
531 normalizeStringArg(strArg2);
533 SIZED_QString(cmdString, MAX_FMTLEN+strArg1.length()+strArg2.length());
534 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
535 return cmdString;
538 QString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
540 assert(cmd >= 0 && cmd < NUM_CMDS);
541 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);
543 SIZED_QString(cmdString, MAX_FMTLEN+60);
544 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
545 return cmdString;
548 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
550 assert(cmd >= 0 && cmd < NUM_CMDS);
551 assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);
553 if (cmd == DCrun) {
554 m_haveCoreFile = false;
557 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
560 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg,
561 bool clearLow)
563 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
566 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
567 bool clearLow)
570 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
573 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
574 bool clearLow)
576 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
579 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
580 bool clearLow)
582 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
585 CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
586 bool clearLow)
588 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
591 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
593 return queueCmdString(cmd, cmds[cmd].fmt, mode);
596 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg,
597 QueueMode mode)
599 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
602 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
603 QueueMode mode)
605 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
608 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
609 QueueMode mode)
611 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
614 CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
615 QueueMode mode)
617 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
620 void GdbDriver::terminate()
622 kill(SIGTERM);
623 m_state = DSidle;
626 void GdbDriver::detachAndTerminate()
628 kill(SIGINT);
629 flushCommands();
630 executeCmdString(DCinitialize, "detach\nquit\n", true);
633 void GdbDriver::interruptInferior()
635 kill(SIGINT);
636 // remove accidentally queued commands
637 flushHiPriQueue();
640 static bool isErrorExpr(const char* output)
642 return
643 strncmp(output, "Cannot access memory at", 23) == 0 ||
644 strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
645 strncmp(output, "Attempt to take contents of ", 28) == 0 ||
646 strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
647 strncmp(output, "There is no member or method named", 34) == 0 ||
648 strncmp(output, "A parse error in expression", 27) == 0 ||
649 strncmp(output, "No symbol \"", 11) == 0 ||
650 strncmp(output, "Internal error: ", 16) == 0;
654 * Returns true if the output is an error message. If wantErrorValue is
655 * true, a new VarTree object is created and filled with the error message.
656 * If there are warnings, they are skipped and output points past the warnings
657 * on return (even if there \e are errors).
659 static bool parseErrorMessage(const char*& output,
660 VarTree*& variable, bool wantErrorValue)
662 // skip warnings
663 while (strncmp(output, "warning:", 8) == 0)
665 char* end = strchr(output+8, '\n');
666 if (end == 0)
667 output += strlen(output);
668 else
669 output = end+1;
672 if (isErrorExpr(output))
674 if (wantErrorValue) {
675 // put the error message as value in the variable
676 variable = new VarTree(QString(), VarTree::NKplain);
677 const char* endMsg = strchr(output, '\n');
678 if (endMsg == 0)
679 endMsg = output + strlen(output);
680 variable->m_value = FROM_LATIN1(output, endMsg-output);
681 } else {
682 variable = 0;
684 return true;
686 return false;
689 #if QT_VERSION >= 300
690 union Qt2QChar {
691 short s;
692 struct {
693 uchar row;
694 uchar cell;
695 } qch;
697 #endif
699 void GdbDriver::setPrintQStringDataCmd(const char* cmd)
701 // don't accept the command if it is empty
702 if (cmd == 0 || *cmd == '\0')
703 return;
704 assert(strlen(cmd) <= MAX_FMTLEN);
705 cmds[DCprintQStringStruct].fmt = cmd;
708 VarTree* GdbDriver::parseQCharArray(const char* output, bool wantErrorValue, bool qt3like)
710 VarTree* variable = 0;
713 * Parse off white space. gdb sometimes prints white space first if the
714 * printed array leaded to an error.
716 while (isspace(*output))
717 output++;
719 // special case: empty string (0 repetitions)
720 if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
722 variable = new VarTree(QString(), VarTree::NKplain);
723 variable->m_value = "\"\"";
724 return variable;
727 // check for error conditions
728 if (parseErrorMessage(output, variable, wantErrorValue))
729 return variable;
731 // parse the array
733 // find '='
734 const char* p = output;
735 p = strchr(p, '=');
736 if (p == 0) {
737 goto error;
739 // skip white space
740 do {
741 p++;
742 } while (isspace(*p));
744 if (*p == '{')
746 // this is the real data
747 p++; /* skip '{' */
749 // parse the array
750 QString result;
751 QString repeatCount;
752 enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
754 * A matrix for separators between the individual "things"
755 * that are added to the string. The first index is a bool,
756 * the second index is from the enum above.
758 static const char* separator[2][3] = {
759 { "\"", 0, ", \"" }, /* normal char is added */
760 { "'", "\", '", ", '" } /* repeated char is added */
763 while (isdigit(*p)) {
764 // parse a number
765 char* end;
766 unsigned short value = (unsigned short) strtoul(p, &end, 0);
767 if (end == p)
768 goto error; /* huh? no valid digits */
769 // skip separator and search for a repeat count
770 p = end;
771 while (isspace(*p) || *p == ',')
772 p++;
773 bool repeats = strncmp(p, "<repeats ", 9) == 0;
774 if (repeats) {
775 const char* start = p;
776 p = strchr(p+9, '>'); /* search end and advance */
777 if (p == 0)
778 goto error;
779 p++; /* skip '>' */
780 repeatCount = FROM_LATIN1(start, p-start);
781 while (isspace(*p) || *p == ',')
782 p++;
784 // p is now at the next char (or the end)
786 // interpret the value as a QChar
787 // TODO: make cross-architecture compatible
788 QChar ch;
789 if (qt3like) {
790 ch = QChar(value);
791 } else {
792 #if QT_VERSION < 300
793 (unsigned short&)ch = value;
794 #else
795 Qt2QChar c;
796 c.s = value;
797 ch.setRow(c.qch.row);
798 ch.setCell(c.qch.cell);
799 #endif
802 // escape a few frequently used characters
803 char escapeCode = '\0';
804 switch (ch.latin1()) {
805 case '\n': escapeCode = 'n'; break;
806 case '\r': escapeCode = 'r'; break;
807 case '\t': escapeCode = 't'; break;
808 case '\b': escapeCode = 'b'; break;
809 case '\"': escapeCode = '\"'; break;
810 case '\\': escapeCode = '\\'; break;
811 case '\0': if (value == 0) { escapeCode = '0'; } break;
814 // add separator
815 result += separator[repeats][lastThing];
816 // add char
817 if (escapeCode != '\0') {
818 result += '\\';
819 ch = escapeCode;
821 result += ch;
823 // fixup repeat count and lastThing
824 if (repeats) {
825 result += "' ";
826 result += repeatCount;
827 lastThing = wasRepeat;
828 } else {
829 lastThing = wasChar;
832 if (*p != '}')
833 goto error;
835 // closing quote
836 if (lastThing == wasChar)
837 result += "\"";
839 // assign the value
840 variable = new VarTree(QString(), VarTree::NKplain);
841 variable->m_value = result;
843 else if (strncmp(p, "true", 4) == 0)
845 variable = new VarTree(QString(), VarTree::NKplain);
846 variable->m_value = "QString::null";
848 else if (strncmp(p, "false", 5) == 0)
850 variable = new VarTree(QString(), VarTree::NKplain);
851 variable->m_value = "(null)";
853 else
854 goto error;
855 return variable;
857 error:
858 if (wantErrorValue) {
859 variable = new VarTree(QString(), VarTree::NKplain);
860 variable->m_value = "internal parse error";
862 return variable;
865 static VarTree* parseVar(const char*& s)
867 const char* p = s;
869 // skip whitespace
870 while (isspace(*p))
871 p++;
873 QString name;
874 VarTree::NameKind kind;
875 if (!parseName(p, name, kind)) {
876 return 0;
879 // go for '='
880 while (isspace(*p))
881 p++;
882 if (*p != '=') {
883 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name));
884 return 0;
886 // skip the '=' and more whitespace
887 p++;
888 while (isspace(*p))
889 p++;
891 VarTree* variable = new VarTree(name, kind);
892 variable->setDeleteChildren(true);
894 if (!parseValue(p, variable)) {
895 delete variable;
896 return 0;
898 s = p;
899 return variable;
902 static void skipNested(const char*& s, char opening, char closing)
904 const char* p = s;
906 // parse a nested type
907 int nest = 1;
908 p++;
910 * Search for next matching `closing' char, skipping nested pairs of
911 * `opening' and `closing'.
913 while (*p && nest > 0) {
914 if (*p == opening) {
915 nest++;
916 } else if (*p == closing) {
917 nest--;
919 p++;
921 if (nest != 0) {
922 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
924 s = p;
928 * This function skips text that is delimited by nested angle bracktes, '<>'.
929 * A complication arises because the delimited text can contain the names of
930 * operator<<, operator>>, operator<, and operator>, which have to be treated
931 * specially so that they do not count towards the nesting of '<>'.
932 * This function assumes that the delimited text does not contain strings.
934 static void skipNestedAngles(const char*& s)
936 const char* p = s;
938 int nest = 1;
939 p++; // skip the initial '<'
940 while (*p && nest > 0)
942 // Below we can check for p-s >= 9 instead of 8 because
943 // *s is '<' and cannot be part of "operator".
944 if (*p == '<')
946 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
947 if (p[1] == '<')
948 p++;
949 } else {
950 nest++;
953 else if (*p == '>')
955 if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
956 if (p[1] == '>')
957 p++;
958 } else {
959 nest--;
962 p++;
964 if (nest != 0) {
965 TRACE(QString().sprintf("parse error: mismatching <> at %-20.20s", s));
967 s = p;
971 * Find the end of line that is not inside braces
973 static void findEnd(const char*& s)
975 const char* p = s;
976 while (*p && *p!='\n') {
977 while (*p && *p!='\n' && *p!='{')
978 p++;
979 if (*p=='{') {
980 p++;
981 skipNested(p, '{', '}'); p--;
984 s = p;
987 static bool isNumberish(const char ch)
989 return (ch>='0' && ch<='9') || ch=='.' || ch=='x';
992 void skipString(const char*& p)
994 moreStrings:
995 // opening quote
996 char quote = *p++;
997 while (*p != quote) {
998 if (*p == '\\') {
999 // skip escaped character
1000 // no special treatment for octal values necessary
1001 p++;
1003 // simply return if no more characters
1004 if (*p == '\0')
1005 return;
1006 p++;
1008 // closing quote
1009 p++;
1011 * Strings can consist of several parts, some of which contain repeated
1012 * characters.
1014 if (quote == '\'') {
1015 // look ahaead for <repeats 123 times>
1016 const char* q = p+1;
1017 while (isspace(*q))
1018 q++;
1019 if (strncmp(q, "<repeats ", 9) == 0) {
1020 p = q+9;
1021 while (*p != '\0' && *p != '>')
1022 p++;
1023 if (*p != '\0') {
1024 p++; /* skip the '>' */
1028 // is the string continued?
1029 if (*p == ',') {
1030 // look ahead for another quote
1031 const char* q = p+1;
1032 while (isspace(*q))
1033 q++;
1034 if (*q == '"' || *q == '\'') {
1035 // yes!
1036 p = q;
1037 goto moreStrings;
1040 /* very long strings are followed by `...' */
1041 if (*p == '.' && p[1] == '.' && p[2] == '.') {
1042 p += 3;
1046 static void skipNestedWithString(const char*& s, char opening, char closing)
1048 const char* p = s;
1050 // parse a nested expression
1051 int nest = 1;
1052 p++;
1054 * Search for next matching `closing' char, skipping nested pairs of
1055 * `opening' and `closing' as well as strings.
1057 while (*p && nest > 0) {
1058 if (*p == opening) {
1059 nest++;
1060 } else if (*p == closing) {
1061 nest--;
1062 } else if (*p == '\'' || *p == '\"') {
1063 skipString(p);
1064 continue;
1066 p++;
1068 if (nest > 0) {
1069 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
1071 s = p;
1074 inline void skipName(const char*& p)
1076 // allow : (for enumeration values) and $ and . (for _vtbl.)
1077 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
1078 p++;
1081 static bool parseName(const char*& s, QString& name, VarTree::NameKind& kind)
1083 kind = VarTree::NKplain;
1085 const char* p = s;
1086 // examples of names:
1087 // name
1088 // <Object>
1089 // <string<a,b<c>,7> >
1091 if (*p == '<') {
1092 skipNestedAngles(p);
1093 name = FROM_LATIN1(s, p - s);
1094 kind = VarTree::NKtype;
1096 else
1098 // name, which might be "static"; allow dot for "_vtbl."
1099 skipName(p);
1100 if (p == s) {
1101 TRACE(QString().sprintf("parse error: not a name %-20.20s", s));
1102 return false;
1104 int len = p - s;
1105 if (len == 6 && strncmp(s, "static", 6) == 0) {
1106 kind = VarTree::NKstatic;
1108 // its a static variable, name comes now
1109 while (isspace(*p))
1110 p++;
1111 s = p;
1112 skipName(p);
1113 if (p == s) {
1114 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s));
1115 return false;
1117 len = p - s;
1119 name = FROM_LATIN1(s, len);
1121 // return the new position
1122 s = p;
1123 return true;
1126 static bool parseValue(const char*& s, VarTree* variable)
1128 variable->m_value = "";
1130 repeat:
1131 if (*s == '{') {
1132 // Sometimes we find the following output:
1133 // {<text variable, no debug info>} 0x40012000 <access>
1134 // {<data variable, no debug info>}
1135 // {<variable (not text or data), no debug info>}
1136 if (strncmp(s, "{<text variable, ", 17) == 0 ||
1137 strncmp(s, "{<data variable, ", 17) == 0 ||
1138 strncmp(s, "{<variable (not text or data), ", 31) == 0)
1140 const char* start = s;
1141 skipNested(s, '{', '}');
1142 variable->m_value = FROM_LATIN1(start, s-start);
1143 variable->m_value += ' '; // add only a single space
1144 while (isspace(*s))
1145 s++;
1146 goto repeat;
1148 else
1150 s++;
1151 if (!parseNested(s, variable)) {
1152 return false;
1154 // must be the closing brace
1155 if (*s != '}') {
1156 TRACE("parse error: missing } of " + variable->getText());
1157 return false;
1159 s++;
1160 // final white space
1161 while (isspace(*s))
1162 s++;
1164 } else {
1165 // examples of leaf values (cannot be the empty string):
1166 // 123
1167 // -123
1168 // 23.575e+37
1169 // 0x32a45
1170 // @0x012ab4
1171 // (DwContentType&) @0x8123456: {...}
1172 // 0x32a45 "text"
1173 // 10 '\n'
1174 // <optimized out>
1175 // 0x823abc <Array<int> virtual table>
1176 // (void (*)()) 0x8048480 <f(E *, char)>
1177 // (E *) 0xbffff450
1178 // red
1179 // &parseP (HTMLClueV *, char *)
1180 // Variable "x" is not available.
1181 // The value of variable 'x' is distributed...
1182 // -nan(0xfffff081defa0)
1184 const char*p = s;
1186 // check for type
1187 QString type;
1188 if (*p == '(') {
1189 skipNested(p, '(', ')');
1191 while (isspace(*p))
1192 p++;
1193 variable->m_value = FROM_LATIN1(s, p - s);
1196 bool reference = false;
1197 if (*p == '@') {
1198 // skip reference marker
1199 p++;
1200 reference = true;
1202 const char* start = p;
1203 if (*p == '-')
1204 p++;
1206 // some values consist of more than one token
1207 bool checkMultiPart = false;
1209 if (p[0] == '0' && p[1] == 'x') {
1210 // parse hex number
1211 p += 2;
1212 while (isxdigit(*p))
1213 p++;
1216 * Assume this is a pointer, but only if it's not a reference, since
1217 * references can't be expanded.
1219 if (!reference) {
1220 variable->m_varKind = VarTree::VKpointer;
1221 } else {
1223 * References are followed by a colon, in which case we'll
1224 * find the value following the reference address.
1226 if (*p == ':') {
1227 p++;
1228 } else {
1229 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1230 reference = false;
1233 checkMultiPart = true;
1234 } else if (isdigit(*p)) {
1235 // parse decimal number, possibly a float
1236 while (isdigit(*p))
1237 p++;
1238 if (*p == '.') { /* TODO: obey i18n? */
1239 // In long arrays an integer may be followed by '...'.
1240 // We test for this situation and don't gobble the '...'.
1241 if (p[1] != '.' || p[0] != '.') {
1242 // fractional part
1243 p++;
1244 while (isdigit(*p))
1245 p++;
1248 if (*p == 'e' || *p == 'E') {
1249 p++;
1250 // exponent
1251 if (*p == '-' || *p == '+')
1252 p++;
1253 while (isdigit(*p))
1254 p++;
1257 // for char variables there is the char, eg. 10 '\n'
1258 checkMultiPart = true;
1259 } else if (*p == '<') {
1260 // e.g. <optimized out>
1261 skipNestedAngles(p);
1262 } else if (*p == '"' || *p == '\'') {
1263 // character may have multipart: '\000' <repeats 11 times>
1264 checkMultiPart = *p == '\'';
1265 // found a string
1266 skipString(p);
1267 } else if (*p == '&') {
1268 // function pointer
1269 p++;
1270 skipName(p);
1271 while (isspace(*p)) {
1272 p++;
1274 if (*p == '(') {
1275 skipNested(p, '(', ')');
1277 } else if (strncmp(p, "Variable \"", 10) == 0) {
1278 // Variable "x" is not available.
1279 p += 10; // skip to "
1280 skipName(p);
1281 if (strncmp(p, "\" is not available.", 19) == 0) {
1282 p += 19;
1284 } else if (strncmp(p, "The value of variable '", 23) == 0) {
1285 p += 23;
1286 skipName(p);
1287 const char* e = strchr(p, '.');
1288 if (e == 0) {
1289 p += strlen(p);
1290 } else {
1291 p = e+1;
1293 } else {
1294 // must be an enumeration value
1295 skipName(p);
1296 // hmm, not necessarily: nan (floating point Not a Number)
1297 // is followed by a number in ()
1298 if (*p == '(')
1299 skipNested(p, '(', ')');
1301 variable->m_value += FROM_LATIN1(start, p - start);
1303 // remove line breaks from the value; this is ok since
1304 // string values never contain a literal line break
1305 variable->m_value.replace('\n', ' ');
1307 if (checkMultiPart) {
1308 // white space
1309 while (isspace(*p))
1310 p++;
1311 // may be followed by a string or <...>
1312 start = p;
1314 if (*p == '"' || *p == '\'') {
1315 skipString(p);
1316 } else if (*p == '<') {
1317 // if this value is part of an array, it might be followed
1318 // by <repeats 15 times>, which we don't skip here
1319 if (strncmp(p, "<repeats ", 9) != 0)
1320 skipNestedAngles(p);
1322 if (p != start) {
1323 // there is always a blank before the string,
1324 // which we will include in the final string value
1325 variable->m_value += FROM_LATIN1(start-1, (p - start)+1);
1326 // if this was a pointer, reset that flag since we
1327 // now got the value
1328 variable->m_varKind = VarTree::VKsimple;
1332 if (variable->m_value.length() == 0) {
1333 TRACE("parse error: no value for " + variable->getText());
1334 return false;
1337 // final white space
1338 while (isspace(*p))
1339 p++;
1340 s = p;
1343 * If this was a reference, the value follows. It might even be a
1344 * composite variable!
1346 if (reference) {
1347 goto repeat;
1350 if (variable->m_varKind == VarTree::VKpointer) {
1351 variable->setDelayedExpanding(true);
1355 return true;
1358 static bool parseNested(const char*& s, VarTree* variable)
1360 // could be a structure or an array
1361 while (isspace(*s))
1362 s++;
1364 const char* p = s;
1365 bool isStruct = false;
1367 * If there is a name followed by an = or an < -- which starts a type
1368 * name -- or "static", it is a structure
1370 if (*p == '<' || *p == '}') {
1371 isStruct = true;
1372 } else if (strncmp(p, "static ", 7) == 0) {
1373 isStruct = true;
1374 } else if (isalpha(*p) || *p == '_' || *p == '$') {
1375 // look ahead for a comma after the name
1376 skipName(p);
1377 while (isspace(*p))
1378 p++;
1379 if (*p == '=') {
1380 isStruct = true;
1382 p = s; /* rescan the name */
1384 if (isStruct) {
1385 if (!parseVarSeq(p, variable)) {
1386 return false;
1388 variable->m_varKind = VarTree::VKstruct;
1389 } else {
1390 if (!parseValueSeq(p, variable)) {
1391 return false;
1393 variable->m_varKind = VarTree::VKarray;
1395 s = p;
1396 return true;
1399 static bool parseVarSeq(const char*& s, VarTree* variable)
1401 // parse a comma-separated sequence of variables
1402 VarTree* var = variable; /* var != 0 to indicate success if empty seq */
1403 for (;;) {
1404 if (*s == '}')
1405 break;
1406 if (strncmp(s, "<No data fields>}", 17) == 0)
1408 // no member variables, so break out immediately
1409 s += 16; /* go to the closing brace */
1410 break;
1412 var = parseVar(s);
1413 if (var == 0)
1414 break; /* syntax error */
1415 variable->appendChild(var);
1416 if (*s != ',')
1417 break;
1418 // skip the comma and whitespace
1419 s++;
1420 while (isspace(*s))
1421 s++;
1423 return var != 0;
1426 static bool parseValueSeq(const char*& s, VarTree* variable)
1428 // parse a comma-separated sequence of variables
1429 int index = 0;
1430 bool good;
1431 for (;;) {
1432 QString name;
1433 name.sprintf("[%d]", index);
1434 VarTree* var = new VarTree(name, VarTree::NKplain);
1435 var->setDeleteChildren(true);
1436 good = parseValue(s, var);
1437 if (!good) {
1438 delete var;
1439 return false;
1441 // a value may be followed by "<repeats 45 times>"
1442 if (strncmp(s, "<repeats ", 9) == 0) {
1443 s += 9;
1444 char* end;
1445 int l = strtol(s, &end, 10);
1446 if (end == s || strncmp(end, " times>", 7) != 0) {
1447 // should not happen
1448 delete var;
1449 return false;
1451 TRACE(QString().sprintf("found <repeats %d times> in array", l));
1452 // replace name and advance index
1453 name.sprintf("[%d .. %d]", index, index+l-1);
1454 var->setText(name);
1455 index += l;
1456 // skip " times>" and space
1457 s = end+7;
1458 // possible final space
1459 while (isspace(*s))
1460 s++;
1461 } else {
1462 index++;
1464 variable->appendChild(var);
1465 // long arrays may be terminated by '...'
1466 if (strncmp(s, "...", 3) == 0) {
1467 s += 3;
1468 VarTree* var = new VarTree("...", VarTree::NKplain);
1469 var->setDeleteChildren(true);
1470 var->m_value = i18n("<additional entries of the array suppressed>");
1471 variable->appendChild(var);
1472 break;
1474 if (*s != ',') {
1475 break;
1477 // skip the comma and whitespace
1478 s++;
1479 while (isspace(*s))
1480 s++;
1481 // sometimes there is a closing brace after a comma
1482 // if (*s == '}')
1483 // break;
1485 return true;
1489 * Parses a stack frame.
1491 static void parseFrameInfo(const char*& s, QString& func,
1492 QString& file, int& lineNo, DbgAddr& address)
1494 const char* p = s;
1496 // next may be a hexadecimal address
1497 if (*p == '0') {
1498 const char* start = p;
1499 p++;
1500 if (*p == 'x')
1501 p++;
1502 while (isxdigit(*p))
1503 p++;
1504 address = FROM_LATIN1(start, p-start);
1505 if (strncmp(p, " in ", 4) == 0)
1506 p += 4;
1507 } else {
1508 address = DbgAddr();
1510 const char* start = p;
1511 // check for special signal handler frame
1512 if (strncmp(p, "<signal handler called>", 23) == 0) {
1513 func = FROM_LATIN1(start, 23);
1514 file = QString();
1515 lineNo = -1;
1516 s = p+23;
1517 if (*s == '\n')
1518 s++;
1519 return;
1523 * Skip the function name. It is terminated by a left parenthesis
1524 * which does not delimit "(anonymous namespace)" and which is
1525 * outside the angle brackets <> of template parameter lists.
1527 while (*p != '\0')
1529 if (*p == '<') {
1530 // check for operator<< and operator<
1531 if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
1533 p++;
1534 if (*p == '<')
1535 p++;
1537 else
1539 // skip template parameter list
1540 skipNestedAngles(p);
1542 } else if (*p == '(') {
1543 if (strncmp(p, "(anonymous namespace)", 21) != 0)
1544 break; // parameter list found
1545 p += 21;
1546 } else {
1547 p++;
1551 if (*p == '\0') {
1552 func = start;
1553 file = QString();
1554 lineNo = -1;
1555 s = p;
1556 return;
1559 * Skip parameters. But notice that for complicated conversion
1560 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1561 * pointer to function) as well as operator()(...) we have to skip
1562 * additional pairs of parentheses. Furthermore, recent gdbs write the
1563 * demangled name followed by the arguments in a pair of parentheses,
1564 * where the demangled name can end in "const".
1566 do {
1567 skipNestedWithString(p, '(', ')');
1568 while (isspace(*p))
1569 p++;
1570 // skip "const"
1571 if (strncmp(p, "const", 5) == 0) {
1572 p += 5;
1573 while (isspace(*p))
1574 p++;
1576 } while (*p == '(');
1578 // check for file position
1579 if (strncmp(p, "at ", 3) == 0) {
1580 p += 3;
1581 const char* fileStart = p;
1582 // go for the end of the line
1583 while (*p != '\0' && *p != '\n')
1584 p++;
1585 // search back for colon
1586 const char* colon = p;
1587 do {
1588 --colon;
1589 } while (*colon != ':');
1590 file = FROM_LATIN1(fileStart, colon-fileStart);
1591 lineNo = atoi(colon+1)-1;
1592 // skip new-line
1593 if (*p != '\0')
1594 p++;
1595 } else {
1596 // check for "from shared lib"
1597 if (strncmp(p, "from ", 5) == 0) {
1598 p += 5;
1599 // go for the end of the line
1600 while (*p != '\0' && *p != '\n')
1601 p++;
1602 // skip new-line
1603 if (*p != '\0')
1604 p++;
1606 file = "";
1607 lineNo = -1;
1609 // construct the function name (including file info)
1610 if (*p == '\0') {
1611 func = start;
1612 } else {
1613 func = FROM_LATIN1(start, p-start-1); /* don't include \n */
1615 s = p;
1618 * Replace \n (and whitespace around it) in func by a blank. We cannot
1619 * use QString::simplifyWhiteSpace() for this because this would also
1620 * simplify space that belongs to a string arguments that gdb sometimes
1621 * prints in the argument lists of the function.
1623 ASSERT(!isspace(func[0].latin1())); /* there must be non-white before first \n */
1624 int nl = 0;
1625 while ((nl = func.find('\n', nl)) >= 0) {
1626 // search back to the beginning of the whitespace
1627 int startWhite = nl;
1628 do {
1629 --startWhite;
1630 } while (isspace(func[startWhite].latin1()));
1631 startWhite++;
1632 // search forward to the end of the whitespace
1633 do {
1634 nl++;
1635 } while (isspace(func[nl].latin1()));
1636 // replace
1637 func.replace(startWhite, nl-startWhite, " ");
1638 /* continue searching for more \n's at this place: */
1639 nl = startWhite+1;
1645 * Parses a stack frame including its frame number
1647 static bool parseFrame(const char*& s, int& frameNo, QString& func,
1648 QString& file, int& lineNo, DbgAddr& address)
1650 // Example:
1651 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1652 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1654 // must start with a hash mark followed by number
1655 // or with "Breakpoint " followed by number and comma
1656 if (s[0] == '#') {
1657 if (!isdigit(s[1]))
1658 return false;
1659 s++; /* skip the hash mark */
1660 } else if (strncmp(s, "Breakpoint ", 11) == 0) {
1661 if (!isdigit(s[11]))
1662 return false;
1663 s += 11; /* skip "Breakpoint" */
1664 } else
1665 return false;
1667 // frame number
1668 frameNo = atoi(s);
1669 while (isdigit(*s))
1670 s++;
1671 // space and comma
1672 while (isspace(*s) || *s == ',')
1673 s++;
1674 parseFrameInfo(s, func, file, lineNo, address);
1675 return true;
1678 void GdbDriver::parseBackTrace(const char* output, QList<StackFrame>& stack)
1680 QString func, file;
1681 int lineNo, frameNo;
1682 DbgAddr address;
1684 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1685 StackFrame* frm = new StackFrame;
1686 frm->frameNo = frameNo;
1687 frm->fileName = file;
1688 frm->lineNo = lineNo;
1689 frm->address = address;
1690 frm->var = new VarTree(func, VarTree::NKplain);
1691 stack.append(frm);
1695 bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
1696 QString& file, int& lineNo, DbgAddr& address)
1698 QString func;
1699 return ::parseFrame(output, frameNo, func, file, lineNo, address);
1703 bool GdbDriver::parseBreakList(const char* output, QList<Breakpoint>& brks)
1705 // skip first line, which is the headline
1706 const char* p = strchr(output, '\n');
1707 if (p == 0)
1708 return false;
1709 p++;
1710 if (*p == '\0')
1711 return false;
1713 // split up a line
1714 QString location;
1715 QString address;
1716 int hits = 0;
1717 uint ignoreCount = 0;
1718 QString condition;
1719 const char* end;
1720 char* dummy;
1721 while (*p != '\0') {
1722 // get Num
1723 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1724 p = dummy;
1725 // get Type
1726 while (isspace(*p))
1727 p++;
1728 Breakpoint::Type bpType = Breakpoint::breakpoint;
1729 if (strncmp(p, "breakpoint", 10) == 0) {
1730 p += 10;
1731 } else if (strncmp(p, "hw watchpoint", 13) == 0) {
1732 bpType = Breakpoint::watchpoint;
1733 p += 13;
1734 } else if (strncmp(p, "watchpoint", 10) == 0) {
1735 bpType = Breakpoint::watchpoint;
1736 p += 10;
1738 while (isspace(*p))
1739 p++;
1740 if (*p == '\0')
1741 break;
1742 // get Disp
1743 char disp = *p++;
1744 while (*p != '\0' && !isspace(*p)) /* "keep" or "del" */
1745 p++;
1746 while (isspace(*p))
1747 p++;
1748 if (*p == '\0')
1749 break;
1750 // get Enb
1751 char enable = *p++;
1752 while (*p != '\0' && !isspace(*p)) /* "y" or "n" */
1753 p++;
1754 while (isspace(*p))
1755 p++;
1756 if (*p == '\0')
1757 break;
1758 // the address, if present
1759 if (bpType == Breakpoint::breakpoint &&
1760 strncmp(p, "0x", 2) == 0)
1762 const char* start = p;
1763 while (*p != '\0' && !isspace(*p))
1764 p++;
1765 address = FROM_LATIN1(start, p-start);
1766 while (isspace(*p) && *p != '\n')
1767 p++;
1768 if (*p == '\0')
1769 break;
1770 } else {
1771 address = QString();
1773 // remainder is location, hit and ignore count, condition
1774 hits = 0;
1775 ignoreCount = 0;
1776 condition = QString();
1777 end = strchr(p, '\n');
1778 if (end == 0) {
1779 location = p;
1780 p += location.length();
1781 } else {
1782 location = FROM_LATIN1(p, end-p).stripWhiteSpace();
1783 p = end+1; /* skip over \n */
1786 // may be continued in next line
1787 while (isspace(*p)) { /* p points to beginning of line */
1788 // skip white space at beginning of line
1789 while (isspace(*p))
1790 p++;
1792 // seek end of line
1793 end = strchr(p, '\n');
1794 if (end == 0)
1795 end = p+strlen(p);
1797 if (strncmp(p, "breakpoint already hit", 22) == 0) {
1798 // extract the hit count
1799 p += 22;
1800 hits = strtol(p, &dummy, 10);
1801 TRACE(QString().sprintf("hit count %d", hits));
1802 } else if (strncmp(p, "stop only if ", 13) == 0) {
1803 // extract condition
1804 p += 13;
1805 condition = FROM_LATIN1(p, end-p).stripWhiteSpace();
1806 TRACE("condition: "+condition);
1807 } else if (strncmp(p, "ignore next ", 12) == 0) {
1808 // extract ignore count
1809 p += 12;
1810 ignoreCount = strtol(p, &dummy, 10);
1811 TRACE(QString().sprintf("ignore count %d", ignoreCount));
1812 } else {
1813 // indeed a continuation
1814 location += " " + FROM_LATIN1(p, end-p).stripWhiteSpace();
1816 p = end;
1817 if (*p != '\0')
1818 p++; /* skip '\n' */
1820 Breakpoint* bp = new Breakpoint;
1821 bp->id = bpNum;
1822 bp->type = bpType;
1823 bp->temporary = disp == 'd';
1824 bp->enabled = enable == 'y';
1825 bp->location = location;
1826 bp->address = address;
1827 bp->hitCount = hits;
1828 bp->ignoreCount = ignoreCount;
1829 bp->condition = condition;
1830 brks.append(bp);
1832 return true;
1835 bool GdbDriver::parseThreadList(const char* output, QList<ThreadInfo>& threads)
1837 if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
1838 // no threads
1839 return true;
1842 int id;
1843 QString systag;
1844 QString func, file;
1845 int lineNo;
1846 DbgAddr address;
1848 const char* p = output;
1849 while (*p != '\0') {
1850 // seach look for thread id, watching out for the focus indicator
1851 bool hasFocus = false;
1852 while (isspace(*p)) /* may be \n from prev line: see "No stack" below */
1853 p++;
1854 if (*p == '*') {
1855 hasFocus = true;
1856 p++;
1857 // there follows only whitespace
1859 char* end;
1860 id = strtol(p, &end, 10);
1861 if (p == end) {
1862 // syntax error: no number found; bail out
1863 return true;
1865 p = end;
1867 // skip space
1868 while (isspace(*p))
1869 p++;
1872 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1874 end = strstr(p, " ");
1875 if (end == 0) {
1876 // syntax error; bail out
1877 return true;
1879 systag = FROM_LATIN1(p, end-p);
1880 p = end+2;
1883 * Now follows a standard stack frame. Sometimes, however, gdb
1884 * catches a thread at an instant where it doesn't have a stack.
1886 if (strncmp(p, "[No stack.]", 11) != 0) {
1887 ::parseFrameInfo(p, func, file, lineNo, address);
1888 } else {
1889 func = "[No stack]";
1890 file = QString();
1891 lineNo = -1;
1892 address = QString();
1893 p += 11; /* \n is skipped above */
1896 ThreadInfo* thr = new ThreadInfo;
1897 thr->id = id;
1898 thr->threadName = systag;
1899 thr->hasFocus = hasFocus;
1900 thr->function = func;
1901 thr->fileName = file;
1902 thr->lineNo = lineNo;
1903 thr->address = address;
1904 threads.append(thr);
1906 return true;
1909 static bool parseNewBreakpoint(const char* o, int& id,
1910 QString& file, int& lineNo, QString& address);
1911 static bool parseNewWatchpoint(const char* o, int& id,
1912 QString& expr);
1914 bool GdbDriver::parseBreakpoint(const char* output, int& id,
1915 QString& file, int& lineNo, QString& address)
1917 const char* o = output;
1918 // skip lines of that begin with "(Cannot find"
1919 while (strncmp(o, "(Cannot find", 12) == 0) {
1920 o = strchr(o, '\n');
1921 if (o == 0)
1922 return false;
1923 o++; /* skip newline */
1926 if (strncmp(o, "Breakpoint ", 11) == 0) {
1927 output += 11; /* skip "Breakpoint " */
1928 return ::parseNewBreakpoint(output, id, file, lineNo, address);
1929 } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
1930 output += 20;
1931 return ::parseNewWatchpoint(output, id, address);
1932 } else if (strncmp(o, "Watchpoint ", 11) == 0) {
1933 output += 11;
1934 return ::parseNewWatchpoint(output, id, address);
1936 return false;
1939 static bool parseNewBreakpoint(const char* o, int& id,
1940 QString& file, int& lineNo, QString& address)
1942 // breakpoint id
1943 char* p;
1944 id = strtoul(o, &p, 10);
1945 if (p == o)
1946 return false;
1948 // check for the address
1949 if (strncmp(p, " at 0x", 6) == 0) {
1950 char* start = p+4; /* skip " at ", but not 0x */
1951 p += 6;
1952 while (isxdigit(*p))
1953 ++p;
1954 address = FROM_LATIN1(start, p-start);
1957 // file name
1958 char* fileStart = strstr(p, "file ");
1959 if (fileStart == 0)
1960 return !address.isEmpty(); /* parse error only if there's no address */
1961 fileStart += 5;
1963 // line number
1964 char* numStart = strstr(fileStart, ", line ");
1965 QString fileName = FROM_LATIN1(fileStart, numStart-fileStart);
1966 numStart += 7;
1967 int line = strtoul(numStart, &p, 10);
1968 if (numStart == p)
1969 return false;
1971 file = fileName;
1972 lineNo = line-1; /* zero-based! */
1973 return true;
1976 static bool parseNewWatchpoint(const char* o, int& id,
1977 QString& expr)
1979 // watchpoint id
1980 char* p;
1981 id = strtoul(o, &p, 10);
1982 if (p == o)
1983 return false;
1985 if (strncmp(p, ": ", 2) != 0)
1986 return false;
1987 p += 2;
1989 // all the rest on the line is the expression
1990 expr = FROM_LATIN1(p, strlen(p)).stripWhiteSpace();
1991 return true;
1994 void GdbDriver::parseLocals(const char* output, QList<VarTree>& newVars)
1996 // check for possible error conditions
1997 if (strncmp(output, "No symbol table", 15) == 0)
1999 return;
2002 while (*output != '\0') {
2003 while (isspace(*output))
2004 output++;
2005 if (*output == '\0')
2006 break;
2007 // skip occurrences of "No locals" and "No args"
2008 if (strncmp(output, "No locals", 9) == 0 ||
2009 strncmp(output, "No arguments", 12) == 0)
2011 output = strchr(output, '\n');
2012 if (output == 0) {
2013 break;
2015 continue;
2018 VarTree* variable = parseVar(output);
2019 if (variable == 0) {
2020 break;
2022 // do not add duplicates
2023 for (VarTree* o = newVars.first(); o != 0; o = newVars.next()) {
2024 if (o->getText() == variable->getText()) {
2025 delete variable;
2026 goto skipDuplicate;
2029 newVars.append(variable);
2030 skipDuplicate:;
2034 bool GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue, VarTree*& var)
2036 // check for error conditions
2037 if (parseErrorMessage(output, var, wantErrorValue))
2039 return false;
2040 } else {
2041 // parse the variable
2042 var = parseVar(output);
2043 return true;
2047 bool GdbDriver::parseChangeWD(const char* output, QString& message)
2049 bool isGood = false;
2050 message = QString(output).simplifyWhiteSpace();
2051 if (message.isEmpty()) {
2052 message = i18n("New working directory: ") + m_programWD;
2053 isGood = true;
2055 return isGood;
2058 bool GdbDriver::parseChangeExecutable(const char* output, QString& message)
2060 message = output;
2062 m_haveCoreFile = false;
2065 * Lines starting with the following do not indicate errors:
2066 * Using host libthread_db
2067 * (no debugging symbols found)
2069 while (strncmp(output, "Using host libthread_db", 23) == 0 ||
2070 strncmp(output, "(no debugging symbols found)", 28) == 0)
2072 // this line is good, go to the next one
2073 const char* end = strchr(output, '\n');
2074 if (end == 0)
2075 output += strlen(output);
2076 else
2077 output = end+1;
2081 * If we've parsed all lines, there was no error.
2083 return output[0] == '\0';
2086 bool GdbDriver::parseCoreFile(const char* output)
2088 // if command succeeded, gdb emits a line starting with "#0 "
2089 m_haveCoreFile = strstr(output, "\n#0 ") != 0;
2090 return m_haveCoreFile;
2093 uint GdbDriver::parseProgramStopped(const char* output, QString& message)
2095 // optionally: "program changed, rereading symbols",
2096 // followed by:
2097 // "Program exited normally"
2098 // "Program terminated with wignal SIGSEGV"
2099 // "Program received signal SIGINT" or other signal
2100 // "Breakpoint..."
2102 // go through the output, line by line, checking what we have
2103 const char* start = output - 1;
2104 uint flags = SFprogramActive;
2105 message = QString();
2106 do {
2107 start++; /* skip '\n' */
2109 if (strncmp(start, "Program ", 8) == 0 ||
2110 strncmp(start, "ptrace: ", 8) == 0) {
2112 * When we receive a signal, the program remains active.
2114 * Special: If we "stopped" in a corefile, the string "Program
2115 * terminated with signal"... is displayed. (Normally, we see
2116 * "Program received signal"... when a signal happens.)
2118 if (strncmp(start, "Program exited", 14) == 0 ||
2119 (strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
2120 strncmp(start, "ptrace: ", 8) == 0)
2122 flags &= ~SFprogramActive;
2125 // set message
2126 const char* endOfMessage = strchr(start, '\n');
2127 if (endOfMessage == 0)
2128 endOfMessage = start + strlen(start);
2129 message = FROM_LATIN1(start, endOfMessage-start);
2130 } else if (strncmp(start, "Breakpoint ", 11) == 0) {
2132 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2133 * that it stopped at a temporary breakpoint).
2135 flags |= SFrefreshBreak;
2136 } else if (strstr(start, "re-reading symbols.") != 0) {
2137 flags |= SFrefreshSource;
2140 // next line, please
2141 start = strchr(start, '\n');
2142 } while (start != 0);
2145 * Gdb only notices when new threads have appeared, but not when a
2146 * thread finishes. So we always have to assume that the list of
2147 * threads has changed.
2149 flags |= SFrefreshThreads;
2151 return flags;
2154 void GdbDriver::parseSharedLibs(const char* output, QStrList& shlibs)
2156 if (strncmp(output, "No shared libraries loaded", 26) == 0)
2157 return;
2159 // parse the table of shared libraries
2161 // strip off head line
2162 output = strchr(output, '\n');
2163 if (output == 0)
2164 return;
2165 output++; /* skip '\n' */
2166 QString shlibName;
2167 while (*output != '\0') {
2168 // format of a line is
2169 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2170 // 3 blocks of non-space followed by space
2171 for (int i = 0; *output != '\0' && i < 3; i++) {
2172 while (*output != '\0' && !isspace(*output)) { /* non-space */
2173 output++;
2175 while (isspace(*output)) { /* space */
2176 output++;
2179 if (*output == '\0')
2180 return;
2181 const char* start = output;
2182 output = strchr(output, '\n');
2183 if (output == 0)
2184 output = start + strlen(start);
2185 shlibName = FROM_LATIN1(start, output-start);
2186 if (*output != '\0')
2187 output++;
2188 shlibs.append(shlibName);
2189 TRACE("found shared lib " + shlibName);
2193 bool GdbDriver::parseFindType(const char* output, QString& type)
2195 if (strncmp(output, "type = ", 7) != 0)
2196 return false;
2199 * Everything else is the type. We strip off any leading "const" and any
2200 * trailing "&" on the grounds that neither affects the decoding of the
2201 * object. We also strip off all white-space from the type.
2203 output += 7;
2204 if (strncmp(output, "const ", 6) == 0)
2205 output += 6;
2206 type = output;
2207 type.replace(QRegExp("\\s+"), "");
2208 if (type.endsWith("&"))
2209 type.truncate(type.length() - 1);
2210 return true;
2213 void GdbDriver::parseRegisters(const char* output, QList<RegisterInfo>& regs)
2215 if (strncmp(output, "The program has no registers now", 32) == 0) {
2216 return;
2219 QString regName;
2220 QString value;
2222 // parse register values
2223 while (*output != '\0')
2225 // skip space at the start of the line
2226 while (isspace(*output))
2227 output++;
2229 // register name
2230 const char* start = output;
2231 while (*output != '\0' && !isspace(*output))
2232 output++;
2233 if (*output == '\0')
2234 break;
2235 regName = FROM_LATIN1(start, output-start);
2237 // skip space
2238 while (isspace(*output))
2239 output++;
2241 RegisterInfo* reg = new RegisterInfo;
2242 reg->regName = regName;
2245 * If we find a brace now, this is a vector register. We look for
2246 * the closing brace and treat the result as cooked value.
2248 if (*output == '{')
2250 start = output;
2251 skipNested(output, '{', '}');
2252 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2253 // skip space, but not the end of line
2254 while (isspace(*output) && *output != '\n')
2255 output++;
2256 // get rid of the braces at the begining and the end
2257 value.remove(0, 1);
2258 if (value[value.length()-1] == '}') {
2259 value = value.left(value.length()-1);
2261 // gdb 5.3 doesn't print a separate set of raw values
2262 if (*output == '{') {
2263 // another set of vector follows
2264 // what we have so far is the raw value
2265 reg->rawValue = value;
2267 start = output;
2268 skipNested(output, '{', '}');
2269 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2270 } else {
2271 // for gdb 5.3
2272 // find first type that does not have an array, this is the RAW value
2273 const char* end=start;
2274 findEnd(end);
2275 const char* cur=start;
2276 while (cur<end) {
2277 while (*cur != '=' && cur<end)
2278 cur++;
2279 cur++;
2280 while (isspace(*cur) && cur<end)
2281 cur++;
2282 if (isNumberish(*cur)) {
2283 end=cur;
2284 while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
2285 end++;
2286 QString rawValue = FROM_LATIN1(cur, end-cur).simplifyWhiteSpace();
2287 reg->rawValue = rawValue;
2289 if (rawValue.left(2)=="0x") {
2290 // ok we have a raw value, now get it's type
2291 end=cur-1;
2292 while (isspace(*end) || *end=='=') end--;
2293 end++;
2294 cur=end-1;
2295 while (*cur!='{' && *cur!=' ')
2296 cur--;
2297 cur++;
2298 reg->type=FROM_LATIN1(cur, end-cur);
2301 // end while loop
2302 cur=end;
2305 // skip to the end of line
2306 while (*output != '\0' && *output != '\n')
2307 output++;
2308 // get rid of the braces at the begining and the end
2309 value.remove(0, 1);
2310 if (value[value.length()-1] == '}') {
2311 value = value.left(value.length()-1);
2314 reg->cookedValue = value;
2316 else
2318 // the rest of the line is the register value
2319 start = output;
2320 output = strchr(output,'\n');
2321 if (output == 0)
2322 output = start + strlen(start);
2323 value = FROM_LATIN1(start, output-start).simplifyWhiteSpace();
2326 * We split the raw from the cooked values.
2327 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2328 * Here, the cooked value comes first, and the raw value is in
2329 * the second part.
2331 int pos = value.find(" (raw ");
2332 if (pos >= 0)
2334 reg->cookedValue = value.left(pos);
2335 reg->rawValue = value.mid(pos+6);
2336 if (reg->rawValue.right(1) == ")") // remove closing bracket
2337 reg->rawValue.truncate(reg->rawValue.length()-1);
2339 else
2342 * In other cases we split off the first token (separated by
2343 * whitespace). It is the raw value. The remainder of the line
2344 * is the cooked value.
2346 int pos = value.find(' ');
2347 if (pos < 0) {
2348 reg->rawValue = value;
2349 reg->cookedValue = QString();
2350 } else {
2351 reg->rawValue = value.left(pos);
2352 reg->cookedValue = value.mid(pos+1);
2356 if (*output != '\0')
2357 output++; /* skip '\n' */
2359 regs.append(reg);
2363 bool GdbDriver::parseInfoLine(const char* output, QString& addrFrom, QString& addrTo)
2365 // "is at address" or "starts at address"
2366 const char* start = strstr(output, "s at address ");
2367 if (start == 0)
2368 return false;
2370 start += 13;
2371 const char* p = start;
2372 while (*p != '\0' && !isspace(*p))
2373 p++;
2374 addrFrom = FROM_LATIN1(start, p-start);
2376 start = strstr(p, "and ends at ");
2377 if (start == 0) {
2378 addrTo = addrFrom;
2379 return true;
2382 start += 12;
2383 p = start;
2384 while (*p != '\0' && !isspace(*p))
2385 p++;
2386 addrTo = FROM_LATIN1(start, p-start);
2388 return true;
2391 void GdbDriver::parseDisassemble(const char* output, QList<DisassembledCode>& code)
2393 code.clear();
2395 if (strncmp(output, "Dump of assembler", 17) != 0) {
2396 // error message?
2397 DisassembledCode c;
2398 c.code = output;
2399 code.append(new DisassembledCode(c));
2400 return;
2403 // remove first line
2404 const char* p = strchr(output, '\n');
2405 if (p == 0)
2406 return; /* not a regular output */
2408 p++;
2410 // remove last line
2411 const char* end = strstr(output, "End of assembler");
2412 if (end == 0)
2413 end = p + strlen(p);
2415 DbgAddr address;
2417 // remove function offsets from the lines
2418 while (p != end)
2420 const char* start = p;
2421 // address
2422 while (p != end && !isspace(*p))
2423 p++;
2424 address = FROM_LATIN1(start, p-start);
2426 // function name (enclosed in '<>', followed by ':')
2427 while (p != end && *p != '<')
2428 p++;
2429 if (*p == '<')
2430 skipNestedAngles(p);
2431 if (*p == ':')
2432 p++;
2434 // space until code
2435 while (p != end && isspace(*p))
2436 p++;
2438 // code until end of line
2439 start = p;
2440 while (p != end && *p != '\n')
2441 p++;
2442 if (p != end) /* include '\n' */
2443 p++;
2445 DisassembledCode* c = new DisassembledCode;
2446 c->address = address;
2447 c->code = FROM_LATIN1(start, p-start);
2448 code.append(c);
2452 QString GdbDriver::parseMemoryDump(const char* output, QList<MemoryDump>& memdump)
2454 if (isErrorExpr(output)) {
2455 // error; strip space
2456 QString msg = output;
2457 return msg.stripWhiteSpace();
2460 const char* p = output; /* save typing */
2461 DbgAddr addr;
2462 QString dump;
2464 // the address
2465 while (*p != 0) {
2466 const char* start = p;
2467 while (*p != '\0' && *p != ':' && !isspace(*p))
2468 p++;
2469 addr = FROM_LATIN1(start, p-start);
2470 if (*p != ':') {
2471 // parse function offset
2472 while (isspace(*p))
2473 p++;
2474 start = p;
2475 while (*p != '\0' && !(*p == ':' && isspace(p[1])))
2476 p++;
2477 addr.fnoffs = FROM_LATIN1(start, p-start);
2479 if (*p == ':')
2480 p++;
2481 // skip space; this may skip a new-line char!
2482 while (isspace(*p))
2483 p++;
2484 // everything to the end of the line is the memory dump
2485 const char* end = strchr(p, '\n');
2486 if (end != 0) {
2487 dump = FROM_LATIN1(p, end-p);
2488 p = end+1;
2489 } else {
2490 dump = FROM_LATIN1(p, strlen(p));
2491 p += strlen(p);
2493 MemoryDump* md = new MemoryDump;
2494 md->address = addr;
2495 md->dump = dump;
2496 memdump.append(md);
2499 return QString();
2502 QString GdbDriver::editableValue(VarTree* value)
2504 const char* s = value->m_value.latin1();
2506 // if the variable is a pointer value that contains a cast,
2507 // remove the cast
2508 if (*s == '(') {
2509 skipNested(s, '(', ')');
2510 // skip space
2511 while (isspace(*s))
2512 ++s;
2515 repeat:
2516 const char* start = s;
2518 if (strncmp(s, "0x", 2) == 0)
2520 s += 2;
2521 while (isxdigit(*s))
2522 ++s;
2525 * What we saw so far might have been a reference. If so, edit the
2526 * referenced value. Otherwise, edit the pointer.
2528 if (*s == ':') {
2529 // a reference
2530 ++s;
2531 goto repeat;
2533 // a pointer
2534 // if it's a pointer to a string, remove the string
2535 const char* end = s;
2536 while (isspace(*s))
2537 ++s;
2538 if (*s == '"') {
2539 // a string
2540 return FROM_LATIN1(start, end-start);
2541 } else {
2542 // other pointer
2543 return FROM_LATIN1(start, strlen(start));
2547 // else leave it unchanged (or stripped of the reference preamble)
2548 return s;
2551 QString GdbDriver::parseSetVariable(const char* output)
2553 // if there is any output, it is an error message
2554 QString msg = output;
2555 return msg.stripWhiteSpace();
2559 #include "gdbdriver.moc"