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_promptMinLen
= 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
;
689 #if QT_VERSION >= 300
699 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
701 VarTree
* variable
= 0;
704 * Parse off white space. gdb sometimes prints white space first if the
705 * printed array leaded to an error.
707 const char* p
= output
;
711 // check if this is an error indicating that gdb does not know about QString::null
712 if (cmds
[DCprintQStringStruct
].fmt
== printQStringStructFmt
&&
713 strncmp(p
, "Internal error: could not find static variable null", 51) == 0)
715 /* QString::null doesn't work, use an alternate expression */
716 cmds
[DCprintQStringStruct
].fmt
= printQStringStructNoNullFmt
;
717 // continue and let parseOffErrorExpr catch the error
720 // special case: empty string (0 repetitions)
721 if (strncmp(p
, "Invalid number 0 of repetitions", 31) == 0)
723 variable
= new VarTree(QString(), VarTree::NKplain
);
724 variable
->m_value
= "\"\"";
728 // check for error conditions
729 if (parseErrorMessage(p
, variable
, wantErrorValue
))
742 } while (isspace(*p
));
746 // this is the real data
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
)) {
766 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
768 goto error
; /* huh? no valid digits */
769 // skip separator and search for a repeat count
771 while (isspace(*p
) || *p
== ',')
773 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
775 const char* start
= p
;
776 p
= strchr(p
+9, '>'); /* search end and advance */
780 repeatCount
= FROM_LATIN1(start
, p
-start
);
781 while (isspace(*p
) || *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
793 (unsigned short&)ch
= value
;
797 ch
.setRow(c
.qch
.row
);
798 ch
.setCell(c
.qch
.cell
);
802 // escape a few frequently used characters
803 char escapeCode
= '\0';
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;
812 // since we only deal with ascii values must always escape '\0'
813 case '\0': escapeCode
= '0'; break;
815 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
820 result
+= separator
[repeats
][lastThing
];
822 if (escapeCode
!= '\0') {
828 // fixup repeat count and lastThing
831 result
+= repeatCount
;
832 lastThing
= wasRepeat
;
841 if (lastThing
== wasChar
)
845 variable
= new VarTree(QString(), VarTree::NKplain
);
846 variable
->m_value
= result
;
848 else if (strncmp(p
, "true", 4) == 0)
850 variable
= new VarTree(QString(), VarTree::NKplain
);
851 variable
->m_value
= "QString::null";
853 else if (strncmp(p
, "false", 5) == 0)
855 variable
= new VarTree(QString(), VarTree::NKplain
);
856 variable
->m_value
= "(null)";
863 if (wantErrorValue
) {
864 variable
= new VarTree(QString(), VarTree::NKplain
);
865 variable
->m_value
= "internal parse error";
870 static VarTree
* parseVar(const char*& s
)
879 VarTree::NameKind kind
;
880 if (!parseName(p
, name
, kind
)) {
888 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
891 // skip the '=' and more whitespace
896 VarTree
* variable
= new VarTree(name
, kind
);
897 variable
->setDeleteChildren(true);
899 if (!parseValue(p
, variable
)) {
907 static void skipNested(const char*& s
, char opening
, char closing
)
911 // parse a nested type
915 * Search for next matching `closing' char, skipping nested pairs of
916 * `opening' and `closing'.
918 while (*p
&& nest
> 0) {
921 } else if (*p
== closing
) {
927 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
932 void skipString(const char*& p
)
937 while (*p
!= quote
) {
939 // skip escaped character
940 // no special treatment for octal values necessary
943 // simply return if no more characters
951 * Strings can consist of several parts, some of which contain repeated
955 // look ahaead for <repeats 123 times>
959 if (strncmp(q
, "<repeats ", 9) == 0) {
961 while (*p
!= '\0' && *p
!= '>')
964 p
++; /* skip the '>' */
968 // is the string continued?
970 // look ahead for another quote
974 if (*q
== '"' || *q
== '\'') {
980 /* very long strings are followed by `...' */
981 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
986 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
990 // parse a nested expression
994 * Search for next matching `closing' char, skipping nested pairs of
995 * `opening' and `closing' as well as strings.
997 while (*p
&& nest
> 0) {
1000 } else if (*p
== closing
) {
1002 } else if (*p
== '\'' || *p
== '\"') {
1009 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1014 inline void skipName(const char*& p
)
1016 // allow : (for enumeration values) and $ and . (for _vtbl.)
1017 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1021 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1023 kind
= VarTree::NKplain
;
1026 // examples of names:
1029 // <string<a,b<c>,7> >
1032 skipNested(p
, '<', '>');
1033 name
= FROM_LATIN1(s
, p
- s
);
1034 kind
= VarTree::NKtype
;
1038 // name, which might be "static"; allow dot for "_vtbl."
1041 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1045 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1046 kind
= VarTree::NKstatic
;
1048 // its a static variable, name comes now
1054 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1059 name
= FROM_LATIN1(s
, len
);
1061 // return the new position
1066 static bool parseValue(const char*& s
, VarTree
* variable
)
1068 variable
->m_value
= "";
1073 if (!parseNested(s
, variable
)) {
1076 // must be the closing brace
1078 TRACE("parse error: missing } of " + variable
->getText());
1082 // final white space
1086 // examples of leaf values (cannot be the empty string):
1092 // (DwContentType&) @0x8123456: {...}
1096 // 0x823abc <Array<int> virtual table>
1097 // (void (*)()) 0x8048480 <f(E *, char)>
1100 // &parseP (HTMLClueV *, char *)
1107 skipNested(p
, '(', ')');
1111 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1114 bool reference
= false;
1116 // skip reference marker
1120 const char* start
= p
;
1124 // some values consist of more than one token
1125 bool checkMultiPart
= false;
1127 if (p
[0] == '0' && p
[1] == 'x') {
1130 while (isxdigit(*p
))
1134 * Assume this is a pointer, but only if it's not a reference, since
1135 * references can't be expanded.
1138 variable
->m_varKind
= VarTree::VKpointer
;
1141 * References are followed by a colon, in which case we'll
1142 * find the value following the reference address.
1147 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1151 checkMultiPart
= true;
1152 } else if (isdigit(*p
)) {
1153 // parse decimal number, possibly a float
1156 if (*p
== '.') { /* TODO: obey i18n? */
1162 if (*p
== 'e' || *p
== 'E') {
1165 if (*p
== '-' || *p
== '+')
1171 // for char variables there is the char, eg. 10 '\n'
1172 checkMultiPart
= true;
1173 } else if (*p
== '<') {
1174 // e.g. <optimized out>
1175 skipNested(p
, '<', '>');
1176 } else if (*p
== '"' || *p
== '\'') {
1177 // character may have multipart: '\000' <repeats 11 times>
1178 checkMultiPart
= *p
== '\'';
1181 } else if (*p
== '&') {
1185 while (isspace(*p
)) {
1189 skipNested(p
, '(', ')');
1192 // must be an enumeration value
1195 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1197 if (checkMultiPart
) {
1201 // may be followed by a string or <...>
1204 if (*p
== '"' || *p
== '\'') {
1206 } else if (*p
== '<') {
1207 // if this value is part of an array, it might be followed
1208 // by <repeats 15 times>, which we don't skip here
1209 if (strncmp(p
, "<repeats ", 9) != 0)
1210 skipNested(p
, '<', '>');
1213 // there is always a blank before the string,
1214 // which we will include in the final string value
1215 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1216 // if this was a pointer, reset that flag since we
1217 // now got the value
1218 variable
->m_varKind
= VarTree::VKsimple
;
1222 if (variable
->m_value
.length() == 0) {
1223 TRACE("parse error: no value for " + variable
->getText());
1227 // final white space
1233 * If this was a reference, the value follows. It might even be a
1234 * composite variable!
1240 if (variable
->m_varKind
== VarTree::VKpointer
) {
1241 variable
->setDelayedExpanding(true);
1248 static bool parseNested(const char*& s
, VarTree
* variable
)
1250 // could be a structure or an array
1255 bool isStruct
= false;
1257 * If there is a name followed by an = or an < -- which starts a type
1258 * name -- or "static", it is a structure
1260 if (*p
== '<' || *p
== '}') {
1262 } else if (strncmp(p
, "static ", 7) == 0) {
1264 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1265 // look ahead for a comma after the name
1272 p
= s
; /* rescan the name */
1275 if (!parseVarSeq(p
, variable
)) {
1278 variable
->m_varKind
= VarTree::VKstruct
;
1280 if (!parseValueSeq(p
, variable
)) {
1283 variable
->m_varKind
= VarTree::VKarray
;
1289 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1291 // parse a comma-separated sequence of variables
1292 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1296 if (strncmp(s
, "<No data fields>}", 17) == 0)
1298 // no member variables, so break out immediately
1299 s
+= 16; /* go to the closing brace */
1304 break; /* syntax error */
1305 variable
->appendChild(var
);
1308 // skip the comma and whitespace
1316 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1318 // parse a comma-separated sequence of variables
1323 name
.sprintf("[%d]", index
);
1324 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1325 var
->setDeleteChildren(true);
1326 good
= parseValue(s
, var
);
1331 // a value may be followed by "<repeats 45 times>"
1332 if (strncmp(s
, "<repeats ", 9) == 0) {
1335 int l
= strtol(s
, &end
, 10);
1336 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1337 // should not happen
1341 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1342 // replace name and advance index
1343 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1346 // skip " times>" and space
1348 // possible final space
1354 variable
->appendChild(var
);
1358 // skip the comma and whitespace
1362 // sometimes there is a closing brace after a comma
1369 #if QT_VERSION < 200
1370 #define ISSPACE(c) isspace((c))
1373 #define ISSPACE(c) (c).isSpace()
1377 * Parses a stack frame.
1379 static void parseFrameInfo(const char*& s
, QString
& func
,
1380 QString
& file
, int& lineNo
, DbgAddr
& address
)
1384 // next may be a hexadecimal address
1386 const char* start
= p
;
1390 while (isxdigit(*p
))
1392 address
= FROM_LATIN1(start
, p
-start
);
1393 if (strncmp(p
, " in ", 4) == 0)
1396 address
= DbgAddr();
1398 const char* start
= p
;
1399 // check for special signal handler frame
1400 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1401 func
= FROM_LATIN1(start
, 23);
1409 // search opening parenthesis
1410 while (*p
!= '\0' && *p
!= '(')
1420 * Skip parameters. But notice that for complicated conversion
1421 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1422 * pointer to function) as well as operator()(...) we have to skip
1423 * additional pairs of parentheses.
1426 skipNestedWithString(p
, '(', ')');
1429 } while (*p
== '(');
1431 // check for file position
1432 if (strncmp(p
, "at ", 3) == 0) {
1434 const char* fileStart
= p
;
1435 // go for the end of the line
1436 while (*p
!= '\0' && *p
!= '\n')
1438 // search back for colon
1439 const char* colon
= p
;
1442 } while (*colon
!= ':');
1443 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1444 lineNo
= atoi(colon
+1)-1;
1449 // check for "from shared lib"
1450 if (strncmp(p
, "from ", 5) == 0) {
1452 // go for the end of the line
1453 while (*p
!= '\0' && *p
!= '\n')
1462 // construct the function name (including file info)
1466 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1471 * Replace \n (and whitespace around it) in func by a blank. We cannot
1472 * use QString::simplifyWhiteSpace() for this because this would also
1473 * simplify space that belongs to a string arguments that gdb sometimes
1474 * prints in the argument lists of the function.
1476 ASSERT(!ISSPACE(func
[0])); /* there must be non-white before first \n */
1478 while ((nl
= func
.find('\n', nl
)) >= 0) {
1479 // search back to the beginning of the whitespace
1480 int startWhite
= nl
;
1483 } while (ISSPACE(func
[startWhite
]));
1485 // search forward to the end of the whitespace
1488 } while (ISSPACE(func
[nl
]));
1490 func
.replace(startWhite
, nl
-startWhite
, " ");
1491 /* continue searching for more \n's at this place: */
1499 * Parses a stack frame including its frame number
1501 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1502 QString
& file
, int& lineNo
, DbgAddr
& address
)
1505 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1507 // must start with a hash mark followed by number
1508 if (s
[0] != '#' || !isdigit(s
[1]))
1511 s
++; /* skip the hash mark */
1519 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1523 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1526 int lineNo
, frameNo
;
1529 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1530 StackFrame
* frm
= new StackFrame
;
1531 frm
->frameNo
= frameNo
;
1532 frm
->fileName
= file
;
1533 frm
->lineNo
= lineNo
;
1534 frm
->address
= address
;
1535 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1540 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1541 QString
& file
, int& lineNo
, DbgAddr
& address
)
1544 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1548 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1550 // skip first line, which is the headline
1551 const char* p
= strchr(output
, '\n');
1562 uint ignoreCount
= 0;
1566 while (*p
!= '\0') {
1568 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1573 Breakpoint::Type bpType
;
1574 if (strncmp(p
, "breakpoint", 10) == 0) {
1575 bpType
= Breakpoint::breakpoint
;
1577 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1578 bpType
= Breakpoint::watchpoint
;
1580 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1581 bpType
= Breakpoint::watchpoint
;
1590 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1598 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1604 // the address, if present
1605 if (bpType
== Breakpoint::breakpoint
&&
1606 strncmp(p
, "0x", 2) == 0)
1608 const char* start
= p
;
1609 while (*p
!= '\0' && !isspace(*p
))
1611 address
= FROM_LATIN1(start
, p
-start
);
1612 while (isspace(*p
) && *p
!= '\n')
1617 address
= QString();
1619 // remainder is location, hit and ignore count, condition
1622 condition
= QString();
1623 end
= strchr(p
, '\n');
1626 p
+= location
.length();
1628 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1629 p
= end
+1; /* skip over \n */
1632 // may be continued in next line
1633 while (isspace(*p
)) { /* p points to beginning of line */
1634 // skip white space at beginning of line
1639 end
= strchr(p
, '\n');
1643 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1644 // extract the hit count
1646 hits
= strtol(p
, &dummy
, 10);
1647 TRACE(QString().sprintf("hit count %d", hits
));
1648 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1649 // extract condition
1651 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1652 TRACE("condition: "+condition
);
1653 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1654 // extract ignore count
1656 ignoreCount
= strtol(p
, &dummy
, 10);
1657 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1659 // indeed a continuation
1660 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1664 p
++; /* skip '\n' */
1666 Breakpoint
* bp
= new Breakpoint
;
1669 bp
->temporary
= disp
== 'd';
1670 bp
->enabled
= enable
== 'y';
1671 bp
->location
= location
;
1672 bp
->address
= address
;
1673 bp
->hitCount
= hits
;
1674 bp
->ignoreCount
= ignoreCount
;
1675 bp
->condition
= condition
;
1681 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1683 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1694 const char* p
= output
;
1695 while (*p
!= '\0') {
1696 // seach look for thread id, watching out for the focus indicator
1697 bool hasFocus
= false;
1698 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1703 // there follows only whitespace
1706 id
= strtol(p
, &end
, 10);
1708 // syntax error: no number found; bail out
1718 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1720 end
= strstr(p
, " ");
1722 // syntax error; bail out
1725 systag
= FROM_LATIN1(p
, end
-p
);
1729 * Now follows a standard stack frame. Sometimes, however, gdb
1730 * catches a thread at an instant where it doesn't have a stack.
1732 if (strncmp(p
, "[No stack.]", 11) != 0) {
1733 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1735 func
= "[No stack]";
1738 address
= QString();
1739 p
+= 11; /* \n is skipped above */
1742 ThreadInfo
* thr
= new ThreadInfo
;
1744 thr
->threadName
= systag
;
1745 thr
->hasFocus
= hasFocus
;
1746 thr
->function
= func
;
1747 thr
->fileName
= file
;
1748 thr
->lineNo
= lineNo
;
1749 thr
->address
= address
;
1750 threads
.append(thr
);
1755 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1756 QString
& file
, int& lineNo
)
1758 const char* o
= output
;
1759 // skip lines of that begin with "(Cannot find"
1760 while (strncmp(o
, "(Cannot find", 12) == 0) {
1761 o
= strchr(o
, '\n');
1764 o
++; /* skip newline */
1767 if (strncmp(o
, "Breakpoint ", 11) != 0)
1771 output
+= 11; /* skip "Breakpoint " */
1773 int num
= strtoul(output
, &p
, 10);
1778 char* fileStart
= strstr(p
, "file ");
1784 char* numStart
= strstr(fileStart
, ", line ");
1785 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1787 int line
= strtoul(numStart
, &p
, 10);
1793 lineNo
= line
-1; /* zero-based! */
1797 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1799 // check for possible error conditions
1800 if (strncmp(output
, "No symbol table", 15) == 0)
1805 while (*output
!= '\0') {
1806 while (isspace(*output
))
1808 if (*output
== '\0')
1810 // skip occurrences of "No locals" and "No args"
1811 if (strncmp(output
, "No locals", 9) == 0 ||
1812 strncmp(output
, "No arguments", 12) == 0)
1814 output
= strchr(output
, '\n');
1821 VarTree
* variable
= parseVar(output
);
1822 if (variable
== 0) {
1825 // do not add duplicates
1826 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
1827 if (o
->getText() == variable
->getText()) {
1832 newVars
.append(variable
);
1837 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
1839 // check for error conditions
1840 if (parseErrorMessage(output
, var
, wantErrorValue
))
1844 // parse the variable
1845 var
= parseVar(output
);
1850 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
1852 bool isGood
= false;
1853 message
= QString(output
).simplifyWhiteSpace();
1854 if (message
.isEmpty()) {
1855 message
= i18n("New working directory: ") + m_programWD
;
1861 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
1865 m_haveCoreFile
= false;
1868 * The command is successful if there is no output or the single
1869 * message (no debugging symbols found)...
1872 output
[0] == '\0' ||
1873 strcmp(output
, "(no debugging symbols found)...") == 0;
1876 bool GdbDriver::parseCoreFile(const char* output
)
1878 // if command succeeded, gdb emits a line starting with "#0 "
1879 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
1880 return m_haveCoreFile
;
1883 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
1885 // optionally: "program changed, rereading symbols",
1887 // "Program exited normally"
1888 // "Program terminated with wignal SIGSEGV"
1889 // "Program received signal SIGINT" or other signal
1892 // go through the output, line by line, checking what we have
1893 const char* start
= output
- 1;
1894 uint flags
= SFprogramActive
;
1895 message
= QString();
1897 start
++; /* skip '\n' */
1899 if (strncmp(start
, "Program ", 8) == 0 ||
1900 strncmp(start
, "ptrace: ", 8) == 0) {
1902 * When we receive a signal, the program remains active.
1904 * Special: If we "stopped" in a corefile, the string "Program
1905 * terminated with signal"... is displayed. (Normally, we see
1906 * "Program received signal"... when a signal happens.)
1908 if (strncmp(start
, "Program exited", 14) == 0 ||
1909 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
1910 strncmp(start
, "ptrace: ", 8) == 0)
1912 flags
&= ~SFprogramActive
;
1916 const char* endOfMessage
= strchr(start
, '\n');
1917 if (endOfMessage
== 0)
1918 endOfMessage
= start
+ strlen(start
);
1919 message
= FROM_LATIN1(start
, endOfMessage
-start
);
1920 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
1922 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
1923 * that it stopped at a temporary breakpoint).
1925 flags
|= SFrefreshBreak
;
1926 } else if (strstr(start
, "re-reading symbols.") != 0) {
1927 flags
|= SFrefreshSource
;
1930 // next line, please
1931 start
= strchr(start
, '\n');
1932 } while (start
!= 0);
1935 * Gdb only notices when new threads have appeared, but not when a
1936 * thread finishes. So we always have to assume that the list of
1937 * threads has changed.
1939 flags
|= SFrefreshThreads
;
1944 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
1946 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
1949 // parse the table of shared libraries
1951 // strip off head line
1952 output
= strchr(output
, '\n');
1955 output
++; /* skip '\n' */
1957 while (*output
!= '\0') {
1958 // format of a line is
1959 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
1960 // 3 blocks of non-space followed by space
1961 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
1962 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
1965 while (isspace(*output
)) { /* space */
1969 if (*output
== '\0')
1971 const char* start
= output
;
1972 output
= strchr(output
, '\n');
1974 output
= start
+ strlen(start
);
1975 shlibName
= FROM_LATIN1(start
, output
-start
);
1976 if (*output
!= '\0')
1978 shlibs
.append(shlibName
);
1979 TRACE("found shared lib " + shlibName
);
1983 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
1985 if (strncmp(output
, "type = ", 7) != 0)
1989 * Everything else is the type. We strip off all white-space from the
1994 type
.replace(QRegExp("\\s+"), "");
1998 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2000 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2007 // parse register values
2008 while (*output
!= '\0')
2010 // skip space at the start of the line
2011 while (isspace(*output
))
2015 const char* start
= output
;
2016 while (*output
!= '\0' && !isspace(*output
))
2018 if (*output
== '\0')
2020 regName
= FROM_LATIN1(start
, output
-start
);
2023 while (isspace(*output
))
2025 // the rest of the line is the register value
2027 output
= strchr(output
,'\n');
2029 output
= start
+ strlen(start
);
2030 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2032 if (*output
!= '\0')
2033 output
++; /* skip '\n' */
2035 RegisterInfo
* reg
= new RegisterInfo
;
2036 reg
->regName
= regName
;
2039 * We split the raw from the cooked values. For this purpose, we
2040 * split off the first token (separated by whitespace). It is the
2041 * raw value. The remainder of the line is the cooked value.
2043 int pos
= value
.find(' ');
2045 reg
->rawValue
= value
;
2046 reg
->cookedValue
= QString();
2048 reg
->rawValue
= value
.left(pos
);
2049 reg
->cookedValue
= value
.mid(pos
+1,value
.length());
2051 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2052 * Here the raw value is actually in the second part.
2054 if (reg
->cookedValue
.left(5) == "(raw ") {
2055 QString raw
= reg
->cookedValue
.right(reg
->cookedValue
.length()-5);
2056 if (raw
[raw
.length()-1] == ')') /* remove closing bracket */
2057 raw
= raw
.left(raw
.length()-1);
2058 reg
->cookedValue
= reg
->rawValue
;
2059 reg
->rawValue
= raw
;
2067 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2069 const char* start
= strstr(output
, "starts at address ");
2074 const char* p
= start
;
2075 while (*p
!= '\0' && !isspace(*p
))
2077 addrFrom
= FROM_LATIN1(start
, p
-start
);
2079 start
= strstr(p
, "and ends at ");
2085 while (*p
!= '\0' && !isspace(*p
))
2087 addrTo
= FROM_LATIN1(start
, p
-start
);
2092 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2096 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2100 code
.append(new DisassembledCode(c
));
2104 // remove first line
2105 const char* p
= strchr(output
, '\n');
2107 return; /* not a regular output */
2112 const char* end
= strstr(output
, "\nEnd of assembler");
2114 end
= p
+ strlen(p
);
2118 // remove function offsets from the lines
2121 const char* start
= p
;
2123 while (p
!= end
&& !isspace(*p
))
2125 address
= FROM_LATIN1(start
, p
-start
);
2127 // function name (enclosed in '<>', followed by ':')
2128 while (p
!= end
&& *p
!= '<')
2131 skipNested(p
, '<', '>');
2136 while (p
!= end
&& isspace(*p
))
2139 // code until end of line
2141 while (p
!= end
&& *p
!= '\n')
2143 if (p
!= end
) /* include '\n' */
2146 DisassembledCode
* c
= new DisassembledCode
;
2147 c
->address
= address
;
2148 c
->code
= FROM_LATIN1(start
, p
-start
);
2153 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2155 if (isErrorExpr(output
)) {
2156 // error; strip space
2157 QString msg
= output
;
2158 return msg
.stripWhiteSpace();
2161 const char* p
= output
; /* save typing */
2167 const char* start
= p
;
2168 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2170 addr
= FROM_LATIN1(start
, p
-start
);
2172 // parse function offset
2176 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2178 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2182 // skip space; this may skip a new-line char!
2185 // everything to the end of the line is the memory dump
2186 const char* end
= strchr(p
, '\n');
2188 dump
= FROM_LATIN1(p
, end
-p
);
2191 dump
= FROM_LATIN1(p
, strlen(p
));
2194 MemoryDump
* md
= new MemoryDump
;
2204 #include "gdbdriver.moc"