Make toolbar visible again.
[kdbg.git] / kdbg / dbgdriver.h
blob6f472b880cd10aad378948f8bbe77032fed31f14
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 QString fnoffs;
27 DbgAddr() { }
28 DbgAddr(const QString& aa);
29 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
30 void operator=(const QString& aa);
31 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
32 QString asString() const;
33 bool isEmpty() const { return a.isEmpty(); }
34 protected:
35 void cleanAddr();
37 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
38 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
41 enum DbgCommand {
42 DCinitialize,
43 DCtty,
44 DCexecutable,
45 DCtargetremote,
46 DCcorefile,
47 DCattach,
48 DCinfolinemain,
49 DCinfolocals,
50 DCinforegisters,
51 DCexamine,
52 DCinfoline,
53 DCdisassemble,
54 DCsetargs,
55 DCsetenv,
56 DCunsetenv,
57 DCcd,
58 DCbt,
59 DCrun,
60 DCcont,
61 DCstep,
62 DCstepi,
63 DCnext,
64 DCnexti,
65 DCfinish,
66 DCuntil, /* line number is zero-based! */
67 DCkill,
68 DCbreaktext,
69 DCbreakline, /* line number is zero-based! */
70 DCtbreakline, /* line number is zero-based! */
71 DCbreakaddr,
72 DCtbreakaddr,
73 DCwatchpoint,
74 DCdelete,
75 DCenable,
76 DCdisable,
77 DCprint,
78 DCprintStruct,
79 DCprintQStringStruct,
80 DCframe,
81 DCfindType,
82 DCinfosharedlib,
83 DCthread,
84 DCinfothreads,
85 DCinfobreak,
86 DCcondition,
87 DCignore
90 enum RunDevNull {
91 RDNstdin = 0x1, /* redirect stdin to /dev/null */
92 RDNstdout = 0x2, /* redirect stdout to /dev/null */
93 RDNstderr = 0x4 /* redirect stderr to /dev/null */
96 /**
97 * How the memory dump is formated. The lowest 4 bits define the size of
98 * the entities. The higher bits define how these are formatted. Note that
99 * not all combinations make sense.
101 enum MemoryDumpType {
102 // sizes
103 MDTbyte = 0x1,
104 MDThalfword = 0x2,
105 MDTword = 0x3,
106 MDTgiantword = 0x4,
107 MDTsizemask = 0xf,
108 // formats
109 MDThex = 0x10,
110 MDTsigned = 0x20,
111 MDTunsigned = 0x30,
112 MDToctal = 0x40,
113 MDTbinary = 0x50,
114 MDTaddress = 0x60,
115 MDTchar = 0x70,
116 MDTfloat = 0x80,
117 MDTstring = 0x90,
118 MDTinsn = 0xa0,
119 MDTformatmask = 0xf0
123 * Debugger commands are placed in a queue. Only one command at a time is
124 * sent down to the debugger. All other commands in the queue are retained
125 * until the sent command has been processed by gdb. The debugger tells us
126 * that it's done with the command by sending the prompt. The output of the
127 * debugger is parsed at that time. Then, if more commands are in the
128 * queue, the next one is sent to the debugger.
130 struct CmdQueueItem
132 DbgCommand m_cmd;
133 QString m_cmdString;
134 bool m_committed; /* just a debugging aid */
135 // remember which expression when printing an expression
136 VarTree* m_expr;
137 ExprWnd* m_exprWnd;
138 // remember file position
139 QString m_fileName;
140 int m_lineNo;
141 // whether command was emitted due to direct user request (only set when relevant)
142 bool m_byUser;
144 CmdQueueItem(DbgCommand cmd, const QString& str) :
145 m_cmd(cmd),
146 m_cmdString(str),
147 m_committed(false),
148 m_expr(0),
149 m_exprWnd(0),
150 m_lineNo(0),
151 m_byUser(false)
156 * The information about a breakpoint that is parsed from the list of
157 * breakpoints.
159 struct Breakpoint
161 int id; /* gdb's number */
162 enum Type {
163 breakpoint, watchpoint
164 } type;
165 bool temporary;
166 bool enabled;
167 QString location;
168 DbgAddr address; /* exact address of breakpoint */
169 QString condition; /* condition as printed by gdb */
170 int ignoreCount; /* ignore next that may hits */
171 int hitCount; /* as reported by gdb */
172 // the following items repeat the location, but in a better usable way
173 QString fileName;
174 int lineNo; /* zero-based line number */
178 * Information about a stack frame.
180 struct FrameInfo
182 QString fileName;
183 int lineNo; /* zero-based line number */
184 DbgAddr address; /* exact address of PC */
188 * The information about a stack frame as parsed from the backtrace.
190 struct StackFrame : FrameInfo
192 int frameNo;
193 VarTree* var; /* more information if non-zero */
194 StackFrame() : var(0) { }
195 ~StackFrame();
199 * The information about a thread as parsed from the threads list.
201 struct ThreadInfo : FrameInfo
203 int id; /* gdb's number */
204 QString threadName; /* the SYSTAG */
205 QString function; /* where thread is halted */
206 bool hasFocus; /* the thread whose stack we are watching */
210 * Register information
212 struct RegisterInfo
214 QString regName;
215 QString rawValue;
216 QString cookedValue; /* may be empty */
220 * Disassembled code
222 struct DisassembledCode
224 DbgAddr address;
225 QString code;
229 * Memory contents
231 struct MemoryDump
233 DbgAddr address;
234 QString dump;
238 * This is an abstract base class for debugger process.
240 * This class represents the debugger program. It provides the low-level
241 * interface to the commandline debugger. As such it implements the
242 * commands and parses the output.
244 class DebuggerDriver : public KProcess
246 Q_OBJECT
247 public:
248 DebuggerDriver();
249 virtual ~DebuggerDriver() = 0;
251 virtual QString driverName() const = 0;
253 * Returns the default command string to invoke the debugger driver.
255 virtual QString defaultInvocation() const = 0;
257 virtual bool startup(QString cmdStr);
258 void dequeueCmdByVar(VarTree* var);
259 void setLogFileName(const QString& fname) { m_logFileName = fname; }
261 protected:
262 QString m_runCmd;
264 enum DebuggerState {
265 DSidle, /* gdb waits for input */
266 DSinterrupted, /* a command was interrupted */
267 DSrunningLow, /* gdb is running a low-priority command */
268 DSrunning, /* gdb waits for program */
269 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
270 DScommandSentLow /* low-prioritycommand has been sent */
272 DebuggerState m_state;
274 public:
275 bool isIdle() const { return m_state == DSidle; }
277 * Tells whether a high prority command would be executed immediately.
279 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
281 protected:
282 char* m_output; /* normal gdb output */
283 int m_outputLen; /* current accumulated output */
284 int m_outputAlloc; /* space available in m_gdbOutput */
285 #if QT_VERSION < 200
286 typedef QString DelayedStr;
287 #else
288 typedef QCString DelayedStr;
289 #endif
290 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
291 /* but before signal wroteStdin arrived */
293 public:
295 * Enqueues a high-priority command. High-priority commands are
296 * executed before any low-priority commands. No user interaction is
297 * possible as long as there is a high-priority command in the queue.
299 virtual CmdQueueItem* executeCmd(DbgCommand,
300 bool clearLow = false) = 0;
301 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
302 bool clearLow = false) = 0;
303 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
304 bool clearLow = false) = 0;
305 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
306 bool clearLow = false) = 0;
307 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
308 bool clearLow = false) = 0;
309 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
310 bool clearLow = false) = 0;
312 enum QueueMode {
313 QMnormal, /* queues the command last */
314 QMoverride, /* removes an already queued command */
315 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
319 * Enqueues a low-priority command. Low-priority commands are executed
320 * after any high-priority commands.
322 virtual CmdQueueItem* queueCmd(DbgCommand,
323 QueueMode mode) = 0;
324 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
325 QueueMode mode) = 0;
326 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
327 QueueMode mode) = 0;
328 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
329 QueueMode mode) = 0;
330 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
331 QueueMode mode) = 0;
334 * Flushes the command queues.
335 * @param hipriOnly if true, only the high priority queue is flushed.
337 virtual void flushCommands(bool hipriOnly = false);
340 * Terminates the debugger process.
342 virtual void terminate() = 0;
345 * Terminates the debugger process, but also detaches any program that
346 * it has been attached to.
348 virtual void detachAndTerminate() = 0;
351 * Interrupts the debuggee.
353 virtual void interruptInferior() = 0;
356 * Parses the output as an array of QChars.
358 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue) = 0;
361 * Parses a back-trace (the output of the DCbt command).
363 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
366 * Parses the output of the DCframe command;
367 * @param frameNo Returns the frame number.
368 * @param file Returns the source file name.
369 * @param lineNo The zero-based line number.
370 * @param address Returns the exact address.
371 * @return false if the frame could not be parsed successfully. The
372 * output values are undefined in this case.
374 virtual bool parseFrameChange(const char* output, int& frameNo,
375 QString& file, int& lineNo, DbgAddr& address) = 0;
378 * Parses a list of breakpoints.
379 * @param output The output of the debugger.
380 * @param brks The list of new #Breakpoint objects. The list
381 * must initially be empty.
382 * @return False if there was an error before the first breakpoint
383 * was found. Even if true is returned, #brks may be empty.
385 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
388 * Parses a list of threads.
389 * @param output The output of the debugger.
390 * @param threads The list of new #ThreadInfo objects. The list
391 * must initially be empty.
392 * @return False if there was an error before the first thread entry
393 * was found. Even if true is returned, #threads may be empty.
395 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
398 * Parses the output when the program stops to see whether this it
399 * stopped due to a breakpoint.
400 * @param output The output of the debugger.
401 * @param id Returns the breakpoint id.
402 * @param file Returns the file name in which the breakpoint is.
403 * @param lineNo Returns he zero-based line number of the breakpoint.
404 * @return False if there was no breakpoint.
406 virtual bool parseBreakpoint(const char* output, int& id,
407 QString& file, int& lineNo) = 0;
410 * Parses the output of the DCinfolocals command.
411 * @param output The output of the debugger.
412 * @param newVars Receives the parsed variable values. The values are
413 * simply append()ed to the supplied list.
415 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
418 * Parses the output of a DCprint or DCprintStruct command.
419 * @param output The output of the debugger.
420 * @param wantErrorValue Specifies whether the error message should be
421 * provided as the value of a NKplain variable. If this is false,
422 * #var will be 0 if the printed value is an error message.
423 * @param var Returns the variable value. It is set to 0 if there was
424 * a parse error or if the output is an error message and wantErrorValue
425 * is false. If it is not 0, #var->text() will return junk and must be
426 * set using #var->setText().
427 * @return false if the output is an error message. Even if true is
428 * returned, #var might still be 0 (due to a parse error).
430 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
431 VarTree*& var) = 0;
434 * Parses the output of the DCcd command.
435 * @return false if the message is an error message.
437 virtual bool parseChangeWD(const char* output, QString& message) = 0;
440 * Parses the output of the DCexecutable command.
441 * @return false if an error occured.
443 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
446 * Parses the output of the DCcorefile command.
447 * @return false if the core file was not loaded successfully.
449 virtual bool parseCoreFile(const char* output) = 0;
451 enum StopFlags {
452 SFrefreshSource = 1, /* refresh of source code is needed */
453 SFrefreshBreak = 2, /* refresh breakpoints */
454 SFrefreshThreads = 4, /* refresh thread list */
455 SFprogramActive = 128 /* program remains active */
458 * Parses the output of commands that execute (a piece of) the program.
459 * @return The inclusive OR of zero or more of the StopFlags.
461 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
464 * Parses the output of the DCsharedlibs command.
466 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
469 * Parses the output of the DCfindType command.
470 * @return true if a type was found.
472 virtual bool parseFindType(const char* output, QString& type) = 0;
475 * Parses the output of the DCinforegisters command.
477 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
480 * Parses the output of the DCinfoline command. Returns false if the
481 * two addresses could not be found.
483 virtual bool parseInfoLine(const char* output,
484 QString& addrFrom, QString& addrTo) = 0;
487 * Parses the ouput of the DCdisassemble command.
489 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
492 * Parses a memory dump. Returns an empty string if no error was found;
493 * otherwise it contains an error message.
495 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
497 protected:
498 /** Removes all commands from the low-priority queue. */
499 void flushLoPriQueue();
500 /** Removes all commands from the high-priority queue. */
501 void flushHiPriQueue();
503 QQueue<CmdQueueItem> m_hipriCmdQueue;
504 QList<CmdQueueItem> m_lopriCmdQueue;
506 * The active command is kept separately from other pending commands.
508 CmdQueueItem* m_activeCmd;
510 * Helper function that queues the given command string in the
511 * low-priority queue.
513 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
514 QueueMode mode);
516 * Helper function that queues the given command string in the
517 * high-priority queue.
519 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
520 bool clearLow);
521 void writeCommand();
522 virtual void commandFinished(CmdQueueItem* cmd) = 0;
524 protected:
525 /** @internal */
526 virtual int commSetupDoneC();
528 char m_prompt[10];
529 int m_promptLen;
530 char m_promptLastChar;
532 // log file
533 QString m_logFileName;
534 QFile m_logFile;
536 protected slots:
537 void slotReceiveOutput(KProcess*, char* buffer, int buflen);
538 void slotCommandRead(KProcess*);
539 void slotExited(KProcess*);
541 signals:
543 * This signal is emitted when the output of a command has been fully
544 * collected and is ready to be interpreted.
546 void commandReceived(CmdQueueItem* cmd, const char* output);
549 * This signal is emitted when the debugger recognizes that a specific
550 * location in a file ought to be displayed.
552 * Gdb's --fullname option supports this for the step, next, frame, and
553 * run commands (and possibly others).
555 * @param file specifies the file; this is not necessarily a full path
556 * name, and if it is relative, you won't know relative to what, you
557 * can only guess.
558 * @param lineNo specifies the line number (0-based!) (this may be
559 * negative, in which case the file should be activated, but the line
560 * should NOT be changed).
561 * @param address specifies the exact address of the PC or is empty.
563 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
566 * This signal is emitted when a command that starts the inferior has
567 * been submitted to the debugger.
569 void inferiorRunning();
572 * This signal is emitted when all output from the debugger has been
573 * consumed and no more commands are in the queues.
575 void enterIdleState();
578 #endif // DBGDRIVER_H