Add missing forward declaration required by recent Qt/KDE.
[kdbg.git] / kdbg / dbgdriver.h
blob0c67fbb62017dc3b5830f1008291aff33856869b
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;
22 /**
23 * A type representing an address.
25 struct DbgAddr
27 QString a;
28 QString fnoffs;
29 DbgAddr() { }
30 DbgAddr(const QString& aa);
31 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
32 void operator=(const QString& aa);
33 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
34 QString asString() const;
35 bool isEmpty() const { return a.isEmpty(); }
36 protected:
37 void cleanAddr();
39 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
40 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
43 enum DbgCommand {
44 DCinitialize,
45 DCtty,
46 DCexecutable,
47 DCtargetremote,
48 DCcorefile,
49 DCattach,
50 DCinfolinemain,
51 DCinfolocals,
52 DCinforegisters,
53 DCexamine,
54 DCinfoline,
55 DCdisassemble,
56 DCsetargs,
57 DCsetenv,
58 DCunsetenv,
59 DCcd,
60 DCbt,
61 DCrun,
62 DCcont,
63 DCstep,
64 DCstepi,
65 DCnext,
66 DCnexti,
67 DCfinish,
68 DCuntil, /* line number is zero-based! */
69 DCkill,
70 DCbreaktext,
71 DCbreakline, /* line number is zero-based! */
72 DCtbreakline, /* line number is zero-based! */
73 DCbreakaddr,
74 DCtbreakaddr,
75 DCwatchpoint,
76 DCdelete,
77 DCenable,
78 DCdisable,
79 DCprint,
80 DCprintStruct,
81 DCprintQStringStruct,
82 DCframe,
83 DCfindType,
84 DCinfosharedlib,
85 DCthread,
86 DCinfothreads,
87 DCinfobreak,
88 DCcondition,
89 DCignore
92 enum RunDevNull {
93 RDNstdin = 0x1, /* redirect stdin to /dev/null */
94 RDNstdout = 0x2, /* redirect stdout to /dev/null */
95 RDNstderr = 0x4 /* redirect stderr to /dev/null */
98 /**
99 * How the memory dump is formated. The lowest 4 bits define the size of
100 * the entities. The higher bits define how these are formatted. Note that
101 * not all combinations make sense.
103 enum MemoryDumpType {
104 // sizes
105 MDTbyte = 0x1,
106 MDThalfword = 0x2,
107 MDTword = 0x3,
108 MDTgiantword = 0x4,
109 MDTsizemask = 0xf,
110 // formats
111 MDThex = 0x10,
112 MDTsigned = 0x20,
113 MDTunsigned = 0x30,
114 MDToctal = 0x40,
115 MDTbinary = 0x50,
116 MDTaddress = 0x60,
117 MDTchar = 0x70,
118 MDTfloat = 0x80,
119 MDTstring = 0x90,
120 MDTinsn = 0xa0,
121 MDTformatmask = 0xf0
125 * Debugger commands are placed in a queue. Only one command at a time is
126 * sent down to the debugger. All other commands in the queue are retained
127 * until the sent command has been processed by gdb. The debugger tells us
128 * that it's done with the command by sending the prompt. The output of the
129 * debugger is parsed at that time. Then, if more commands are in the
130 * queue, the next one is sent to the debugger.
132 struct CmdQueueItem
134 DbgCommand m_cmd;
135 QString m_cmdString;
136 bool m_committed; /* just a debugging aid */
137 // remember which expression when printing an expression
138 VarTree* m_expr;
139 ExprWnd* m_exprWnd;
140 // remember file position
141 QString m_fileName;
142 int m_lineNo;
143 // whether command was emitted due to direct user request (only set when relevant)
144 bool m_byUser;
146 CmdQueueItem(DbgCommand cmd, const QString& str) :
147 m_cmd(cmd),
148 m_cmdString(str),
149 m_committed(false),
150 m_expr(0),
151 m_exprWnd(0),
152 m_lineNo(0),
153 m_byUser(false)
158 * The information about a breakpoint that is parsed from the list of
159 * breakpoints.
161 struct Breakpoint
163 int id; /* gdb's number */
164 enum Type {
165 breakpoint, watchpoint
166 } type;
167 bool temporary;
168 bool enabled;
169 QString location;
170 DbgAddr address; /* exact address of breakpoint */
171 QString condition; /* condition as printed by gdb */
172 int ignoreCount; /* ignore next that may hits */
173 int hitCount; /* as reported by gdb */
174 // the following items repeat the location, but in a better usable way
175 QString fileName;
176 int lineNo; /* zero-based line number */
180 * Information about a stack frame.
182 struct FrameInfo
184 QString fileName;
185 int lineNo; /* zero-based line number */
186 DbgAddr address; /* exact address of PC */
190 * The information about a stack frame as parsed from the backtrace.
192 struct StackFrame : FrameInfo
194 int frameNo;
195 VarTree* var; /* more information if non-zero */
196 StackFrame() : var(0) { }
197 ~StackFrame();
201 * The information about a thread as parsed from the threads list.
203 struct ThreadInfo : FrameInfo
205 int id; /* gdb's number */
206 QString threadName; /* the SYSTAG */
207 QString function; /* where thread is halted */
208 bool hasFocus; /* the thread whose stack we are watching */
212 * Register information
214 struct RegisterInfo
216 QString regName;
217 QString rawValue;
218 QString cookedValue; /* may be empty */
222 * Disassembled code
224 struct DisassembledCode
226 DbgAddr address;
227 QString code;
231 * Memory contents
233 struct MemoryDump
235 DbgAddr address;
236 QString dump;
240 * This is an abstract base class for debugger process.
242 * This class represents the debugger program. It provides the low-level
243 * interface to the commandline debugger. As such it implements the
244 * commands and parses the output.
246 class DebuggerDriver : public KProcess
248 Q_OBJECT
249 public:
250 DebuggerDriver();
251 virtual ~DebuggerDriver() = 0;
253 virtual QString driverName() const = 0;
255 * Returns the default command string to invoke the debugger driver.
257 virtual QString defaultInvocation() const = 0;
259 virtual bool startup(QString cmdStr);
260 void dequeueCmdByVar(VarTree* var);
261 void setLogFileName(const QString& fname) { m_logFileName = fname; }
263 protected:
264 QString m_runCmd;
266 enum DebuggerState {
267 DSidle, /* gdb waits for input */
268 DSinterrupted, /* a command was interrupted */
269 DSrunningLow, /* gdb is running a low-priority command */
270 DSrunning, /* gdb waits for program */
271 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
272 DScommandSentLow /* low-prioritycommand has been sent */
274 DebuggerState m_state;
276 public:
277 bool isIdle() const { return m_state == DSidle; }
279 * Tells whether a high prority command would be executed immediately.
281 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
283 protected:
284 char* m_output; /* normal gdb output */
285 size_t m_outputLen; /* amount of data so far accumulated in m_output */
286 size_t m_outputAlloc; /* space available in m_output */
287 typedef QCString DelayedStr;
288 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
289 /* but before signal wroteStdin arrived */
291 public:
293 * Enqueues a high-priority command. High-priority commands are
294 * executed before any low-priority commands. No user interaction is
295 * possible as long as there is a high-priority command in the queue.
297 virtual CmdQueueItem* executeCmd(DbgCommand,
298 bool clearLow = false) = 0;
299 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
300 bool clearLow = false) = 0;
301 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
302 bool clearLow = false) = 0;
303 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
304 bool clearLow = false) = 0;
305 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
306 bool clearLow = false) = 0;
307 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
308 bool clearLow = false) = 0;
310 enum QueueMode {
311 QMnormal, /* queues the command last */
312 QMoverride, /* removes an already queued command */
313 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
317 * Enqueues a low-priority command. Low-priority commands are executed
318 * after any high-priority commands.
320 virtual CmdQueueItem* queueCmd(DbgCommand,
321 QueueMode mode) = 0;
322 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
323 QueueMode mode) = 0;
324 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
325 QueueMode mode) = 0;
326 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
327 QueueMode mode) = 0;
328 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
329 QueueMode mode) = 0;
332 * Flushes the command queues.
333 * @param hipriOnly if true, only the high priority queue is flushed.
335 virtual void flushCommands(bool hipriOnly = false);
338 * Terminates the debugger process.
340 virtual void terminate() = 0;
343 * Terminates the debugger process, but also detaches any program that
344 * it has been attached to.
346 virtual void detachAndTerminate() = 0;
349 * Interrupts the debuggee.
351 virtual void interruptInferior() = 0;
354 * Parses the output as an array of QChars.
356 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
359 * Parses a back-trace (the output of the DCbt command).
361 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
364 * Parses the output of the DCframe command;
365 * @param frameNo Returns the frame number.
366 * @param file Returns the source file name.
367 * @param lineNo The zero-based line number.
368 * @param address Returns the exact address.
369 * @return false if the frame could not be parsed successfully. The
370 * output values are undefined in this case.
372 virtual bool parseFrameChange(const char* output, int& frameNo,
373 QString& file, int& lineNo, DbgAddr& address) = 0;
376 * Parses a list of breakpoints.
377 * @param output The output of the debugger.
378 * @param brks The list of new #Breakpoint objects. The list
379 * must initially be empty.
380 * @return False if there was an error before the first breakpoint
381 * was found. Even if true is returned, #brks may be empty.
383 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
386 * Parses a list of threads.
387 * @param output The output of the debugger.
388 * @param threads The list of new #ThreadInfo objects. The list
389 * must initially be empty.
390 * @return False if there was an error before the first thread entry
391 * was found. Even if true is returned, #threads may be empty.
393 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
396 * Parses the output when the program stops to see whether this it
397 * stopped due to a breakpoint.
398 * @param output The output of the debugger.
399 * @param id Returns the breakpoint id.
400 * @param file Returns the file name in which the breakpoint is.
401 * @param lineNo Returns he zero-based line number of the breakpoint.
402 * @return False if there was no breakpoint.
404 virtual bool parseBreakpoint(const char* output, int& id,
405 QString& file, int& lineNo) = 0;
408 * Parses the output of the DCinfolocals command.
409 * @param output The output of the debugger.
410 * @param newVars Receives the parsed variable values. The values are
411 * simply append()ed to the supplied list.
413 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
416 * Parses the output of a DCprint or DCprintStruct command.
417 * @param output The output of the debugger.
418 * @param wantErrorValue Specifies whether the error message should be
419 * provided as the value of a NKplain variable. If this is false,
420 * #var will be 0 if the printed value is an error message.
421 * @param var Returns the variable value. It is set to 0 if there was
422 * a parse error or if the output is an error message and wantErrorValue
423 * is false. If it is not 0, #var->text() will return junk and must be
424 * set using #var->setText().
425 * @return false if the output is an error message. Even if true is
426 * returned, #var might still be 0 (due to a parse error).
428 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
429 VarTree*& var) = 0;
432 * Parses the output of the DCcd command.
433 * @return false if the message is an error message.
435 virtual bool parseChangeWD(const char* output, QString& message) = 0;
438 * Parses the output of the DCexecutable command.
439 * @return false if an error occured.
441 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
444 * Parses the output of the DCcorefile command.
445 * @return false if the core file was not loaded successfully.
447 virtual bool parseCoreFile(const char* output) = 0;
449 enum StopFlags {
450 SFrefreshSource = 1, /* refresh of source code is needed */
451 SFrefreshBreak = 2, /* refresh breakpoints */
452 SFrefreshThreads = 4, /* refresh thread list */
453 SFprogramActive = 128 /* program remains active */
456 * Parses the output of commands that execute (a piece of) the program.
457 * @return The inclusive OR of zero or more of the StopFlags.
459 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
462 * Parses the output of the DCsharedlibs command.
464 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
467 * Parses the output of the DCfindType command.
468 * @return true if a type was found.
470 virtual bool parseFindType(const char* output, QString& type) = 0;
473 * Parses the output of the DCinforegisters command.
475 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
478 * Parses the output of the DCinfoline command. Returns false if the
479 * two addresses could not be found.
481 virtual bool parseInfoLine(const char* output,
482 QString& addrFrom, QString& addrTo) = 0;
485 * Parses the ouput of the DCdisassemble command.
487 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
490 * Parses a memory dump. Returns an empty string if no error was found;
491 * otherwise it contains an error message.
493 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
495 protected:
496 /** Removes all commands from the low-priority queue. */
497 void flushLoPriQueue();
498 /** Removes all commands from the high-priority queue. */
499 void flushHiPriQueue();
501 QQueue<CmdQueueItem> m_hipriCmdQueue;
502 QList<CmdQueueItem> m_lopriCmdQueue;
504 * The active command is kept separately from other pending commands.
506 CmdQueueItem* m_activeCmd;
508 * Helper function that queues the given command string in the
509 * low-priority queue.
511 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
512 QueueMode mode);
514 * Helper function that queues the given command string in the
515 * high-priority queue.
517 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
518 bool clearLow);
519 void writeCommand();
520 virtual void commandFinished(CmdQueueItem* cmd) = 0;
522 protected:
523 /** @internal */
524 virtual int commSetupDoneC();
526 char m_prompt[10];
527 size_t m_promptMinLen;
528 char m_promptLastChar;
529 QRegExp m_promptRE;
531 // log file
532 QString m_logFileName;
533 QFile m_logFile;
535 protected slots:
536 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
537 virtual void slotCommandRead(KProcess*);
538 virtual void slotExited(KProcess*);
540 signals:
542 * This signal is emitted when the output of a command has been fully
543 * collected and is ready to be interpreted.
545 void commandReceived(CmdQueueItem* cmd, const char* output);
548 * This signal is emitted when the debugger recognizes that a specific
549 * location in a file ought to be displayed.
551 * Gdb's --fullname option supports this for the step, next, frame, and
552 * run commands (and possibly others).
554 * @param file specifies the file; this is not necessarily a full path
555 * name, and if it is relative, you won't know relative to what, you
556 * can only guess.
557 * @param lineNo specifies the line number (0-based!) (this may be
558 * negative, in which case the file should be activated, but the line
559 * should NOT be changed).
560 * @param address specifies the exact address of the PC or is empty.
562 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
565 * This signal is emitted when a command that starts the inferior has
566 * been submitted to the debugger.
568 void inferiorRunning();
571 * This signal is emitted when all output from the debugger has been
572 * consumed and no more commands are in the queues.
574 void enterIdleState();
577 #endif // DBGDRIVER_H