3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
9 #include <qstringlist.h>
10 #include <klocale.h> /* i18n */
12 #include <stdlib.h> /* strtol, atoi */
13 #include <string.h> /* strcpy */
21 static void skipString(const char*& p
);
22 static void skipNested(const char*& s
, char opening
, char closing
);
23 static VarTree
* parseVar(const char*& s
);
24 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
);
25 static bool parseValue(const char*& s
, VarTree
* variable
);
26 static bool parseNested(const char*& s
, VarTree
* variable
);
27 static bool parseVarSeq(const char*& s
, VarTree
* variable
);
28 static bool parseValueSeq(const char*& s
, VarTree
* variable
);
30 #define PROMPT "(kdbg)"
32 #define PROMPT_LAST_CHAR ')' /* needed when searching for prompt string */
35 // TODO: make this cmd info stuff non-static to allow multiple
36 // simultaneous gdbs to run!
40 const char* fmt
; /* format string */
42 argNone
, argString
, argNum
,
43 argStringNum
, argNumString
,
48 static const char printQStringStructFmt
[] =
49 // if the string data is junk, fail early
50 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
51 // print an array of shorts
52 "(*(unsigned short*)$qstrunicode)@"
54 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
55 // if unicode data is 0, check if it is QString::null
56 ":($qstrdata==QString::null.d)\n";
58 // However, gdb can't always find QString::null (I don't know why)
59 // which results in an error; we autodetect this situation in
60 // parseQt2QStrings and revert to a safe expression.
61 // Same as above except for last line:
62 static const char printQStringStructNoNullFmt
[] =
63 "print ($qstrunicode=($qstrdata=(%s))->unicode)?"
64 "(*(unsigned short*)$qstrunicode)@"
65 "(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
69 * The following array of commands must be sorted by the DC* values,
70 * because they are used as indices.
72 static GdbCmdInfo cmds
[] = {
73 { DCinitialize
, "", GdbCmdInfo::argNone
},
74 { DCtty
, "tty %s\n", GdbCmdInfo::argString
},
75 { DCexecutable
, "file \"%s\"\n", GdbCmdInfo::argString
},
76 { DCtargetremote
, "target remote %s\n", GdbCmdInfo::argString
},
77 { DCcorefile
, "core-file %s\n", GdbCmdInfo::argString
},
78 { DCattach
, "attach %s\n", GdbCmdInfo::argString
},
79 { DCinfolinemain
, "info line main\n", GdbCmdInfo::argNone
},
80 { DCinfolocals
, "kdbg__alllocals\n", GdbCmdInfo::argNone
},
81 { DCinforegisters
, "info all-registers\n", GdbCmdInfo::argNone
},
82 { DCexamine
, "x %s %s\n", GdbCmdInfo::argString2
},
83 { DCinfoline
, "info line %s:%d\n", GdbCmdInfo::argStringNum
},
84 { DCdisassemble
, "disassemble %s %s\n", GdbCmdInfo::argString2
},
85 { DCsetargs
, "set args %s\n", GdbCmdInfo::argString
},
86 { DCsetenv
, "set env %s %s\n", GdbCmdInfo::argString2
},
87 { DCunsetenv
, "unset env %s\n", GdbCmdInfo::argString
},
88 { DCsetoption
, "setoption %s %d\n", GdbCmdInfo::argStringNum
},
89 { DCcd
, "cd %s\n", GdbCmdInfo::argString
},
90 { DCbt
, "bt\n", GdbCmdInfo::argNone
},
91 { DCrun
, "run\n", GdbCmdInfo::argNone
},
92 { DCcont
, "cont\n", GdbCmdInfo::argNone
},
93 { DCstep
, "step\n", GdbCmdInfo::argNone
},
94 { DCstepi
, "stepi\n", GdbCmdInfo::argNone
},
95 { DCnext
, "next\n", GdbCmdInfo::argNone
},
96 { DCnexti
, "nexti\n", GdbCmdInfo::argNone
},
97 { DCfinish
, "finish\n", GdbCmdInfo::argNone
},
98 { DCuntil
, "until %s:%d\n", GdbCmdInfo::argStringNum
},
99 { DCkill
, "kill\n", GdbCmdInfo::argNone
},
100 { DCbreaktext
, "break %s\n", GdbCmdInfo::argString
},
101 { DCbreakline
, "break %s:%d\n", GdbCmdInfo::argStringNum
},
102 { DCtbreakline
, "tbreak %s:%d\n", GdbCmdInfo::argStringNum
},
103 { DCbreakaddr
, "break *%s\n", GdbCmdInfo::argString
},
104 { DCtbreakaddr
, "tbreak *%s\n", GdbCmdInfo::argString
},
105 { DCwatchpoint
, "watch %s\n", GdbCmdInfo::argString
},
106 { DCdelete
, "delete %d\n", GdbCmdInfo::argNum
},
107 { DCenable
, "enable %d\n", GdbCmdInfo::argNum
},
108 { DCdisable
, "disable %d\n", GdbCmdInfo::argNum
},
109 { DCprint
, "print %s\n", GdbCmdInfo::argString
},
110 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
111 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
112 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
113 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
114 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
115 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
116 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
117 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
118 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
119 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
122 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
123 #define MAX_FMTLEN 200
125 GdbDriver::GdbDriver() :
127 m_gdbMajor(4), m_gdbMinor(16)
129 strcpy(m_prompt
, PROMPT
);
130 m_promptMinLen
= PROMPT_LEN
;
131 m_promptLastChar
= PROMPT_LAST_CHAR
;
134 // check command info array
136 for (int i
= 0; i
< NUM_CMDS
; i
++) {
137 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
138 assert(i
== cmds
[i
].cmd
);
139 // a format string must be associated
140 assert(cmds
[i
].fmt
!= 0);
141 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
142 // format string must match arg specification
143 switch (cmds
[i
].argsNeeded
) {
144 case GdbCmdInfo::argNone
:
145 assert(strchr(cmds
[i
].fmt
, '%') == 0);
147 case GdbCmdInfo::argString
:
148 perc
= strchr(cmds
[i
].fmt
, '%');
149 assert(perc
!= 0 && perc
[1] == 's');
150 assert(strchr(perc
+2, '%') == 0);
152 case GdbCmdInfo::argNum
:
153 perc
= strchr(cmds
[i
].fmt
, '%');
154 assert(perc
!= 0 && perc
[1] == 'd');
155 assert(strchr(perc
+2, '%') == 0);
157 case GdbCmdInfo::argStringNum
:
158 perc
= strchr(cmds
[i
].fmt
, '%');
159 assert(perc
!= 0 && perc
[1] == 's');
160 perc
= strchr(perc
+2, '%');
161 assert(perc
!= 0 && perc
[1] == 'd');
162 assert(strchr(perc
+2, '%') == 0);
164 case GdbCmdInfo::argNumString
:
165 perc
= strchr(cmds
[i
].fmt
, '%');
166 assert(perc
!= 0 && perc
[1] == 'd');
167 perc
= strchr(perc
+2, '%');
168 assert(perc
!= 0 && perc
[1] == 's');
169 assert(strchr(perc
+2, '%') == 0);
171 case GdbCmdInfo::argString2
:
172 perc
= strchr(cmds
[i
].fmt
, '%');
173 assert(perc
!= 0 && perc
[1] == 's');
174 perc
= strchr(perc
+2, '%');
175 assert(perc
!= 0 && perc
[1] == 's');
176 assert(strchr(perc
+2, '%') == 0);
178 case GdbCmdInfo::argNum2
:
179 perc
= strchr(cmds
[i
].fmt
, '%');
180 assert(perc
!= 0 && perc
[1] == 'd');
181 perc
= strchr(perc
+2, '%');
182 assert(perc
!= 0 && perc
[1] == 'd');
183 assert(strchr(perc
+2, '%') == 0);
187 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
188 assert(strlen(printQStringStructNoNullFmt
) <= MAX_FMTLEN
);
192 GdbDriver::~GdbDriver()
197 QString
GdbDriver::driverName() const
202 QString
GdbDriver::defaultGdb()
206 " --fullname" /* to get standard file names each time the prog stops */
207 " --nx"; /* do not execute initialization files */
210 QString
GdbDriver::defaultInvocation() const
212 if (m_defaultCmd
.isEmpty()) {
219 QStringList
GdbDriver::boolOptionList() const
222 return QStringList();
225 bool GdbDriver::startup(QString cmdStr
)
227 if (!DebuggerDriver::startup(cmdStr
))
230 static const char gdbInitialize
[] =
232 * Work around buggy gdbs that do command line editing even if they
233 * are not on a tty. The readline library echos every command back
234 * in this case, which is confusing for us.
238 "set print static-members off\n"
239 "set print asm-demangle on\n"
241 * Write a short macro that prints all locals: local variables and
242 * function arguments.
244 "define kdbg__alllocals\n"
245 "info locals\n" /* local vars supersede args with same name */
246 "info args\n" /* therefore, arguments must come last */
248 // change prompt string and synchronize with gdb
249 "set prompt " PROMPT
"\n"
252 executeCmdString(DCinitialize
, gdbInitialize
, false);
254 // assume that QString::null is ok
255 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
260 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
262 // command string must be committed
263 if (!cmd
->m_committed
) {
265 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
270 switch (cmd
->m_cmd
) {
272 // get version number from preamble
275 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
276 int offset
= GDBVersion
.match(m_output
, 0, &len
);
278 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
280 m_gdbMajor
= strtol(start
, &end
, 10);
281 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
283 // nothing was parsed
288 // assume some default version (what would make sense?)
292 // use a feasible core-file command
293 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
294 cmds
[DCcorefile
].fmt
= "target core %s\n";
296 cmds
[DCcorefile
].fmt
= "core-file %s\n";
303 /* ok, the command is ready */
304 emit
commandReceived(cmd
, m_output
);
306 switch (cmd
->m_cmd
) {
325 * The --fullname option makes gdb send a special normalized sequence print
326 * each time the program stops and at some other points. The sequence has
327 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
329 void GdbDriver::parseMarker()
331 char* startMarker
= strstr(m_output
, "\032\032");
332 if (startMarker
== 0)
335 // extract the marker
337 TRACE(QString("found marker: ") + startMarker
);
338 char* endMarker
= strchr(startMarker
, '\n');
344 // extract filename and line number
345 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
348 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
349 if (lineNoStart
>= 0) {
350 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
353 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
354 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
356 // now show the window
357 startMarker
[lineNoStart
] = '\0'; /* split off file name */
358 emit
activateFileLine(startMarker
, lineNo
-1, address
);
364 * Escapes characters that might lead to problems when they appear on gdb's
367 static void normalizeStringArg(QString
& arg
)
370 * Remove trailing backslashes. This approach is a little simplistic,
371 * but we know that there is at the moment no case where a trailing
372 * backslash would make sense.
374 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
375 arg
= arg
.left(arg
.length()-1);
380 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
382 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
383 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
385 normalizeStringArg(strArg
);
388 // need the working directory when parsing the output
389 m_programWD
= strArg
;
390 } else if (cmd
== DCsetargs
) {
391 // attach saved redirection
392 strArg
+= m_redirect
;
395 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg
.length());
396 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
400 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
402 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
403 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
405 SIZED_QString(cmdString
, MAX_FMTLEN
+30);
407 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
411 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
413 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
414 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
415 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
419 normalizeStringArg(strArg
);
421 SIZED_QString(cmdString
, MAX_FMTLEN
+30+strArg
.length());
426 * intArg specifies which channels should be redirected to
427 * /dev/null. It is a value or'ed together from RDNstdin,
428 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
431 * Note: We rely on that after the DCtty a DCsetargs will follow,
432 * which will ultimately apply the redirection.
434 static const char* const runRedir
[8] = {
438 " </dev/null >/dev/null",
440 " </dev/null 2>/dev/null",
442 " </dev/null >/dev/null 2>&1"
444 if (strArg
.isEmpty())
445 intArg
= 7; /* failsafe if no tty */
446 m_redirect
= runRedir
[intArg
& 7];
448 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
451 if (cmd
== DCexamine
) {
452 // make a format specifier from the intArg
453 static const char size
[16] = {
454 '\0', 'b', 'h', 'w', 'g'
456 static const char format
[16] = {
457 '\0', 'x', 'd', 'u', 'o', 't',
458 'a', 'c', 'f', 's', 'i'
460 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
461 assert(MDTformatmask
== 0xf0); /* next 4 bits */
462 int count
= 16; /* number of entities to print */
463 char sizeSpec
= size
[intArg
& MDTsizemask
];
464 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
465 assert(sizeSpec
!= '\0');
466 assert(formatSpec
!= '\0');
467 // adjust count such that 16 lines are printed
468 switch (intArg
& MDTformatmask
) {
469 case MDTstring
: case MDTinsn
:
470 break; /* no modification needed */
472 // all cases drop through:
473 switch (intArg
& MDTsizemask
) {
485 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
487 return makeCmdString(DCexamine
, spec
, strArg
);
490 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
492 // line numbers are zero-based
493 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
494 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
498 if (cmd
== DCinfoline
)
500 // must split off file name part
501 int slash
= strArg
.findRev('/');
503 strArg
= strArg
.right(strArg
.length()-slash
-1);
505 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
509 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
514 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
516 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
517 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
519 normalizeStringArg(strArg1
);
520 normalizeStringArg(strArg2
);
522 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg1
.length()+strArg2
.length());
523 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
527 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
529 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
530 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
532 SIZED_QString(cmdString
, MAX_FMTLEN
+60);
533 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
537 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
539 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
540 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
543 m_haveCoreFile
= false;
546 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
549 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
552 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
555 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
559 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
562 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
565 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
568 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
571 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
574 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
577 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
580 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
582 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
585 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
588 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
591 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
594 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
597 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
600 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
603 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
606 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
609 void GdbDriver::terminate()
615 void GdbDriver::detachAndTerminate()
619 executeCmdString(DCinitialize
, "detach\nquit\n", true);
622 void GdbDriver::interruptInferior()
625 // remove accidentally queued commands
629 static bool isErrorExpr(const char* output
)
632 strncmp(output
, "Cannot access memory at", 23) == 0 ||
633 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
634 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
635 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
636 strncmp(output
, "There is no member or method named", 34) == 0 ||
637 strncmp(output
, "A parse error in expression", 27) == 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
);
666 #if QT_VERSION >= 300
676 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
678 VarTree
* variable
= 0;
681 * Parse off white space. gdb sometimes prints white space first if the
682 * printed array leaded to an error.
684 const char* p
= output
;
688 // check if this is an error indicating that gdb does not know about QString::null
689 if (cmds
[DCprintQStringStruct
].fmt
== printQStringStructFmt
&&
690 strncmp(p
, "Internal error: could not find static variable null", 51) == 0)
692 /* QString::null doesn't work, use an alternate expression */
693 cmds
[DCprintQStringStruct
].fmt
= printQStringStructNoNullFmt
;
694 // continue and let parseOffErrorExpr catch the error
697 // special case: empty string (0 repetitions)
698 if (strncmp(p
, "Invalid number 0 of repetitions", 31) == 0)
700 variable
= new VarTree(QString(), VarTree::NKplain
);
701 variable
->m_value
= "\"\"";
705 // check for error conditions
706 if (parseErrorMessage(p
, variable
, wantErrorValue
))
719 } while (isspace(*p
));
723 // this is the real data
729 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
731 * A matrix for separators between the individual "things"
732 * that are added to the string. The first index is a bool,
733 * the second index is from the enum above.
735 static const char* separator
[2][3] = {
736 { "\"", 0, ", \"" }, /* normal char is added */
737 { "'", "\", '", ", '" } /* repeated char is added */
740 while (isdigit(*p
)) {
743 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
745 goto error
; /* huh? no valid digits */
746 // skip separator and search for a repeat count
748 while (isspace(*p
) || *p
== ',')
750 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
752 const char* start
= p
;
753 p
= strchr(p
+9, '>'); /* search end and advance */
757 repeatCount
= FROM_LATIN1(start
, p
-start
);
758 while (isspace(*p
) || *p
== ',')
761 // p is now at the next char (or the end)
763 // interpret the value as a QChar
764 // TODO: make cross-architecture compatible
770 (unsigned short&)ch
= value
;
774 ch
.setRow(c
.qch
.row
);
775 ch
.setCell(c
.qch
.cell
);
779 // escape a few frequently used characters
780 char escapeCode
= '\0';
781 switch (ch
.latin1()) {
782 case '\n': escapeCode
= 'n'; break;
783 case '\r': escapeCode
= 'r'; break;
784 case '\t': escapeCode
= 't'; break;
785 case '\b': escapeCode
= 'b'; break;
786 case '\"': escapeCode
= '\"'; break;
787 case '\\': escapeCode
= '\\'; break;
788 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
792 result
+= separator
[repeats
][lastThing
];
794 if (escapeCode
!= '\0') {
800 // fixup repeat count and lastThing
803 result
+= repeatCount
;
804 lastThing
= wasRepeat
;
813 if (lastThing
== wasChar
)
817 variable
= new VarTree(QString(), VarTree::NKplain
);
818 variable
->m_value
= result
;
820 else if (strncmp(p
, "true", 4) == 0)
822 variable
= new VarTree(QString(), VarTree::NKplain
);
823 variable
->m_value
= "QString::null";
825 else if (strncmp(p
, "false", 5) == 0)
827 variable
= new VarTree(QString(), VarTree::NKplain
);
828 variable
->m_value
= "(null)";
835 if (wantErrorValue
) {
836 variable
= new VarTree(QString(), VarTree::NKplain
);
837 variable
->m_value
= "internal parse error";
842 static VarTree
* parseVar(const char*& s
)
851 VarTree::NameKind kind
;
852 if (!parseName(p
, name
, kind
)) {
860 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
863 // skip the '=' and more whitespace
868 VarTree
* variable
= new VarTree(name
, kind
);
869 variable
->setDeleteChildren(true);
871 if (!parseValue(p
, variable
)) {
879 static void skipNested(const char*& s
, char opening
, char closing
)
883 // parse a nested type
887 * Search for next matching `closing' char, skipping nested pairs of
888 * `opening' and `closing'.
890 while (*p
&& nest
> 0) {
893 } else if (*p
== closing
) {
899 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
904 void skipString(const char*& p
)
909 while (*p
!= quote
) {
911 // skip escaped character
912 // no special treatment for octal values necessary
915 // simply return if no more characters
923 * Strings can consist of several parts, some of which contain repeated
927 // look ahaead for <repeats 123 times>
931 if (strncmp(q
, "<repeats ", 9) == 0) {
933 while (*p
!= '\0' && *p
!= '>')
936 p
++; /* skip the '>' */
940 // is the string continued?
942 // look ahead for another quote
946 if (*q
== '"' || *q
== '\'') {
952 /* very long strings are followed by `...' */
953 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
958 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
962 // parse a nested expression
966 * Search for next matching `closing' char, skipping nested pairs of
967 * `opening' and `closing' as well as strings.
969 while (*p
&& nest
> 0) {
972 } else if (*p
== closing
) {
974 } else if (*p
== '\'' || *p
== '\"') {
981 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
986 inline void skipName(const char*& p
)
988 // allow : (for enumeration values) and $ and . (for _vtbl.)
989 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
993 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
995 kind
= VarTree::NKplain
;
998 // examples of names:
1001 // <string<a,b<c>,7> >
1004 skipNested(p
, '<', '>');
1005 name
= FROM_LATIN1(s
, p
- s
);
1006 kind
= VarTree::NKtype
;
1010 // name, which might be "static"; allow dot for "_vtbl."
1013 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1017 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1018 kind
= VarTree::NKstatic
;
1020 // its a static variable, name comes now
1026 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1031 name
= FROM_LATIN1(s
, len
);
1033 // return the new position
1038 static bool parseValue(const char*& s
, VarTree
* variable
)
1040 variable
->m_value
= "";
1045 if (!parseNested(s
, variable
)) {
1048 // must be the closing brace
1050 TRACE("parse error: missing } of " + variable
->getText());
1054 // final white space
1058 // examples of leaf values (cannot be the empty string):
1064 // (DwContentType&) @0x8123456: {...}
1068 // 0x823abc <Array<int> virtual table>
1069 // (void (*)()) 0x8048480 <f(E *, char)>
1072 // &parseP (HTMLClueV *, char *)
1079 skipNested(p
, '(', ')');
1083 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1086 bool reference
= false;
1088 // skip reference marker
1092 const char* start
= p
;
1096 // some values consist of more than one token
1097 bool checkMultiPart
= false;
1099 if (p
[0] == '0' && p
[1] == 'x') {
1102 while (isxdigit(*p
))
1106 * Assume this is a pointer, but only if it's not a reference, since
1107 * references can't be expanded.
1110 variable
->m_varKind
= VarTree::VKpointer
;
1113 * References are followed by a colon, in which case we'll
1114 * find the value following the reference address.
1119 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1123 checkMultiPart
= true;
1124 } else if (isdigit(*p
)) {
1125 // parse decimal number, possibly a float
1128 if (*p
== '.') { /* TODO: obey i18n? */
1134 if (*p
== 'e' || *p
== 'E') {
1137 if (*p
== '-' || *p
== '+')
1143 // for char variables there is the char, eg. 10 '\n'
1144 checkMultiPart
= true;
1145 } else if (*p
== '<') {
1146 // e.g. <optimized out>
1147 skipNested(p
, '<', '>');
1148 } else if (*p
== '"' || *p
== '\'') {
1149 // character may have multipart: '\000' <repeats 11 times>
1150 checkMultiPart
= *p
== '\'';
1153 } else if (*p
== '&') {
1157 while (isspace(*p
)) {
1161 skipNested(p
, '(', ')');
1164 // must be an enumeration value
1167 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1169 if (checkMultiPart
) {
1173 // may be followed by a string or <...>
1176 if (*p
== '"' || *p
== '\'') {
1178 } else if (*p
== '<') {
1179 // if this value is part of an array, it might be followed
1180 // by <repeats 15 times>, which we don't skip here
1181 if (strncmp(p
, "<repeats ", 9) != 0)
1182 skipNested(p
, '<', '>');
1185 // there is always a blank before the string,
1186 // which we will include in the final string value
1187 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1188 // if this was a pointer, reset that flag since we
1189 // now got the value
1190 variable
->m_varKind
= VarTree::VKsimple
;
1194 if (variable
->m_value
.length() == 0) {
1195 TRACE("parse error: no value for " + variable
->getText());
1199 // final white space
1205 * If this was a reference, the value follows. It might even be a
1206 * composite variable!
1212 if (variable
->m_varKind
== VarTree::VKpointer
) {
1213 variable
->setDelayedExpanding(true);
1220 static bool parseNested(const char*& s
, VarTree
* variable
)
1222 // could be a structure or an array
1227 bool isStruct
= false;
1229 * If there is a name followed by an = or an < -- which starts a type
1230 * name -- or "static", it is a structure
1232 if (*p
== '<' || *p
== '}') {
1234 } else if (strncmp(p
, "static ", 7) == 0) {
1236 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1237 // look ahead for a comma after the name
1244 p
= s
; /* rescan the name */
1247 if (!parseVarSeq(p
, variable
)) {
1250 variable
->m_varKind
= VarTree::VKstruct
;
1252 if (!parseValueSeq(p
, variable
)) {
1255 variable
->m_varKind
= VarTree::VKarray
;
1261 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1263 // parse a comma-separated sequence of variables
1264 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1268 if (strncmp(s
, "<No data fields>}", 17) == 0)
1270 // no member variables, so break out immediately
1271 s
+= 16; /* go to the closing brace */
1276 break; /* syntax error */
1277 variable
->appendChild(var
);
1280 // skip the comma and whitespace
1288 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1290 // parse a comma-separated sequence of variables
1295 name
.sprintf("[%d]", index
);
1296 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1297 var
->setDeleteChildren(true);
1298 good
= parseValue(s
, var
);
1303 // a value may be followed by "<repeats 45 times>"
1304 if (strncmp(s
, "<repeats ", 9) == 0) {
1307 int l
= strtol(s
, &end
, 10);
1308 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1309 // should not happen
1313 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1314 // replace name and advance index
1315 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1318 // skip " times>" and space
1320 // possible final space
1326 variable
->appendChild(var
);
1330 // skip the comma and whitespace
1334 // sometimes there is a closing brace after a comma
1342 * Parses a stack frame.
1344 static void parseFrameInfo(const char*& s
, QString
& func
,
1345 QString
& file
, int& lineNo
, DbgAddr
& address
)
1349 // next may be a hexadecimal address
1351 const char* start
= p
;
1355 while (isxdigit(*p
))
1357 address
= FROM_LATIN1(start
, p
-start
);
1358 if (strncmp(p
, " in ", 4) == 0)
1361 address
= DbgAddr();
1363 const char* start
= p
;
1364 // check for special signal handler frame
1365 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1366 func
= FROM_LATIN1(start
, 23);
1374 // search opening parenthesis
1375 while (*p
!= '\0' && *p
!= '(')
1385 * Skip parameters. But notice that for complicated conversion
1386 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1387 * pointer to function) as well as operator()(...) we have to skip
1388 * additional pairs of parentheses. Furthermore, recent gdbs write the
1389 * demangled name followed by the arguments in a pair of parentheses,
1390 * where the demangled name can end in "const".
1393 skipNestedWithString(p
, '(', ')');
1397 if (strncmp(p
, "const", 5) == 0) {
1402 } while (*p
== '(');
1404 // check for file position
1405 if (strncmp(p
, "at ", 3) == 0) {
1407 const char* fileStart
= p
;
1408 // go for the end of the line
1409 while (*p
!= '\0' && *p
!= '\n')
1411 // search back for colon
1412 const char* colon
= p
;
1415 } while (*colon
!= ':');
1416 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1417 lineNo
= atoi(colon
+1)-1;
1422 // check for "from shared lib"
1423 if (strncmp(p
, "from ", 5) == 0) {
1425 // go for the end of the line
1426 while (*p
!= '\0' && *p
!= '\n')
1435 // construct the function name (including file info)
1439 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1444 * Replace \n (and whitespace around it) in func by a blank. We cannot
1445 * use QString::simplifyWhiteSpace() for this because this would also
1446 * simplify space that belongs to a string arguments that gdb sometimes
1447 * prints in the argument lists of the function.
1449 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1451 while ((nl
= func
.find('\n', nl
)) >= 0) {
1452 // search back to the beginning of the whitespace
1453 int startWhite
= nl
;
1456 } while (isspace(func
[startWhite
].latin1()));
1458 // search forward to the end of the whitespace
1461 } while (isspace(func
[nl
].latin1()));
1463 func
.replace(startWhite
, nl
-startWhite
, " ");
1464 /* continue searching for more \n's at this place: */
1471 * Parses a stack frame including its frame number
1473 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1474 QString
& file
, int& lineNo
, DbgAddr
& address
)
1477 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1478 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1480 // must start with a hash mark followed by number
1481 // or with "Breakpoint " followed by number and comma
1485 s
++; /* skip the hash mark */
1486 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1487 if (!isdigit(s
[11]))
1489 s
+= 11; /* skip "Breakpoint" */
1498 while (isspace(*s
) || *s
== ',')
1500 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1504 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1507 int lineNo
, frameNo
;
1510 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1511 StackFrame
* frm
= new StackFrame
;
1512 frm
->frameNo
= frameNo
;
1513 frm
->fileName
= file
;
1514 frm
->lineNo
= lineNo
;
1515 frm
->address
= address
;
1516 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1521 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1522 QString
& file
, int& lineNo
, DbgAddr
& address
)
1525 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1529 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1531 // skip first line, which is the headline
1532 const char* p
= strchr(output
, '\n');
1543 uint ignoreCount
= 0;
1547 while (*p
!= '\0') {
1549 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1554 Breakpoint::Type bpType
;
1555 if (strncmp(p
, "breakpoint", 10) == 0) {
1556 bpType
= Breakpoint::breakpoint
;
1558 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1559 bpType
= Breakpoint::watchpoint
;
1561 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1562 bpType
= Breakpoint::watchpoint
;
1571 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1579 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1585 // the address, if present
1586 if (bpType
== Breakpoint::breakpoint
&&
1587 strncmp(p
, "0x", 2) == 0)
1589 const char* start
= p
;
1590 while (*p
!= '\0' && !isspace(*p
))
1592 address
= FROM_LATIN1(start
, p
-start
);
1593 while (isspace(*p
) && *p
!= '\n')
1598 address
= QString();
1600 // remainder is location, hit and ignore count, condition
1603 condition
= QString();
1604 end
= strchr(p
, '\n');
1607 p
+= location
.length();
1609 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1610 p
= end
+1; /* skip over \n */
1613 // may be continued in next line
1614 while (isspace(*p
)) { /* p points to beginning of line */
1615 // skip white space at beginning of line
1620 end
= strchr(p
, '\n');
1624 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1625 // extract the hit count
1627 hits
= strtol(p
, &dummy
, 10);
1628 TRACE(QString().sprintf("hit count %d", hits
));
1629 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1630 // extract condition
1632 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1633 TRACE("condition: "+condition
);
1634 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1635 // extract ignore count
1637 ignoreCount
= strtol(p
, &dummy
, 10);
1638 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1640 // indeed a continuation
1641 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1645 p
++; /* skip '\n' */
1647 Breakpoint
* bp
= new Breakpoint
;
1650 bp
->temporary
= disp
== 'd';
1651 bp
->enabled
= enable
== 'y';
1652 bp
->location
= location
;
1653 bp
->address
= address
;
1654 bp
->hitCount
= hits
;
1655 bp
->ignoreCount
= ignoreCount
;
1656 bp
->condition
= condition
;
1662 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1664 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1675 const char* p
= output
;
1676 while (*p
!= '\0') {
1677 // seach look for thread id, watching out for the focus indicator
1678 bool hasFocus
= false;
1679 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1684 // there follows only whitespace
1687 id
= strtol(p
, &end
, 10);
1689 // syntax error: no number found; bail out
1699 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1701 end
= strstr(p
, " ");
1703 // syntax error; bail out
1706 systag
= FROM_LATIN1(p
, end
-p
);
1710 * Now follows a standard stack frame. Sometimes, however, gdb
1711 * catches a thread at an instant where it doesn't have a stack.
1713 if (strncmp(p
, "[No stack.]", 11) != 0) {
1714 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1716 func
= "[No stack]";
1719 address
= QString();
1720 p
+= 11; /* \n is skipped above */
1723 ThreadInfo
* thr
= new ThreadInfo
;
1725 thr
->threadName
= systag
;
1726 thr
->hasFocus
= hasFocus
;
1727 thr
->function
= func
;
1728 thr
->fileName
= file
;
1729 thr
->lineNo
= lineNo
;
1730 thr
->address
= address
;
1731 threads
.append(thr
);
1736 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1737 QString
& file
, int& lineNo
)
1739 const char* o
= output
;
1740 // skip lines of that begin with "(Cannot find"
1741 while (strncmp(o
, "(Cannot find", 12) == 0) {
1742 o
= strchr(o
, '\n');
1745 o
++; /* skip newline */
1748 if (strncmp(o
, "Breakpoint ", 11) != 0)
1752 output
+= 11; /* skip "Breakpoint " */
1754 int num
= strtoul(output
, &p
, 10);
1759 char* fileStart
= strstr(p
, "file ");
1765 char* numStart
= strstr(fileStart
, ", line ");
1766 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1768 int line
= strtoul(numStart
, &p
, 10);
1774 lineNo
= line
-1; /* zero-based! */
1778 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1780 // check for possible error conditions
1781 if (strncmp(output
, "No symbol table", 15) == 0)
1786 while (*output
!= '\0') {
1787 while (isspace(*output
))
1789 if (*output
== '\0')
1791 // skip occurrences of "No locals" and "No args"
1792 if (strncmp(output
, "No locals", 9) == 0 ||
1793 strncmp(output
, "No arguments", 12) == 0)
1795 output
= strchr(output
, '\n');
1802 VarTree
* variable
= parseVar(output
);
1803 if (variable
== 0) {
1806 // do not add duplicates
1807 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
1808 if (o
->getText() == variable
->getText()) {
1813 newVars
.append(variable
);
1818 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
1820 // check for error conditions
1821 if (parseErrorMessage(output
, var
, wantErrorValue
))
1825 // parse the variable
1826 var
= parseVar(output
);
1831 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
1833 bool isGood
= false;
1834 message
= QString(output
).simplifyWhiteSpace();
1835 if (message
.isEmpty()) {
1836 message
= i18n("New working directory: ") + m_programWD
;
1842 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
1846 m_haveCoreFile
= false;
1849 * The command is successful if there is no output or the single
1850 * message (no debugging symbols found)...
1853 output
[0] == '\0' ||
1854 strcmp(output
, "(no debugging symbols found)...") == 0;
1857 bool GdbDriver::parseCoreFile(const char* output
)
1859 // if command succeeded, gdb emits a line starting with "#0 "
1860 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
1861 return m_haveCoreFile
;
1864 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
1866 // optionally: "program changed, rereading symbols",
1868 // "Program exited normally"
1869 // "Program terminated with wignal SIGSEGV"
1870 // "Program received signal SIGINT" or other signal
1873 // go through the output, line by line, checking what we have
1874 const char* start
= output
- 1;
1875 uint flags
= SFprogramActive
;
1876 message
= QString();
1878 start
++; /* skip '\n' */
1880 if (strncmp(start
, "Program ", 8) == 0 ||
1881 strncmp(start
, "ptrace: ", 8) == 0) {
1883 * When we receive a signal, the program remains active.
1885 * Special: If we "stopped" in a corefile, the string "Program
1886 * terminated with signal"... is displayed. (Normally, we see
1887 * "Program received signal"... when a signal happens.)
1889 if (strncmp(start
, "Program exited", 14) == 0 ||
1890 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
1891 strncmp(start
, "ptrace: ", 8) == 0)
1893 flags
&= ~SFprogramActive
;
1897 const char* endOfMessage
= strchr(start
, '\n');
1898 if (endOfMessage
== 0)
1899 endOfMessage
= start
+ strlen(start
);
1900 message
= FROM_LATIN1(start
, endOfMessage
-start
);
1901 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
1903 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
1904 * that it stopped at a temporary breakpoint).
1906 flags
|= SFrefreshBreak
;
1907 } else if (strstr(start
, "re-reading symbols.") != 0) {
1908 flags
|= SFrefreshSource
;
1911 // next line, please
1912 start
= strchr(start
, '\n');
1913 } while (start
!= 0);
1916 * Gdb only notices when new threads have appeared, but not when a
1917 * thread finishes. So we always have to assume that the list of
1918 * threads has changed.
1920 flags
|= SFrefreshThreads
;
1925 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
1927 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
1930 // parse the table of shared libraries
1932 // strip off head line
1933 output
= strchr(output
, '\n');
1936 output
++; /* skip '\n' */
1938 while (*output
!= '\0') {
1939 // format of a line is
1940 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
1941 // 3 blocks of non-space followed by space
1942 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
1943 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
1946 while (isspace(*output
)) { /* space */
1950 if (*output
== '\0')
1952 const char* start
= output
;
1953 output
= strchr(output
, '\n');
1955 output
= start
+ strlen(start
);
1956 shlibName
= FROM_LATIN1(start
, output
-start
);
1957 if (*output
!= '\0')
1959 shlibs
.append(shlibName
);
1960 TRACE("found shared lib " + shlibName
);
1964 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
1966 if (strncmp(output
, "type = ", 7) != 0)
1970 * Everything else is the type. We strip off all white-space from the
1975 type
.replace(QRegExp("\\s+"), "");
1979 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
1981 if (strncmp(output
, "The program has no registers now", 32) == 0) {
1988 // parse register values
1989 while (*output
!= '\0')
1991 // skip space at the start of the line
1992 while (isspace(*output
))
1996 const char* start
= output
;
1997 while (*output
!= '\0' && !isspace(*output
))
1999 if (*output
== '\0')
2001 regName
= FROM_LATIN1(start
, output
-start
);
2004 while (isspace(*output
))
2006 // the rest of the line is the register value
2008 output
= strchr(output
,'\n');
2010 output
= start
+ strlen(start
);
2011 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2013 if (*output
!= '\0')
2014 output
++; /* skip '\n' */
2016 RegisterInfo
* reg
= new RegisterInfo
;
2017 reg
->regName
= regName
;
2020 * We split the raw from the cooked values. For this purpose, we
2021 * split off the first token (separated by whitespace). It is the
2022 * raw value. The remainder of the line is the cooked value.
2024 int pos
= value
.find(' ');
2026 reg
->rawValue
= value
;
2027 reg
->cookedValue
= QString();
2029 reg
->rawValue
= value
.left(pos
);
2030 reg
->cookedValue
= value
.mid(pos
+1,value
.length());
2032 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2033 * Here the raw value is actually in the second part.
2035 if (reg
->cookedValue
.left(5) == "(raw ") {
2036 QString raw
= reg
->cookedValue
.right(reg
->cookedValue
.length()-5);
2037 if (raw
[raw
.length()-1] == ')') /* remove closing bracket */
2038 raw
= raw
.left(raw
.length()-1);
2039 reg
->cookedValue
= reg
->rawValue
;
2040 reg
->rawValue
= raw
;
2048 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2050 const char* start
= strstr(output
, "starts at address ");
2055 const char* p
= start
;
2056 while (*p
!= '\0' && !isspace(*p
))
2058 addrFrom
= FROM_LATIN1(start
, p
-start
);
2060 start
= strstr(p
, "and ends at ");
2066 while (*p
!= '\0' && !isspace(*p
))
2068 addrTo
= FROM_LATIN1(start
, p
-start
);
2073 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2077 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2081 code
.append(new DisassembledCode(c
));
2085 // remove first line
2086 const char* p
= strchr(output
, '\n');
2088 return; /* not a regular output */
2093 const char* end
= strstr(output
, "\nEnd of assembler");
2095 end
= p
+ strlen(p
);
2099 // remove function offsets from the lines
2102 const char* start
= p
;
2104 while (p
!= end
&& !isspace(*p
))
2106 address
= FROM_LATIN1(start
, p
-start
);
2108 // function name (enclosed in '<>', followed by ':')
2109 while (p
!= end
&& *p
!= '<')
2112 skipNested(p
, '<', '>');
2117 while (p
!= end
&& isspace(*p
))
2120 // code until end of line
2122 while (p
!= end
&& *p
!= '\n')
2124 if (p
!= end
) /* include '\n' */
2127 DisassembledCode
* c
= new DisassembledCode
;
2128 c
->address
= address
;
2129 c
->code
= FROM_LATIN1(start
, p
-start
);
2134 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2136 if (isErrorExpr(output
)) {
2137 // error; strip space
2138 QString msg
= output
;
2139 return msg
.stripWhiteSpace();
2142 const char* p
= output
; /* save typing */
2148 const char* start
= p
;
2149 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2151 addr
= FROM_LATIN1(start
, p
-start
);
2153 // parse function offset
2157 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2159 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2163 // skip space; this may skip a new-line char!
2166 // everything to the end of the line is the memory dump
2167 const char* end
= strchr(p
, '\n');
2169 dump
= FROM_LATIN1(p
, end
-p
);
2172 dump
= FROM_LATIN1(p
, strlen(p
));
2175 MemoryDump
* md
= new MemoryDump
;
2185 #include "gdbdriver.moc"