Latest updates from Keith for xsldbg 3.0.5.
[kdbg.git] / kdbg / dbgdriver.h
blob20ad0577b3f8c93162494ebc881174debefade1d
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #ifndef DBGDRIVER_H
7 #define DBGDRIVER_H
9 #include <qqueue.h>
10 #include <qlist.h>
11 #include <qfile.h>
12 #include <qregexp.h>
13 #include <kprocess.h>
16 class VarTree;
17 class ExprWnd;
18 class KDebugger;
19 class QStrList;
20 class QStringList;
23 /**
24 * A type representing an address.
26 struct DbgAddr
28 QString a;
29 QString fnoffs;
30 DbgAddr() { }
31 DbgAddr(const QString& aa);
32 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
33 void operator=(const QString& aa);
34 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
35 QString asString() const;
36 bool isEmpty() const { return a.isEmpty(); }
37 protected:
38 void cleanAddr();
40 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
41 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
44 enum DbgCommand {
45 DCinitialize,
46 DCtty,
47 DCexecutable,
48 DCtargetremote,
49 DCcorefile,
50 DCattach,
51 DCinfolinemain,
52 DCinfolocals,
53 DCinforegisters,
54 DCexamine,
55 DCinfoline,
56 DCdisassemble,
57 DCsetargs,
58 DCsetenv,
59 DCunsetenv,
60 DCsetoption, /* debugger options */
61 DCcd,
62 DCbt,
63 DCrun,
64 DCcont,
65 DCstep,
66 DCstepi,
67 DCnext,
68 DCnexti,
69 DCfinish,
70 DCuntil, /* line number is zero-based! */
71 DCkill,
72 DCbreaktext,
73 DCbreakline, /* line number is zero-based! */
74 DCtbreakline, /* line number is zero-based! */
75 DCbreakaddr,
76 DCtbreakaddr,
77 DCwatchpoint,
78 DCdelete,
79 DCenable,
80 DCdisable,
81 DCprint,
82 DCprintStruct,
83 DCprintQStringStruct,
84 DCframe,
85 DCfindType,
86 DCinfosharedlib,
87 DCthread,
88 DCinfothreads,
89 DCinfobreak,
90 DCcondition,
91 DCignore
94 enum RunDevNull {
95 RDNstdin = 0x1, /* redirect stdin to /dev/null */
96 RDNstdout = 0x2, /* redirect stdout to /dev/null */
97 RDNstderr = 0x4 /* redirect stderr to /dev/null */
101 * How the memory dump is formated. The lowest 4 bits define the size of
102 * the entities. The higher bits define how these are formatted. Note that
103 * not all combinations make sense.
105 enum MemoryDumpType {
106 // sizes
107 MDTbyte = 0x1,
108 MDThalfword = 0x2,
109 MDTword = 0x3,
110 MDTgiantword = 0x4,
111 MDTsizemask = 0xf,
112 // formats
113 MDThex = 0x10,
114 MDTsigned = 0x20,
115 MDTunsigned = 0x30,
116 MDToctal = 0x40,
117 MDTbinary = 0x50,
118 MDTaddress = 0x60,
119 MDTchar = 0x70,
120 MDTfloat = 0x80,
121 MDTstring = 0x90,
122 MDTinsn = 0xa0,
123 MDTformatmask = 0xf0
127 * Debugger commands are placed in a queue. Only one command at a time is
128 * sent down to the debugger. All other commands in the queue are retained
129 * until the sent command has been processed by gdb. The debugger tells us
130 * that it's done with the command by sending the prompt. The output of the
131 * debugger is parsed at that time. Then, if more commands are in the
132 * queue, the next one is sent to the debugger.
134 struct CmdQueueItem
136 DbgCommand m_cmd;
137 QString m_cmdString;
138 bool m_committed; /* just a debugging aid */
139 // remember which expression when printing an expression
140 VarTree* m_expr;
141 ExprWnd* m_exprWnd;
142 // remember file position
143 QString m_fileName;
144 int m_lineNo;
145 // whether command was emitted due to direct user request (only set when relevant)
146 bool m_byUser;
148 CmdQueueItem(DbgCommand cmd, const QString& str) :
149 m_cmd(cmd),
150 m_cmdString(str),
151 m_committed(false),
152 m_expr(0),
153 m_exprWnd(0),
154 m_lineNo(0),
155 m_byUser(false)
160 * The information about a breakpoint that is parsed from the list of
161 * breakpoints.
163 struct Breakpoint
165 int id; /* gdb's number */
166 enum Type {
167 breakpoint, watchpoint
168 } type;
169 bool temporary;
170 bool enabled;
171 QString location;
172 DbgAddr address; /* exact address of breakpoint */
173 QString condition; /* condition as printed by gdb */
174 int ignoreCount; /* ignore next that may hits */
175 int hitCount; /* as reported by gdb */
176 // the following items repeat the location, but in a better usable way
177 QString fileName;
178 int lineNo; /* zero-based line number */
182 * Information about a stack frame.
184 struct FrameInfo
186 QString fileName;
187 int lineNo; /* zero-based line number */
188 DbgAddr address; /* exact address of PC */
192 * The information about a stack frame as parsed from the backtrace.
194 struct StackFrame : FrameInfo
196 int frameNo;
197 VarTree* var; /* more information if non-zero */
198 StackFrame() : var(0) { }
199 ~StackFrame();
203 * The information about a thread as parsed from the threads list.
205 struct ThreadInfo : FrameInfo
207 int id; /* gdb's number */
208 QString threadName; /* the SYSTAG */
209 QString function; /* where thread is halted */
210 bool hasFocus; /* the thread whose stack we are watching */
214 * Register information
216 struct RegisterInfo
218 QString regName;
219 QString rawValue;
220 QString cookedValue; /* may be empty */
224 * Disassembled code
226 struct DisassembledCode
228 DbgAddr address;
229 QString code;
233 * Memory contents
235 struct MemoryDump
237 DbgAddr address;
238 QString dump;
242 * This is an abstract base class for debugger process.
244 * This class represents the debugger program. It provides the low-level
245 * interface to the commandline debugger. As such it implements the
246 * commands and parses the output.
248 class DebuggerDriver : public KProcess
250 Q_OBJECT
251 public:
252 DebuggerDriver();
253 virtual ~DebuggerDriver() = 0;
255 virtual QString driverName() const = 0;
257 * Returns the default command string to invoke the debugger driver.
259 virtual QString defaultInvocation() const = 0;
262 * Returns a list of options that can be turned on and off.
264 virtual QStringList boolOptionList() const = 0;
266 virtual bool startup(QString cmdStr);
267 void dequeueCmdByVar(VarTree* var);
268 void setLogFileName(const QString& fname) { m_logFileName = fname; }
270 protected:
271 QString m_runCmd;
273 enum DebuggerState {
274 DSidle, /* gdb waits for input */
275 DSinterrupted, /* a command was interrupted */
276 DSrunningLow, /* gdb is running a low-priority command */
277 DSrunning, /* gdb waits for program */
278 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
279 DScommandSentLow /* low-prioritycommand has been sent */
281 DebuggerState m_state;
283 public:
284 bool isIdle() const { return m_state == DSidle; }
286 * Tells whether a high prority command would be executed immediately.
288 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
290 protected:
291 char* m_output; /* normal gdb output */
292 size_t m_outputLen; /* amount of data so far accumulated in m_output */
293 size_t m_outputAlloc; /* space available in m_output */
294 typedef QCString DelayedStr;
295 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
296 /* but before signal wroteStdin arrived */
298 public:
300 * Enqueues a high-priority command. High-priority commands are
301 * executed before any low-priority commands. No user interaction is
302 * possible as long as there is a high-priority command in the queue.
304 virtual CmdQueueItem* executeCmd(DbgCommand,
305 bool clearLow = false) = 0;
306 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
307 bool clearLow = false) = 0;
308 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
309 bool clearLow = false) = 0;
310 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
311 bool clearLow = false) = 0;
312 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
313 bool clearLow = false) = 0;
314 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
315 bool clearLow = false) = 0;
317 enum QueueMode {
318 QMnormal, /* queues the command last */
319 QMoverride, /* removes an already queued command */
320 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
324 * Enqueues a low-priority command. Low-priority commands are executed
325 * after any high-priority commands.
327 virtual CmdQueueItem* queueCmd(DbgCommand,
328 QueueMode mode) = 0;
329 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
330 QueueMode mode) = 0;
331 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
332 QueueMode mode) = 0;
333 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
334 QueueMode mode) = 0;
335 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
336 QueueMode mode) = 0;
339 * Flushes the command queues.
340 * @param hipriOnly if true, only the high priority queue is flushed.
342 virtual void flushCommands(bool hipriOnly = false);
345 * Terminates the debugger process.
347 virtual void terminate() = 0;
350 * Terminates the debugger process, but also detaches any program that
351 * it has been attached to.
353 virtual void detachAndTerminate() = 0;
356 * Interrupts the debuggee.
358 virtual void interruptInferior() = 0;
361 * Parses the output as an array of QChars.
363 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
366 * Parses a back-trace (the output of the DCbt command).
368 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
371 * Parses the output of the DCframe command;
372 * @param frameNo Returns the frame number.
373 * @param file Returns the source file name.
374 * @param lineNo The zero-based line number.
375 * @param address Returns the exact address.
376 * @return false if the frame could not be parsed successfully. The
377 * output values are undefined in this case.
379 virtual bool parseFrameChange(const char* output, int& frameNo,
380 QString& file, int& lineNo, DbgAddr& address) = 0;
383 * Parses a list of breakpoints.
384 * @param output The output of the debugger.
385 * @param brks The list of new #Breakpoint objects. The list
386 * must initially be empty.
387 * @return False if there was an error before the first breakpoint
388 * was found. Even if true is returned, #brks may be empty.
390 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
393 * Parses a list of threads.
394 * @param output The output of the debugger.
395 * @param threads The list of new #ThreadInfo objects. The list
396 * must initially be empty.
397 * @return False if there was an error before the first thread entry
398 * was found. Even if true is returned, #threads may be empty.
400 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
403 * Parses the output when the program stops to see whether this it
404 * stopped due to a breakpoint.
405 * @param output The output of the debugger.
406 * @param id Returns the breakpoint id.
407 * @param file Returns the file name in which the breakpoint is.
408 * @param lineNo Returns he zero-based line number of the breakpoint.
409 * @return False if there was no breakpoint.
411 virtual bool parseBreakpoint(const char* output, int& id,
412 QString& file, int& lineNo) = 0;
415 * Parses the output of the DCinfolocals command.
416 * @param output The output of the debugger.
417 * @param newVars Receives the parsed variable values. The values are
418 * simply append()ed to the supplied list.
420 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
423 * Parses the output of a DCprint or DCprintStruct command.
424 * @param output The output of the debugger.
425 * @param wantErrorValue Specifies whether the error message should be
426 * provided as the value of a NKplain variable. If this is false,
427 * #var will be 0 if the printed value is an error message.
428 * @param var Returns the variable value. It is set to 0 if there was
429 * a parse error or if the output is an error message and wantErrorValue
430 * is false. If it is not 0, #var->text() will return junk and must be
431 * set using #var->setText().
432 * @return false if the output is an error message. Even if true is
433 * returned, #var might still be 0 (due to a parse error).
435 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
436 VarTree*& var) = 0;
439 * Parses the output of the DCcd command.
440 * @return false if the message is an error message.
442 virtual bool parseChangeWD(const char* output, QString& message) = 0;
445 * Parses the output of the DCexecutable command.
446 * @return false if an error occured.
448 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
451 * Parses the output of the DCcorefile command.
452 * @return false if the core file was not loaded successfully.
454 virtual bool parseCoreFile(const char* output) = 0;
456 enum StopFlags {
457 SFrefreshSource = 1, /* refresh of source code is needed */
458 SFrefreshBreak = 2, /* refresh breakpoints */
459 SFrefreshThreads = 4, /* refresh thread list */
460 SFprogramActive = 128 /* program remains active */
463 * Parses the output of commands that execute (a piece of) the program.
464 * @return The inclusive OR of zero or more of the StopFlags.
466 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
469 * Parses the output of the DCsharedlibs command.
471 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
474 * Parses the output of the DCfindType command.
475 * @return true if a type was found.
477 virtual bool parseFindType(const char* output, QString& type) = 0;
480 * Parses the output of the DCinforegisters command.
482 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
485 * Parses the output of the DCinfoline command. Returns false if the
486 * two addresses could not be found.
488 virtual bool parseInfoLine(const char* output,
489 QString& addrFrom, QString& addrTo) = 0;
492 * Parses the ouput of the DCdisassemble command.
494 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
497 * Parses a memory dump. Returns an empty string if no error was found;
498 * otherwise it contains an error message.
500 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
502 protected:
503 /** Removes all commands from the low-priority queue. */
504 void flushLoPriQueue();
505 /** Removes all commands from the high-priority queue. */
506 void flushHiPriQueue();
508 QQueue<CmdQueueItem> m_hipriCmdQueue;
509 QList<CmdQueueItem> m_lopriCmdQueue;
511 * The active command is kept separately from other pending commands.
513 CmdQueueItem* m_activeCmd;
515 * Helper function that queues the given command string in the
516 * low-priority queue.
518 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
519 QueueMode mode);
521 * Helper function that queues the given command string in the
522 * high-priority queue.
524 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
525 bool clearLow);
526 void writeCommand();
527 virtual void commandFinished(CmdQueueItem* cmd) = 0;
529 protected:
530 /** @internal */
531 virtual int commSetupDoneC();
533 char m_prompt[10];
534 size_t m_promptMinLen;
535 char m_promptLastChar;
536 QRegExp m_promptRE;
538 // log file
539 QString m_logFileName;
540 QFile m_logFile;
542 protected slots:
543 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
544 virtual void slotCommandRead(KProcess*);
545 virtual void slotExited(KProcess*);
547 signals:
549 * This signal is emitted when the output of a command has been fully
550 * collected and is ready to be interpreted.
552 void commandReceived(CmdQueueItem* cmd, const char* output);
555 * This signal is emitted when the debugger recognizes that a specific
556 * location in a file ought to be displayed.
558 * Gdb's --fullname option supports this for the step, next, frame, and
559 * run commands (and possibly others).
561 * @param file specifies the file; this is not necessarily a full path
562 * name, and if it is relative, you won't know relative to what, you
563 * can only guess.
564 * @param lineNo specifies the line number (0-based!) (this may be
565 * negative, in which case the file should be activated, but the line
566 * should NOT be changed).
567 * @param address specifies the exact address of the PC or is empty.
569 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
572 * This signal is emitted when a command that starts the inferior has
573 * been submitted to the debugger.
575 void inferiorRunning();
578 * This signal is emitted when all output from the debugger has been
579 * consumed and no more commands are in the queues.
581 void enterIdleState();
584 #endif // DBGDRIVER_H