Recognize the ... that terminates large arrays with many different values.
[kdbg.git] / kdbg / xsldbgdriver.cpp
blob2683243c9a500eb20d03bd809a75093b3d67d9af
1 // $Id$
3 // Copyright by Johannes Sixt, Keith Isdale
4 // This file is under GPL, the GNU General Public Licence
6 #include "xsldbgdriver.h"
7 #include "exprwnd.h"
8 #include <qstringlist.h>
9 #include <klocale.h> /* i18n */
10 #include <ctype.h>
11 #include <stdlib.h> /* strtol, atoi */
12 #include <string.h> /* strcpy */
13 #include <kmessagebox.h>
15 #include "assert.h"
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #include "mydebug.h"
22 static VarTree *parseVar(const char *&s);
23 static bool parseName(const char *&s, QString & name,
24 VarTree::NameKind & kind);
25 static bool parseValue(const char *&s, VarTree * variable);
26 static bool isErrorExpr(const char *output);
28 #define TERM_IO_ALLOWED 1
30 // TODO: make this cmd info stuff non-static to allow multiple
31 // simultaneous gdbs to run!
33 struct XsldbgCmdInfo {
34 DbgCommand cmd;
35 const char *fmt; /* format string */
36 enum Args {
37 argNone, argString, argNum,
38 argStringNum, argNumString,
39 argString2, argNum2
40 } argsNeeded;
44 * The following array of commands must be sorted by the DC* values,
45 * because they are used as indices.
47 static XsldbgCmdInfo cmds[] = {
48 {DCinitialize, "init\n", XsldbgCmdInfo::argNone},
49 {DCtty, "tty %s\n", XsldbgCmdInfo::argString},
50 {DCexecutable, "source %s\n", XsldbgCmdInfo::argString}, /* force a restart */
51 {DCtargetremote, "print 'target remote %s'\n", XsldbgCmdInfo::argString},
52 {DCcorefile, "data %s\n", XsldbgCmdInfo::argString}, /* force a restart */
53 {DCattach, "print 'attach %s'\n", XsldbgCmdInfo::argString},
54 {DCinfolinemain, "print 'info main line'\n", XsldbgCmdInfo::argNone},
55 {DCinfolocals, "locals -f\n", XsldbgCmdInfo::argNone},
56 {DCinforegisters, "print 'info reg'\n", XsldbgCmdInfo::argNone},
57 {DCexamine, "print 'x %s %s'\n", XsldbgCmdInfo::argString2},
58 {DCinfoline, "print 'templates %s:%d'\n", XsldbgCmdInfo::argStringNum},
59 {DCdisassemble, "print 'disassemble %s %s'\n", XsldbgCmdInfo::argString2},
60 {DCsetargs, "data %s\n", XsldbgCmdInfo::argString},
61 {DCsetenv, "addparam %s %s\n", XsldbgCmdInfo::argString2},
62 {DCunsetenv, "unset env %s\n", XsldbgCmdInfo::argString},
63 {DCsetoption, "setoption %s %d\n", XsldbgCmdInfo::argStringNum},
64 {DCcd, "chdir %s\n", XsldbgCmdInfo::argString},
65 {DCbt, "where\n", XsldbgCmdInfo::argNone},
66 {DCrun, "run\nsource\n", XsldbgCmdInfo::argNone}, /* Ensure that at the start
67 of executing XSLT we show the XSLT file */
68 {DCcont, "continue\n", XsldbgCmdInfo::argNone},
69 {DCstep, "step\n", XsldbgCmdInfo::argNone},
70 {DCstepi, "step\n", XsldbgCmdInfo::argNone},
71 {DCnext, "next\n", XsldbgCmdInfo::argNone},
72 {DCnexti, "next\n", XsldbgCmdInfo::argNone},
73 {DCfinish, "stepup\n", XsldbgCmdInfo::argNone},
74 {DCuntil, "continue %s:%d\n", XsldbgCmdInfo::argStringNum},
75 {DCkill, "quit\n", XsldbgCmdInfo::argNone},
76 {DCbreaktext, "break %s\n", XsldbgCmdInfo::argString},
77 {DCbreakline, "break -l %s %d\n", XsldbgCmdInfo::argStringNum},
78 {DCtbreakline, "break -l %s %d\n", XsldbgCmdInfo::argStringNum },
79 {DCbreakaddr, "print `break *%s`\n", XsldbgCmdInfo::argString },
80 {DCtbreakaddr, "print `tbreak *%s`\n", XsldbgCmdInfo::argString },
81 {DCwatchpoint, "print 'watch %s'\n", XsldbgCmdInfo::argString},
82 {DCdelete, "delete %d\n", XsldbgCmdInfo::argNum},
83 {DCenable, "enable %d\n", XsldbgCmdInfo::argNum},
84 {DCdisable, "disable %d\n", XsldbgCmdInfo::argNum},
85 {DCprint, "print %s\n", XsldbgCmdInfo::argString},
86 {DCprintDeref, "print 'print (*%s)'\n", XsldbgCmdInfo::argString},
87 {DCprintStruct, "print 'print %s'\n", XsldbgCmdInfo::argString},
88 {DCprintQStringStruct, "print 'print %s'\n", XsldbgCmdInfo::argString},
89 {DCframe, "frame %d\n", XsldbgCmdInfo::argNum},
90 {DCfindType, "print 'whatis %s'\n", XsldbgCmdInfo::argString},
91 {DCinfosharedlib, "stylesheets\n", XsldbgCmdInfo::argNone},
92 {DCthread, "print 'thread %d'\n", XsldbgCmdInfo::argNum},
93 {DCinfothreads, "print 'info threads'\n", XsldbgCmdInfo::argNone},
94 {DCinfobreak, "show\n", XsldbgCmdInfo::argNone},
95 {DCcondition, "print 'condition %d %s'\n", XsldbgCmdInfo::argNumString},
96 {DCsetpc, "print 'set variable $pc=%s'\n", XsldbgCmdInfo::argString},
97 {DCignore, "print 'ignore %d %d'\n", XsldbgCmdInfo::argNum2},
98 {DCsetvariable, "set %s %s\n", XsldbgCmdInfo::argString2},
101 #define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
102 #define MAX_FMTLEN 200
104 XsldbgDriver::XsldbgDriver():
105 DebuggerDriver(), m_gdbMajor(2), m_gdbMinor(0)
107 m_promptRE.setPattern("\\(xsldbg\\) .*> $");
108 m_promptMinLen = 11;
109 m_promptLastChar = ' ';
111 m_markerRE.setPattern("^Breakpoint at file ");
112 m_haveDataFile = FALSE;
114 #ifndef NDEBUG
115 // check command info array
116 char *perc;
118 for (int i = 0; i < NUM_CMDS; i++) {
119 // must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
120 assert(i == cmds[i].cmd);
121 // a format string must be associated
122 assert(cmds[i].fmt != 0);
123 assert(strlen(cmds[i].fmt) <= MAX_FMTLEN);
124 // format string must match arg specification
125 switch (cmds[i].argsNeeded) {
126 case XsldbgCmdInfo::argNone:
127 assert(strchr(cmds[i].fmt, '%') == 0);
128 break;
129 case XsldbgCmdInfo::argString:
130 perc = strchr(cmds[i].fmt, '%');
131 assert(perc != 0 && perc[1] == 's');
132 assert(strchr(perc + 2, '%') == 0);
133 break;
134 case XsldbgCmdInfo::argNum:
135 perc = strchr(cmds[i].fmt, '%');
136 assert(perc != 0 && perc[1] == 'd');
137 assert(strchr(perc + 2, '%') == 0);
138 break;
139 case XsldbgCmdInfo::argStringNum:
140 perc = strchr(cmds[i].fmt, '%');
141 assert(perc != 0 && perc[1] == 's');
142 perc = strchr(perc + 2, '%');
143 assert(perc != 0 && perc[1] == 'd');
144 assert(strchr(perc + 2, '%') == 0);
145 break;
146 case XsldbgCmdInfo::argNumString:
147 perc = strchr(cmds[i].fmt, '%');
148 assert(perc != 0 && perc[1] == 'd');
149 perc = strchr(perc + 2, '%');
150 assert(perc != 0 && perc[1] == 's');
151 assert(strchr(perc + 2, '%') == 0);
152 break;
153 case XsldbgCmdInfo::argString2:
154 perc = strchr(cmds[i].fmt, '%');
155 assert(perc != 0 && perc[1] == 's');
156 perc = strchr(perc + 2, '%');
157 assert(perc != 0 && perc[1] == 's');
158 assert(strchr(perc + 2, '%') == 0);
159 break;
160 case XsldbgCmdInfo::argNum2:
161 perc = strchr(cmds[i].fmt, '%');
162 assert(perc != 0 && perc[1] == 'd');
163 perc = strchr(perc + 2, '%');
164 assert(perc != 0 && perc[1] == 'd');
165 assert(strchr(perc + 2, '%') == 0);
166 break;
169 #endif
172 XsldbgDriver::~XsldbgDriver()
177 QString
178 XsldbgDriver::driverName() const
180 return "XSLDBG";
183 QString
184 XsldbgDriver::defaultXsldbg()
186 return "xsldbg --shell --gdb";
189 QString
190 XsldbgDriver::defaultInvocation() const
192 return defaultXsldbg();
195 QStringList XsldbgDriver::boolOptionList() const
197 QStringList allOptions;
198 allOptions.append("verbose");
199 allOptions.append("repeat");
200 allOptions.append("debug");
201 allOptions.append("novalid");
202 allOptions.append("noout");
203 allOptions.append("html");
204 allOptions.append("docbook");
205 allOptions.append("nonet");
206 allOptions.append("catalogs");
207 allOptions.append("xinclude");
208 allOptions.append("profile");
209 return allOptions;
213 void
214 XsldbgDriver::slotReceiveOutput(KProcess * process, char *buffer,
215 int buflen)
217 //TRACE(buffer);
218 if (m_state != DSidle) {
219 // TRACE(buffer);
220 DebuggerDriver::slotReceiveOutput(process, buffer, buflen);
221 } else {
222 if (strncmp(buffer, "quit", 4) == 0) {
223 TRACE("Ignoring text when xsldbg is quiting");
224 } else {
225 TRACE
226 ("Stray output received by XsldbgDriver::slotReceiveOutput");
227 TRACE(buffer);
232 bool
233 XsldbgDriver::startup(QString cmdStr)
235 if (!DebuggerDriver::startup(cmdStr))
236 return false;
238 static const char xsldbgInitialize[] = "pwd\n"; /* don't need to do anything else */
240 executeCmdString(DCinitialize, xsldbgInitialize, false);
242 return true;
245 void
246 XsldbgDriver::commandFinished(CmdQueueItem * cmd)
249 TRACE(__PRETTY_FUNCTION__);
250 // command string must be committed
251 if (!cmd->m_committed) {
252 // not commited!
253 TRACE("calling " +
254 (__PRETTY_FUNCTION__ +
255 (" with uncommited command:\n\t" + cmd->m_cmdString)));
256 return;
259 switch (cmd->m_cmd) {
260 case DCinitialize:
261 // get version number from preamble
263 int len;
264 QRegExp xsldbgVersion("^XSLDBG [0-9]+\\.[0-9]+\\.[0-9]+");
265 int offset = xsldbgVersion.match(m_output, 0, &len);
267 if (offset >= 0) {
268 char *start = m_output + offset + 7; // skip "^XSLDBG "
269 char *end;
271 TRACE("Reading version");
272 TRACE(start);
273 m_gdbMajor = strtol(start, &end, 10);
274 m_gdbMinor = strtol(end + 1, 0, 10); // skip "."
275 if (start == end) {
276 // nothing was parsed
277 m_gdbMajor = 0;
278 m_gdbMinor = 7;
280 } else {
281 // assume some default version (what would make sense?)
282 m_gdbMajor = 0;
283 m_gdbMinor = 7;
285 TRACE(QString("Got version ") +
286 QString::number(m_gdbMajor) + "." +
287 QString::number(m_gdbMinor));
288 break;
290 default:;
292 /* ok, the command is ready */
293 emit commandReceived(cmd, m_output);
295 switch (cmd->m_cmd) {
296 case DCbt:
297 case DCinfolocals:
298 case DCrun:
299 case DCcont:
300 case DCstep:
301 case DCnext:
302 case DCfinish:{
303 if (!::isErrorExpr(m_output))
304 parseMarker();
305 else{
306 // This only shows an error for DCinfolocals
307 // need to update KDebugger::handleRunCommand ?
308 KMessageBox::sorry(0L, m_output);
311 break;
313 case DCinfolinemain:
314 if (!m_xslFile.isEmpty())
315 emit activateFileLine(m_xslFile, 0, DbgAddr());
316 break;
318 default:;
322 void
323 XsldbgDriver::parseMarker()
326 // TRACE("parseMarker : xsldbg");
327 // TRACE(m_output);
328 int len, markerStart = -1;
329 char *p = m_output;
331 while (markerStart == -1) {
332 if ((p == 0) || (*p == '\0')) {
333 m_output[0] = '\0';
334 return;
336 //TRACE(QString("parseMarker is looking at :") + p);
337 markerStart = m_markerRE.match(p, 0, &len);
338 if (markerStart == -1) {
339 // try to marker on next line !
340 p = strchr(p, '\n');
341 if ((p != 0) && (*p != '\0'))
342 p++;
347 // extract the marker
348 char *startMarker = p + markerStart + len;
350 //TRACE(QString("found marker:") + startMarker);
351 char *endMarker = strchr(startMarker, '\n');
353 if (endMarker == 0)
354 return;
356 *endMarker = '\0';
358 // extract filename and line number
359 static QRegExp MarkerRE(" : line [0-9]+");
361 int lineNoStart = MarkerRE.match(startMarker, 0, &len);
363 if (lineNoStart >= 0) {
364 int lineNo = atoi(startMarker + lineNoStart + 8);
366 DbgAddr address;
368 // now show the window
369 startMarker[lineNoStart] = '\0'; /* split off file name */
370 TRACE("Got file and line number");
371 TRACE(QString(startMarker) + ": " + QString::number(lineNo));
372 emit activateFileLine(startMarker, lineNo - 1, address);
378 * Escapes characters that might lead to problems when they appear on gdb's
379 * command line.
381 static void
382 normalizeStringArg(QString & arg)
385 * Remove trailing backslashes. This approach is a little simplistic,
386 * but we know that there is at the moment no case where a trailing
387 * backslash would make sense.
389 while (!arg.isEmpty() && arg[arg.length() - 1] == '\\') {
390 arg = arg.left(arg.length() - 1);
395 QString
396 XsldbgDriver::makeCmdString(DbgCommand cmd, QString strArg)
398 assert(cmd >= 0 && cmd < NUM_CMDS);
399 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argString);
401 normalizeStringArg(strArg);
403 if (cmd == DCcd) {
404 // need the working directory when parsing the output
405 m_programWD = strArg;
406 } else if (cmd == DCexecutable) {
407 // want to display the XSL file
408 m_xslFile = strArg;
411 SIZED_QString(cmdString, MAX_FMTLEN + strArg.length());
412 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
413 return cmdString;
416 QString
417 XsldbgDriver::makeCmdString(DbgCommand cmd, int intArg)
419 assert(cmd >= 0 && cmd < NUM_CMDS);
420 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argNum);
422 SIZED_QString(cmdString, MAX_FMTLEN + 30);
424 cmdString.sprintf(cmds[cmd].fmt, intArg);
425 return cmdString;
428 QString
429 XsldbgDriver::makeCmdString(DbgCommand cmd, QString strArg, int intArg)
431 assert(cmd >= 0 && cmd < NUM_CMDS);
432 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argStringNum ||
433 cmds[cmd].argsNeeded == XsldbgCmdInfo::argNumString ||
434 cmd == DCexamine || cmd == DCtty);
436 normalizeStringArg(strArg);
438 SIZED_QString(cmdString, MAX_FMTLEN + 30 + strArg.length());
440 if (cmd == DCtty) {
442 * intArg specifies which channels should be redirected to
443 * /dev/null. It is a value or'ed together from RDNstdin,
444 * RDNstdout, RDNstderr.
446 static const char *const runRedir[8] = {
448 " </dev/null",
449 " >/dev/null",
450 " </dev/null >/dev/null",
451 " 2>/dev/null",
452 " </dev/null 2>/dev/null",
453 " >/dev/null 2>&1",
454 " </dev/null >/dev/null 2>&1"
457 if (strArg.isEmpty())
458 intArg = 7; /* failsafe if no tty */
459 m_redirect = runRedir[intArg & 7];
461 return makeCmdString(DCtty, strArg); /* note: no problem if strArg empty */
464 if (cmd == DCexamine) {
465 // make a format specifier from the intArg
466 static const char size[16] = {
467 '\0', 'b', 'h', 'w', 'g'
469 static const char format[16] = {
470 '\0', 'x', 'd', 'u', 'o', 't',
471 'a', 'c', 'f', 's', 'i'
474 assert(MDTsizemask == 0xf); /* lowest 4 bits */
475 assert(MDTformatmask == 0xf0); /* next 4 bits */
476 int count = 16; /* number of entities to print */
477 char sizeSpec = size[intArg & MDTsizemask];
478 char formatSpec = format[(intArg & MDTformatmask) >> 4];
480 assert(sizeSpec != '\0');
481 assert(formatSpec != '\0');
482 // adjust count such that 16 lines are printed
483 switch (intArg & MDTformatmask) {
484 case MDTstring:
485 case MDTinsn:
486 break; /* no modification needed */
487 default:
488 // all cases drop through:
489 switch (intArg & MDTsizemask) {
490 case MDTbyte:
491 case MDThalfword:
492 count *= 2;
493 case MDTword:
494 count *= 2;
495 case MDTgiantword:
496 count *= 2;
498 break;
500 QString spec;
502 spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);
504 return makeCmdString(DCexamine, spec, strArg);
507 if (cmds[cmd].argsNeeded == XsldbgCmdInfo::argStringNum) {
508 // line numbers are zero-based
509 if (cmd == DCuntil || cmd == DCbreakline ||
510 cmd == DCtbreakline || cmd == DCinfoline) {
511 intArg++;
513 if (cmd == DCinfoline) {
514 // must split off file name part
515 int slash = strArg.findRev('/');
517 if (slash >= 0)
518 strArg = strArg.right(strArg.length() - slash - 1);
520 cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
521 } else {
522 cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
524 return cmdString;
527 QString
528 XsldbgDriver::makeCmdString(DbgCommand cmd, QString strArg1,
529 QString strArg2)
531 assert(cmd >= 0 && cmd < NUM_CMDS);
532 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argString2);
534 normalizeStringArg(strArg1);
535 normalizeStringArg(strArg2);
537 SIZED_QString(cmdString,
538 MAX_FMTLEN + strArg1.length() + strArg2.length());
539 cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
540 return cmdString;
543 QString
544 XsldbgDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
546 assert(cmd >= 0 && cmd < NUM_CMDS);
547 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argNum2);
549 SIZED_QString(cmdString, MAX_FMTLEN + 60);
550 cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
551 return cmdString;
554 CmdQueueItem *
555 XsldbgDriver::executeCmd(DbgCommand cmd, bool clearLow)
557 assert(cmd >= 0 && cmd < NUM_CMDS);
558 assert(cmds[cmd].argsNeeded == XsldbgCmdInfo::argNone);
560 if (cmd == DCrun) {
561 m_haveCoreFile = false;
564 return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
567 CmdQueueItem *
568 XsldbgDriver::executeCmd(DbgCommand cmd, QString strArg, bool clearLow)
570 return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
573 CmdQueueItem *
574 XsldbgDriver::executeCmd(DbgCommand cmd, int intArg, bool clearLow)
577 return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
580 CmdQueueItem *
581 XsldbgDriver::executeCmd(DbgCommand cmd, QString strArg, int intArg,
582 bool clearLow)
584 return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg),
585 clearLow);
588 CmdQueueItem *
589 XsldbgDriver::executeCmd(DbgCommand cmd, QString strArg1, QString strArg2,
590 bool clearLow)
592 return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2),
593 clearLow);
596 CmdQueueItem *
597 XsldbgDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
598 bool clearLow)
600 return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2),
601 clearLow);
604 CmdQueueItem *
605 XsldbgDriver::queueCmd(DbgCommand cmd, QueueMode mode)
607 return queueCmdString(cmd, cmds[cmd].fmt, mode);
610 CmdQueueItem *
611 XsldbgDriver::queueCmd(DbgCommand cmd, QString strArg, QueueMode mode)
613 return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
616 CmdQueueItem *
617 XsldbgDriver::queueCmd(DbgCommand cmd, int intArg, QueueMode mode)
619 return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
622 CmdQueueItem *
623 XsldbgDriver::queueCmd(DbgCommand cmd, QString strArg, int intArg,
624 QueueMode mode)
626 return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
629 CmdQueueItem *
630 XsldbgDriver::queueCmd(DbgCommand cmd, QString strArg1, QString strArg2,
631 QueueMode mode)
633 return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
636 void
637 XsldbgDriver::terminate()
639 qDebug("XsldbgDriver::Terminate");
640 flushCommands();
641 executeCmdString(DCinitialize, "quit\n", true);
642 kill(SIGTERM);
643 m_state = DSidle;
646 void
647 XsldbgDriver::detachAndTerminate()
649 qDebug("XsldbgDriver::detachAndTerminate");
650 flushCommands();
651 executeCmdString(DCinitialize, "quit\n", true);
652 kill(SIGINT);
655 void
656 XsldbgDriver::interruptInferior()
658 // remove accidentally queued commands
659 qDebug("interruptInferior");
660 flushHiPriQueue();
661 kill(SIGINT);
664 static bool
665 isErrorExpr(const char *output)
667 int wordIndex;
668 bool result = false;
669 #define ERROR_WORD_COUNT 5
670 static const char *errorWords[ERROR_WORD_COUNT] = {
671 "Error:",
672 "error:", // libxslt error
673 "Unknown command",
674 "Warning:",
675 "warning:" // libxslt warning
677 static int errorWordLength[ERROR_WORD_COUNT] = {
678 6, /* Error */
679 6, /* rror */
680 15, /* Unknown command*/
681 8, /* Warning */
682 8 /* warning */
685 for (wordIndex = 0; wordIndex < ERROR_WORD_COUNT; wordIndex++){
686 if (strncmp(output,
687 errorWords[wordIndex],
688 errorWordLength[wordIndex]) == 0){
689 result = true;
690 TRACE(QString("Error/Warning from xsldbg ") + output);
691 break;
695 return result;
699 * Returns true if the output is an error message. If wantErrorValue is
700 * true, a new VarTree object is created and filled with the error message.
702 static bool
703 parseErrorMessage(const char *output,
704 VarTree * &variable, bool wantErrorValue)
706 if (isErrorExpr(output)) {
707 if (wantErrorValue) {
708 // put the error message as value in the variable
709 variable = new VarTree(QString(), VarTree::NKplain);
710 const char *endMsg = strchr(output, '\n');
712 if (endMsg == 0)
713 endMsg = output + strlen(output);
714 variable->m_value = FROM_LATIN1(output, endMsg - output);
715 } else {
716 variable = 0;
718 return true;
720 return false;
724 void
725 XsldbgDriver::setPrintQStringDataCmd(const char* /*cmd*/)
729 VarTree *
730 XsldbgDriver::parseQCharArray(const char */*output*/, bool /*wantErrorValue*/,
731 bool /*qt3like*/)
733 VarTree *variable = 0;
735 TRACE("XsldbgDriver::parseQCharArray not implmented");
736 return variable;
739 static VarTree *
740 parseVar(const char *&s)
742 const char *p = s;
743 bool foundLocalVar = false;
744 VarTree *variable = 0L;
745 QString name;
747 VarTree::NameKind kind;
749 TRACE(__PRETTY_FUNCTION__);
750 TRACE(p);
752 if (parseErrorMessage(p, variable, false) == true) {
753 TRACE("Found error message");
754 return variable;
757 if (strncmp(p, " Local", 6) == 0) {
758 foundLocalVar = true;
759 /* skip " Local" */
760 p = p + 6;
761 TRACE("Found local variable");
762 } else if (strncmp(p, " Global", 7) == 0) {
763 /* skip " Global" */
764 p = p + 7;
765 TRACE("Found global variable");
766 } else if (strncmp(p, "= ", 2) == 0) {
767 /* we're processing the result of a "print command" */
768 /* find next line */
769 char *nextLine = strchr(p, '\n');
771 TRACE("Found print expr");
772 if (nextLine) {
773 char nameBuffer[100];
775 p = p + 2; /* skip the "= " */
776 strncpy(nameBuffer, p, nextLine - p);
777 p = nextLine + 1;
778 variable = new VarTree(nameBuffer, kind);
779 if (variable != 0L) {
780 variable->m_varKind = VarTree::VKsimple;
781 variable->setDeleteChildren(true);
782 parseValue(p, variable);
784 return variable;
786 } else
787 return variable; /* don't know what to do this this data abort!! */
789 // skip whitespace
790 while (isspace(*p))
791 p++;
793 if (*p != '='){
794 // No value provided just a name
795 TRACE(QString("Parse var: name") + p);
796 if (!parseName(p, name, kind)) {
797 return 0;
799 variable = new VarTree(name, kind);
800 if (variable != 0L) {
801 variable->m_varKind = VarTree::VKsimple;
802 variable->setDeleteChildren(true);
804 }else{
805 p++;
806 // skip whitespace
807 while (isspace(*p))
808 p++;
809 TRACE(QString("Parse var: name") + p);
810 if (!parseName(p, name, kind)) {
811 return 0;
813 variable = new VarTree(name, kind);
814 if (variable != 0L) {
815 variable->m_varKind = VarTree::VKsimple;
816 variable->setDeleteChildren(true);
818 if (*p == '\n')
819 p++;
820 if (!parseValue(p, variable)) {
821 delete variable;
822 return 0;
826 if (*p == '\n')
827 p++;
829 s = p;
830 return variable;
834 inline void
835 skipName(const char *&p)
837 // allow : (for enumeration values) and $ and . (for _vtbl.)
838 while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
839 p++;
842 static bool
843 parseName(const char *&s, QString & name, VarTree::NameKind & kind)
845 /* qDebug(__PRETTY_FUNCTION__); */
846 kind = VarTree::NKplain;
848 const char *p = s;
849 int len = 0;
851 // examples of names:
852 // help_cmd
854 while ((*p != '\n') && (*p != '\0')) {
855 len++;
856 p++;
860 name = FROM_LATIN1(s, len);
861 /* XSL variables will have a $ prefix to be evaluated
862 * properly */
863 //TRACE(QString("parseName got name" ) + name);
865 // return the new position
866 s = p;
867 return true;
870 static bool
871 parseValue(const char *&s, VarTree * variable)
873 const char *start = s, *end = s;
874 VarTree * childValue;
875 #define VALUE_END_MARKER_INDEX 0
877 /* This mark the end of a value */
878 static const char *marker[] = {
879 "\032\032", /* value end marker*/
880 "(xsldbg) ",
881 "Breakpoint at", /* stepped to next location */
882 "Breakpoint in", /* reached a set breakpoint */
883 "Reached ", /* reached template */
884 "Error:",
885 "Warning:",
886 "runtime error",
887 "xmlXPathEval:",
890 static char valueBuffer[2048];
891 int markerIndex = 0, foundEnd = 0;
892 size_t copySize;
894 if (variable == 0L)
895 return false; /* should never happen but .. */
897 while (start && (*start != '\0')) {
898 /* look for the next marker */
899 for (markerIndex = 0; marker[markerIndex] != 0; markerIndex++) {
900 foundEnd =
901 strncmp(start, marker[markerIndex],
902 strlen(marker[markerIndex])) == 0;
903 if (foundEnd)
904 break;
907 if (foundEnd)
908 break;
911 end = strchr(start, '\n');
912 if (end)
913 copySize = end - start;
914 else
915 copySize = strlen(start);
916 if (copySize >= sizeof(valueBuffer))
917 copySize = sizeof(valueBuffer)-1;
919 strncpy(valueBuffer, start, copySize);
920 valueBuffer[copySize] = '\0';
921 TRACE("Got value :");
922 TRACE(valueBuffer);
923 if ((variable->m_varKind == VarTree::VKsimple)) {
924 if (!variable->m_value.isEmpty()){
925 variable->m_varKind = VarTree::VKarray;
926 childValue = new VarTree(variable->m_value, VarTree::NKplain);
927 variable->appendChild(childValue);
928 childValue = new VarTree(valueBuffer, VarTree::NKplain);
929 variable->appendChild(childValue);
930 variable->m_value = "";
931 }else{
932 variable->m_value = valueBuffer;
934 }else{
935 childValue = new VarTree(valueBuffer, VarTree::NKplain);
936 variable->appendChild(childValue);
939 if (*end =='\n'){
940 start = end + 1;
941 }else{
942 start = end + 1;
943 break;
947 if (foundEnd == 0)
948 TRACE(QString("Unable to find end on value near :") + start);
950 // If we've got something otherthan a end of value marker then
951 // advance to the end of this buffer
952 if (markerIndex != VALUE_END_MARKER_INDEX){
953 while (start && *start != '\0')
954 start++;
955 }else{
956 start = start + strlen(marker[0]);
959 s = start;
961 return true;
966 * Parses a stack frame.
968 static void
969 parseFrameInfo(const char *&s, QString & func,
970 QString & file, int &lineNo, DbgAddr & /*address*/)
972 const char *p = s, *endPos = s + strlen(s);
973 QString lineNoString;
975 lineNo = -1;
977 /* skip 'template :\" */
978 p = p + 11;
979 //TRACE("parseFrameInfo");
980 // TRACE(p);
981 func = "";
982 while ((*p != '\"') && (*p != '\0')) {
983 func.append(*p);
984 p++;
986 ASSERT(p <= endPos);
987 if (p >= endPos) {
988 /* panic */
989 return;
992 /* skip mode :".*" */
993 while (p && *p != '"')
994 p++;
995 if (p)
996 p++;
997 while (p && *p != '"')
998 p++;
999 if (p)
1000 p++;
1001 while (p && *p != '"')
1002 p++;
1003 /* skip '" in file ' */
1004 p = p + 10;
1005 // TRACE(p);
1006 file = "";
1007 while (!isspace(*p) && (*p != '\0')) {
1008 file.append(*p);
1009 p++;
1011 ASSERT(p <= endPos);
1012 if (p >= endPos) {
1013 /* panic */
1014 return;
1016 // TRACE(p);
1017 /* skip ' : line '" */
1018 p = p + 8;
1019 // TRACE(p);
1020 ASSERT(p <= endPos);
1021 if (p >= endPos) {
1022 /* panic */
1023 return;
1025 if (isdigit(*p)) {
1026 /* KDbg uses an offset of +1 for its line numbers */
1027 lineNo = atoi(p) - 1;
1028 lineNoString = QString::number(lineNo);
1030 /* convert func into format needed */
1031 func.append(" at ");
1032 func.append(file);
1033 func.append(':');
1034 func.append(lineNoString);
1036 //TRACE(QString("Got frame : template :\"") + func + "\"file:" +
1037 // file + " line : " + lineNoString);
1039 /*advance to next line */
1040 p = strchr(p, '\n');
1041 if (p)
1042 p++;
1043 s = p;
1047 #undef ISSPACE
1050 * Parses a stack frame including its frame number
1052 static bool
1053 parseFrame(const char *&s, int &frameNo, QString & func,
1054 QString & file, int &lineNo, DbgAddr & address)
1057 // TRACE("XsldbgDriver ::parseFrame");
1058 /* skip leading 'where' or 'frame <frame_no>' */
1059 if ((strncmp(s, "where", 5) == 0) || (strncmp(s, "frame", 5) == 0)) {
1060 s = strchr(s, '\n');
1061 if ((*s != '\0') && (*s != '#'))
1062 s++;
1064 // TRACE(s);
1066 // Example:
1067 //#1 template :"/" in file /home/keith/anon_CVS/xsldbg/docs/en/xsldoc.xsl : line 21
1068 // must start with a hash mark followed by number
1069 if (s[0] != '#' || !isdigit(s[1]))
1070 return false;
1072 //TRACE("XsldbgDriver ::parseFrame got #");
1073 s++; /* skip the hash mark */
1074 // frame number
1075 frameNo = atoi(s);
1076 while (isdigit(*s))
1077 s++;
1079 //TRACE(QString("Got frame ").append(QString::number(frameNo)));
1080 // space
1081 while (isspace(*s))
1082 s++;
1083 parseFrameInfo(s, func, file, lineNo, address);
1084 // TRACE("Will next look at ");
1085 // TRACE(s);
1086 return true;
1089 void
1090 XsldbgDriver::parseBackTrace(const char *output,
1091 QList < StackFrame > &stack)
1093 QString func, file;
1094 int lineNo, frameNo;
1095 DbgAddr address;
1097 while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
1098 StackFrame *frm = new StackFrame;
1100 frm->frameNo = frameNo;
1101 frm->fileName = file;
1102 frm->lineNo = lineNo;
1103 frm->address = address;
1104 frm->var = new VarTree(func, VarTree::NKplain);
1105 stack.append(frm);
1109 bool
1110 XsldbgDriver::parseFrameChange(const char *output, int &frameNo,
1111 QString & file, int &lineNo,
1112 DbgAddr & address)
1114 QString func;
1116 return::parseFrame(output, frameNo, func, file, lineNo, address);
1120 bool
1121 XsldbgDriver::parseBreakList(const char *output,
1122 QList < Breakpoint > &brks)
1124 TRACE("parseBreakList");
1125 /* skip the first blank line */
1126 const char *p;
1128 // split up a line
1129 QString location, file, lineNo;
1130 QString address;
1131 QString templateName;
1132 int hits = 0;
1133 int enabled = 0;
1134 uint ignoreCount = 0;
1135 QString condition;
1136 char *dummy;
1137 p = strchr(output, '\n');/* skip the first blank line*/
1139 while ((p != 0) && (*p != '\0')) {
1140 templateName = QString();
1141 p++;
1142 Breakpoint::Type bpType = Breakpoint::breakpoint;
1143 //qDebug("Looking at :%s", p);
1144 if (strncmp(p, " Breakpoint", 11) != 0)
1145 break;
1146 p = p + 11;
1147 if (*p == '\0')
1148 break;
1150 //TRACE(p);
1151 // get Num
1152 long bpNum = strtol(p, &dummy, 10); /* don't care about overflows */
1154 p = dummy;
1155 if ((p == 0) || (p[1] == '\0'))
1156 break;
1157 p++;
1159 //TRACE(p);
1160 // Get breakpoint state ie enabled/disabled
1161 if (strncmp(p, "enabled", 7) == 0) {
1162 enabled = true;
1163 p = p + 7;
1164 } else {
1165 if (strncmp(p, "disabled", 8) == 0) {
1166 p = p + 8;
1167 enabled = false;
1168 } else{
1169 TRACE("Parse error in breakpoint list");
1170 TRACE(p);
1171 return false;
1175 //TRACE("Looking for template");
1176 //TRACE(p);
1178 /* check for ' for template :"'*/
1179 if (strncmp(p, " for template :\"", 16) == 0){
1180 p = p + 16;
1181 //TRACE("Looking for template name near");
1182 //TRACE(p);
1183 /* get the template name */
1184 while (p && (*p != '\0') && (*p != '\"')){
1185 templateName.append(*p);
1186 p++;
1188 if (*p == '\"'){
1189 p++;
1190 }else{
1191 TRACE("Error missed \" near");
1192 TRACE(p);
1196 //TRACE("Looking for mode near");
1197 //TRACE(p);
1198 if (strncmp(p, " mode :\"", 8) == 0) {
1199 p = p + 8;
1200 while (p && *p != '\"')
1201 p++;
1202 if (p)
1203 p++;
1206 if (strncmp(p, " in file ", 9) != 0){
1207 TRACE("Parse error in breakpoint list");
1208 TRACE(p);
1209 return false;
1213 /* skip ' in file ' */
1214 p = p + 9;
1215 // TRACE(p);
1217 /* grab file name */
1218 while (!isspace(*p)) {
1219 file.append(*p);
1220 p++;
1222 if (*p == '\0')
1223 break;
1225 /* skip ' : line ' */
1226 p = p + 8;
1227 //TRACE(p);
1228 while (isdigit(*p)) {
1229 lineNo.append(*p);
1230 p++;
1234 //TRACE("Got breakpoint");
1236 Breakpoint *bp = new Breakpoint;
1238 if (bp != 0) {
1239 // take 1 of line number
1240 lineNo.setNum(lineNo.toInt() - 1);
1241 bp->id = bpNum;
1242 bp->type = bpType;
1243 bp->temporary = false;
1244 bp->enabled = enabled;
1245 location.append("in ").append(templateName).append(" at ");
1246 location.append(file).append(":").append(lineNo);
1247 bp->location = location;
1248 bp->fileName = file;
1249 bp->lineNo = lineNo.toInt();
1250 bp->address = address;
1251 bp->hitCount = hits;
1252 bp->ignoreCount = ignoreCount;
1253 bp->condition = condition;
1254 brks.append(bp);
1255 location = "";
1256 lineNo = "";
1257 file = "";
1258 } else
1259 TRACE("Outof memory, breakpoint not created");
1261 if (p != 0) {
1262 p = strchr(p, '\n');
1265 return true;
1268 bool
1269 XsldbgDriver::parseThreadList(const char */*output*/,
1270 QList < ThreadInfo > &/*threads*/)
1272 return true;
1275 bool
1276 XsldbgDriver::parseBreakpoint(const char *output, int &id,
1277 QString &file, int &lineNo, QString &address)
1279 // check for errors
1280 if ( strncmp(output, "Error:", 6) == 0) {
1281 return false;
1284 char *dummy;
1285 if (strncmp(output, "Breakpoint ", 11) != 0)
1286 return false;
1288 output += 11;
1289 if (!isdigit(*output))
1290 return false;
1292 // get Num
1293 id = strtol(output, &dummy, 10); /* don't care about overflows */
1294 if (output == dummy)
1295 return false;
1297 // the file name + lineNo will be filled in later from the breakpoint list
1298 file = address = QString();
1299 lineNo = 0;
1300 return true;
1303 void
1304 XsldbgDriver::parseLocals(const char *output, QList < VarTree > &newVars)
1307 /* keep going until error or xsldbg prompt is found */
1308 while (*output != '\0') {
1309 VarTree *variable = parseVar(output);
1311 if (variable == 0) {
1312 break;
1314 // do not add duplicates
1315 for (VarTree * o = newVars.first(); o != 0; o = newVars.next()) {
1316 if (o->getText() == variable->getText()) {
1317 delete variable;
1319 goto skipDuplicate;
1322 newVars.append(variable);
1323 skipDuplicate:;
1328 bool
1329 XsldbgDriver::parsePrintExpr(const char *output, bool wantErrorValue,
1330 VarTree * &var)
1332 // check for error conditions
1333 if (parseErrorMessage(output, var, wantErrorValue)) {
1334 return false;
1335 } else {
1336 // parse the variable
1337 var = parseVar(output);
1338 return true;
1342 bool
1343 XsldbgDriver::parseChangeWD(const char *output, QString & message)
1345 bool isGood = false;
1347 if (strncmp(output, "Change to directory", 20) == 0) {
1348 output = output + 20; /* skip 'Change to directory' */
1349 message = QString(output).simplifyWhiteSpace();
1350 if (message.isEmpty()) {
1351 message = i18n("New working directory: ") + m_programWD;
1352 isGood = true;
1355 return isGood;
1358 bool
1359 XsldbgDriver::parseChangeExecutable(const char *output, QString & message)
1361 message = output;
1362 TRACE(QString("XsldbgDriver::parseChangeExecutable :") + output);
1363 m_haveCoreFile = false;
1366 * The command is successful if there is no output or the single
1367 * message (no debugging symbols found)...
1369 QRegExp exp(".*Load of source deferred use run command.*");
1370 int len, index = exp.match(output, 0, &len);
1372 if (index != -1) {
1373 TRACE("Parsed stylesheet executable");
1374 message = "";
1376 return (output[0] == '\0') || (index != -1);
1379 bool
1380 XsldbgDriver::parseCoreFile(const char *output)
1382 TRACE("XsldbgDriver::parseCoreFile");
1383 TRACE(output);
1384 QRegExp exp(".*Load of xml data deferred use run command.*");
1385 int len, index = exp.match(output, 0, &len);
1387 if (index != -1) {
1388 m_haveCoreFile = true;
1389 TRACE("Parsed xml data file");
1392 return m_haveCoreFile;
1395 uint
1396 XsldbgDriver::parseProgramStopped(const char *output, QString & message)
1398 /* Not sure about this function leave it here for the moment */
1400 * return DebuggerDriver::SFrefreshBreak & DebuggerDriver::SFprogramActive;
1403 // go through the output, line by line, checking what we have
1404 const char *start = output - 1;
1405 uint flags = SFprogramActive;
1407 message = QString();
1408 do {
1409 start++; /* skip '\n' */
1411 if (strncmp(start, "Finished stylesheet\n\032\032\n", 21) == 0){
1412 flags &= ~SFprogramActive;
1413 break;
1416 // next line, please
1417 start = strchr(start, '\n');
1418 } while (start != 0);
1420 return flags;
1423 void
1424 XsldbgDriver::parseSharedLibs(const char */*output*/, QStrList & /*shlibs*/)
1426 /* empty */
1429 bool
1430 XsldbgDriver::parseFindType(const char */*output*/, QString & /*type*/)
1432 return true;
1435 void
1436 XsldbgDriver::parseRegisters(const char */*output*/,
1437 QList < RegisterInfo > &/*regs*/)
1442 bool
1443 XsldbgDriver::parseInfoLine(const char */*output*/, QString & /*addrFrom*/,
1444 QString & /*addrTo*/)
1446 return false;
1449 void
1450 XsldbgDriver::parseDisassemble(const char */*output*/,
1451 QList < DisassembledCode > &/*code*/)
1453 /* empty */
1456 QString
1457 XsldbgDriver::parseMemoryDump(const char */*output*/,
1458 QList < MemoryDump > &/*memdump*/)
1460 return i18n("No memory dump available");
1463 QString
1464 XsldbgDriver::parseSetVariable(const char */*output*/)
1466 QString msg;
1467 return msg;
1471 #include "xsldbgdriver.moc"