Fixed that the debugger window really moves itself to the foreground.
[kdbg.git] / kdbg / dbgdriver.h
blob4ff75bee8d5fc6f87eba8063d3bdd63ce6f4f0ee
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 <qptrqueue.h>
10 #include <qptrlist.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 DCprintDeref,
83 DCprintStruct,
84 DCprintQStringStruct,
85 DCframe,
86 DCfindType,
87 DCinfosharedlib,
88 DCthread,
89 DCinfothreads,
90 DCinfobreak,
91 DCcondition,
92 DCsetpc,
93 DCignore,
94 DCsetvariable
97 enum RunDevNull {
98 RDNstdin = 0x1, /* redirect stdin to /dev/null */
99 RDNstdout = 0x2, /* redirect stdout to /dev/null */
100 RDNstderr = 0x4 /* redirect stderr to /dev/null */
104 * How the memory dump is formated. The lowest 4 bits define the size of
105 * the entities. The higher bits define how these are formatted. Note that
106 * not all combinations make sense.
108 enum MemoryDumpType {
109 // sizes
110 MDTbyte = 0x1,
111 MDThalfword = 0x2,
112 MDTword = 0x3,
113 MDTgiantword = 0x4,
114 MDTsizemask = 0xf,
115 // formats
116 MDThex = 0x10,
117 MDTsigned = 0x20,
118 MDTunsigned = 0x30,
119 MDToctal = 0x40,
120 MDTbinary = 0x50,
121 MDTaddress = 0x60,
122 MDTchar = 0x70,
123 MDTfloat = 0x80,
124 MDTstring = 0x90,
125 MDTinsn = 0xa0,
126 MDTformatmask = 0xf0
129 struct Breakpoint;
132 * Debugger commands are placed in a queue. Only one command at a time is
133 * sent down to the debugger. All other commands in the queue are retained
134 * until the sent command has been processed by gdb. The debugger tells us
135 * that it's done with the command by sending the prompt. The output of the
136 * debugger is parsed at that time. Then, if more commands are in the
137 * queue, the next one is sent to the debugger.
139 struct CmdQueueItem
141 DbgCommand m_cmd;
142 QString m_cmdString;
143 bool m_committed; /* just a debugging aid */
144 // remember which expression when printing an expression
145 VarTree* m_expr;
146 ExprWnd* m_exprWnd;
147 // remember file position
148 QString m_fileName;
149 int m_lineNo;
150 // the breakpoint info
151 Breakpoint* m_brkpt;
152 // whether command was emitted due to direct user request (only set when relevant)
153 bool m_byUser;
155 CmdQueueItem(DbgCommand cmd, const QString& str) :
156 m_cmd(cmd),
157 m_cmdString(str),
158 m_committed(false),
159 m_expr(0),
160 m_exprWnd(0),
161 m_lineNo(0),
162 m_brkpt(0),
163 m_byUser(false)
168 * The information about a breakpoint that is parsed from the list of
169 * breakpoints.
171 struct Breakpoint
173 int id; /* gdb's number */
174 enum Type {
175 breakpoint, watchpoint
176 } type;
177 bool temporary;
178 bool enabled;
179 QString location;
180 QString text; /* text if set using DCbreaktext */
181 DbgAddr address; /* exact address of breakpoint */
182 QString condition; /* condition as printed by gdb */
183 int ignoreCount; /* ignore next that may hits */
184 int hitCount; /* as reported by gdb */
185 // the following items repeat the location, but in a better usable way
186 QString fileName;
187 int lineNo; /* zero-based line number */
188 Breakpoint();
189 bool isOrphaned() const { return id < 0; }
193 * Information about a stack frame.
195 struct FrameInfo
197 QString fileName;
198 int lineNo; /* zero-based line number */
199 DbgAddr address; /* exact address of PC */
203 * The information about a stack frame as parsed from the backtrace.
205 struct StackFrame : FrameInfo
207 int frameNo;
208 VarTree* var; /* more information if non-zero */
209 StackFrame() : var(0) { }
210 ~StackFrame();
214 * The information about a thread as parsed from the threads list.
216 struct ThreadInfo : FrameInfo
218 int id; /* gdb's number */
219 QString threadName; /* the SYSTAG */
220 QString function; /* where thread is halted */
221 bool hasFocus; /* the thread whose stack we are watching */
225 * Register information
227 struct RegisterInfo
229 QString regName;
230 QString rawValue;
231 QString cookedValue; /* may be empty */
232 QString type; /* of vector register if not empty */
236 * Disassembled code
238 struct DisassembledCode
240 DbgAddr address;
241 QString code;
245 * Memory contents
247 struct MemoryDump
249 DbgAddr address;
250 QString dump;
254 * This is an abstract base class for debugger process.
256 * This class represents the debugger program. It provides the low-level
257 * interface to the commandline debugger. As such it implements the
258 * commands and parses the output.
260 class DebuggerDriver : public KProcess
262 Q_OBJECT
263 public:
264 DebuggerDriver();
265 virtual ~DebuggerDriver() = 0;
267 virtual QString driverName() const = 0;
269 * Returns the default command string to invoke the debugger driver.
271 virtual QString defaultInvocation() const = 0;
274 * Returns a list of options that can be turned on and off.
276 virtual QStringList boolOptionList() const = 0;
278 virtual bool startup(QString cmdStr);
279 void dequeueCmdByVar(VarTree* var);
280 void setLogFileName(const QString& fname) { m_logFileName = fname; }
282 protected:
283 QString m_runCmd;
285 enum DebuggerState {
286 DSidle, /* gdb waits for input */
287 DSinterrupted, /* a command was interrupted */
288 DSrunningLow, /* gdb is running a low-priority command */
289 DSrunning, /* gdb waits for program */
290 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
291 DScommandSentLow /* low-prioritycommand has been sent */
293 DebuggerState m_state;
295 public:
296 bool isIdle() const { return m_state == DSidle; }
298 * Tells whether a high prority command would be executed immediately.
300 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
302 protected:
303 char* m_output; /* normal gdb output */
304 size_t m_outputLen; /* amount of data so far accumulated in m_output */
305 size_t m_outputAlloc; /* space available in m_output */
306 typedef QCString DelayedStr;
307 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
308 /* but before signal wroteStdin arrived */
310 public:
312 * Enqueues a high-priority command. High-priority commands are
313 * executed before any low-priority commands. No user interaction is
314 * possible as long as there is a high-priority command in the queue.
316 virtual CmdQueueItem* executeCmd(DbgCommand,
317 bool clearLow = false) = 0;
318 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
319 bool clearLow = false) = 0;
320 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
321 bool clearLow = false) = 0;
322 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
323 bool clearLow = false) = 0;
324 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
325 bool clearLow = false) = 0;
326 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
327 bool clearLow = false) = 0;
329 enum QueueMode {
330 QMnormal, /* queues the command last */
331 QMoverride, /* removes an already queued command */
332 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
336 * Enqueues a low-priority command. Low-priority commands are executed
337 * after any high-priority commands.
339 virtual CmdQueueItem* queueCmd(DbgCommand,
340 QueueMode mode) = 0;
341 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
342 QueueMode mode) = 0;
343 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
344 QueueMode mode) = 0;
345 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
346 QueueMode mode) = 0;
347 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
348 QueueMode mode) = 0;
351 * Flushes the command queues.
352 * @param hipriOnly if true, only the high priority queue is flushed.
354 virtual void flushCommands(bool hipriOnly = false);
357 * Terminates the debugger process.
359 virtual void terminate() = 0;
362 * Terminates the debugger process, but also detaches any program that
363 * it has been attached to.
365 virtual void detachAndTerminate() = 0;
368 * Interrupts the debuggee.
370 virtual void interruptInferior() = 0;
373 * Specifies the command that prints the QString data.
375 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
378 * Parses the output as an array of QChars.
380 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
383 * Parses a back-trace (the output of the DCbt command).
385 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
388 * Parses the output of the DCframe command;
389 * @param frameNo Returns the frame number.
390 * @param file Returns the source file name.
391 * @param lineNo The zero-based line number.
392 * @param address Returns the exact address.
393 * @return false if the frame could not be parsed successfully. The
394 * output values are undefined in this case.
396 virtual bool parseFrameChange(const char* output, int& frameNo,
397 QString& file, int& lineNo, DbgAddr& address) = 0;
400 * Parses a list of breakpoints.
401 * @param output The output of the debugger.
402 * @param brks The list of new #Breakpoint objects. The list
403 * must initially be empty.
404 * @return False if there was an error before the first breakpoint
405 * was found. Even if true is returned, #brks may be empty.
407 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
410 * Parses a list of threads.
411 * @param output The output of the debugger.
412 * @param threads The list of new #ThreadInfo objects. The list
413 * must initially be empty.
414 * @return False if there was an error before the first thread entry
415 * was found. Even if true is returned, #threads may be empty.
417 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
420 * Parses the output when the program stops to see whether this it
421 * stopped due to a breakpoint.
422 * @param output The output of the debugger.
423 * @param id Returns the breakpoint id.
424 * @param file Returns the file name in which the breakpoint is.
425 * @param lineNo Returns the zero-based line number of the breakpoint.
426 * @param address Returns the address of the breakpoint.
427 * @return False if there was no breakpoint.
429 virtual bool parseBreakpoint(const char* output, int& id,
430 QString& file, int& lineNo, QString& address) = 0;
433 * Parses the output of the DCinfolocals command.
434 * @param output The output of the debugger.
435 * @param newVars Receives the parsed variable values. The values are
436 * simply append()ed to the supplied list.
438 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
441 * Parses the output of a DCprint or DCprintStruct command.
442 * @param output The output of the debugger.
443 * @param wantErrorValue Specifies whether the error message should be
444 * provided as the value of a NKplain variable. If this is false,
445 * #var will be 0 if the printed value is an error message.
446 * @param var Returns the variable value. It is set to 0 if there was
447 * a parse error or if the output is an error message and wantErrorValue
448 * is false. If it is not 0, #var->text() will return junk and must be
449 * set using #var->setText().
450 * @return false if the output is an error message. Even if true is
451 * returned, #var might still be 0 (due to a parse error).
453 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
454 VarTree*& var) = 0;
457 * Parses the output of the DCcd command.
458 * @return false if the message is an error message.
460 virtual bool parseChangeWD(const char* output, QString& message) = 0;
463 * Parses the output of the DCexecutable command.
464 * @return false if an error occured.
466 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
469 * Parses the output of the DCcorefile command.
470 * @return false if the core file was not loaded successfully.
472 virtual bool parseCoreFile(const char* output) = 0;
474 enum StopFlags {
475 SFrefreshSource = 1, /* refresh of source code is needed */
476 SFrefreshBreak = 2, /* refresh breakpoints */
477 SFrefreshThreads = 4, /* refresh thread list */
478 SFprogramActive = 128 /* program remains active */
481 * Parses the output of commands that execute (a piece of) the program.
482 * @return The inclusive OR of zero or more of the StopFlags.
484 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
487 * Parses the output of the DCsharedlibs command.
489 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
492 * Parses the output of the DCfindType command.
493 * @return true if a type was found.
495 virtual bool parseFindType(const char* output, QString& type) = 0;
498 * Parses the output of the DCinforegisters command.
500 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
503 * Parses the output of the DCinfoline command. Returns false if the
504 * two addresses could not be found.
506 virtual bool parseInfoLine(const char* output,
507 QString& addrFrom, QString& addrTo) = 0;
510 * Parses the ouput of the DCdisassemble command.
512 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
515 * Parses a memory dump. Returns an empty string if no error was found;
516 * otherwise it contains an error message.
518 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
521 * Parses the output of the DCsetvariable command. Returns an empty
522 * string if no error was found; otherwise it contains an error
523 * message.
525 virtual QString parseSetVariable(const char* output) = 0;
528 * Returns a value that the user can edit.
530 virtual QString editableValue(VarTree* value);
532 protected:
533 /** Removes all commands from the low-priority queue. */
534 void flushLoPriQueue();
535 /** Removes all commands from the high-priority queue. */
536 void flushHiPriQueue();
538 QQueue<CmdQueueItem> m_hipriCmdQueue;
539 QList<CmdQueueItem> m_lopriCmdQueue;
541 * The active command is kept separately from other pending commands.
543 CmdQueueItem* m_activeCmd;
545 * Helper function that queues the given command string in the
546 * low-priority queue.
548 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
549 QueueMode mode);
551 * Helper function that queues the given command string in the
552 * high-priority queue.
554 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
555 bool clearLow);
556 void writeCommand();
557 virtual void commandFinished(CmdQueueItem* cmd) = 0;
559 protected:
560 /** @internal */
561 virtual int commSetupDoneC();
563 char m_prompt[10];
564 size_t m_promptMinLen;
565 char m_promptLastChar;
566 QRegExp m_promptRE;
568 // log file
569 QString m_logFileName;
570 QFile m_logFile;
572 protected slots:
573 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
574 virtual void slotCommandRead(KProcess*);
575 virtual void slotExited(KProcess*);
577 signals:
579 * This signal is emitted when the output of a command has been fully
580 * collected and is ready to be interpreted.
582 void commandReceived(CmdQueueItem* cmd, const char* output);
585 * This signal is emitted when the debugger recognizes that a specific
586 * location in a file ought to be displayed.
588 * Gdb's --fullname option supports this for the step, next, frame, and
589 * run commands (and possibly others).
591 * @param file specifies the file; this is not necessarily a full path
592 * name, and if it is relative, you won't know relative to what, you
593 * can only guess.
594 * @param lineNo specifies the line number (0-based!) (this may be
595 * negative, in which case the file should be activated, but the line
596 * should NOT be changed).
597 * @param address specifies the exact address of the PC or is empty.
599 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
602 * This signal is emitted when a command that starts the inferior has
603 * been submitted to the debugger.
605 void inferiorRunning();
608 * This signal is emitted when all output from the debugger has been
609 * consumed and no more commands are in the queues.
611 void enterIdleState();
614 #endif // DBGDRIVER_H