Get rid of the option not to delete tree item children.
[kdbg.git] / kdbg / dbgdriver.h
blob66ff8d774aa4e68be9ad4a4d3181c24ac3b5c375
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 DCprintWChar,
95 DCsetvariable
98 enum RunDevNull {
99 RDNstdin = 0x1, /* redirect stdin to /dev/null */
100 RDNstdout = 0x2, /* redirect stdout to /dev/null */
101 RDNstderr = 0x4 /* redirect stderr to /dev/null */
105 * How the memory dump is formated. The lowest 4 bits define the size of
106 * the entities. The higher bits define how these are formatted. Note that
107 * not all combinations make sense.
109 enum MemoryDumpType {
110 // sizes
111 MDTbyte = 0x1,
112 MDThalfword = 0x2,
113 MDTword = 0x3,
114 MDTgiantword = 0x4,
115 MDTsizemask = 0xf,
116 // formats
117 MDThex = 0x10,
118 MDTsigned = 0x20,
119 MDTunsigned = 0x30,
120 MDToctal = 0x40,
121 MDTbinary = 0x50,
122 MDTaddress = 0x60,
123 MDTchar = 0x70,
124 MDTfloat = 0x80,
125 MDTstring = 0x90,
126 MDTinsn = 0xa0,
127 MDTformatmask = 0xf0
130 struct Breakpoint;
133 * Debugger commands are placed in a queue. Only one command at a time is
134 * sent down to the debugger. All other commands in the queue are retained
135 * until the sent command has been processed by gdb. The debugger tells us
136 * that it's done with the command by sending the prompt. The output of the
137 * debugger is parsed at that time. Then, if more commands are in the
138 * queue, the next one is sent to the debugger.
140 struct CmdQueueItem
142 DbgCommand m_cmd;
143 QString m_cmdString;
144 bool m_committed; /* just a debugging aid */
145 // remember which expression when printing an expression
146 VarTree* m_expr;
147 ExprWnd* m_exprWnd;
148 // remember file position
149 QString m_fileName;
150 int m_lineNo;
151 // the breakpoint info
152 Breakpoint* m_brkpt;
153 // whether command was emitted due to direct user request (only set when relevant)
154 bool m_byUser;
156 CmdQueueItem(DbgCommand cmd, const QString& str) :
157 m_cmd(cmd),
158 m_cmdString(str),
159 m_committed(false),
160 m_expr(0),
161 m_exprWnd(0),
162 m_lineNo(0),
163 m_brkpt(0),
164 m_byUser(false)
169 * The information about a breakpoint that is parsed from the list of
170 * breakpoints.
172 struct Breakpoint
174 int id; /* gdb's number */
175 enum Type {
176 breakpoint, watchpoint
177 } type;
178 bool temporary;
179 bool enabled;
180 QString location;
181 QString text; /* text if set using DCbreaktext */
182 DbgAddr address; /* exact address of breakpoint */
183 QString condition; /* condition as printed by gdb */
184 int ignoreCount; /* ignore next that may hits */
185 int hitCount; /* as reported by gdb */
186 // the following items repeat the location, but in a better usable way
187 QString fileName;
188 int lineNo; /* zero-based line number */
189 Breakpoint();
190 bool isOrphaned() const { return id < 0; }
194 * Information about a stack frame.
196 struct FrameInfo
198 QString fileName;
199 int lineNo; /* zero-based line number */
200 DbgAddr address; /* exact address of PC */
204 * The information about a stack frame as parsed from the backtrace.
206 struct StackFrame : FrameInfo
208 int frameNo;
209 VarTree* var; /* more information if non-zero */
210 StackFrame() : var(0) { }
211 ~StackFrame();
215 * The information about a thread as parsed from the threads list.
217 struct ThreadInfo : FrameInfo
219 int id; /* gdb's number */
220 QString threadName; /* the SYSTAG */
221 QString function; /* where thread is halted */
222 bool hasFocus; /* the thread whose stack we are watching */
226 * Register information
228 struct RegisterInfo
230 QString regName;
231 QString rawValue;
232 QString cookedValue; /* may be empty */
233 QString type; /* of vector register if not empty */
237 * Disassembled code
239 struct DisassembledCode
241 DbgAddr address;
242 QString code;
246 * Memory contents
248 struct MemoryDump
250 DbgAddr address;
251 QString dump;
255 * This is an abstract base class for debugger process.
257 * This class represents the debugger program. It provides the low-level
258 * interface to the commandline debugger. As such it implements the
259 * commands and parses the output.
261 class DebuggerDriver : public KProcess
263 Q_OBJECT
264 public:
265 DebuggerDriver();
266 virtual ~DebuggerDriver() = 0;
268 virtual QString driverName() const = 0;
270 * Returns the default command string to invoke the debugger driver.
272 virtual QString defaultInvocation() const = 0;
275 * Returns a list of options that can be turned on and off.
277 virtual QStringList boolOptionList() const = 0;
279 virtual bool startup(QString cmdStr);
280 void dequeueCmdByVar(VarTree* var);
281 void setLogFileName(const QString& fname) { m_logFileName = fname; }
283 protected:
284 QString m_runCmd;
286 enum DebuggerState {
287 DSidle, /* gdb waits for input */
288 DSinterrupted, /* a command was interrupted */
289 DSrunningLow, /* gdb is running a low-priority command */
290 DSrunning, /* gdb waits for program */
291 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
292 DScommandSentLow /* low-prioritycommand has been sent */
294 DebuggerState m_state;
296 public:
297 bool isIdle() const { return m_state == DSidle; }
299 * Tells whether a high prority command would be executed immediately.
301 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
303 protected:
304 char* m_output; /* normal gdb output */
305 size_t m_outputLen; /* amount of data so far accumulated in m_output */
306 size_t m_outputAlloc; /* space available in m_output */
307 typedef QCString DelayedStr;
308 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
309 /* but before signal wroteStdin arrived */
311 public:
313 * Enqueues a high-priority command. High-priority commands are
314 * executed before any low-priority commands. No user interaction is
315 * possible as long as there is a high-priority command in the queue.
317 virtual CmdQueueItem* executeCmd(DbgCommand,
318 bool clearLow = false) = 0;
319 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
320 bool clearLow = false) = 0;
321 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
322 bool clearLow = false) = 0;
323 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
324 bool clearLow = false) = 0;
325 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
326 bool clearLow = false) = 0;
327 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
328 bool clearLow = false) = 0;
330 enum QueueMode {
331 QMnormal, /* queues the command last */
332 QMoverride, /* removes an already queued command */
333 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
337 * Enqueues a low-priority command. Low-priority commands are executed
338 * after any high-priority commands.
340 virtual CmdQueueItem* queueCmd(DbgCommand,
341 QueueMode mode) = 0;
342 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
343 QueueMode mode) = 0;
344 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
345 QueueMode mode) = 0;
346 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
347 QueueMode mode) = 0;
348 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
349 QueueMode mode) = 0;
352 * Flushes the command queues.
353 * @param hipriOnly if true, only the high priority queue is flushed.
355 virtual void flushCommands(bool hipriOnly = false);
358 * Terminates the debugger process.
360 virtual void terminate() = 0;
363 * Terminates the debugger process, but also detaches any program that
364 * it has been attached to.
366 virtual void detachAndTerminate() = 0;
369 * Interrupts the debuggee.
371 virtual void interruptInferior() = 0;
374 * Specifies the command that prints the QString data.
376 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
379 * Parses the output as an array of QChars.
381 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
384 * Parses a back-trace (the output of the DCbt command).
386 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
389 * Parses the output of the DCframe command;
390 * @param frameNo Returns the frame number.
391 * @param file Returns the source file name.
392 * @param lineNo The zero-based line number.
393 * @param address Returns the exact address.
394 * @return false if the frame could not be parsed successfully. The
395 * output values are undefined in this case.
397 virtual bool parseFrameChange(const char* output, int& frameNo,
398 QString& file, int& lineNo, DbgAddr& address) = 0;
401 * Parses a list of breakpoints.
402 * @param output The output of the debugger.
403 * @param brks The list of new #Breakpoint objects. The list
404 * must initially be empty.
405 * @return False if there was an error before the first breakpoint
406 * was found. Even if true is returned, #brks may be empty.
408 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
411 * Parses a list of threads.
412 * @param output The output of the debugger.
413 * @param threads The list of new #ThreadInfo objects. The list
414 * must initially be empty.
415 * @return False if there was an error before the first thread entry
416 * was found. Even if true is returned, #threads may be empty.
418 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
421 * Parses the output when the program stops to see whether this it
422 * stopped due to a breakpoint.
423 * @param output The output of the debugger.
424 * @param id Returns the breakpoint id.
425 * @param file Returns the file name in which the breakpoint is.
426 * @param lineNo Returns the zero-based line number of the breakpoint.
427 * @param address Returns the address of the breakpoint.
428 * @return False if there was no breakpoint.
430 virtual bool parseBreakpoint(const char* output, int& id,
431 QString& file, int& lineNo, QString& address) = 0;
434 * Parses the output of the DCinfolocals command.
435 * @param output The output of the debugger.
436 * @param newVars Receives the parsed variable values. The values are
437 * simply append()ed to the supplied list.
439 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
442 * Parses the output of a DCprint or DCprintStruct command.
443 * @param output The output of the debugger.
444 * @param wantErrorValue Specifies whether the error message should be
445 * provided as the value of a NKplain variable. If this is false,
446 * #var will be 0 if the printed value is an error message.
447 * @param var Returns the variable value. It is set to 0 if there was
448 * a parse error or if the output is an error message and wantErrorValue
449 * is false. If it is not 0, #var->text() will return junk and must be
450 * set using #var->setText().
451 * @return false if the output is an error message. Even if true is
452 * returned, #var might still be 0 (due to a parse error).
454 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
455 VarTree*& var) = 0;
458 * Parses the output of the DCcd command.
459 * @return false if the message is an error message.
461 virtual bool parseChangeWD(const char* output, QString& message) = 0;
464 * Parses the output of the DCexecutable command.
465 * @return false if an error occured.
467 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
470 * Parses the output of the DCcorefile command.
471 * @return false if the core file was not loaded successfully.
473 virtual bool parseCoreFile(const char* output) = 0;
475 enum StopFlags {
476 SFrefreshSource = 1, /* refresh of source code is needed */
477 SFrefreshBreak = 2, /* refresh breakpoints */
478 SFrefreshThreads = 4, /* refresh thread list */
479 SFprogramActive = 128 /* program remains active */
482 * Parses the output of commands that execute (a piece of) the program.
483 * @return The inclusive OR of zero or more of the StopFlags.
485 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
488 * Parses the output of the DCsharedlibs command.
490 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
493 * Parses the output of the DCfindType command.
494 * @return true if a type was found.
496 virtual bool parseFindType(const char* output, QString& type) = 0;
499 * Parses the output of the DCinforegisters command.
501 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
504 * Parses the output of the DCinfoline command. Returns false if the
505 * two addresses could not be found.
507 virtual bool parseInfoLine(const char* output,
508 QString& addrFrom, QString& addrTo) = 0;
511 * Parses the ouput of the DCdisassemble command.
513 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
516 * Parses a memory dump. Returns an empty string if no error was found;
517 * otherwise it contains an error message.
519 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
522 * Parses the output of the DCsetvariable command. Returns an empty
523 * string if no error was found; otherwise it contains an error
524 * message.
526 virtual QString parseSetVariable(const char* output) = 0;
529 * Returns a value that the user can edit.
531 virtual QString editableValue(VarTree* value);
533 protected:
534 /** Removes all commands from the low-priority queue. */
535 void flushLoPriQueue();
536 /** Removes all commands from the high-priority queue. */
537 void flushHiPriQueue();
539 QQueue<CmdQueueItem> m_hipriCmdQueue;
540 QList<CmdQueueItem> m_lopriCmdQueue;
542 * The active command is kept separately from other pending commands.
544 CmdQueueItem* m_activeCmd;
546 * Helper function that queues the given command string in the
547 * low-priority queue.
549 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
550 QueueMode mode);
552 * Helper function that queues the given command string in the
553 * high-priority queue.
555 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
556 bool clearLow);
557 void writeCommand();
558 virtual void commandFinished(CmdQueueItem* cmd) = 0;
560 protected:
561 /** @internal */
562 virtual int commSetupDoneC();
564 char m_prompt[10];
565 size_t m_promptMinLen;
566 char m_promptLastChar;
567 QRegExp m_promptRE;
569 // log file
570 QString m_logFileName;
571 QFile m_logFile;
573 protected slots:
574 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
575 virtual void slotCommandRead(KProcess*);
576 virtual void slotExited(KProcess*);
578 signals:
580 * This signal is emitted when the output of a command has been fully
581 * collected and is ready to be interpreted.
583 void commandReceived(CmdQueueItem* cmd, const char* output);
586 * This signal is emitted when the debugger recognizes that a specific
587 * location in a file ought to be displayed.
589 * Gdb's --fullname option supports this for the step, next, frame, and
590 * run commands (and possibly others).
592 * @param file specifies the file; this is not necessarily a full path
593 * name, and if it is relative, you won't know relative to what, you
594 * can only guess.
595 * @param lineNo specifies the line number (0-based!) (this may be
596 * negative, in which case the file should be activated, but the line
597 * should NOT be changed).
598 * @param address specifies the exact address of the PC or is empty.
600 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
603 * This signal is emitted when a command that starts the inferior has
604 * been submitted to the debugger.
606 void inferiorRunning();
609 * This signal is emitted when all output from the debugger has been
610 * consumed and no more commands are in the queues.
612 void enterIdleState();
615 #endif // DBGDRIVER_H