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 { DCprintDeref
, "print *(%s)\n", GdbCmdInfo::argString
},
111 { DCprintStruct
, "print %s\n", GdbCmdInfo::argString
},
112 { DCprintQStringStruct
, printQStringStructFmt
, GdbCmdInfo::argString
},
113 { DCframe
, "frame %d\n", GdbCmdInfo::argNum
},
114 { DCfindType
, "whatis %s\n", GdbCmdInfo::argString
},
115 { DCinfosharedlib
, "info sharedlibrary\n", GdbCmdInfo::argNone
},
116 { DCthread
, "thread %d\n", GdbCmdInfo::argNum
},
117 { DCinfothreads
, "info threads\n", GdbCmdInfo::argNone
},
118 { DCinfobreak
, "info breakpoints\n", GdbCmdInfo::argNone
},
119 { DCcondition
, "condition %d %s\n", GdbCmdInfo::argNumString
},
120 { DCsetpc
, "set variable $pc=%s\n", GdbCmdInfo::argString
},
121 { DCignore
, "ignore %d %d\n", GdbCmdInfo::argNum2
},
122 { DCsetvariable
, "set variable %s=%s\n", GdbCmdInfo::argString2
},
125 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
126 #define MAX_FMTLEN 200
128 GdbDriver::GdbDriver() :
130 m_gdbMajor(4), m_gdbMinor(16)
132 strcpy(m_prompt
, PROMPT
);
133 m_promptMinLen
= PROMPT_LEN
;
134 m_promptLastChar
= PROMPT_LAST_CHAR
;
137 // check command info array
139 for (int i
= 0; i
< NUM_CMDS
; i
++) {
140 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
141 assert(i
== cmds
[i
].cmd
);
142 // a format string must be associated
143 assert(cmds
[i
].fmt
!= 0);
144 assert(strlen(cmds
[i
].fmt
) <= MAX_FMTLEN
);
145 // format string must match arg specification
146 switch (cmds
[i
].argsNeeded
) {
147 case GdbCmdInfo::argNone
:
148 assert(strchr(cmds
[i
].fmt
, '%') == 0);
150 case GdbCmdInfo::argString
:
151 perc
= strchr(cmds
[i
].fmt
, '%');
152 assert(perc
!= 0 && perc
[1] == 's');
153 assert(strchr(perc
+2, '%') == 0);
155 case GdbCmdInfo::argNum
:
156 perc
= strchr(cmds
[i
].fmt
, '%');
157 assert(perc
!= 0 && perc
[1] == 'd');
158 assert(strchr(perc
+2, '%') == 0);
160 case GdbCmdInfo::argStringNum
:
161 perc
= strchr(cmds
[i
].fmt
, '%');
162 assert(perc
!= 0 && perc
[1] == 's');
163 perc
= strchr(perc
+2, '%');
164 assert(perc
!= 0 && perc
[1] == 'd');
165 assert(strchr(perc
+2, '%') == 0);
167 case GdbCmdInfo::argNumString
:
168 perc
= strchr(cmds
[i
].fmt
, '%');
169 assert(perc
!= 0 && perc
[1] == 'd');
170 perc
= strchr(perc
+2, '%');
171 assert(perc
!= 0 && perc
[1] == 's');
172 assert(strchr(perc
+2, '%') == 0);
174 case GdbCmdInfo::argString2
:
175 perc
= strchr(cmds
[i
].fmt
, '%');
176 assert(perc
!= 0 && perc
[1] == 's');
177 perc
= strchr(perc
+2, '%');
178 assert(perc
!= 0 && perc
[1] == 's');
179 assert(strchr(perc
+2, '%') == 0);
181 case GdbCmdInfo::argNum2
:
182 perc
= strchr(cmds
[i
].fmt
, '%');
183 assert(perc
!= 0 && perc
[1] == 'd');
184 perc
= strchr(perc
+2, '%');
185 assert(perc
!= 0 && perc
[1] == 'd');
186 assert(strchr(perc
+2, '%') == 0);
190 assert(strlen(printQStringStructFmt
) <= MAX_FMTLEN
);
191 assert(strlen(printQStringStructNoNullFmt
) <= MAX_FMTLEN
);
195 GdbDriver::~GdbDriver()
200 QString
GdbDriver::driverName() const
205 QString
GdbDriver::defaultGdb()
209 " --fullname" /* to get standard file names each time the prog stops */
210 " --nx"; /* do not execute initialization files */
213 QString
GdbDriver::defaultInvocation() const
215 if (m_defaultCmd
.isEmpty()) {
222 QStringList
GdbDriver::boolOptionList() const
225 return QStringList();
228 bool GdbDriver::startup(QString cmdStr
)
230 if (!DebuggerDriver::startup(cmdStr
))
233 static const char gdbInitialize
[] =
235 * Work around buggy gdbs that do command line editing even if they
236 * are not on a tty. The readline library echos every command back
237 * in this case, which is confusing for us.
241 "set print static-members off\n"
242 "set print asm-demangle on\n"
244 * Write a short macro that prints all locals: local variables and
245 * function arguments.
247 "define kdbg__alllocals\n"
248 "info locals\n" /* local vars supersede args with same name */
249 "info args\n" /* therefore, arguments must come last */
251 // change prompt string and synchronize with gdb
252 "set prompt " PROMPT
"\n"
255 executeCmdString(DCinitialize
, gdbInitialize
, false);
257 // assume that QString::null is ok
258 cmds
[DCprintQStringStruct
].fmt
= printQStringStructFmt
;
263 void GdbDriver::commandFinished(CmdQueueItem
* cmd
)
265 // command string must be committed
266 if (!cmd
->m_committed
) {
268 TRACE("calling " + (__PRETTY_FUNCTION__
+ (" with uncommited command:\n\t" +
273 switch (cmd
->m_cmd
) {
275 // get version number from preamble
278 QRegExp
GDBVersion("\\nGDB [0-9]+\\.[0-9]+");
279 int offset
= GDBVersion
.match(m_output
, 0, &len
);
281 char* start
= m_output
+ offset
+ 5; // skip "\nGDB "
283 m_gdbMajor
= strtol(start
, &end
, 10);
284 m_gdbMinor
= strtol(end
+ 1, 0, 10); // skip "."
286 // nothing was parsed
291 // assume some default version (what would make sense?)
295 // use a feasible core-file command
296 if (m_gdbMajor
> 4 || (m_gdbMajor
== 4 && m_gdbMinor
>= 16)) {
297 cmds
[DCcorefile
].fmt
= "target core %s\n";
299 cmds
[DCcorefile
].fmt
= "core-file %s\n";
306 /* ok, the command is ready */
307 emit
commandReceived(cmd
, m_output
);
309 switch (cmd
->m_cmd
) {
328 * The --fullname option makes gdb send a special normalized sequence print
329 * each time the program stops and at some other points. The sequence has
330 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
332 void GdbDriver::parseMarker()
334 char* startMarker
= strstr(m_output
, "\032\032");
335 if (startMarker
== 0)
338 // extract the marker
340 TRACE(QString("found marker: ") + startMarker
);
341 char* endMarker
= strchr(startMarker
, '\n');
347 // extract filename and line number
348 static QRegExp
MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");
351 int lineNoStart
= MarkerRE
.match(startMarker
, 0, &len
);
352 if (lineNoStart
>= 0) {
353 int lineNo
= atoi(startMarker
+ lineNoStart
+1);
356 const char* addrStart
= startMarker
+ lineNoStart
+ len
- 2;
357 DbgAddr address
= QString(addrStart
).stripWhiteSpace();
359 // now show the window
360 startMarker
[lineNoStart
] = '\0'; /* split off file name */
361 emit
activateFileLine(startMarker
, lineNo
-1, address
);
367 * Escapes characters that might lead to problems when they appear on gdb's
370 static void normalizeStringArg(QString
& arg
)
373 * Remove trailing backslashes. This approach is a little simplistic,
374 * but we know that there is at the moment no case where a trailing
375 * backslash would make sense.
377 while (!arg
.isEmpty() && arg
[arg
.length()-1] == '\\') {
378 arg
= arg
.left(arg
.length()-1);
383 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
)
385 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
386 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString
);
388 normalizeStringArg(strArg
);
391 // need the working directory when parsing the output
392 m_programWD
= strArg
;
393 } else if (cmd
== DCsetargs
) {
394 // attach saved redirection
395 strArg
+= m_redirect
;
398 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg
.length());
399 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1());
403 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg
)
405 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
406 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum
);
408 SIZED_QString(cmdString
, MAX_FMTLEN
+30);
410 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
);
414 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg
, int intArg
)
416 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
417 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
||
418 cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNumString
||
422 normalizeStringArg(strArg
);
424 SIZED_QString(cmdString
, MAX_FMTLEN
+30+strArg
.length());
429 * intArg specifies which channels should be redirected to
430 * /dev/null. It is a value or'ed together from RDNstdin,
431 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
434 * Note: We rely on that after the DCtty a DCsetargs will follow,
435 * which will ultimately apply the redirection.
437 static const char* const runRedir
[8] = {
441 " </dev/null >/dev/null",
443 " </dev/null 2>/dev/null",
445 " </dev/null >/dev/null 2>&1"
447 if (strArg
.isEmpty())
448 intArg
= 7; /* failsafe if no tty */
449 m_redirect
= runRedir
[intArg
& 7];
451 return makeCmdString(DCtty
, strArg
); /* note: no problem if strArg empty */
454 if (cmd
== DCexamine
) {
455 // make a format specifier from the intArg
456 static const char size
[16] = {
457 '\0', 'b', 'h', 'w', 'g'
459 static const char format
[16] = {
460 '\0', 'x', 'd', 'u', 'o', 't',
461 'a', 'c', 'f', 's', 'i'
463 assert(MDTsizemask
== 0xf); /* lowest 4 bits */
464 assert(MDTformatmask
== 0xf0); /* next 4 bits */
465 int count
= 16; /* number of entities to print */
466 char sizeSpec
= size
[intArg
& MDTsizemask
];
467 char formatSpec
= format
[(intArg
& MDTformatmask
) >> 4];
468 assert(sizeSpec
!= '\0');
469 assert(formatSpec
!= '\0');
470 // adjust count such that 16 lines are printed
471 switch (intArg
& MDTformatmask
) {
472 case MDTstring
: case MDTinsn
:
473 break; /* no modification needed */
475 // all cases drop through:
476 switch (intArg
& MDTsizemask
) {
488 spec
.sprintf("/%d%c%c", count
, sizeSpec
, formatSpec
);
490 return makeCmdString(DCexamine
, spec
, strArg
);
493 if (cmds
[cmd
].argsNeeded
== GdbCmdInfo::argStringNum
)
495 // line numbers are zero-based
496 if (cmd
== DCuntil
|| cmd
== DCbreakline
||
497 cmd
== DCtbreakline
|| cmd
== DCinfoline
)
501 if (cmd
== DCinfoline
)
503 // must split off file name part
504 int slash
= strArg
.findRev('/');
506 strArg
= strArg
.right(strArg
.length()-slash
-1);
508 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg
.latin1(), intArg
);
512 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg
, strArg
.latin1());
517 QString
GdbDriver::makeCmdString(DbgCommand cmd
, QString strArg1
, QString strArg2
)
519 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
520 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argString2
);
522 normalizeStringArg(strArg1
);
523 normalizeStringArg(strArg2
);
525 SIZED_QString(cmdString
, MAX_FMTLEN
+strArg1
.length()+strArg2
.length());
526 cmdString
.sprintf(cmds
[cmd
].fmt
, strArg1
.latin1(), strArg2
.latin1());
530 QString
GdbDriver::makeCmdString(DbgCommand cmd
, int intArg1
, int intArg2
)
532 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
533 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNum2
);
535 SIZED_QString(cmdString
, MAX_FMTLEN
+60);
536 cmdString
.sprintf(cmds
[cmd
].fmt
, intArg1
, intArg2
);
540 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, bool clearLow
)
542 assert(cmd
>= 0 && cmd
< NUM_CMDS
);
543 assert(cmds
[cmd
].argsNeeded
== GdbCmdInfo::argNone
);
546 m_haveCoreFile
= false;
549 return executeCmdString(cmd
, cmds
[cmd
].fmt
, clearLow
);
552 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
,
555 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
), clearLow
);
558 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg
,
562 return executeCmdString(cmd
, makeCmdString(cmd
, intArg
), clearLow
);
565 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg
, int intArg
,
568 return executeCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), clearLow
);
571 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
574 return executeCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), clearLow
);
577 CmdQueueItem
* GdbDriver::executeCmd(DbgCommand cmd
, int intArg1
, int intArg2
,
580 return executeCmdString(cmd
, makeCmdString(cmd
, intArg1
, intArg2
), clearLow
);
583 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QueueMode mode
)
585 return queueCmdString(cmd
, cmds
[cmd
].fmt
, mode
);
588 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
,
591 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
), mode
);
594 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, int intArg
,
597 return queueCmdString(cmd
, makeCmdString(cmd
, intArg
), mode
);
600 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg
, int intArg
,
603 return queueCmdString(cmd
, makeCmdString(cmd
, strArg
, intArg
), mode
);
606 CmdQueueItem
* GdbDriver::queueCmd(DbgCommand cmd
, QString strArg1
, QString strArg2
,
609 return queueCmdString(cmd
, makeCmdString(cmd
, strArg1
, strArg2
), mode
);
612 void GdbDriver::terminate()
618 void GdbDriver::detachAndTerminate()
622 executeCmdString(DCinitialize
, "detach\nquit\n", true);
625 void GdbDriver::interruptInferior()
628 // remove accidentally queued commands
632 static bool isErrorExpr(const char* output
)
635 strncmp(output
, "Cannot access memory at", 23) == 0 ||
636 strncmp(output
, "Attempt to dereference a generic pointer", 40) == 0 ||
637 strncmp(output
, "Attempt to take contents of ", 28) == 0 ||
638 strncmp(output
, "Attempt to use a type name as an expression", 43) == 0 ||
639 strncmp(output
, "There is no member or method named", 34) == 0 ||
640 strncmp(output
, "A parse error in expression", 27) == 0 ||
641 strncmp(output
, "No symbol \"", 11) == 0 ||
642 strncmp(output
, "Internal error: ", 16) == 0;
646 * Returns true if the output is an error message. If wantErrorValue is
647 * true, a new VarTree object is created and filled with the error message.
649 static bool parseErrorMessage(const char* output
,
650 VarTree
*& variable
, bool wantErrorValue
)
652 if (isErrorExpr(output
))
654 if (wantErrorValue
) {
655 // put the error message as value in the variable
656 variable
= new VarTree(QString(), VarTree::NKplain
);
657 const char* endMsg
= strchr(output
, '\n');
659 endMsg
= output
+ strlen(output
);
660 variable
->m_value
= FROM_LATIN1(output
, endMsg
-output
);
669 #if QT_VERSION >= 300
679 VarTree
* GdbDriver::parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
)
681 VarTree
* variable
= 0;
684 * Parse off white space. gdb sometimes prints white space first if the
685 * printed array leaded to an error.
687 const char* p
= output
;
691 // check if this is an error indicating that gdb does not know about QString::null
692 if (cmds
[DCprintQStringStruct
].fmt
== printQStringStructFmt
&&
693 (strncmp(p
, "Internal error: could not find static variable null", 51) == 0 ||
695 * At least gdb 5.2.1 reports the following error when it accesses
696 * QString::null. Checking for it can result in false positives
697 * (where the error is actually triggered by a real NULL pointer
698 * in a badly initialized QString variable) but the consequences
701 strncmp(p
, "Cannot access memory at address 0x0", 35) == 0))
703 /* QString::null doesn't work, use an alternate expression */
704 cmds
[DCprintQStringStruct
].fmt
= printQStringStructNoNullFmt
;
705 // continue and let parseOffErrorExpr catch the error
708 // special case: empty string (0 repetitions)
709 if (strncmp(p
, "Invalid number 0 of repetitions", 31) == 0)
711 variable
= new VarTree(QString(), VarTree::NKplain
);
712 variable
->m_value
= "\"\"";
716 // check for error conditions
717 if (parseErrorMessage(p
, variable
, wantErrorValue
))
730 } while (isspace(*p
));
734 // this is the real data
740 enum { wasNothing
, wasChar
, wasRepeat
} lastThing
= wasNothing
;
742 * A matrix for separators between the individual "things"
743 * that are added to the string. The first index is a bool,
744 * the second index is from the enum above.
746 static const char* separator
[2][3] = {
747 { "\"", 0, ", \"" }, /* normal char is added */
748 { "'", "\", '", ", '" } /* repeated char is added */
751 while (isdigit(*p
)) {
754 unsigned short value
= (unsigned short) strtoul(p
, &end
, 0);
756 goto error
; /* huh? no valid digits */
757 // skip separator and search for a repeat count
759 while (isspace(*p
) || *p
== ',')
761 bool repeats
= strncmp(p
, "<repeats ", 9) == 0;
763 const char* start
= p
;
764 p
= strchr(p
+9, '>'); /* search end and advance */
768 repeatCount
= FROM_LATIN1(start
, p
-start
);
769 while (isspace(*p
) || *p
== ',')
772 // p is now at the next char (or the end)
774 // interpret the value as a QChar
775 // TODO: make cross-architecture compatible
781 (unsigned short&)ch
= value
;
785 ch
.setRow(c
.qch
.row
);
786 ch
.setCell(c
.qch
.cell
);
790 // escape a few frequently used characters
791 char escapeCode
= '\0';
792 switch (ch
.latin1()) {
793 case '\n': escapeCode
= 'n'; break;
794 case '\r': escapeCode
= 'r'; break;
795 case '\t': escapeCode
= 't'; break;
796 case '\b': escapeCode
= 'b'; break;
797 case '\"': escapeCode
= '\"'; break;
798 case '\\': escapeCode
= '\\'; break;
799 case '\0': if (value
== 0) { escapeCode
= '0'; } break;
803 result
+= separator
[repeats
][lastThing
];
805 if (escapeCode
!= '\0') {
811 // fixup repeat count and lastThing
814 result
+= repeatCount
;
815 lastThing
= wasRepeat
;
824 if (lastThing
== wasChar
)
828 variable
= new VarTree(QString(), VarTree::NKplain
);
829 variable
->m_value
= result
;
831 else if (strncmp(p
, "true", 4) == 0)
833 variable
= new VarTree(QString(), VarTree::NKplain
);
834 variable
->m_value
= "QString::null";
836 else if (strncmp(p
, "false", 5) == 0)
838 variable
= new VarTree(QString(), VarTree::NKplain
);
839 variable
->m_value
= "(null)";
846 if (wantErrorValue
) {
847 variable
= new VarTree(QString(), VarTree::NKplain
);
848 variable
->m_value
= "internal parse error";
853 static VarTree
* parseVar(const char*& s
)
862 VarTree::NameKind kind
;
863 if (!parseName(p
, name
, kind
)) {
871 TRACE(QString().sprintf("parse error: = not found after %s", (const char*)name
));
874 // skip the '=' and more whitespace
879 VarTree
* variable
= new VarTree(name
, kind
);
880 variable
->setDeleteChildren(true);
882 if (!parseValue(p
, variable
)) {
890 static void skipNested(const char*& s
, char opening
, char closing
)
894 // parse a nested type
898 * Search for next matching `closing' char, skipping nested pairs of
899 * `opening' and `closing'.
901 while (*p
&& nest
> 0) {
904 } else if (*p
== closing
) {
910 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
916 * Find the end of line that is not inside braces
918 static void findEnd(const char*& s
)
921 while (*p
&& *p
!='\n') {
922 while (*p
&& *p
!='\n' && *p
!='{')
926 skipNested(p
, '{', '}'); p
--;
932 static bool isNumberish(const char ch
)
934 return (ch
>='0' && ch
<='9') || ch
=='.' || ch
=='x';
937 void skipString(const char*& p
)
942 while (*p
!= quote
) {
944 // skip escaped character
945 // no special treatment for octal values necessary
948 // simply return if no more characters
956 * Strings can consist of several parts, some of which contain repeated
960 // look ahaead for <repeats 123 times>
964 if (strncmp(q
, "<repeats ", 9) == 0) {
966 while (*p
!= '\0' && *p
!= '>')
969 p
++; /* skip the '>' */
973 // is the string continued?
975 // look ahead for another quote
979 if (*q
== '"' || *q
== '\'') {
985 /* very long strings are followed by `...' */
986 if (*p
== '.' && p
[1] == '.' && p
[2] == '.') {
991 static void skipNestedWithString(const char*& s
, char opening
, char closing
)
995 // parse a nested expression
999 * Search for next matching `closing' char, skipping nested pairs of
1000 * `opening' and `closing' as well as strings.
1002 while (*p
&& nest
> 0) {
1003 if (*p
== opening
) {
1005 } else if (*p
== closing
) {
1007 } else if (*p
== '\'' || *p
== '\"') {
1014 TRACE(QString().sprintf("parse error: mismatching %c%c at %-20.20s", opening
, closing
, s
));
1019 inline void skipName(const char*& p
)
1021 // allow : (for enumeration values) and $ and . (for _vtbl.)
1022 while (isalnum(*p
) || *p
== '_' || *p
== ':' || *p
== '$' || *p
== '.')
1026 static bool parseName(const char*& s
, QString
& name
, VarTree::NameKind
& kind
)
1028 kind
= VarTree::NKplain
;
1031 // examples of names:
1034 // <string<a,b<c>,7> >
1037 skipNested(p
, '<', '>');
1038 name
= FROM_LATIN1(s
, p
- s
);
1039 kind
= VarTree::NKtype
;
1043 // name, which might be "static"; allow dot for "_vtbl."
1046 TRACE(QString().sprintf("parse error: not a name %-20.20s", s
));
1050 if (len
== 6 && strncmp(s
, "static", 6) == 0) {
1051 kind
= VarTree::NKstatic
;
1053 // its a static variable, name comes now
1059 TRACE(QString().sprintf("parse error: not a name after static %-20.20s", s
));
1064 name
= FROM_LATIN1(s
, len
);
1066 // return the new position
1071 static bool parseValue(const char*& s
, VarTree
* variable
)
1073 variable
->m_value
= "";
1078 if (!parseNested(s
, variable
)) {
1081 // must be the closing brace
1083 TRACE("parse error: missing } of " + variable
->getText());
1087 // final white space
1091 // examples of leaf values (cannot be the empty string):
1097 // (DwContentType&) @0x8123456: {...}
1101 // 0x823abc <Array<int> virtual table>
1102 // (void (*)()) 0x8048480 <f(E *, char)>
1105 // &parseP (HTMLClueV *, char *)
1106 // Variable "x" is not available.
1113 skipNested(p
, '(', ')');
1117 variable
->m_value
= FROM_LATIN1(s
, p
- s
);
1120 bool reference
= false;
1122 // skip reference marker
1126 const char* start
= p
;
1130 // some values consist of more than one token
1131 bool checkMultiPart
= false;
1133 if (p
[0] == '0' && p
[1] == 'x') {
1136 while (isxdigit(*p
))
1140 * Assume this is a pointer, but only if it's not a reference, since
1141 * references can't be expanded.
1144 variable
->m_varKind
= VarTree::VKpointer
;
1147 * References are followed by a colon, in which case we'll
1148 * find the value following the reference address.
1153 // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
1157 checkMultiPart
= true;
1158 } else if (isdigit(*p
)) {
1159 // parse decimal number, possibly a float
1162 if (*p
== '.') { /* TODO: obey i18n? */
1168 if (*p
== 'e' || *p
== 'E') {
1171 if (*p
== '-' || *p
== '+')
1177 // for char variables there is the char, eg. 10 '\n'
1178 checkMultiPart
= true;
1179 } else if (*p
== '<') {
1180 // e.g. <optimized out>
1181 skipNested(p
, '<', '>');
1182 } else if (*p
== '"' || *p
== '\'') {
1183 // character may have multipart: '\000' <repeats 11 times>
1184 checkMultiPart
= *p
== '\'';
1187 } else if (*p
== '&') {
1191 while (isspace(*p
)) {
1195 skipNested(p
, '(', ')');
1197 } else if (strncmp(p
, "Variable \"", 10) == 0) {
1198 // Variable "x" is not available.
1199 p
+= 10; // skip to "
1201 if (strncmp(p
, "\" is not available.", 19) == 0) {
1205 // must be an enumeration value
1208 variable
->m_value
+= FROM_LATIN1(start
, p
- start
);
1210 if (checkMultiPart
) {
1214 // may be followed by a string or <...>
1217 if (*p
== '"' || *p
== '\'') {
1219 } else if (*p
== '<') {
1220 // if this value is part of an array, it might be followed
1221 // by <repeats 15 times>, which we don't skip here
1222 if (strncmp(p
, "<repeats ", 9) != 0)
1223 skipNested(p
, '<', '>');
1226 // there is always a blank before the string,
1227 // which we will include in the final string value
1228 variable
->m_value
+= FROM_LATIN1(start
-1, (p
- start
)+1);
1229 // if this was a pointer, reset that flag since we
1230 // now got the value
1231 variable
->m_varKind
= VarTree::VKsimple
;
1235 if (variable
->m_value
.length() == 0) {
1236 TRACE("parse error: no value for " + variable
->getText());
1240 // final white space
1246 * If this was a reference, the value follows. It might even be a
1247 * composite variable!
1253 if (variable
->m_varKind
== VarTree::VKpointer
) {
1254 variable
->setDelayedExpanding(true);
1261 static bool parseNested(const char*& s
, VarTree
* variable
)
1263 // could be a structure or an array
1268 bool isStruct
= false;
1270 * If there is a name followed by an = or an < -- which starts a type
1271 * name -- or "static", it is a structure
1273 if (*p
== '<' || *p
== '}') {
1275 } else if (strncmp(p
, "static ", 7) == 0) {
1277 } else if (isalpha(*p
) || *p
== '_' || *p
== '$') {
1278 // look ahead for a comma after the name
1285 p
= s
; /* rescan the name */
1288 if (!parseVarSeq(p
, variable
)) {
1291 variable
->m_varKind
= VarTree::VKstruct
;
1293 if (!parseValueSeq(p
, variable
)) {
1296 variable
->m_varKind
= VarTree::VKarray
;
1302 static bool parseVarSeq(const char*& s
, VarTree
* variable
)
1304 // parse a comma-separated sequence of variables
1305 VarTree
* var
= variable
; /* var != 0 to indicate success if empty seq */
1309 if (strncmp(s
, "<No data fields>}", 17) == 0)
1311 // no member variables, so break out immediately
1312 s
+= 16; /* go to the closing brace */
1317 break; /* syntax error */
1318 variable
->appendChild(var
);
1321 // skip the comma and whitespace
1329 static bool parseValueSeq(const char*& s
, VarTree
* variable
)
1331 // parse a comma-separated sequence of variables
1336 name
.sprintf("[%d]", index
);
1337 VarTree
* var
= new VarTree(name
, VarTree::NKplain
);
1338 var
->setDeleteChildren(true);
1339 good
= parseValue(s
, var
);
1344 // a value may be followed by "<repeats 45 times>"
1345 if (strncmp(s
, "<repeats ", 9) == 0) {
1348 int l
= strtol(s
, &end
, 10);
1349 if (end
== s
|| strncmp(end
, " times>", 7) != 0) {
1350 // should not happen
1354 TRACE(QString().sprintf("found <repeats %d times> in array", l
));
1355 // replace name and advance index
1356 name
.sprintf("[%d .. %d]", index
, index
+l
-1);
1359 // skip " times>" and space
1361 // possible final space
1367 variable
->appendChild(var
);
1371 // skip the comma and whitespace
1375 // sometimes there is a closing brace after a comma
1383 * Parses a stack frame.
1385 static void parseFrameInfo(const char*& s
, QString
& func
,
1386 QString
& file
, int& lineNo
, DbgAddr
& address
)
1390 // next may be a hexadecimal address
1392 const char* start
= p
;
1396 while (isxdigit(*p
))
1398 address
= FROM_LATIN1(start
, p
-start
);
1399 if (strncmp(p
, " in ", 4) == 0)
1402 address
= DbgAddr();
1404 const char* start
= p
;
1405 // check for special signal handler frame
1406 if (strncmp(p
, "<signal handler called>", 23) == 0) {
1407 func
= FROM_LATIN1(start
, 23);
1415 // search opening parenthesis
1416 while (*p
!= '\0' && *p
!= '(')
1426 * Skip parameters. But notice that for complicated conversion
1427 * functions (eg. "operator int(**)()()", ie. convert to pointer to
1428 * pointer to function) as well as operator()(...) we have to skip
1429 * additional pairs of parentheses. Furthermore, recent gdbs write the
1430 * demangled name followed by the arguments in a pair of parentheses,
1431 * where the demangled name can end in "const".
1434 skipNestedWithString(p
, '(', ')');
1438 if (strncmp(p
, "const", 5) == 0) {
1443 } while (*p
== '(');
1445 // check for file position
1446 if (strncmp(p
, "at ", 3) == 0) {
1448 const char* fileStart
= p
;
1449 // go for the end of the line
1450 while (*p
!= '\0' && *p
!= '\n')
1452 // search back for colon
1453 const char* colon
= p
;
1456 } while (*colon
!= ':');
1457 file
= FROM_LATIN1(fileStart
, colon
-fileStart
);
1458 lineNo
= atoi(colon
+1)-1;
1463 // check for "from shared lib"
1464 if (strncmp(p
, "from ", 5) == 0) {
1466 // go for the end of the line
1467 while (*p
!= '\0' && *p
!= '\n')
1476 // construct the function name (including file info)
1480 func
= FROM_LATIN1(start
, p
-start
-1); /* don't include \n */
1485 * Replace \n (and whitespace around it) in func by a blank. We cannot
1486 * use QString::simplifyWhiteSpace() for this because this would also
1487 * simplify space that belongs to a string arguments that gdb sometimes
1488 * prints in the argument lists of the function.
1490 ASSERT(!isspace(func
[0].latin1())); /* there must be non-white before first \n */
1492 while ((nl
= func
.find('\n', nl
)) >= 0) {
1493 // search back to the beginning of the whitespace
1494 int startWhite
= nl
;
1497 } while (isspace(func
[startWhite
].latin1()));
1499 // search forward to the end of the whitespace
1502 } while (isspace(func
[nl
].latin1()));
1504 func
.replace(startWhite
, nl
-startWhite
, " ");
1505 /* continue searching for more \n's at this place: */
1512 * Parses a stack frame including its frame number
1514 static bool parseFrame(const char*& s
, int& frameNo
, QString
& func
,
1515 QString
& file
, int& lineNo
, DbgAddr
& address
)
1518 // #1 0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
1519 // Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155
1521 // must start with a hash mark followed by number
1522 // or with "Breakpoint " followed by number and comma
1526 s
++; /* skip the hash mark */
1527 } else if (strncmp(s
, "Breakpoint ", 11) == 0) {
1528 if (!isdigit(s
[11]))
1530 s
+= 11; /* skip "Breakpoint" */
1539 while (isspace(*s
) || *s
== ',')
1541 parseFrameInfo(s
, func
, file
, lineNo
, address
);
1545 void GdbDriver::parseBackTrace(const char* output
, QList
<StackFrame
>& stack
)
1548 int lineNo
, frameNo
;
1551 while (::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
)) {
1552 StackFrame
* frm
= new StackFrame
;
1553 frm
->frameNo
= frameNo
;
1554 frm
->fileName
= file
;
1555 frm
->lineNo
= lineNo
;
1556 frm
->address
= address
;
1557 frm
->var
= new VarTree(func
, VarTree::NKplain
);
1562 bool GdbDriver::parseFrameChange(const char* output
, int& frameNo
,
1563 QString
& file
, int& lineNo
, DbgAddr
& address
)
1566 return ::parseFrame(output
, frameNo
, func
, file
, lineNo
, address
);
1570 bool GdbDriver::parseBreakList(const char* output
, QList
<Breakpoint
>& brks
)
1572 // skip first line, which is the headline
1573 const char* p
= strchr(output
, '\n');
1584 uint ignoreCount
= 0;
1588 while (*p
!= '\0') {
1590 long bpNum
= strtol(p
, &dummy
, 10); /* don't care about overflows */
1595 Breakpoint::Type bpType
= Breakpoint::breakpoint
;
1596 if (strncmp(p
, "breakpoint", 10) == 0) {
1598 } else if (strncmp(p
, "hw watchpoint", 13) == 0) {
1599 bpType
= Breakpoint::watchpoint
;
1601 } else if (strncmp(p
, "watchpoint", 10) == 0) {
1602 bpType
= Breakpoint::watchpoint
;
1611 while (*p
!= '\0' && !isspace(*p
)) /* "keep" or "del" */
1619 while (*p
!= '\0' && !isspace(*p
)) /* "y" or "n" */
1625 // the address, if present
1626 if (bpType
== Breakpoint::breakpoint
&&
1627 strncmp(p
, "0x", 2) == 0)
1629 const char* start
= p
;
1630 while (*p
!= '\0' && !isspace(*p
))
1632 address
= FROM_LATIN1(start
, p
-start
);
1633 while (isspace(*p
) && *p
!= '\n')
1638 address
= QString();
1640 // remainder is location, hit and ignore count, condition
1643 condition
= QString();
1644 end
= strchr(p
, '\n');
1647 p
+= location
.length();
1649 location
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1650 p
= end
+1; /* skip over \n */
1653 // may be continued in next line
1654 while (isspace(*p
)) { /* p points to beginning of line */
1655 // skip white space at beginning of line
1660 end
= strchr(p
, '\n');
1664 if (strncmp(p
, "breakpoint already hit", 22) == 0) {
1665 // extract the hit count
1667 hits
= strtol(p
, &dummy
, 10);
1668 TRACE(QString().sprintf("hit count %d", hits
));
1669 } else if (strncmp(p
, "stop only if ", 13) == 0) {
1670 // extract condition
1672 condition
= FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1673 TRACE("condition: "+condition
);
1674 } else if (strncmp(p
, "ignore next ", 12) == 0) {
1675 // extract ignore count
1677 ignoreCount
= strtol(p
, &dummy
, 10);
1678 TRACE(QString().sprintf("ignore count %d", ignoreCount
));
1680 // indeed a continuation
1681 location
+= " " + FROM_LATIN1(p
, end
-p
).stripWhiteSpace();
1685 p
++; /* skip '\n' */
1687 Breakpoint
* bp
= new Breakpoint
;
1690 bp
->temporary
= disp
== 'd';
1691 bp
->enabled
= enable
== 'y';
1692 bp
->location
= location
;
1693 bp
->address
= address
;
1694 bp
->hitCount
= hits
;
1695 bp
->ignoreCount
= ignoreCount
;
1696 bp
->condition
= condition
;
1702 bool GdbDriver::parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
)
1704 if (strcmp(output
, "\n") == 0 || strncmp(output
, "No stack.", 9) == 0) {
1715 const char* p
= output
;
1716 while (*p
!= '\0') {
1717 // seach look for thread id, watching out for the focus indicator
1718 bool hasFocus
= false;
1719 while (isspace(*p
)) /* may be \n from prev line: see "No stack" below */
1724 // there follows only whitespace
1727 id
= strtol(p
, &end
, 10);
1729 // syntax error: no number found; bail out
1739 * Now follows the thread's SYSTAG. It is terminated by two blanks.
1741 end
= strstr(p
, " ");
1743 // syntax error; bail out
1746 systag
= FROM_LATIN1(p
, end
-p
);
1750 * Now follows a standard stack frame. Sometimes, however, gdb
1751 * catches a thread at an instant where it doesn't have a stack.
1753 if (strncmp(p
, "[No stack.]", 11) != 0) {
1754 ::parseFrameInfo(p
, func
, file
, lineNo
, address
);
1756 func
= "[No stack]";
1759 address
= QString();
1760 p
+= 11; /* \n is skipped above */
1763 ThreadInfo
* thr
= new ThreadInfo
;
1765 thr
->threadName
= systag
;
1766 thr
->hasFocus
= hasFocus
;
1767 thr
->function
= func
;
1768 thr
->fileName
= file
;
1769 thr
->lineNo
= lineNo
;
1770 thr
->address
= address
;
1771 threads
.append(thr
);
1776 static bool parseNewBreakpoint(const char* o
, int& id
,
1777 QString
& file
, int& lineNo
, QString
& address
);
1778 static bool parseNewWatchpoint(const char* o
, int& id
,
1781 bool GdbDriver::parseBreakpoint(const char* output
, int& id
,
1782 QString
& file
, int& lineNo
, QString
& address
)
1784 const char* o
= output
;
1785 // skip lines of that begin with "(Cannot find"
1786 while (strncmp(o
, "(Cannot find", 12) == 0) {
1787 o
= strchr(o
, '\n');
1790 o
++; /* skip newline */
1793 if (strncmp(o
, "Breakpoint ", 11) == 0) {
1794 output
+= 11; /* skip "Breakpoint " */
1795 return ::parseNewBreakpoint(output
, id
, file
, lineNo
, address
);
1796 } else if (strncmp(o
, "Hardware watchpoint ", 20) == 0) {
1798 return ::parseNewWatchpoint(output
, id
, address
);
1799 } else if (strncmp(o
, "Watchpoint ", 11) == 0) {
1801 return ::parseNewWatchpoint(output
, id
, address
);
1806 static bool parseNewBreakpoint(const char* o
, int& id
,
1807 QString
& file
, int& lineNo
, QString
& address
)
1811 id
= strtoul(o
, &p
, 10);
1815 // check for the address
1816 if (strncmp(p
, " at 0x", 6) == 0) {
1817 char* start
= p
+4; /* skip " at ", but not 0x */
1819 while (isxdigit(*p
))
1821 address
= FROM_LATIN1(start
, p
-start
);
1825 char* fileStart
= strstr(p
, "file ");
1827 return !address
.isEmpty(); /* parse error only if there's no address */
1831 char* numStart
= strstr(fileStart
, ", line ");
1832 QString fileName
= FROM_LATIN1(fileStart
, numStart
-fileStart
);
1834 int line
= strtoul(numStart
, &p
, 10);
1839 lineNo
= line
-1; /* zero-based! */
1843 static bool parseNewWatchpoint(const char* o
, int& id
,
1848 id
= strtoul(o
, &p
, 10);
1852 if (strncmp(p
, ": ", 2) != 0)
1856 // all the rest on the line is the expression
1857 expr
= FROM_LATIN1(p
, strlen(p
)).stripWhiteSpace();
1861 void GdbDriver::parseLocals(const char* output
, QList
<VarTree
>& newVars
)
1863 // check for possible error conditions
1864 if (strncmp(output
, "No symbol table", 15) == 0)
1869 while (*output
!= '\0') {
1870 while (isspace(*output
))
1872 if (*output
== '\0')
1874 // skip occurrences of "No locals" and "No args"
1875 if (strncmp(output
, "No locals", 9) == 0 ||
1876 strncmp(output
, "No arguments", 12) == 0)
1878 output
= strchr(output
, '\n');
1885 VarTree
* variable
= parseVar(output
);
1886 if (variable
== 0) {
1889 // do not add duplicates
1890 for (VarTree
* o
= newVars
.first(); o
!= 0; o
= newVars
.next()) {
1891 if (o
->getText() == variable
->getText()) {
1896 newVars
.append(variable
);
1901 bool GdbDriver::parsePrintExpr(const char* output
, bool wantErrorValue
, VarTree
*& var
)
1903 // check for error conditions
1904 if (parseErrorMessage(output
, var
, wantErrorValue
))
1908 // parse the variable
1909 var
= parseVar(output
);
1914 bool GdbDriver::parseChangeWD(const char* output
, QString
& message
)
1916 bool isGood
= false;
1917 message
= QString(output
).simplifyWhiteSpace();
1918 if (message
.isEmpty()) {
1919 message
= i18n("New working directory: ") + m_programWD
;
1925 bool GdbDriver::parseChangeExecutable(const char* output
, QString
& message
)
1929 m_haveCoreFile
= false;
1932 * Lines starting with the following do not indicate errors:
1933 * Using host libthread_db
1934 * (no debugging symbols found)
1936 while (strncmp(output
, "Using host libthread_db", 23) == 0 ||
1937 strncmp(output
, "(no debugging symbols found)", 28) == 0)
1939 // this line is good, go to the next one
1940 const char* end
= strchr(output
, '\n');
1942 output
+= strlen(output
);
1948 * If we've parsed all lines, there was no error.
1950 return output
[0] == '\0';
1953 bool GdbDriver::parseCoreFile(const char* output
)
1955 // if command succeeded, gdb emits a line starting with "#0 "
1956 m_haveCoreFile
= strstr(output
, "\n#0 ") != 0;
1957 return m_haveCoreFile
;
1960 uint
GdbDriver::parseProgramStopped(const char* output
, QString
& message
)
1962 // optionally: "program changed, rereading symbols",
1964 // "Program exited normally"
1965 // "Program terminated with wignal SIGSEGV"
1966 // "Program received signal SIGINT" or other signal
1969 // go through the output, line by line, checking what we have
1970 const char* start
= output
- 1;
1971 uint flags
= SFprogramActive
;
1972 message
= QString();
1974 start
++; /* skip '\n' */
1976 if (strncmp(start
, "Program ", 8) == 0 ||
1977 strncmp(start
, "ptrace: ", 8) == 0) {
1979 * When we receive a signal, the program remains active.
1981 * Special: If we "stopped" in a corefile, the string "Program
1982 * terminated with signal"... is displayed. (Normally, we see
1983 * "Program received signal"... when a signal happens.)
1985 if (strncmp(start
, "Program exited", 14) == 0 ||
1986 (strncmp(start
, "Program terminated", 18) == 0 && !m_haveCoreFile
) ||
1987 strncmp(start
, "ptrace: ", 8) == 0)
1989 flags
&= ~SFprogramActive
;
1993 const char* endOfMessage
= strchr(start
, '\n');
1994 if (endOfMessage
== 0)
1995 endOfMessage
= start
+ strlen(start
);
1996 message
= FROM_LATIN1(start
, endOfMessage
-start
);
1997 } else if (strncmp(start
, "Breakpoint ", 11) == 0) {
1999 * We stopped at a (permanent) breakpoint (gdb doesn't tell us
2000 * that it stopped at a temporary breakpoint).
2002 flags
|= SFrefreshBreak
;
2003 } else if (strstr(start
, "re-reading symbols.") != 0) {
2004 flags
|= SFrefreshSource
;
2007 // next line, please
2008 start
= strchr(start
, '\n');
2009 } while (start
!= 0);
2012 * Gdb only notices when new threads have appeared, but not when a
2013 * thread finishes. So we always have to assume that the list of
2014 * threads has changed.
2016 flags
|= SFrefreshThreads
;
2021 void GdbDriver::parseSharedLibs(const char* output
, QStrList
& shlibs
)
2023 if (strncmp(output
, "No shared libraries loaded", 26) == 0)
2026 // parse the table of shared libraries
2028 // strip off head line
2029 output
= strchr(output
, '\n');
2032 output
++; /* skip '\n' */
2034 while (*output
!= '\0') {
2035 // format of a line is
2036 // 0x404c5000 0x40580d90 Yes /lib/libc.so.5
2037 // 3 blocks of non-space followed by space
2038 for (int i
= 0; *output
!= '\0' && i
< 3; i
++) {
2039 while (*output
!= '\0' && !isspace(*output
)) { /* non-space */
2042 while (isspace(*output
)) { /* space */
2046 if (*output
== '\0')
2048 const char* start
= output
;
2049 output
= strchr(output
, '\n');
2051 output
= start
+ strlen(start
);
2052 shlibName
= FROM_LATIN1(start
, output
-start
);
2053 if (*output
!= '\0')
2055 shlibs
.append(shlibName
);
2056 TRACE("found shared lib " + shlibName
);
2060 bool GdbDriver::parseFindType(const char* output
, QString
& type
)
2062 if (strncmp(output
, "type = ", 7) != 0)
2066 * Everything else is the type. We strip off all white-space from the
2071 type
.replace(QRegExp("\\s+"), "");
2075 void GdbDriver::parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
)
2077 if (strncmp(output
, "The program has no registers now", 32) == 0) {
2084 // parse register values
2085 while (*output
!= '\0')
2087 // skip space at the start of the line
2088 while (isspace(*output
))
2092 const char* start
= output
;
2093 while (*output
!= '\0' && !isspace(*output
))
2095 if (*output
== '\0')
2097 regName
= FROM_LATIN1(start
, output
-start
);
2100 while (isspace(*output
))
2103 RegisterInfo
* reg
= new RegisterInfo
;
2104 reg
->regName
= regName
;
2107 * If we find a brace now, this is a vector register. We look for
2108 * the closing brace and treat the result as cooked value.
2113 skipNested(output
, '{', '}');
2114 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2115 // skip space, but not the end of line
2116 while (isspace(*output
) && *output
!= '\n')
2118 // get rid of the braces at the begining and the end
2120 if (value
[value
.length()-1] == '}') {
2121 value
= value
.left(value
.length()-1);
2123 // gdb 5.3 doesn't print a separate set of raw values
2124 if (*output
== '{') {
2125 // another set of vector follows
2126 // what we have so far is the raw value
2127 reg
->rawValue
= value
;
2130 skipNested(output
, '{', '}');
2131 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2134 // find first type that does not have an array, this is the RAW value
2135 const char* end
=start
;
2137 const char* cur
=start
;
2139 while (*cur
!= '=' && cur
<end
)
2142 while (isspace(*cur
) && cur
<end
)
2144 if (isNumberish(*cur
)) {
2146 while (*end
&& (*end
!='}') && (*end
!=',') && (*end
!='\n'))
2148 QString rawValue
= FROM_LATIN1(cur
, end
-cur
).simplifyWhiteSpace();
2149 reg
->rawValue
= rawValue
;
2151 if (rawValue
.left(2)=="0x") {
2152 // ok we have a raw value, now get it's type
2154 while (isspace(*end
) || *end
=='=') end
--;
2157 while (*cur
!='{' && *cur
!=' ')
2160 reg
->type
=FROM_LATIN1(cur
, end
-cur
);
2167 // skip to the end of line
2168 while (*output
!= '\0' && *output
!= '\n')
2170 // get rid of the braces at the begining and the end
2172 if (value
[value
.length()-1] == '}') {
2173 value
= value
.left(value
.length()-1);
2176 reg
->cookedValue
= value
;
2180 // the rest of the line is the register value
2182 output
= strchr(output
,'\n');
2184 output
= start
+ strlen(start
);
2185 value
= FROM_LATIN1(start
, output
-start
).simplifyWhiteSpace();
2188 * We split the raw from the cooked values.
2189 * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
2190 * Here, the cooked value comes first, and the raw value is in
2193 int pos
= value
.find(" (raw ");
2196 reg
->cookedValue
= value
.left(pos
);
2197 reg
->rawValue
= value
.mid(pos
+6);
2198 if (reg
->rawValue
.right(1) == ")") // remove closing bracket
2199 reg
->rawValue
.truncate(reg
->rawValue
.length()-1);
2204 * In other cases we split off the first token (separated by
2205 * whitespace). It is the raw value. The remainder of the line
2206 * is the cooked value.
2208 int pos
= value
.find(' ');
2210 reg
->rawValue
= value
;
2211 reg
->cookedValue
= QString();
2213 reg
->rawValue
= value
.left(pos
);
2214 reg
->cookedValue
= value
.mid(pos
+1);
2218 if (*output
!= '\0')
2219 output
++; /* skip '\n' */
2225 bool GdbDriver::parseInfoLine(const char* output
, QString
& addrFrom
, QString
& addrTo
)
2227 // "is at address" or "starts at address"
2228 const char* start
= strstr(output
, "s at address ");
2233 const char* p
= start
;
2234 while (*p
!= '\0' && !isspace(*p
))
2236 addrFrom
= FROM_LATIN1(start
, p
-start
);
2238 start
= strstr(p
, "and ends at ");
2246 while (*p
!= '\0' && !isspace(*p
))
2248 addrTo
= FROM_LATIN1(start
, p
-start
);
2253 void GdbDriver::parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
)
2257 if (strncmp(output
, "Dump of assembler", 17) != 0) {
2261 code
.append(new DisassembledCode(c
));
2265 // remove first line
2266 const char* p
= strchr(output
, '\n');
2268 return; /* not a regular output */
2273 const char* end
= strstr(output
, "End of assembler");
2275 end
= p
+ strlen(p
);
2279 // remove function offsets from the lines
2282 const char* start
= p
;
2284 while (p
!= end
&& !isspace(*p
))
2286 address
= FROM_LATIN1(start
, p
-start
);
2288 // function name (enclosed in '<>', followed by ':')
2289 while (p
!= end
&& *p
!= '<')
2292 skipNested(p
, '<', '>');
2297 while (p
!= end
&& isspace(*p
))
2300 // code until end of line
2302 while (p
!= end
&& *p
!= '\n')
2304 if (p
!= end
) /* include '\n' */
2307 DisassembledCode
* c
= new DisassembledCode
;
2308 c
->address
= address
;
2309 c
->code
= FROM_LATIN1(start
, p
-start
);
2314 QString
GdbDriver::parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
)
2316 if (isErrorExpr(output
)) {
2317 // error; strip space
2318 QString msg
= output
;
2319 return msg
.stripWhiteSpace();
2322 const char* p
= output
; /* save typing */
2328 const char* start
= p
;
2329 while (*p
!= '\0' && *p
!= ':' && !isspace(*p
))
2331 addr
= FROM_LATIN1(start
, p
-start
);
2333 // parse function offset
2337 while (*p
!= '\0' && !(*p
== ':' && isspace(p
[1])))
2339 addr
.fnoffs
= FROM_LATIN1(start
, p
-start
);
2343 // skip space; this may skip a new-line char!
2346 // everything to the end of the line is the memory dump
2347 const char* end
= strchr(p
, '\n');
2349 dump
= FROM_LATIN1(p
, end
-p
);
2352 dump
= FROM_LATIN1(p
, strlen(p
));
2355 MemoryDump
* md
= new MemoryDump
;
2364 QString
GdbDriver::editableValue(VarTree
* value
)
2366 const char* s
= value
->m_value
.latin1();
2368 // if the variable is a pointer value that contains a cast,
2371 skipNested(s
, '(', ')');
2378 const char* start
= s
;
2380 if (strncmp(s
, "0x", 2) == 0)
2383 while (isxdigit(*s
))
2387 * What we saw so far might have been a reference. If so, edit the
2388 * referenced value. Otherwise, edit the pointer.
2396 // if it's a pointer to a string, remove the string
2397 const char* end
= s
;
2402 return FROM_LATIN1(start
, end
-start
);
2405 return FROM_LATIN1(start
, strlen(start
));
2409 // else leave it unchanged (or stripped of the reference preamble)
2413 QString
GdbDriver::parseSetVariable(const char* output
)
2415 // if there is any output, it is an error message
2416 QString msg
= output
;
2417 return msg
.stripWhiteSpace();
2421 #include "gdbdriver.moc"