3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
10 #include <klocale.h> /* i18n */
15 #include <stdlib.h> /* strtol, atoi */
16 #include <string.h> /* strcpy */
24 static void skipString(const char*& p
);
25 static void skipNested(const char*& s
, char opening
, char closing
);
26 static VarTree
* parseVar(const char*& s
);
27 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
);
28 static bool parseValue(const char*& s
, VarTree
* variable
);
29 static bool parseNested(const char*& s
, VarTree
* variable
);
30 static bool parseVarSeq(const char*& s
, VarTree
* variable
);
31 static bool parseValueSeq(const char*& s
, VarTree
* variable
);
33 #define PROMPT "(kdbg)"
35 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
38 // TODO: make this cmd info stuff non-static to allow multiple
39 // simultaneous gdbs to run!
43 const char* fmt
; /* format string */
45 argNone
, argString
, argNum
,
46 argStringNum
, argNumString
,
51 static const char printQStringStructFmt
[] =
52 // if the string data is junk, fail early
53 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
54 // print an array of shorts
55 "(*(unsigned short*)$qstrunicode)@"
57 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
58 // if unicode data is 0, check if it is QString::null
59 ":($qstrdata==QString::null.d)\n";
61 // However, gdb can't always find QString::null (I don't know why)
62 // which results in an error; we autodetect this situation in
63 // parseQt2QStrings and revert to a safe expression.
64 // Same as above except for last line:
65 static const char printQStringStructNoNullFmt
[] =
66 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
67 "(*(unsigned short*)$qstrunicode)@"
68 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
72 * The following array of commands must be sorted by the DC* values,
73 * because they are used as indices.
75 static GdbCmdInfo cmds
[] = {
76 { DCinitialize
, "", GdbCmdInfo::argNone
},
77 { DCtty
, "tty %s\n", GdbCmdInfo::argString
},
78 { DCexecutable
, "file \"%s\"\n", GdbCmdInfo::argString
},
79 { DCtargetremote
, "target remote %s\n", GdbCmdInfo::argString
},
80 { DCcorefile
, "core-file %s\n", GdbCmdInfo::argString
},
81 { DCattach
, "attach %s\n", GdbCmdInfo::argString
},
82 { DCinfolinemain
, "info line main\n", GdbCmdInfo::argNone
},
83 { DCinfolocals
, "kdbg__alllocals\n", GdbCmdInfo::argNone
},
84 { DCinforegisters
, "info all-registers\n", GdbCmdInfo::argNone
},
85 { DCexamine
, "x %s %s\n", GdbCmdInfo::argString2
},
86 { DCinfoline
, "info line %s:%d\n", GdbCmdInfo::argStringNum
},
87 { DCdisassemble
, "disassemble %s %s\n", GdbCmdInfo::argString2
},
88 { DCsetargs
, "set args %s\n", GdbCmdInfo::argString
},
89 { DCsetenv
, "set env %s %s\n", GdbCmdInfo::argString2
},
90 { DCunsetenv
, "unset env %s\n", GdbCmdInfo::argString
},
91 { DCcd
, "cd %s\n", GdbCmdInfo::argString
},
92 { DCbt
, "bt\n", GdbCmdInfo::argNone
},
93 { DCrun
, "run\n", GdbCmdInfo::argNone
},
94 { DCcont
, "cont\n", GdbCmdInfo::argNone
},
95 { DCstep
, "step\n", GdbCmdInfo::argNone
},
96 { DCstepi
, "stepi\n", GdbCmdInfo::argNone
},
97 { DCnext
, "next\n", GdbCmdInfo::argNone
},
98 { DCnexti
, "nexti\n", GdbCmdInfo::argNone
},
99 { DCfinish
, "finish\n", GdbCmdInfo::argNone
},
100 { DCuntil
, "until %s:%d\n", GdbCmdInfo::argStringNum
},
101 { DCkill
, "kill\n", GdbCmdInfo::argNone
},
102 { DCbreaktext
, "break %s\n", GdbCmdInfo::argString
},
103 { DCbreakline
, "break %s:%d\n", GdbCmdInfo::argStringNum
},
104 { DCtbreakline
, "tbreak %s:%d\n", GdbCmdInfo::argStringNum
},
105 { DCbreakaddr
, "break *%s\n", GdbCmdInfo::argString
},
106 { DCtbreakaddr
, "tbreak *%s\n", GdbCmdInfo::argString
},
107 { DCwatchpoint
, "watch %s\n", GdbCmdInfo::argString
},
108 { DCdelete
, "delete %d\n", GdbCmdInfo::argNum
},
109 { DCenable
, "enable %d\n", GdbCmdInfo::argNum
},
110 { DCdisable
, "disable %d\n", GdbCmdInfo::argNum
},
111 { DCprint
, "print %s\n", GdbCmdInfo::argString
},
112 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
113 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
114 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
115 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
116 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
117 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
118 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
119 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
120 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
121 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
124 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
125 #define MAX_FMTLEN 200
127 GdbDriver::GdbDriver() :
129 m_gdbMajor(4), m_gdbMinor(16)
131 strcpy(m_prompt
, PROMPT
);
132 m_promptLen
= PROMPT_LEN
;
133 m_promptLastChar
= PROMPT_LAST_CHAR
;
136 // check command info array
138 for (int i
= 0; i
< NUM_CMDS
; i
++) {
139 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
140 assert(i
== cmds
[i
].cmd
);
141 // a format string must be associated
142 assert(cmds
[i
].fmt
!= 0);
143 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
144 // format string must match arg specification
145 switch (cmds
[i
].argsNeeded
) {
146 case GdbCmdInfo::argNone
:
147 assert(strchr(cmds
[i
].fmt
, '%') == 0);
149 case GdbCmdInfo::argString
:
150 perc
= strchr(cmds
[i
].fmt
, '%');
151 assert(perc
!= 0 && perc
[1] == 's');
152 assert(strchr(perc
+2, '%') == 0);
154 case GdbCmdInfo::argNum
:
155 perc
= strchr(cmds
[i
].fmt
, '%');
156 assert(perc
!= 0 && perc
[1] == 'd');
157 assert(strchr(perc
+2, '%') == 0);
159 case GdbCmdInfo::argStringNum
:
160 perc
= strchr(cmds
[i
].fmt
, '%');
161 assert(perc
!= 0 && perc
[1] == 's');
162 perc
= strchr(perc
+2, '%');
163 assert(perc
!= 0 && perc
[1] == 'd');
164 assert(strchr(perc
+2, '%') == 0);
166 case GdbCmdInfo::argNumString
:
167 perc
= strchr(cmds
[i
].fmt
, '%');
168 assert(perc
!= 0 && perc
[1] == 'd');
169 perc
= strchr(perc
+2, '%');
170 assert(perc
!= 0 && perc
[1] == 's');
171 assert(strchr(perc
+2, '%') == 0);
173 case GdbCmdInfo::argString2
:
174 perc
= strchr(cmds
[i
].fmt
, '%');
175 assert(perc
!= 0 && perc
[1] == 's');
176 perc
= strchr(perc
+2, '%');
177 assert(perc
!= 0 && perc
[1] == 's');
178 assert(strchr(perc
+2, '%') == 0);
180 case GdbCmdInfo::argNum2
:
181 perc
= strchr(cmds
[i
].fmt
, '%');
182 assert(perc
!= 0 && perc
[1] == 'd');
183 perc
= strchr(perc
+2, '%');
184 assert(perc
!= 0 && perc
[1] == 'd');
185 assert(strchr(perc
+2, '%') == 0);
189 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
190 assert(strlen(printQStringStructNoNullFmt
) <= MAX_FMTLEN
);
194 GdbDriver::~GdbDriver()
199 QString
GdbDriver::driverName() const
204 QString
GdbDriver::defaultGdb()
208 " --fullname" /* to get standard file names each time the prog stops */
209 " --nx"; /* do not execute initialization files */
212 QString
GdbDriver::defaultInvocation() const
217 bool GdbDriver::startup(QString cmdStr
)
219 if (!DebuggerDriver::startup(cmdStr
))
222 static const char gdbInitialize
[] =
224 * Work around buggy gdbs that do command line editing even if they
225 * are not on a tty. The readline library echos every command back
226 * in this case, which is confusing for us.
230 "set print static-members off\n"
231 "set print asm-demangle on\n"
233 * Write a short macro that prints all locals: local variables and
234 * function arguments.
236 "define kdbg__alllocals\n"
237 "info locals\n" /* local vars supersede args with same name */
238 "info args\n" /* therefore, arguments must come last */
240 // change prompt string and synchronize with gdb
241 "set prompt " PROMPT
"\n"
244 executeCmdString(DCinitialize
, gdbInitialize
, false);
246 // assume that QString::null is ok
247 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
252 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
254 // command string must be committed
255 if (!cmd
->m_committed
) {
257 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
262 switch (cmd
->m_cmd
) {
264 // get version number from preamble
267 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
268 int offset
= GDBVersion
.match(m_output
, 0, &len
);
270 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
272 m_gdbMajor
= strtol(start
, &end
, 10);
273 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
275 // nothing was parsed
280 // assume some default version (what would make sense?)
284 // use a feasible core-file command
285 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
286 cmds
[DCcorefile
].fmt
= "target core %s\n";
288 cmds
[DCcorefile
].fmt
= "core-file %s\n";
295 /* ok, the command is ready */
296 emit
commandReceived(cmd
, m_output
);
298 switch (cmd
->m_cmd
) {
317 * The --fullname option makes gdb send a special normalized sequence print
318 * each time the program stops and at some other points. The sequence has
319 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
321 void GdbDriver::parseMarker()
323 char* startMarker
= strstr(m_output
, "\032\032");
324 if (startMarker
== 0)
327 // extract the marker
329 TRACE(QString("found marker: ") + startMarker
);
330 char* endMarker
= strchr(startMarker
, '\n');
336 // extract filename and line number
337 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
340 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
341 if (lineNoStart
>= 0) {
342 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
345 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
346 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
348 // now show the window
349 startMarker
[lineNoStart
] = '\0'; /* split off file name */
350 emit
activateFileLine(startMarker
, lineNo
-1, address
);
356 * Escapes characters that might lead to problems when they appear on gdb's
359 static void normalizeStringArg(QString
& arg
)
362 * Remove trailing backslashes. This approach is a little simplistic,
363 * but we know that there is at the moment no case where a trailing
364 * backslash would make sense.
366 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
367 arg
= arg
.left(arg
.length()-1);
373 #define LATIN1(str) ((str).isNull() ? "" : (str).data())
375 #define LATIN1(str) (str).latin1()
378 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
380 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
381 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
383 normalizeStringArg(strArg
);
386 // need the working directory when parsing the output
387 m_programWD
= strArg
;
388 } else if (cmd
== DCsetargs
) {
389 // attach saved redirection
393 strArg
+= m_redirect
;
396 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg
.length());
397 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg
));
401 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
403 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
404 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
406 SIZED_QString(cmdString
, MAX_FMTLEN
+30);
408 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
412 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
414 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
415 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
416 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
420 normalizeStringArg(strArg
);
422 SIZED_QString(cmdString
, MAX_FMTLEN
+30+strArg
.length());
427 * intArg specifies which channels should be redirected to
428 * /dev/null. It is a value or'ed together from RDNstdin,
429 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
432 * Note: We rely on that after the DCtty a DCsetargs will follow,
433 * which will ultimately apply the redirection.
435 static const char* const runRedir
[8] = {
439 " </dev/null >/dev/null",
441 " </dev/null 2>/dev/null",
443 " </dev/null >/dev/null 2>&1"
445 if (strArg
.isEmpty())
446 intArg
= 7; /* failsafe if no tty */
447 m_redirect
= runRedir
[intArg
& 7];
449 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
452 if (cmd
== DCexamine
) {
453 // make a format specifier from the intArg
454 static const char size
[16] = {
455 '\0', 'b', 'h', 'w', 'g'
457 static const char format
[16] = {
458 '\0', 'x', 'd', 'u', 'o', 't',
459 'a', 'c', 'f', 's', 'i'
461 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
462 assert(MDTformatmask
== 0xf0); /* next 4 bits */
463 int count
= 16; /* number of entities to print */
464 char sizeSpec
= size
[intArg
& MDTsizemask
];
465 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
466 assert(sizeSpec
!= '\0');
467 assert(formatSpec
!= '\0');
468 // adjust count such that 16 lines are printed
469 switch (intArg
& MDTformatmask
) {
470 case MDTstring
: case MDTinsn
:
471 break; /* no modification needed */
473 // all cases drop through:
474 switch (intArg
& MDTsizemask
) {
486 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
488 return makeCmdString(DCexamine
, spec
, strArg
);
491 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
493 // line numbers are zero-based
494 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
495 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
499 if (cmd
== DCinfoline
)
501 // must split off file name part
502 int slash
= strArg
.findRev('/');
504 strArg
= strArg
.right(strArg
.length()-slash
-1);
506 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg
), intArg
);
510 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, LATIN1(strArg
));
515 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
517 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
518 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
520 normalizeStringArg(strArg1
);
521 normalizeStringArg(strArg2
);
523 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg1
.length()+strArg2
.length());
524 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg1
), LATIN1(strArg2
));
528 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
530 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
531 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
533 SIZED_QString(cmdString
, MAX_FMTLEN
+60);
534 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
538 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
540 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
541 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
544 m_haveCoreFile
= false;
547 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
550 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
553 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
556 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
560 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
563 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
566 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
569 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
572 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
575 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
578 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
581 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
583 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
586 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
589 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
592 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
595 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
598 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
601 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
604 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
607 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
610 void GdbDriver::terminate()
616 void GdbDriver::detachAndTerminate()
620 executeCmdString(DCinitialize
, "detach\nquit\n", true);
623 void GdbDriver::interruptInferior()
626 // remove accidentally queued commands
630 static bool isErrorExpr(const char* output
)
633 strncmp(output
, "Cannot access memory at", 23) == 0 ||
634 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
635 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
636 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
637 strncmp(output
, "There is no member or method named", 34) == 0 ||
638 strncmp(output
, "No symbol \"", 11) == 0 ||
639 strncmp(output
, "Internal error: ", 16) == 0;
643 * Returns true if the output is an error message. If wantErrorValue is
644 * true, a new VarTree object is created and filled with the error message.
646 static bool parseErrorMessage(const char* output
,
647 VarTree
*& variable
, bool wantErrorValue
)
649 if (isErrorExpr(output
))
651 if (wantErrorValue
) {
652 // put the error message as value in the variable
653 variable
= new VarTree(QString(), VarTree::NKplain
);
654 const char* endMsg
= strchr(output
, '\n');
656 endMsg
= output
+ strlen(output
);
657 variable
->m_value
= FROM_LATIN1(output
, endMsg
-output
);
668 // this is the XChar2b on X11
671 static QString
toQString(QChar
* unicode
, int len
);
672 QChar() : row(0), cell(0) { }
673 QChar(char c
) : row(0), cell(c
) { }
674 operator char() const { return row
? 0 : cell
; }
677 QString
QChar::toQString(QChar
* unicode
, int len
)
679 QString
result(len
+1);
680 char* data
= result
.data();
683 data
[len
] = unicode
[len
].cell
;
690 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
)
692 VarTree
* variable
= 0;
695 * Parse off white space. gdb sometimes prints white space first if the
696 * printed array leaded to an error.
698 const char* p
= output
;
702 // check if this is an error indicating that gdb does not know about QString::null
703 if (cmds
[DCprintQStringStruct
].fmt
== printQStringStructFmt
&&
704 strncmp(p
, "Internal error: could not find static variable null", 51) == 0)
706 /* QString::null doesn't work, use an alternate expression */
707 cmds
[DCprintQStringStruct
].fmt
= printQStringStructNoNullFmt
;
708 // continue and let parseOffErrorExpr catch the error
711 // special case: empty string (0 repetitions)
712 if (strncmp(p
, "Invalid number 0 of repetitions", 31) == 0)
714 variable
= new VarTree(QString(), VarTree::NKplain
);
715 variable
->m_value
= "\"\"";
719 // check for error conditions
720 if (parseErrorMessage(p
, variable
, wantErrorValue
))
733 } while (isspace(*p
));
737 // this is the real data
743 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
745 * A matrix for separators between the individual "things"
746 * that are added to the string. The first index is a bool,
747 * the second index is from the enum above.
749 static const char* separator
[2][3] = {
750 { "\"", 0, ", \"" }, /* normal char is added */
751 { "'", "\", '", ", '" } /* repeated char is added */
754 while (isdigit(*p
)) {
757 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
759 goto error
; /* huh? no valid digits */
760 // skip separator and search for a repeat count
762 while (isspace(*p
) || *p
== ',')
764 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
766 const char* start
= p
;
767 p
= strchr(p
+9, '>'); /* search end and advance */
771 repeatCount
= FROM_LATIN1(start
, p
-start
);
772 while (isspace(*p
) || *p
== ',')
775 // p is now at the next char (or the end)
777 // interpret the value as a QChar
778 // TODO: make cross-architecture compatible
780 (unsigned short&)ch
= value
;
782 // escape a few frequently used characters
783 char escapeCode
= '\0';
785 case '\n': escapeCode
= 'n'; break;
786 case '\r': escapeCode
= 'r'; break;
787 case '\t': escapeCode
= 't'; break;
788 case '\b': escapeCode
= 'b'; break;
789 case '\"': escapeCode
= '\"'; break;
790 case '\\': escapeCode
= '\\'; break;
792 // since we only deal with ascii values must always escape '\0'
793 case '\0': escapeCode
= '0'; break;
795 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
800 result
+= separator
[repeats
][lastThing
];
802 if (escapeCode
!= '\0') {
808 // fixup repeat count and lastThing
811 result
+= repeatCount
;
812 lastThing
= wasRepeat
;
821 if (lastThing
== wasChar
)
825 variable
= new VarTree(QString(), VarTree::NKplain
);
826 variable
->m_value
= result
;
828 else if (strncmp(p
, "true", 4) == 0)
830 variable
= new VarTree(QString(), VarTree::NKplain
);
831 variable
->m_value
= "QString::null";
833 else if (strncmp(p
, "false", 5) == 0)
835 variable
= new VarTree(QString(), VarTree::NKplain
);
836 variable
->m_value
= "(null)";
843 if (wantErrorValue
) {
844 variable
= new VarTree(QString(), VarTree::NKplain
);
845 variable
->m_value
= "internal parse error";
850 static VarTree
* parseVar(const char*& s
)
859 VarTree::NameKind kind
;
860 if (!parseName(p
, name
, kind
)) {
868 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
871 // skip the '=' and more whitespace
876 VarTree
* variable
= new VarTree(name
, kind
);
877 variable
->setDeleteChildren(true);
879 if (!parseValue(p
, variable
)) {
887 static void skipNested(const char*& s
, char opening
, char closing
)
891 // parse a nested type
895 * Search for next matching `closing' char, skipping nested pairs of
896 * `opening' and `closing'.
898 while (*p
&& nest
> 0) {
901 } else if (*p
== closing
) {
907 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
912 void skipString(const char*& p
)
917 while (*p
!= quote
) {
919 // skip escaped character
920 // no special treatment for octal values necessary
923 // simply return if no more characters
931 * Strings can consist of several parts, some of which contain repeated
935 // look ahaead for <repeats 123 times>
939 if (strncmp(q
, "<repeats ", 9) == 0) {
941 while (*p
!= '\0' && *p
!= '>')
944 p
++; /* skip the '>' */
948 // is the string continued?
950 // look ahead for another quote
954 if (*q
== '"' || *q
== '\'') {
960 /* very long strings are followed by `...' */
961 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
966 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
970 // parse a nested expression
974 * Search for next matching `closing' char, skipping nested pairs of
975 * `opening' and `closing' as well as strings.
977 while (*p
&& nest
> 0) {
980 } else if (*p
== closing
) {
982 } else if (*p
== '\'' || *p
== '\"') {
989 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
994 inline void skipName(const char*& p
)
996 // allow : (for enumeration values) and $ and . (for _vtbl.)
997 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1001 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1003 kind
= VarTree::NKplain
;
1006 // examples of names:
1009 // <string<a,b<c>,7> >
1012 skipNested(p
, '<', '>');
1013 name
= FROM_LATIN1(s
, p
- s
);
1014 kind
= VarTree::NKtype
;
1018 // name, which might be "static"; allow dot for "_vtbl."
1021 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1025 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1026 kind
= VarTree::NKstatic
;
1028 // its a static variable, name comes now
1034 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1039 name
= FROM_LATIN1(s
, len
);
1041 // return the new position
1046 static bool parseValue(const char*& s
, VarTree
* variable
)
1048 variable
->m_value
= "";
1053 if (!parseNested(s
, variable
)) {
1056 // must be the closing brace
1058 TRACE("parse error: missing } of " + variable
->getText());
1062 // final white space
1066 // examples of leaf values (cannot be the empty string):
1072 // (DwContentType&) @0x8123456: {...}
1076 // 0x823abc <Array<int> virtual table>
1077 // (void (*)()) 0x8048480 <f(E *, char)>
1080 // &parseP (HTMLClueV *, char *)
1087 skipNested(p
, '(', ')');
1091 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1094 bool reference
= false;
1096 // skip reference marker
1100 const char* start
= p
;
1104 // some values consist of more than one token
1105 bool checkMultiPart
= false;
1107 if (p
[0] == '0' && p
[1] == 'x') {
1110 while (isxdigit(*p
))
1114 * Assume this is a pointer, but only if it's not a reference, since
1115 * references can't be expanded.
1118 variable
->m_varKind
= VarTree::VKpointer
;
1121 * References are followed by a colon, in which case we'll
1122 * find the value following the reference address.
1127 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1131 checkMultiPart
= true;
1132 } else if (isdigit(*p
)) {
1133 // parse decimal number, possibly a float
1136 if (*p
== '.') { /* TODO: obey i18n? */
1142 if (*p
== 'e' || *p
== 'E') {
1145 if (*p
== '-' || *p
== '+')
1151 // for char variables there is the char, eg. 10 '\n'
1152 checkMultiPart
= true;
1153 } else if (*p
== '<') {
1154 // e.g. <optimized out>
1155 skipNested(p
, '<', '>');
1156 } else if (*p
== '"' || *p
== '\'') {
1157 // character may have multipart: '\000' <repeats 11 times>
1158 checkMultiPart
= *p
== '\'';
1161 } else if (*p
== '&') {
1165 while (isspace(*p
)) {
1169 skipNested(p
, '(', ')');
1172 // must be an enumeration value
1175 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1177 if (checkMultiPart
) {
1181 // may be followed by a string or <...>
1184 if (*p
== '"' || *p
== '\'') {
1186 } else if (*p
== '<') {
1187 skipNested(p
, '<', '>');
1190 // there is always a blank before the string,
1191 // which we will include in the final string value
1192 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1193 // if this was a pointer, reset that flag since we
1194 // now got the value
1195 variable
->m_varKind
= VarTree::VKsimple
;
1199 if (variable
->m_value
.length() == 0) {
1200 TRACE("parse error: no value for " + variable
->getText());
1204 // final white space
1210 * If this was a reference, the value follows. It might even be a
1211 * composite variable!
1217 if (variable
->m_varKind
== VarTree::VKpointer
) {
1218 variable
->setDelayedExpanding(true);
1225 static bool parseNested(const char*& s
, VarTree
* variable
)
1227 // could be a structure or an array
1232 bool isStruct
= false;
1234 * If there is a name followed by an = or an < -- which starts a type
1235 * name -- or "static", it is a structure
1237 if (*p
== '<' || *p
== '}') {
1239 } else if (strncmp(p
, "static ", 7) == 0) {
1241 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1242 // look ahead for a comma after the name
1249 p
= s
; /* rescan the name */
1252 if (!parseVarSeq(p
, variable
)) {
1255 variable
->m_varKind
= VarTree::VKstruct
;
1257 if (!parseValueSeq(p
, variable
)) {
1260 variable
->m_varKind
= VarTree::VKarray
;
1266 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1268 // parse a comma-separated sequence of variables
1269 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1273 if (strncmp(s
, "<No data fields>}", 17) == 0)
1275 // no member variables, so break out immediately
1276 s
+= 16; /* go to the closing brace */
1281 break; /* syntax error */
1282 variable
->appendChild(var
);
1285 // skip the comma and whitespace
1293 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1295 // parse a comma-separated sequence of variables
1300 name
.sprintf("[%d]", index
);
1302 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1303 var
->setDeleteChildren(true);
1304 good
= parseValue(s
, var
);
1309 variable
->appendChild(var
);
1313 // skip the command and whitespace
1317 // sometimes there is a closing brace after a comma
1324 #if QT_VERSION < 200
1325 #define ISSPACE(c) isspace((c))
1328 #define ISSPACE(c) (c).isSpace()
1332 * Parses a stack frame.
1334 static void parseFrameInfo(const char*& s
, QString
& func
,
1335 QString
& file
, int& lineNo
, DbgAddr
& address
)
1339 // next may be a hexadecimal address
1341 const char* start
= p
;
1345 while (isxdigit(*p
))
1347 address
= FROM_LATIN1(start
, p
-start
);
1348 if (strncmp(p
, " in ", 4) == 0)
1351 const char* start
= p
;
1352 // search opening parenthesis
1353 while (*p
!= '\0' && *p
!= '(')
1363 * Skip parameters. But notice that for complicated conversion
1364 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1365 * pointer to function) as well as operator()(...) we have to skip
1366 * additional pairs of parentheses.
1369 skipNestedWithString(p
, '(', ')');
1372 } while (*p
== '(');
1374 // check for file position
1375 if (strncmp(p
, "at ", 3) == 0) {
1377 const char* fileStart
= p
;
1378 // go for the end of the line
1379 while (*p
!= '\0' && *p
!= '\n')
1381 // search back for colon
1382 const char* colon
= p
;
1385 } while (*colon
!= ':');
1386 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1387 lineNo
= atoi(colon
+1)-1;
1392 // check for "from shared lib"
1393 if (strncmp(p
, "from ", 5) == 0) {
1395 // go for the end of the line
1396 while (*p
!= '\0' && *p
!= '\n')
1405 // construct the function name (including file info)
1409 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1414 * Replace \n (and whitespace around it) in func by a blank. We cannot
1415 * use QString::simplifyWhiteSpace() for this because this would also
1416 * simplify space that belongs to a string arguments that gdb sometimes
1417 * prints in the argument lists of the function.
1419 ASSERT(!ISSPACE(func
[0])); /* there must be non-white before first \n */
1421 while ((nl
= func
.find('\n', nl
)) >= 0) {
1422 // search back to the beginning of the whitespace
1423 int startWhite
= nl
;
1426 } while (ISSPACE(func
[startWhite
]));
1428 // search forward to the end of the whitespace
1431 } while (ISSPACE(func
[nl
]));
1433 func
.replace(startWhite
, nl
-startWhite
, " ");
1434 /* continue searching for more \n's at this place: */
1443 * Parses a stack frame including its frame number
1445 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1446 QString
& file
, int& lineNo
, DbgAddr
& address
)
1449 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1451 // must start with a hash mark followed by number
1452 if (s
[0] != '#' || !isdigit(s
[1]))
1455 s
++; /* skip the hash mark */
1463 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1467 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1470 int lineNo
, frameNo
;
1473 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1474 StackFrame
* frm
= new StackFrame
;
1475 frm
->frameNo
= frameNo
;
1476 frm
->fileName
= file
;
1477 frm
->lineNo
= lineNo
;
1478 frm
->address
= address
;
1479 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1484 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1485 QString
& file
, int& lineNo
, DbgAddr
& address
)
1488 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1492 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1494 // skip first line, which is the headline
1495 const char* p
= strchr(output
, '\n');
1506 uint ignoreCount
= 0;
1510 while (*p
!= '\0') {
1512 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1517 Breakpoint::Type bpType
;
1518 if (strncmp(p
, "breakpoint", 10) == 0) {
1519 bpType
= Breakpoint::breakpoint
;
1521 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1522 bpType
= Breakpoint::watchpoint
;
1524 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1525 bpType
= Breakpoint::watchpoint
;
1534 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1542 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1548 // the address, if present
1549 if (bpType
== Breakpoint::breakpoint
&&
1550 strncmp(p
, "0x", 2) == 0)
1552 const char* start
= p
;
1553 while (*p
!= '\0' && !isspace(*p
))
1555 address
= FROM_LATIN1(start
, p
-start
);
1556 while (isspace(*p
) && *p
!= '\n')
1561 address
= QString();
1563 // remainder is location, hit and ignore count, condition
1566 condition
= QString();
1567 end
= strchr(p
, '\n');
1570 p
+= location
.length();
1572 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1573 p
= end
+1; /* skip over \n */
1576 // may be continued in next line
1577 while (isspace(*p
)) { /* p points to beginning of line */
1578 // skip white space at beginning of line
1583 end
= strchr(p
, '\n');
1587 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1588 // extract the hit count
1590 hits
= strtol(p
, &dummy
, 10);
1591 TRACE(QString().sprintf("hit count %d", hits
));
1592 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1593 // extract condition
1595 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1596 TRACE("condition: "+condition
);
1597 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1598 // extract ignore count
1600 ignoreCount
= strtol(p
, &dummy
, 10);
1601 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1603 // indeed a continuation
1604 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1608 p
++; /* skip '\n' */
1610 Breakpoint
* bp
= new Breakpoint
;
1613 bp
->temporary
= disp
== 'd';
1614 bp
->enabled
= enable
== 'y';
1615 bp
->location
= location
;
1616 bp
->address
= address
;
1617 bp
->hitCount
= hits
;
1618 bp
->ignoreCount
= ignoreCount
;
1619 bp
->condition
= condition
;
1625 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1627 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1638 const char* p
= output
;
1639 while (*p
!= '\0') {
1640 // seach look for thread id, watching out for the focus indicator
1641 bool hasFocus
= false;
1642 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1647 // there follows only whitespace
1650 id
= strtol(p
, &end
, 10);
1652 // syntax error: no number found; bail out
1662 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1664 end
= strstr(p
, " ");
1666 // syntax error; bail out
1669 systag
= FROM_LATIN1(p
, end
-p
);
1673 * Now follows a standard stack frame. Sometimes, however, gdb
1674 * catches a thread at an instant where it doesn't have a stack.
1676 if (strncmp(p
, "[No stack.]", 11) != 0) {
1677 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1679 func
= "[No stack]";
1682 address
= QString();
1683 p
+= 11; /* \n is skipped above */
1686 ThreadInfo
* thr
= new ThreadInfo
;
1688 thr
->threadName
= systag
;
1689 thr
->hasFocus
= hasFocus
;
1690 thr
->function
= func
;
1691 thr
->fileName
= file
;
1692 thr
->lineNo
= lineNo
;
1693 thr
->address
= address
;
1694 threads
.append(thr
);
1699 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1700 QString
& file
, int& lineNo
)
1702 const char* o
= output
;
1703 // skip lines of that begin with "(Cannot find"
1704 while (strncmp(o
, "(Cannot find", 12) == 0) {
1705 o
= strchr(o
, '\n');
1708 o
++; /* skip newline */
1711 if (strncmp(o
, "Breakpoint ", 11) != 0)
1715 output
+= 11; /* skip "Breakpoint " */
1717 int num
= strtoul(output
, &p
, 10);
1722 char* fileStart
= strstr(p
, "file ");
1728 char* numStart
= strstr(fileStart
, ", line ");
1729 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1731 int line
= strtoul(numStart
, &p
, 10);
1737 lineNo
= line
-1; /* zero-based! */
1741 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1743 // check for possible error conditions
1744 if (strncmp(output
, "No symbol table", 15) == 0)
1749 while (*output
!= '\0') {
1750 while (isspace(*output
))
1752 if (*output
== '\0')
1754 // skip occurrences of "No locals" and "No args"
1755 if (strncmp(output
, "No locals", 9) == 0 ||
1756 strncmp(output
, "No arguments", 12) == 0)
1758 output
= strchr(output
, '\n');
1765 VarTree
* variable
= parseVar(output
);
1766 if (variable
== 0) {
1769 // do not add duplicates
1770 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
1771 if (o
->getText() == variable
->getText()) {
1776 newVars
.append(variable
);
1781 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
1783 // check for error conditions
1784 if (parseErrorMessage(output
, var
, wantErrorValue
))
1788 // parse the variable
1789 var
= parseVar(output
);
1794 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
1796 bool isGood
= false;
1797 message
= QString(output
).simplifyWhiteSpace();
1798 if (message
.isEmpty()) {
1799 message
= i18n("New working directory: ") + m_programWD
;
1805 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
1809 m_haveCoreFile
= false;
1812 * The command is successful if there is no output or the single
1813 * message (no debugging symbols found)...
1816 output
[0] == '\0' ||
1817 strcmp(output
, "(no debugging symbols found)...") == 0;
1820 bool GdbDriver::parseCoreFile(const char* output
)
1822 // if command succeeded, gdb emits a line starting with "#0 "
1823 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
1824 return m_haveCoreFile
;
1827 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
1829 // optionally: "program changed, rereading symbols",
1831 // "Program exited normally"
1832 // "Program terminated with wignal SIGSEGV"
1833 // "Program received signal SIGINT" or other signal
1836 // go through the output, line by line, checking what we have
1837 const char* start
= output
- 1;
1838 uint flags
= SFprogramActive
;
1839 message
= QString();
1841 start
++; /* skip '\n' */
1843 if (strncmp(start
, "Program ", 8) == 0 ||
1844 strncmp(start
, "ptrace: ", 8) == 0) {
1846 * When we receive a signal, the program remains active.
1848 * Special: If we "stopped" in a corefile, the string "Program
1849 * terminated with signal"... is displayed. (Normally, we see
1850 * "Program received signal"... when a signal happens.)
1852 if (strncmp(start
, "Program exited", 14) == 0 ||
1853 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
1854 strncmp(start
, "ptrace: ", 8) == 0)
1856 flags
&= ~SFprogramActive
;
1860 const char* endOfMessage
= strchr(start
, '\n');
1861 if (endOfMessage
== 0)
1862 endOfMessage
= start
+ strlen(start
);
1863 message
= FROM_LATIN1(start
, endOfMessage
-start
);
1864 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
1866 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
1867 * that it stopped at a temporary breakpoint).
1869 flags
|= SFrefreshBreak
;
1870 } else if (strstr(start
, "re-reading symbols.") != 0) {
1871 flags
|= SFrefreshSource
;
1874 // next line, please
1875 start
= strchr(start
, '\n');
1876 } while (start
!= 0);
1879 * Gdb only notices when new threads have appeared, but not when a
1880 * thread finishes. So we always have to assume that the list of
1881 * threads has changed.
1883 flags
|= SFrefreshThreads
;
1888 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
1890 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
1893 // parse the table of shared libraries
1895 // strip off head line
1896 output
= strchr(output
, '\n');
1899 output
++; /* skip '\n' */
1901 while (*output
!= '\0') {
1902 // format of a line is
1903 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
1904 // 3 blocks of non-space followed by space
1905 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
1906 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
1909 while (isspace(*output
)) { /* space */
1913 if (*output
== '\0')
1915 const char* start
= output
;
1916 output
= strchr(output
, '\n');
1918 output
= start
+ strlen(start
);
1919 shlibName
= FROM_LATIN1(start
, output
-start
);
1920 if (*output
!= '\0')
1922 shlibs
.append(shlibName
);
1923 TRACE("found shared lib " + shlibName
);
1927 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
1929 if (strncmp(output
, "type = ", 7) != 0)
1933 * Everything else is the type. We strip off all white-space from the
1938 type
.replace(QRegExp("\\s+"), "");
1942 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
1944 if (strncmp(output
, "The program has no registers now", 32) == 0) {
1951 // parse register values
1952 while (*output
!= '\0')
1954 // skip space at the start of the line
1955 while (isspace(*output
))
1959 const char* start
= output
;
1960 while (*output
!= '\0' && !isspace(*output
))
1962 if (*output
== '\0')
1964 regName
= FROM_LATIN1(start
, output
-start
);
1967 while (isspace(*output
))
1969 // the rest of the line is the register value
1971 output
= strchr(output
,'\n');
1973 output
= start
+ strlen(start
);
1974 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
1976 if (*output
!= '\0')
1977 output
++; /* skip '\n' */
1979 RegisterInfo
* reg
= new RegisterInfo
;
1980 reg
->regName
= regName
;
1983 * We split the raw from the cooked values. For this purpose, we
1984 * split off the first token (separated by whitespace). It is the
1985 * raw value. The remainder of the line is the cooked value.
1987 int pos
= value
.find(' ');
1989 reg
->rawValue
= value
;
1990 reg
->cookedValue
= QString();
1992 reg
->rawValue
= value
.left(pos
);
1993 reg
->cookedValue
= value
.mid(pos
+1,value
.length());
1995 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
1996 * Here the raw value is actually in the second part.
1998 if (reg
->cookedValue
.left(5) == "(raw ") {
1999 QString raw
= reg
->cookedValue
.right(reg
->cookedValue
.length()-5);
2000 if (raw
[raw
.length()-1] == ')') /* remove closing bracket */
2001 raw
= raw
.left(raw
.length()-1);
2002 reg
->cookedValue
= reg
->rawValue
;
2003 reg
->rawValue
= raw
;
2011 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2013 const char* start
= strstr(output
, "starts at address ");
2018 const char* p
= start
;
2019 while (*p
!= '\0' && !isspace(*p
))
2021 addrFrom
= FROM_LATIN1(start
, p
-start
);
2023 start
= strstr(p
, "and ends at ");
2029 while (*p
!= '\0' && !isspace(*p
))
2031 addrTo
= FROM_LATIN1(start
, p
-start
);
2036 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2040 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2044 code
.append(new DisassembledCode(c
));
2048 // remove first line
2049 const char* p
= strchr(output
, '\n');
2051 return; /* not a regular output */
2056 const char* end
= strstr(output
, "\nEnd of assembler");
2058 end
= p
+ strlen(p
);
2062 // remove function offsets from the lines
2065 const char* start
= p
;
2067 while (p
!= end
&& !isspace(*p
))
2069 address
= FROM_LATIN1(start
, p
-start
);
2071 // function name (enclosed in '<>', followed by ':')
2072 while (p
!= end
&& *p
!= '<')
2075 skipNested(p
, '<', '>');
2080 while (p
!= end
&& isspace(*p
))
2083 // code until end of line
2085 while (p
!= end
&& *p
!= '\n')
2087 if (p
!= end
) /* include '\n' */
2090 DisassembledCode
* c
= new DisassembledCode
;
2091 c
->address
= address
;
2092 c
->code
= FROM_LATIN1(start
, p
-start
);
2097 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2099 if (isErrorExpr(output
)) {
2100 // error; strip space
2101 QString msg
= output
;
2102 return msg
.stripWhiteSpace();
2105 const char* p
= output
; /* save typing */
2111 const char* start
= p
;
2112 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2114 addr
= FROM_LATIN1(start
, p
-start
);
2116 // parse function offset
2120 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2122 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2126 // skip space; this may skip a new-line char!
2129 // everything to the end of the line is the memory dump
2130 const char* end
= strchr(p
, '\n');
2132 dump
= FROM_LATIN1(p
, end
-p
);
2135 dump
= FROM_LATIN1(p
, strlen(p
));
2138 MemoryDump
* md
= new MemoryDump
;
2148 #include "gdbdriver.moc"