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
214 if (m_defaultCmd
.isEmpty()) {
221 bool GdbDriver::startup(QString cmdStr
)
223 if (!DebuggerDriver::startup(cmdStr
))
226 static const char gdbInitialize
[] =
228 * Work around buggy gdbs that do command line editing even if they
229 * are not on a tty. The readline library echos every command back
230 * in this case, which is confusing for us.
234 "set print static-members off\n"
235 "set print asm-demangle on\n"
237 * Write a short macro that prints all locals: local variables and
238 * function arguments.
240 "define kdbg__alllocals\n"
241 "info locals\n" /* local vars supersede args with same name */
242 "info args\n" /* therefore, arguments must come last */
244 // change prompt string and synchronize with gdb
245 "set prompt " PROMPT
"\n"
248 executeCmdString(DCinitialize
, gdbInitialize
, false);
250 // assume that QString::null is ok
251 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
256 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
258 // command string must be committed
259 if (!cmd
->m_committed
) {
261 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
266 switch (cmd
->m_cmd
) {
268 // get version number from preamble
271 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
272 int offset
= GDBVersion
.match(m_output
, 0, &len
);
274 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
276 m_gdbMajor
= strtol(start
, &end
, 10);
277 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
279 // nothing was parsed
284 // assume some default version (what would make sense?)
288 // use a feasible core-file command
289 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
290 cmds
[DCcorefile
].fmt
= "target core %s\n";
292 cmds
[DCcorefile
].fmt
= "core-file %s\n";
299 /* ok, the command is ready */
300 emit
commandReceived(cmd
, m_output
);
302 switch (cmd
->m_cmd
) {
321 * The --fullname option makes gdb send a special normalized sequence print
322 * each time the program stops and at some other points. The sequence has
323 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
325 void GdbDriver::parseMarker()
327 char* startMarker
= strstr(m_output
, "\032\032");
328 if (startMarker
== 0)
331 // extract the marker
333 TRACE(QString("found marker: ") + startMarker
);
334 char* endMarker
= strchr(startMarker
, '\n');
340 // extract filename and line number
341 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
344 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
345 if (lineNoStart
>= 0) {
346 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
349 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
350 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
352 // now show the window
353 startMarker
[lineNoStart
] = '\0'; /* split off file name */
354 emit
activateFileLine(startMarker
, lineNo
-1, address
);
360 * Escapes characters that might lead to problems when they appear on gdb's
363 static void normalizeStringArg(QString
& arg
)
366 * Remove trailing backslashes. This approach is a little simplistic,
367 * but we know that there is at the moment no case where a trailing
368 * backslash would make sense.
370 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
371 arg
= arg
.left(arg
.length()-1);
377 #define LATIN1(str) ((str).isNull() ? "" : (str).data())
379 #define LATIN1(str) (str).latin1()
382 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
384 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
385 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
387 normalizeStringArg(strArg
);
390 // need the working directory when parsing the output
391 m_programWD
= strArg
;
392 } else if (cmd
== DCsetargs
) {
393 // attach saved redirection
397 strArg
+= m_redirect
;
400 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg
.length());
401 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg
));
405 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
407 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
408 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
410 SIZED_QString(cmdString
, MAX_FMTLEN
+30);
412 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
416 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
418 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
419 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
420 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
424 normalizeStringArg(strArg
);
426 SIZED_QString(cmdString
, MAX_FMTLEN
+30+strArg
.length());
431 * intArg specifies which channels should be redirected to
432 * /dev/null. It is a value or'ed together from RDNstdin,
433 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
436 * Note: We rely on that after the DCtty a DCsetargs will follow,
437 * which will ultimately apply the redirection.
439 static const char* const runRedir
[8] = {
443 " </dev/null >/dev/null",
445 " </dev/null 2>/dev/null",
447 " </dev/null >/dev/null 2>&1"
449 if (strArg
.isEmpty())
450 intArg
= 7; /* failsafe if no tty */
451 m_redirect
= runRedir
[intArg
& 7];
453 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
456 if (cmd
== DCexamine
) {
457 // make a format specifier from the intArg
458 static const char size
[16] = {
459 '\0', 'b', 'h', 'w', 'g'
461 static const char format
[16] = {
462 '\0', 'x', 'd', 'u', 'o', 't',
463 'a', 'c', 'f', 's', 'i'
465 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
466 assert(MDTformatmask
== 0xf0); /* next 4 bits */
467 int count
= 16; /* number of entities to print */
468 char sizeSpec
= size
[intArg
& MDTsizemask
];
469 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
470 assert(sizeSpec
!= '\0');
471 assert(formatSpec
!= '\0');
472 // adjust count such that 16 lines are printed
473 switch (intArg
& MDTformatmask
) {
474 case MDTstring
: case MDTinsn
:
475 break; /* no modification needed */
477 // all cases drop through:
478 switch (intArg
& MDTsizemask
) {
490 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
492 return makeCmdString(DCexamine
, spec
, strArg
);
495 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
497 // line numbers are zero-based
498 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
499 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
503 if (cmd
== DCinfoline
)
505 // must split off file name part
506 int slash
= strArg
.findRev('/');
508 strArg
= strArg
.right(strArg
.length()-slash
-1);
510 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg
), intArg
);
514 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, LATIN1(strArg
));
519 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
521 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
522 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
524 normalizeStringArg(strArg1
);
525 normalizeStringArg(strArg2
);
527 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg1
.length()+strArg2
.length());
528 cmdString
.sprintf(cmds
[cmd
].fmt
, LATIN1(strArg1
), LATIN1(strArg2
));
532 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
534 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
535 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
537 SIZED_QString(cmdString
, MAX_FMTLEN
+60);
538 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
542 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
544 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
545 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
548 m_haveCoreFile
= false;
551 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
554 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
557 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
560 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
564 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
567 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
570 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
573 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
576 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
579 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
582 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
585 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
587 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
590 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
593 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
596 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
599 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
602 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
605 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
608 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
611 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
614 void GdbDriver::terminate()
620 void GdbDriver::detachAndTerminate()
624 executeCmdString(DCinitialize
, "detach\nquit\n", true);
627 void GdbDriver::interruptInferior()
630 // remove accidentally queued commands
634 static bool isErrorExpr(const char* output
)
637 strncmp(output
, "Cannot access memory at", 23) == 0 ||
638 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
639 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
640 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
641 strncmp(output
, "There is no member or method named", 34) == 0 ||
642 strncmp(output
, "No symbol \"", 11) == 0 ||
643 strncmp(output
, "Internal error: ", 16) == 0;
647 * Returns true if the output is an error message. If wantErrorValue is
648 * true, a new VarTree object is created and filled with the error message.
650 static bool parseErrorMessage(const char* output
,
651 VarTree
*& variable
, bool wantErrorValue
)
653 if (isErrorExpr(output
))
655 if (wantErrorValue
) {
656 // put the error message as value in the variable
657 variable
= new VarTree(QString(), VarTree::NKplain
);
658 const char* endMsg
= strchr(output
, '\n');
660 endMsg
= output
+ strlen(output
);
661 variable
->m_value
= FROM_LATIN1(output
, endMsg
-output
);
672 // this is the XChar2b on X11
675 static QString
toQString(QChar
* unicode
, int len
);
676 QChar() : row(0), cell(0) { }
677 QChar(char c
) : row(0), cell(c
) { }
678 operator char() const { return row
? 0 : cell
; }
681 QString
QChar::toQString(QChar
* unicode
, int len
)
683 QString
result(len
+1);
684 char* data
= result
.data();
687 data
[len
] = unicode
[len
].cell
;
693 #if QT_VERSION >= 300
703 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
705 VarTree
* variable
= 0;
708 * Parse off white space. gdb sometimes prints white space first if the
709 * printed array leaded to an error.
711 const char* p
= output
;
715 // check if this is an error indicating that gdb does not know about QString::null
716 if (cmds
[DCprintQStringStruct
].fmt
== printQStringStructFmt
&&
717 strncmp(p
, "Internal error: could not find static variable null", 51) == 0)
719 /* QString::null doesn't work, use an alternate expression */
720 cmds
[DCprintQStringStruct
].fmt
= printQStringStructNoNullFmt
;
721 // continue and let parseOffErrorExpr catch the error
724 // special case: empty string (0 repetitions)
725 if (strncmp(p
, "Invalid number 0 of repetitions", 31) == 0)
727 variable
= new VarTree(QString(), VarTree::NKplain
);
728 variable
->m_value
= "\"\"";
732 // check for error conditions
733 if (parseErrorMessage(p
, variable
, wantErrorValue
))
746 } while (isspace(*p
));
750 // this is the real data
756 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
758 * A matrix for separators between the individual "things"
759 * that are added to the string. The first index is a bool,
760 * the second index is from the enum above.
762 static const char* separator
[2][3] = {
763 { "\"", 0, ", \"" }, /* normal char is added */
764 { "'", "\", '", ", '" } /* repeated char is added */
767 while (isdigit(*p
)) {
770 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
772 goto error
; /* huh? no valid digits */
773 // skip separator and search for a repeat count
775 while (isspace(*p
) || *p
== ',')
777 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
779 const char* start
= p
;
780 p
= strchr(p
+9, '>'); /* search end and advance */
784 repeatCount
= FROM_LATIN1(start
, p
-start
);
785 while (isspace(*p
) || *p
== ',')
788 // p is now at the next char (or the end)
790 // interpret the value as a QChar
791 // TODO: make cross-architecture compatible
797 (unsigned short&)ch
= value
;
801 ch
.setRow(c
.qch
.row
);
802 ch
.setCell(c
.qch
.cell
);
806 // escape a few frequently used characters
807 char escapeCode
= '\0';
809 case '\n': escapeCode
= 'n'; break;
810 case '\r': escapeCode
= 'r'; break;
811 case '\t': escapeCode
= 't'; break;
812 case '\b': escapeCode
= 'b'; break;
813 case '\"': escapeCode
= '\"'; break;
814 case '\\': escapeCode
= '\\'; break;
816 // since we only deal with ascii values must always escape '\0'
817 case '\0': escapeCode
= '0'; break;
819 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
824 result
+= separator
[repeats
][lastThing
];
826 if (escapeCode
!= '\0') {
832 // fixup repeat count and lastThing
835 result
+= repeatCount
;
836 lastThing
= wasRepeat
;
845 if (lastThing
== wasChar
)
849 variable
= new VarTree(QString(), VarTree::NKplain
);
850 variable
->m_value
= result
;
852 else if (strncmp(p
, "true", 4) == 0)
854 variable
= new VarTree(QString(), VarTree::NKplain
);
855 variable
->m_value
= "QString::null";
857 else if (strncmp(p
, "false", 5) == 0)
859 variable
= new VarTree(QString(), VarTree::NKplain
);
860 variable
->m_value
= "(null)";
867 if (wantErrorValue
) {
868 variable
= new VarTree(QString(), VarTree::NKplain
);
869 variable
->m_value
= "internal parse error";
874 static VarTree
* parseVar(const char*& s
)
883 VarTree::NameKind kind
;
884 if (!parseName(p
, name
, kind
)) {
892 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
895 // skip the '=' and more whitespace
900 VarTree
* variable
= new VarTree(name
, kind
);
901 variable
->setDeleteChildren(true);
903 if (!parseValue(p
, variable
)) {
911 static void skipNested(const char*& s
, char opening
, char closing
)
915 // parse a nested type
919 * Search for next matching `closing' char, skipping nested pairs of
920 * `opening' and `closing'.
922 while (*p
&& nest
> 0) {
925 } else if (*p
== closing
) {
931 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
936 void skipString(const char*& p
)
941 while (*p
!= quote
) {
943 // skip escaped character
944 // no special treatment for octal values necessary
947 // simply return if no more characters
955 * Strings can consist of several parts, some of which contain repeated
959 // look ahaead for <repeats 123 times>
963 if (strncmp(q
, "<repeats ", 9) == 0) {
965 while (*p
!= '\0' && *p
!= '>')
968 p
++; /* skip the '>' */
972 // is the string continued?
974 // look ahead for another quote
978 if (*q
== '"' || *q
== '\'') {
984 /* very long strings are followed by `...' */
985 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
990 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
994 // parse a nested expression
998 * Search for next matching `closing' char, skipping nested pairs of
999 * `opening' and `closing' as well as strings.
1001 while (*p
&& nest
> 0) {
1002 if (*p
== opening
) {
1004 } else if (*p
== closing
) {
1006 } else if (*p
== '\'' || *p
== '\"') {
1013 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1018 inline void skipName(const char*& p
)
1020 // allow : (for enumeration values) and $ and . (for _vtbl.)
1021 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1025 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1027 kind
= VarTree::NKplain
;
1030 // examples of names:
1033 // <string<a,b<c>,7> >
1036 skipNested(p
, '<', '>');
1037 name
= FROM_LATIN1(s
, p
- s
);
1038 kind
= VarTree::NKtype
;
1042 // name, which might be "static"; allow dot for "_vtbl."
1045 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1049 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1050 kind
= VarTree::NKstatic
;
1052 // its a static variable, name comes now
1058 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1063 name
= FROM_LATIN1(s
, len
);
1065 // return the new position
1070 static bool parseValue(const char*& s
, VarTree
* variable
)
1072 variable
->m_value
= "";
1077 if (!parseNested(s
, variable
)) {
1080 // must be the closing brace
1082 TRACE("parse error: missing } of " + variable
->getText());
1086 // final white space
1090 // examples of leaf values (cannot be the empty string):
1096 // (DwContentType&) @0x8123456: {...}
1100 // 0x823abc <Array<int> virtual table>
1101 // (void (*)()) 0x8048480 <f(E *, char)>
1104 // &parseP (HTMLClueV *, char *)
1111 skipNested(p
, '(', ')');
1115 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1118 bool reference
= false;
1120 // skip reference marker
1124 const char* start
= p
;
1128 // some values consist of more than one token
1129 bool checkMultiPart
= false;
1131 if (p
[0] == '0' && p
[1] == 'x') {
1134 while (isxdigit(*p
))
1138 * Assume this is a pointer, but only if it's not a reference, since
1139 * references can't be expanded.
1142 variable
->m_varKind
= VarTree::VKpointer
;
1145 * References are followed by a colon, in which case we'll
1146 * find the value following the reference address.
1151 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1155 checkMultiPart
= true;
1156 } else if (isdigit(*p
)) {
1157 // parse decimal number, possibly a float
1160 if (*p
== '.') { /* TODO: obey i18n? */
1166 if (*p
== 'e' || *p
== 'E') {
1169 if (*p
== '-' || *p
== '+')
1175 // for char variables there is the char, eg. 10 '\n'
1176 checkMultiPart
= true;
1177 } else if (*p
== '<') {
1178 // e.g. <optimized out>
1179 skipNested(p
, '<', '>');
1180 } else if (*p
== '"' || *p
== '\'') {
1181 // character may have multipart: '\000' <repeats 11 times>
1182 checkMultiPart
= *p
== '\'';
1185 } else if (*p
== '&') {
1189 while (isspace(*p
)) {
1193 skipNested(p
, '(', ')');
1196 // must be an enumeration value
1199 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1201 if (checkMultiPart
) {
1205 // may be followed by a string or <...>
1208 if (*p
== '"' || *p
== '\'') {
1210 } else if (*p
== '<') {
1211 // if this value is part of an array, it might be followed
1212 // by <repeats 15 times>, which we don't skip here
1213 if (strncmp(p
, "<repeats ", 9) != 0)
1214 skipNested(p
, '<', '>');
1217 // there is always a blank before the string,
1218 // which we will include in the final string value
1219 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1220 // if this was a pointer, reset that flag since we
1221 // now got the value
1222 variable
->m_varKind
= VarTree::VKsimple
;
1226 if (variable
->m_value
.length() == 0) {
1227 TRACE("parse error: no value for " + variable
->getText());
1231 // final white space
1237 * If this was a reference, the value follows. It might even be a
1238 * composite variable!
1244 if (variable
->m_varKind
== VarTree::VKpointer
) {
1245 variable
->setDelayedExpanding(true);
1252 static bool parseNested(const char*& s
, VarTree
* variable
)
1254 // could be a structure or an array
1259 bool isStruct
= false;
1261 * If there is a name followed by an = or an < -- which starts a type
1262 * name -- or "static", it is a structure
1264 if (*p
== '<' || *p
== '}') {
1266 } else if (strncmp(p
, "static ", 7) == 0) {
1268 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1269 // look ahead for a comma after the name
1276 p
= s
; /* rescan the name */
1279 if (!parseVarSeq(p
, variable
)) {
1282 variable
->m_varKind
= VarTree::VKstruct
;
1284 if (!parseValueSeq(p
, variable
)) {
1287 variable
->m_varKind
= VarTree::VKarray
;
1293 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1295 // parse a comma-separated sequence of variables
1296 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1300 if (strncmp(s
, "<No data fields>}", 17) == 0)
1302 // no member variables, so break out immediately
1303 s
+= 16; /* go to the closing brace */
1308 break; /* syntax error */
1309 variable
->appendChild(var
);
1312 // skip the comma and whitespace
1320 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1322 // parse a comma-separated sequence of variables
1327 name
.sprintf("[%d]", index
);
1328 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1329 var
->setDeleteChildren(true);
1330 good
= parseValue(s
, var
);
1335 // a value may be followed by "<repeats 45 times>"
1336 if (strncmp(s
, "<repeats ", 9) == 0) {
1339 int l
= strtol(s
, &end
, 10);
1340 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1341 // should not happen
1345 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1346 // replace name and advance index
1347 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1350 // skip " times>" and space
1352 // possible final space
1358 variable
->appendChild(var
);
1362 // skip the comma and whitespace
1366 // sometimes there is a closing brace after a comma
1373 #if QT_VERSION < 200
1374 #define ISSPACE(c) isspace((c))
1377 #define ISSPACE(c) (c).isSpace()
1381 * Parses a stack frame.
1383 static void parseFrameInfo(const char*& s
, QString
& func
,
1384 QString
& file
, int& lineNo
, DbgAddr
& address
)
1388 // next may be a hexadecimal address
1390 const char* start
= p
;
1394 while (isxdigit(*p
))
1396 address
= FROM_LATIN1(start
, p
-start
);
1397 if (strncmp(p
, " in ", 4) == 0)
1400 address
= DbgAddr();
1402 const char* start
= p
;
1403 // check for special signal handler frame
1404 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1405 func
= FROM_LATIN1(start
, 23);
1413 // search opening parenthesis
1414 while (*p
!= '\0' && *p
!= '(')
1424 * Skip parameters. But notice that for complicated conversion
1425 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1426 * pointer to function) as well as operator()(...) we have to skip
1427 * additional pairs of parentheses.
1430 skipNestedWithString(p
, '(', ')');
1433 } while (*p
== '(');
1435 // check for file position
1436 if (strncmp(p
, "at ", 3) == 0) {
1438 const char* fileStart
= p
;
1439 // go for the end of the line
1440 while (*p
!= '\0' && *p
!= '\n')
1442 // search back for colon
1443 const char* colon
= p
;
1446 } while (*colon
!= ':');
1447 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1448 lineNo
= atoi(colon
+1)-1;
1453 // check for "from shared lib"
1454 if (strncmp(p
, "from ", 5) == 0) {
1456 // go for the end of the line
1457 while (*p
!= '\0' && *p
!= '\n')
1466 // construct the function name (including file info)
1470 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1475 * Replace \n (and whitespace around it) in func by a blank. We cannot
1476 * use QString::simplifyWhiteSpace() for this because this would also
1477 * simplify space that belongs to a string arguments that gdb sometimes
1478 * prints in the argument lists of the function.
1480 ASSERT(!ISSPACE(func
[0])); /* there must be non-white before first \n */
1482 while ((nl
= func
.find('\n', nl
)) >= 0) {
1483 // search back to the beginning of the whitespace
1484 int startWhite
= nl
;
1487 } while (ISSPACE(func
[startWhite
]));
1489 // search forward to the end of the whitespace
1492 } while (ISSPACE(func
[nl
]));
1494 func
.replace(startWhite
, nl
-startWhite
, " ");
1495 /* continue searching for more \n's at this place: */
1503 * Parses a stack frame including its frame number
1505 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1506 QString
& file
, int& lineNo
, DbgAddr
& address
)
1509 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1511 // must start with a hash mark followed by number
1512 if (s
[0] != '#' || !isdigit(s
[1]))
1515 s
++; /* skip the hash mark */
1523 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1527 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1530 int lineNo
, frameNo
;
1533 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1534 StackFrame
* frm
= new StackFrame
;
1535 frm
->frameNo
= frameNo
;
1536 frm
->fileName
= file
;
1537 frm
->lineNo
= lineNo
;
1538 frm
->address
= address
;
1539 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1544 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1545 QString
& file
, int& lineNo
, DbgAddr
& address
)
1548 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1552 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1554 // skip first line, which is the headline
1555 const char* p
= strchr(output
, '\n');
1566 uint ignoreCount
= 0;
1570 while (*p
!= '\0') {
1572 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1577 Breakpoint::Type bpType
;
1578 if (strncmp(p
, "breakpoint", 10) == 0) {
1579 bpType
= Breakpoint::breakpoint
;
1581 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1582 bpType
= Breakpoint::watchpoint
;
1584 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1585 bpType
= Breakpoint::watchpoint
;
1594 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1602 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1608 // the address, if present
1609 if (bpType
== Breakpoint::breakpoint
&&
1610 strncmp(p
, "0x", 2) == 0)
1612 const char* start
= p
;
1613 while (*p
!= '\0' && !isspace(*p
))
1615 address
= FROM_LATIN1(start
, p
-start
);
1616 while (isspace(*p
) && *p
!= '\n')
1621 address
= QString();
1623 // remainder is location, hit and ignore count, condition
1626 condition
= QString();
1627 end
= strchr(p
, '\n');
1630 p
+= location
.length();
1632 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1633 p
= end
+1; /* skip over \n */
1636 // may be continued in next line
1637 while (isspace(*p
)) { /* p points to beginning of line */
1638 // skip white space at beginning of line
1643 end
= strchr(p
, '\n');
1647 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1648 // extract the hit count
1650 hits
= strtol(p
, &dummy
, 10);
1651 TRACE(QString().sprintf("hit count %d", hits
));
1652 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1653 // extract condition
1655 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1656 TRACE("condition: "+condition
);
1657 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1658 // extract ignore count
1660 ignoreCount
= strtol(p
, &dummy
, 10);
1661 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1663 // indeed a continuation
1664 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1668 p
++; /* skip '\n' */
1670 Breakpoint
* bp
= new Breakpoint
;
1673 bp
->temporary
= disp
== 'd';
1674 bp
->enabled
= enable
== 'y';
1675 bp
->location
= location
;
1676 bp
->address
= address
;
1677 bp
->hitCount
= hits
;
1678 bp
->ignoreCount
= ignoreCount
;
1679 bp
->condition
= condition
;
1685 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1687 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1698 const char* p
= output
;
1699 while (*p
!= '\0') {
1700 // seach look for thread id, watching out for the focus indicator
1701 bool hasFocus
= false;
1702 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1707 // there follows only whitespace
1710 id
= strtol(p
, &end
, 10);
1712 // syntax error: no number found; bail out
1722 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1724 end
= strstr(p
, " ");
1726 // syntax error; bail out
1729 systag
= FROM_LATIN1(p
, end
-p
);
1733 * Now follows a standard stack frame. Sometimes, however, gdb
1734 * catches a thread at an instant where it doesn't have a stack.
1736 if (strncmp(p
, "[No stack.]", 11) != 0) {
1737 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1739 func
= "[No stack]";
1742 address
= QString();
1743 p
+= 11; /* \n is skipped above */
1746 ThreadInfo
* thr
= new ThreadInfo
;
1748 thr
->threadName
= systag
;
1749 thr
->hasFocus
= hasFocus
;
1750 thr
->function
= func
;
1751 thr
->fileName
= file
;
1752 thr
->lineNo
= lineNo
;
1753 thr
->address
= address
;
1754 threads
.append(thr
);
1759 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1760 QString
& file
, int& lineNo
)
1762 const char* o
= output
;
1763 // skip lines of that begin with "(Cannot find"
1764 while (strncmp(o
, "(Cannot find", 12) == 0) {
1765 o
= strchr(o
, '\n');
1768 o
++; /* skip newline */
1771 if (strncmp(o
, "Breakpoint ", 11) != 0)
1775 output
+= 11; /* skip "Breakpoint " */
1777 int num
= strtoul(output
, &p
, 10);
1782 char* fileStart
= strstr(p
, "file ");
1788 char* numStart
= strstr(fileStart
, ", line ");
1789 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1791 int line
= strtoul(numStart
, &p
, 10);
1797 lineNo
= line
-1; /* zero-based! */
1801 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1803 // check for possible error conditions
1804 if (strncmp(output
, "No symbol table", 15) == 0)
1809 while (*output
!= '\0') {
1810 while (isspace(*output
))
1812 if (*output
== '\0')
1814 // skip occurrences of "No locals" and "No args"
1815 if (strncmp(output
, "No locals", 9) == 0 ||
1816 strncmp(output
, "No arguments", 12) == 0)
1818 output
= strchr(output
, '\n');
1825 VarTree
* variable
= parseVar(output
);
1826 if (variable
== 0) {
1829 // do not add duplicates
1830 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
1831 if (o
->getText() == variable
->getText()) {
1836 newVars
.append(variable
);
1841 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
1843 // check for error conditions
1844 if (parseErrorMessage(output
, var
, wantErrorValue
))
1848 // parse the variable
1849 var
= parseVar(output
);
1854 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
1856 bool isGood
= false;
1857 message
= QString(output
).simplifyWhiteSpace();
1858 if (message
.isEmpty()) {
1859 message
= i18n("New working directory: ") + m_programWD
;
1865 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
1869 m_haveCoreFile
= false;
1872 * The command is successful if there is no output or the single
1873 * message (no debugging symbols found)...
1876 output
[0] == '\0' ||
1877 strcmp(output
, "(no debugging symbols found)...") == 0;
1880 bool GdbDriver::parseCoreFile(const char* output
)
1882 // if command succeeded, gdb emits a line starting with "#0 "
1883 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
1884 return m_haveCoreFile
;
1887 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
1889 // optionally: "program changed, rereading symbols",
1891 // "Program exited normally"
1892 // "Program terminated with wignal SIGSEGV"
1893 // "Program received signal SIGINT" or other signal
1896 // go through the output, line by line, checking what we have
1897 const char* start
= output
- 1;
1898 uint flags
= SFprogramActive
;
1899 message
= QString();
1901 start
++; /* skip '\n' */
1903 if (strncmp(start
, "Program ", 8) == 0 ||
1904 strncmp(start
, "ptrace: ", 8) == 0) {
1906 * When we receive a signal, the program remains active.
1908 * Special: If we "stopped" in a corefile, the string "Program
1909 * terminated with signal"... is displayed. (Normally, we see
1910 * "Program received signal"... when a signal happens.)
1912 if (strncmp(start
, "Program exited", 14) == 0 ||
1913 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
1914 strncmp(start
, "ptrace: ", 8) == 0)
1916 flags
&= ~SFprogramActive
;
1920 const char* endOfMessage
= strchr(start
, '\n');
1921 if (endOfMessage
== 0)
1922 endOfMessage
= start
+ strlen(start
);
1923 message
= FROM_LATIN1(start
, endOfMessage
-start
);
1924 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
1926 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
1927 * that it stopped at a temporary breakpoint).
1929 flags
|= SFrefreshBreak
;
1930 } else if (strstr(start
, "re-reading symbols.") != 0) {
1931 flags
|= SFrefreshSource
;
1934 // next line, please
1935 start
= strchr(start
, '\n');
1936 } while (start
!= 0);
1939 * Gdb only notices when new threads have appeared, but not when a
1940 * thread finishes. So we always have to assume that the list of
1941 * threads has changed.
1943 flags
|= SFrefreshThreads
;
1948 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
1950 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
1953 // parse the table of shared libraries
1955 // strip off head line
1956 output
= strchr(output
, '\n');
1959 output
++; /* skip '\n' */
1961 while (*output
!= '\0') {
1962 // format of a line is
1963 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
1964 // 3 blocks of non-space followed by space
1965 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
1966 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
1969 while (isspace(*output
)) { /* space */
1973 if (*output
== '\0')
1975 const char* start
= output
;
1976 output
= strchr(output
, '\n');
1978 output
= start
+ strlen(start
);
1979 shlibName
= FROM_LATIN1(start
, output
-start
);
1980 if (*output
!= '\0')
1982 shlibs
.append(shlibName
);
1983 TRACE("found shared lib " + shlibName
);
1987 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
1989 if (strncmp(output
, "type = ", 7) != 0)
1993 * Everything else is the type. We strip off all white-space from the
1998 type
.replace(QRegExp("\\s+"), "");
2002 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2004 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2011 // parse register values
2012 while (*output
!= '\0')
2014 // skip space at the start of the line
2015 while (isspace(*output
))
2019 const char* start
= output
;
2020 while (*output
!= '\0' && !isspace(*output
))
2022 if (*output
== '\0')
2024 regName
= FROM_LATIN1(start
, output
-start
);
2027 while (isspace(*output
))
2029 // the rest of the line is the register value
2031 output
= strchr(output
,'\n');
2033 output
= start
+ strlen(start
);
2034 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2036 if (*output
!= '\0')
2037 output
++; /* skip '\n' */
2039 RegisterInfo
* reg
= new RegisterInfo
;
2040 reg
->regName
= regName
;
2043 * We split the raw from the cooked values. For this purpose, we
2044 * split off the first token (separated by whitespace). It is the
2045 * raw value. The remainder of the line is the cooked value.
2047 int pos
= value
.find(' ');
2049 reg
->rawValue
= value
;
2050 reg
->cookedValue
= QString();
2052 reg
->rawValue
= value
.left(pos
);
2053 reg
->cookedValue
= value
.mid(pos
+1,value
.length());
2055 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2056 * Here the raw value is actually in the second part.
2058 if (reg
->cookedValue
.left(5) == "(raw ") {
2059 QString raw
= reg
->cookedValue
.right(reg
->cookedValue
.length()-5);
2060 if (raw
[raw
.length()-1] == ')') /* remove closing bracket */
2061 raw
= raw
.left(raw
.length()-1);
2062 reg
->cookedValue
= reg
->rawValue
;
2063 reg
->rawValue
= raw
;
2071 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2073 const char* start
= strstr(output
, "starts at address ");
2078 const char* p
= start
;
2079 while (*p
!= '\0' && !isspace(*p
))
2081 addrFrom
= FROM_LATIN1(start
, p
-start
);
2083 start
= strstr(p
, "and ends at ");
2089 while (*p
!= '\0' && !isspace(*p
))
2091 addrTo
= FROM_LATIN1(start
, p
-start
);
2096 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2100 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2104 code
.append(new DisassembledCode(c
));
2108 // remove first line
2109 const char* p
= strchr(output
, '\n');
2111 return; /* not a regular output */
2116 const char* end
= strstr(output
, "\nEnd of assembler");
2118 end
= p
+ strlen(p
);
2122 // remove function offsets from the lines
2125 const char* start
= p
;
2127 while (p
!= end
&& !isspace(*p
))
2129 address
= FROM_LATIN1(start
, p
-start
);
2131 // function name (enclosed in '<>', followed by ':')
2132 while (p
!= end
&& *p
!= '<')
2135 skipNested(p
, '<', '>');
2140 while (p
!= end
&& isspace(*p
))
2143 // code until end of line
2145 while (p
!= end
&& *p
!= '\n')
2147 if (p
!= end
) /* include '\n' */
2150 DisassembledCode
* c
= new DisassembledCode
;
2151 c
->address
= address
;
2152 c
->code
= FROM_LATIN1(start
, p
-start
);
2157 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2159 if (isErrorExpr(output
)) {
2160 // error; strip space
2161 QString msg
= output
;
2162 return msg
.stripWhiteSpace();
2165 const char* p
= output
; /* save typing */
2171 const char* start
= p
;
2172 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2174 addr
= FROM_LATIN1(start
, p
-start
);
2176 // parse function offset
2180 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2182 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2186 // skip space; this may skip a new-line char!
2189 // everything to the end of the line is the memory dump
2190 const char* end
= strchr(p
, '\n');
2192 dump
= FROM_LATIN1(p
, end
-p
);
2195 dump
= FROM_LATIN1(p
, strlen(p
));
2198 MemoryDump
* md
= new MemoryDump
;
2208 #include "gdbdriver.moc"