When an item having the current item is taken out from the tree,
[kdbg.git] / kdbg / dbgdriver.h
blob2aa786ae99e7928a5179755d15f82573fa7e156f
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 <kprocess.h>
15 class VarTree;
16 class ExprWnd;
17 class KDebugger;
20 /**
21 * A type representing an address.
23 struct DbgAddr
25 QString a;
26 DbgAddr() { }
27 DbgAddr(const QString& aa);
28 DbgAddr(const DbgAddr& src) : a(src.a) { }
29 void operator=(const QString& aa);
30 void operator=(const DbgAddr& src) { a = src.a; }
31 QString asString() const;
32 bool isEmpty() const { return a.isEmpty(); }
33 protected:
34 void cleanAddr();
36 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
37 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
40 enum DbgCommand {
41 DCinitialize,
42 DCtty,
43 DCexecutable,
44 DCtargetremote,
45 DCcorefile,
46 DCattach,
47 DCinfolinemain,
48 DCinfolocals,
49 DCinforegisters,
50 DCinfoline,
51 DCdisassemble,
52 DCsetargs,
53 DCsetenv,
54 DCunsetenv,
55 DCcd,
56 DCbt,
57 DCrun,
58 DCcont,
59 DCstep,
60 DCnext,
61 DCfinish,
62 DCuntil, /* line number is zero-based! */
63 DCkill,
64 DCbreaktext,
65 DCbreakline, /* line number is zero-based! */
66 DCtbreakline, /* line number is zero-based! */
67 DCbreakaddr,
68 DCtbreakaddr,
69 DCdelete,
70 DCenable,
71 DCdisable,
72 DCprint,
73 DCprintStruct,
74 DCprintQStringStruct,
75 DCframe,
76 DCfindType,
77 DCinfosharedlib,
78 DCthread,
79 DCinfothreads,
80 DCinfobreak,
81 DCcondition,
82 DCignore
85 enum RunDevNull {
86 RDNstdin = 0x1, /* redirect stdin to /dev/null */
87 RDNstdout = 0x2, /* redirect stdout to /dev/null */
88 RDNstderr = 0x4 /* redirect stderr to /dev/null */
91 /**
92 * Debugger commands are placed in a queue. Only one command at a time is
93 * sent down to the debugger. All other commands in the queue are retained
94 * until the sent command has been processed by gdb. The debugger tells us
95 * that it's done with the command by sending the prompt. The output of the
96 * debugger is parsed at that time. Then, if more commands are in the
97 * queue, the next one is sent to the debugger.
99 struct CmdQueueItem
101 DbgCommand m_cmd;
102 QString m_cmdString;
103 bool m_committed; /* just a debugging aid */
104 // remember which expression when printing an expression
105 VarTree* m_expr;
106 ExprWnd* m_exprWnd;
107 // remember file position
108 QString m_fileName;
109 int m_lineNo;
110 // whether command was emitted due to direct user request (only set when relevant)
111 bool m_byUser;
113 CmdQueueItem(DbgCommand cmd, const QString& str) :
114 m_cmd(cmd),
115 m_cmdString(str),
116 m_committed(false),
117 m_expr(0),
118 m_exprWnd(0),
119 m_lineNo(0),
120 m_byUser(false)
125 * The information about a breakpoint that is parsed from the list of
126 * breakpoints.
128 struct Breakpoint
130 int id; /* gdb's number */
131 bool temporary;
132 bool enabled;
133 QString location;
134 DbgAddr address; /* exact address of breakpoint */
135 QString condition; /* condition as printed by gdb */
136 int ignoreCount; /* ignore next that may hits */
137 int hitCount; /* as reported by gdb */
138 // the following items repeat the location, but in a better usable way
139 QString fileName;
140 int lineNo; /* zero-based line number */
144 * Information about a stack frame.
146 struct FrameInfo
148 QString fileName;
149 int lineNo; /* zero-based line number */
150 DbgAddr address; /* exact address of PC */
154 * The information about a stack frame as parsed from the backtrace.
156 struct StackFrame : FrameInfo
158 int frameNo;
159 VarTree* var; /* more information if non-zero */
160 StackFrame() : var(0) { }
161 ~StackFrame();
165 * The information about a thread as parsed from the threads list.
167 struct ThreadInfo : FrameInfo
169 int id; /* gdb's number */
170 QString threadName; /* the SYSTAG */
171 QString function; /* where thread is halted */
172 bool hasFocus; /* the thread whose stack we are watching */
176 * Register information
178 struct RegisterInfo
180 QString regName;
181 QString rawValue;
182 QString cookedValue; /* may be empty */
186 * Disassembled code
188 struct DisassembledCode
190 DbgAddr address;
191 QString code;
195 * This is an abstract base class for debugger process.
197 * This class represents the debugger program. It provides the low-level
198 * interface to the commandline debugger. As such it implements the
199 * commands and parses the output.
201 class DebuggerDriver : public KProcess
203 Q_OBJECT
204 public:
205 DebuggerDriver();
206 virtual ~DebuggerDriver() = 0;
208 virtual QString driverName() const = 0;
210 * Returns the default command string to invoke the debugger driver.
212 virtual QString defaultInvocation() const = 0;
214 virtual bool startup(QString cmdStr);
215 void dequeueCmdByVar(VarTree* var);
216 void setLogFileName(const QString& fname) { m_logFileName = fname; }
218 protected:
219 QString m_runCmd;
221 enum DebuggerState {
222 DSidle, /* gdb waits for input */
223 DSinterrupted, /* a command was interrupted */
224 DSrunningLow, /* gdb is running a low-priority command */
225 DSrunning, /* gdb waits for program */
226 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
227 DScommandSentLow /* low-prioritycommand has been sent */
229 DebuggerState m_state;
231 public:
232 bool isIdle() const { return m_state == DSidle; }
234 * Tells whether a high prority command would be executed immediately.
236 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
238 protected:
239 char* m_output; /* normal gdb output */
240 int m_outputLen; /* current accumulated output */
241 int m_outputAlloc; /* space available in m_gdbOutput */
242 #if QT_VERSION < 200
243 typedef QString DelayedStr;
244 #else
245 typedef QCString DelayedStr;
246 #endif
247 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
248 /* but before signal wroteStdin arrived */
250 public:
252 * Enqueues a high-priority command. High-priority commands are
253 * executed before any low-priority commands. No user interaction is
254 * possible as long as there is a high-priority command in the queue.
256 virtual CmdQueueItem* executeCmd(DbgCommand,
257 bool clearLow = false) = 0;
258 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
259 bool clearLow = false) = 0;
260 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
261 bool clearLow = false) = 0;
262 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
263 bool clearLow = false) = 0;
264 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
265 bool clearLow = false) = 0;
266 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
267 bool clearLow = false) = 0;
269 enum QueueMode {
270 QMnormal, /* queues the command last */
271 QMoverride, /* removes an already queued command */
272 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
276 * Enqueues a low-priority command. Low-priority commands are executed
277 * after any high-priority commands.
279 virtual CmdQueueItem* queueCmd(DbgCommand,
280 QueueMode mode) = 0;
281 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
282 QueueMode mode) = 0;
283 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
284 QueueMode mode) = 0;
285 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
286 QueueMode mode) = 0;
287 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
288 QueueMode mode) = 0;
291 * Flushes the command queues.
292 * @param hipriOnly if true, only the high priority queue is flushed.
294 virtual void flushCommands(bool hipriOnly = false);
297 * Terminates the debugger process.
299 virtual void terminate() = 0;
302 * Terminates the debugger process, but also detaches any program that
303 * it has been attached to.
305 virtual void detachAndTerminate() = 0;
308 * Interrupts the debuggee.
310 virtual void interruptInferior() = 0;
313 * Parses the output as an array of QChars.
315 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue) = 0;
318 * Parses a back-trace (the output of the DCbt command).
320 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
323 * Parses the output of the DCframe command;
324 * @param frameNo Returns the frame number.
325 * @param file Returns the source file name.
326 * @param lineNo The zero-based line number.
327 * @param address Returns the exact address.
328 * @return false if the frame could not be parsed successfully. The
329 * output values are undefined in this case.
331 virtual bool parseFrameChange(const char* output, int& frameNo,
332 QString& file, int& lineNo, DbgAddr& address) = 0;
335 * Parses a list of breakpoints.
336 * @param output The output of the debugger.
337 * @param brks The list of new #Breakpoint objects. The list
338 * must initially be empty.
339 * @return False if there was an error before the first breakpoint
340 * was found. Even if true is returned, #brks may be empty.
342 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
345 * Parses a list of threads.
346 * @param output The output of the debugger.
347 * @param threads The list of new #ThreadInfo objects. The list
348 * must initially be empty.
349 * @return False if there was an error before the first thread entry
350 * was found. Even if true is returned, #threads may be empty.
352 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
355 * Parses the output when the program stops to see whether this it
356 * stopped due to a breakpoint.
357 * @param output The output of the debugger.
358 * @param id Returns the breakpoint id.
359 * @param file Returns the file name in which the breakpoint is.
360 * @param lineNo Returns he zero-based line number of the breakpoint.
361 * @return False if there was no breakpoint.
363 virtual bool parseBreakpoint(const char* output, int& id,
364 QString& file, int& lineNo) = 0;
367 * Parses the output of the DCinfolocals command.
368 * @param output The output of the debugger.
369 * @param newVars Receives the parsed variable values. The values are
370 * simply append()ed to the supplied list.
372 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
375 * Parses the output of a DCprint or DCprintStruct command.
376 * @param output The output of the debugger.
377 * @param wantErrorValue Specifies whether the error message should be
378 * provided as the value of a NKplain variable. If this is false,
379 * #var will be 0 if the printed value is an error message.
380 * @param var Returns the variable value. It is set to 0 if there was
381 * a parse error or if the output is an error message and wantErrorValue
382 * is false. If it is not 0, #var->text() will return junk and must be
383 * set using #var->setText().
384 * @return false if the output is an error message. Even if true is
385 * returned, #var might still be 0 (due to a parse error).
387 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
388 VarTree*& var) = 0;
391 * Parses the output of the DCcd command.
392 * @return false if the message is an error message.
394 virtual bool parseChangeWD(const char* output, QString& message) = 0;
397 * Parses the output of the DCexecutable command.
398 * @return false if an error occured.
400 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
403 * Parses the output of the DCcorefile command.
404 * @return false if the core file was not loaded successfully.
406 virtual bool parseCoreFile(const char* output) = 0;
408 enum StopFlags {
409 SFrefreshSource = 1, /* refresh of source code is needed */
410 SFrefreshBreak = 2, /* refresh breakpoints */
411 SFrefreshThreads = 4, /* refresh thread list */
412 SFprogramActive = 128 /* program remains active */
415 * Parses the output of commands that execute (a piece of) the program.
416 * @return The inclusive OR of zero or more of the StopFlags.
418 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
421 * Parses the output of the DCsharedlibs command.
423 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
426 * Parses the output of the DCfindType command.
427 * @return true if a type was found.
429 virtual bool parseFindType(const char* output, QString& type) = 0;
432 * Parses the output of the DCinforegisters command.
434 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
437 * Parses the output of the DCinfoline command. Returns false if the
438 * two addresses could not be found.
440 virtual bool parseInfoLine(const char* output,
441 QString& addrFrom, QString& addrTo) = 0;
444 * Parses the ouput of the DCdisassemble command.
446 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
448 protected:
449 /** Removes all commands from the low-priority queue. */
450 void flushLoPriQueue();
451 /** Removes all commands from the high-priority queue. */
452 void flushHiPriQueue();
454 QQueue<CmdQueueItem> m_hipriCmdQueue;
455 QList<CmdQueueItem> m_lopriCmdQueue;
457 * The active command is kept separately from other pending commands.
459 CmdQueueItem* m_activeCmd;
461 * Helper function that queues the given command string in the
462 * low-priority queue.
464 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
465 QueueMode mode);
467 * Helper function that queues the given command string in the
468 * high-priority queue.
470 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
471 bool clearLow);
472 void writeCommand();
473 virtual void commandFinished(CmdQueueItem* cmd) = 0;
475 protected:
476 /** @internal */
477 virtual int commSetupDoneC();
479 char m_prompt[10];
480 int m_promptLen;
481 char m_promptLastChar;
483 // log file
484 QString m_logFileName;
485 QFile m_logFile;
487 protected slots:
488 void slotReceiveOutput(KProcess*, char* buffer, int buflen);
489 void slotCommandRead(KProcess*);
490 void slotExited(KProcess*);
492 signals:
494 * This signal is emitted when the output of a command has been fully
495 * collected and is ready to be interpreted.
497 void commandReceived(CmdQueueItem* cmd, const char* output);
500 * This signal is emitted when the debugger recognizes that a specific
501 * location in a file ought to be displayed.
503 * Gdb's --fullname option supports this for the step, next, frame, and
504 * run commands (and possibly others).
506 * @param file specifies the file; this is not necessarily a full path
507 * name, and if it is relative, you won't know relative to what, you
508 * can only guess.
509 * @param lineNo specifies the line number (0-based!) (this may be
510 * negative, in which case the file should be activated, but the line
511 * should NOT be changed).
512 * @param address specifies the exact address of the PC or is empty.
514 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
517 * This signal is emitted when a command that starts the inferior has
518 * been submitted to the debugger.
520 void inferiorRunning();
523 * This signal is emitted when all output from the debugger has been
524 * consumed and no more commands are in the queues.
526 void enterIdleState();
529 #endif // DBGDRIVER_H