KDbg 2.5.6.
[kdbg.git] / kdbg / dbgdriver.h
blobdb6ed6252caf7d19265ec8f66f50fbdb58b2a8ab
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #ifndef DBGDRIVER_H
8 #define DBGDRIVER_H
10 #include <QFile>
11 #include <QByteArray>
12 #include <QProcess>
13 #include <queue>
14 #include <list>
17 class VarTree;
18 class ExprValue;
19 class ExprWnd;
20 class KDebugger;
21 class QStringList;
24 /**
25 * A type representing an address.
27 struct DbgAddr
29 QString a;
30 QString fnoffs;
31 DbgAddr() { }
32 DbgAddr(const QString& aa);
33 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
34 void operator=(const QString& aa);
35 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
36 QString asString() const;
37 bool isEmpty() const { return a.isEmpty(); }
38 protected:
39 void cleanAddr();
41 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
42 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
45 enum DbgCommand {
46 DCinitialize,
47 DCtty,
48 DCexecutable,
49 DCtargetremote,
50 DCcorefile,
51 DCattach,
52 DCinfolinemain,
53 DCinfolocals,
54 DCinforegisters,
55 DCexamine,
56 DCinfoline,
57 DCdisassemble,
58 DCsetargs,
59 DCsetenv,
60 DCunsetenv,
61 DCsetoption, /* debugger options */
62 DCcd,
63 DCbt,
64 DCrun,
65 DCcont,
66 DCstep,
67 DCstepi,
68 DCnext,
69 DCnexti,
70 DCfinish,
71 DCuntil, /* line number is zero-based! */
72 DCkill,
73 DCbreaktext,
74 DCbreakline, /* line number is zero-based! */
75 DCtbreakline, /* line number is zero-based! */
76 DCbreakaddr,
77 DCtbreakaddr,
78 DCwatchpoint,
79 DCdelete,
80 DCenable,
81 DCdisable,
82 DCprint,
83 DCprintDeref,
84 DCprintStruct,
85 DCprintQStringStruct,
86 DCframe,
87 DCfindType,
88 DCinfosharedlib,
89 DCthread,
90 DCinfothreads,
91 DCinfobreak,
92 DCcondition,
93 DCsetpc,
94 DCignore,
95 DCprintWChar,
96 DCsetvariable
99 enum RunDevNull {
100 RDNstdin = 0x1, /* redirect stdin to /dev/null */
101 RDNstdout = 0x2, /* redirect stdout to /dev/null */
102 RDNstderr = 0x4 /* redirect stderr to /dev/null */
106 * How the memory dump is formated. The lowest 4 bits define the size of
107 * the entities. The higher bits define how these are formatted. Note that
108 * not all combinations make sense.
110 enum MemoryDumpType {
111 // sizes
112 MDTbyte = 0x1,
113 MDThalfword = 0x2,
114 MDTword = 0x3,
115 MDTgiantword = 0x4,
116 MDTsizemask = 0xf,
117 // formats
118 MDThex = 0x10,
119 MDTsigned = 0x20,
120 MDTunsigned = 0x30,
121 MDToctal = 0x40,
122 MDTbinary = 0x50,
123 MDTaddress = 0x60,
124 MDTchar = 0x70,
125 MDTfloat = 0x80,
126 MDTstring = 0x90,
127 MDTinsn = 0xa0,
128 MDTformatmask = 0xf0
131 struct Breakpoint;
134 * Debugger commands are placed in a queue. Only one command at a time is
135 * sent down to the debugger. All other commands in the queue are retained
136 * until the sent command has been processed by gdb. The debugger tells us
137 * that it's done with the command by sending the prompt. The output of the
138 * debugger is parsed at that time. Then, if more commands are in the
139 * queue, the next one is sent to the debugger.
141 struct CmdQueueItem
143 DbgCommand m_cmd;
144 QString m_cmdString;
145 bool m_committed; /* just a debugging aid */
146 // remember which expression when printing an expression
147 VarTree* m_expr;
148 ExprWnd* m_exprWnd;
149 // remember file position
150 QString m_fileName;
151 int m_lineNo;
152 DbgAddr m_addr;
153 // the breakpoint info
154 Breakpoint* m_brkpt;
155 int m_existingBrkpt;
156 // whether command was emitted due to direct user request (only set when relevant)
157 bool m_byUser;
159 CmdQueueItem(DbgCommand cmd, const QString& str) :
160 m_cmd(cmd),
161 m_cmdString(str),
162 m_committed(false),
163 m_expr(0),
164 m_exprWnd(0),
165 m_lineNo(0),
166 m_brkpt(0),
167 m_existingBrkpt(0),
168 m_byUser(false)
171 struct IsEqualCmd
173 IsEqualCmd(DbgCommand cmd, const QString& str) : m_cmd(cmd), m_str(str) { }
174 bool operator()(CmdQueueItem*) const;
175 DbgCommand m_cmd;
176 const QString& m_str;
181 * The information about a breakpoint that is parsed from the list of
182 * breakpoints.
184 struct Breakpoint
186 int id; /* gdb's number */
187 enum Type {
188 breakpoint, watchpoint
189 } type;
190 bool temporary;
191 bool enabled;
192 QString location;
193 QString text; /* text if set using DCbreaktext */
194 DbgAddr address; /* exact address of breakpoint */
195 QString condition; /* condition as printed by gdb */
196 int ignoreCount; /* ignore next that may hits */
197 int hitCount; /* as reported by gdb */
198 // the following items repeat the location, but in a better usable way
199 QString fileName;
200 int lineNo; /* zero-based line number */
201 Breakpoint();
202 bool isOrphaned() const { return id < 0; }
206 * Information about a stack frame.
208 struct FrameInfo
210 QString fileName;
211 int lineNo; /* zero-based line number */
212 DbgAddr address; /* exact address of PC */
216 * The information about a stack frame as parsed from the backtrace.
218 struct StackFrame : FrameInfo
220 int frameNo;
221 ExprValue* var; /* more information if non-zero */
222 StackFrame() : var(0) { }
223 ~StackFrame();
227 * The information about a thread as parsed from the threads list.
229 struct ThreadInfo : FrameInfo
231 int id; /* gdb's number */
232 QString threadName; /* the SYSTAG */
233 QString function; /* where thread is halted */
234 bool hasFocus; /* the thread whose stack we are watching */
238 * Register information
240 struct RegisterInfo
242 QString regName;
243 QString rawValue;
244 QString cookedValue; /* may be empty */
245 QString type; /* of vector register if not empty */
249 * Disassembled code
251 struct DisassembledCode
253 DbgAddr address;
254 QString code;
258 * Memory contents
260 struct MemoryDump
262 DbgAddr address;
263 QString dump;
267 * This is an abstract base class for debugger process.
269 * This class represents the debugger program. It provides the low-level
270 * interface to the commandline debugger. As such it implements the
271 * commands and parses the output.
273 class DebuggerDriver : public QProcess
275 Q_OBJECT
276 public:
277 DebuggerDriver();
278 virtual ~DebuggerDriver() = 0;
280 virtual QString driverName() const = 0;
282 * Returns the default command string to invoke the debugger driver.
284 virtual QString defaultInvocation() const = 0;
287 * Returns a list of options that can be turned on and off.
289 virtual QStringList boolOptionList() const = 0;
291 virtual bool startup(QString cmdStr);
292 void setLogFileName(const QString& fname) { m_logFileName = fname; }
293 bool isRunning() { return state() != NotRunning; }
295 protected:
296 QString m_runCmd;
298 enum DebuggerState {
299 DSidle, /* gdb waits for input */
300 DSinterrupted, /* a command was interrupted */
301 DSrunningLow, /* gdb is running a low-priority command */
302 DSrunning, /* gdb waits for program */
303 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
304 DScommandSentLow /* low-prioritycommand has been sent */
306 DebuggerState m_state;
308 public:
309 bool isIdle() const { return m_state == DSidle; }
311 * Tells whether a high prority command would be executed immediately.
313 bool canExecuteImmediately() const { return m_hipriCmdQueue.empty(); }
315 protected:
316 QByteArray m_output; // normal gdb output
317 std::queue<QByteArray> m_delayedOutput; // output colleced before signal bytesWritten() arrived
319 public:
321 * Enqueues a high-priority command. High-priority commands are
322 * executed before any low-priority commands. No user interaction is
323 * possible as long as there is a high-priority command in the queue.
325 virtual CmdQueueItem* executeCmd(DbgCommand,
326 bool clearLow = false) = 0;
327 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
328 bool clearLow = false) = 0;
329 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
330 bool clearLow = false) = 0;
331 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
332 bool clearLow = false) = 0;
333 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
334 bool clearLow = false) = 0;
335 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
336 bool clearLow = false) = 0;
338 enum QueueMode {
339 QMnormal, /* queues the command last */
340 QMoverride, /* removes an already queued command */
341 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
345 * Enqueues a low-priority command. Low-priority commands are executed
346 * after any high-priority commands.
348 virtual CmdQueueItem* queueCmd(DbgCommand,
349 QueueMode mode) = 0;
350 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
351 QueueMode mode) = 0;
352 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
353 QueueMode mode) = 0;
354 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
355 QueueMode mode) = 0;
356 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
357 QueueMode mode) = 0;
360 * Flushes the command queues.
361 * @param hipriOnly if true, only the high priority queue is flushed.
363 virtual void flushCommands(bool hipriOnly = false);
366 * Terminates the debugger process.
368 virtual void terminate() = 0;
371 * Terminates the debugger process, but also detaches any program that
372 * it has been attached to.
374 virtual void detachAndTerminate() = 0;
377 * Interrupts the debuggee.
379 virtual void interruptInferior() = 0;
382 * Specifies the command that prints the QString data.
384 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
387 * Parses the output as an array of QChars.
389 virtual ExprValue* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
392 * Parses a back-trace (the output of the DCbt command).
394 virtual void parseBackTrace(const char* output, std::list<StackFrame>& stack) = 0;
397 * Parses the output of the DCframe command;
398 * @param frameNo Returns the frame number.
399 * @param file Returns the source file name.
400 * @param lineNo The zero-based line number.
401 * @param address Returns the exact address.
402 * @return false if the frame could not be parsed successfully. The
403 * output values are undefined in this case.
405 virtual bool parseFrameChange(const char* output, int& frameNo,
406 QString& file, int& lineNo, DbgAddr& address) = 0;
409 * Parses a list of breakpoints.
410 * @param output The output of the debugger.
411 * @param brks The list of new #Breakpoint objects. The list
412 * must initially be empty.
413 * @return False if there was an error before the first breakpoint
414 * was found. Even if true is returned, #brks may be empty.
416 virtual bool parseBreakList(const char* output, std::list<Breakpoint>& brks) = 0;
419 * Parses a list of threads.
420 * @param output The output of the debugger.
421 * @return The new thread list. There is no indication if there was
422 * a parse error.
424 virtual std::list<ThreadInfo> parseThreadList(const char* output) = 0;
427 * Parses the output when the program stops to see whether this it
428 * stopped due to a breakpoint.
429 * @param output The output of the debugger.
430 * @param id Returns the breakpoint id.
431 * @param file Returns the file name in which the breakpoint is.
432 * @param lineNo Returns the zero-based line number of the breakpoint.
433 * @param address Returns the address of the breakpoint.
434 * @return False if there was no breakpoint.
436 virtual bool parseBreakpoint(const char* output, int& id,
437 QString& file, int& lineNo, QString& address) = 0;
440 * Parses the output of the DCinfolocals command.
441 * @param output The output of the debugger.
442 * @param newVars Receives the parsed variable values. The values are
443 * simply append()ed to the supplied list.
445 virtual void parseLocals(const char* output, std::list<ExprValue*>& newVars) = 0;
448 * Parses the output of a DCprint or DCprintStruct command.
449 * @param output The output of the debugger.
450 * @param wantErrorValue Specifies whether the error message should be
451 * provided as the value of a NKplain variable. If this is false,
452 * 0 is returned if the printed value is an error message.
453 * @return the parsed value. It is 0 if there was a parse error
454 * or if the output is an error message and #wantErrorValue
455 * is \c false. The returned object's text() is undefined.
457 virtual ExprValue* parsePrintExpr(const char* output, bool wantErrorValue) = 0;
460 * Parses the output of the DCcd command.
461 * @return false if the message is an error message.
463 virtual bool parseChangeWD(const char* output, QString& message) = 0;
466 * Parses the output of the DCexecutable command.
467 * @return false if an error occured.
469 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
472 * Parses the output of the DCcorefile command.
473 * @return false if the core file was not loaded successfully.
475 virtual bool parseCoreFile(const char* output) = 0;
477 enum StopFlags {
478 SFrefreshSource = 1, /* refresh of source code is needed */
479 SFrefreshBreak = 2, /* refresh breakpoints */
480 SFrefreshThreads = 4, /* refresh thread list */
481 SFprogramActive = 128 /* program remains active */
484 * Parses the output of commands that execute (a piece of) the program.
485 * @return The inclusive OR of zero or more of the StopFlags.
487 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
490 * Parses the output of the DCsharedlibs command.
492 virtual QStringList parseSharedLibs(const char* output) = 0;
495 * Parses the output of the DCfindType command.
496 * @return true if a type was found.
498 virtual bool parseFindType(const char* output, QString& type) = 0;
501 * Parses the output of the DCinforegisters command.
503 virtual std::list<RegisterInfo> parseRegisters(const char* output) = 0;
506 * Parses the output of the DCinfoline command. Returns false if the
507 * two addresses could not be found.
509 virtual bool parseInfoLine(const char* output,
510 QString& addrFrom, QString& addrTo) = 0;
513 * Parses the ouput of the DCdisassemble command.
515 virtual std::list<DisassembledCode> parseDisassemble(const char* output) = 0;
518 * Parses a memory dump. Returns an empty string if no error was found;
519 * otherwise it contains an error message.
521 virtual QString parseMemoryDump(const char* output, std::list<MemoryDump>& memdump) = 0;
524 * Parses the output of the DCsetvariable command. Returns an empty
525 * string if no error was found; otherwise it contains an error
526 * message.
528 virtual QString parseSetVariable(const char* output) = 0;
531 * Returns a value that the user can edit.
533 virtual QString editableValue(VarTree* value);
535 protected:
536 /** Removes all commands from the low-priority queue. */
537 void flushLoPriQueue();
538 /** Removes all commands from the high-priority queue. */
539 void flushHiPriQueue();
541 std::queue<CmdQueueItem*> m_hipriCmdQueue;
542 std::list<CmdQueueItem*> m_lopriCmdQueue;
544 * The active command is kept separately from other pending commands.
546 CmdQueueItem* m_activeCmd;
548 * Helper function that queues the given command string in the
549 * low-priority queue.
551 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
552 QueueMode mode);
554 * Helper function that queues the given command string in the
555 * high-priority queue.
557 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
558 bool clearLow);
559 void writeCommand();
560 virtual void commandFinished(CmdQueueItem* cmd) = 0;
562 protected:
563 void processOutput(const QByteArray& data);
566 * Returns the start of the prompt in \a output or -1.
567 * \a len specifies the size of \a output, but in addition, the contents
568 * of \a output are NUL-terminated, i.e., \c output[len] is zero.
570 virtual int findPrompt(const QByteArray& output) const = 0;
572 // log file
573 QString m_logFileName;
574 QFile m_logFile;
576 public slots:
577 void dequeueCmdByVar(VarTree* var);
579 protected slots:
580 virtual void slotReceiveOutput();
581 virtual void slotCommandRead();
582 virtual void slotExited();
584 signals:
586 * This signal is emitted when the output of a command has been fully
587 * collected and is ready to be interpreted.
589 void commandReceived(CmdQueueItem* cmd, const char* output);
592 * This signal is emitted when the debugger recognizes that a specific
593 * location in a file ought to be displayed.
595 * Gdb's --fullname option supports this for the step, next, frame, and
596 * run commands (and possibly others).
598 * @param file specifies the file; this is not necessarily a full path
599 * name, and if it is relative, you won't know relative to what, you
600 * can only guess.
601 * @param lineNo specifies the line number (0-based!) (this may be
602 * negative, in which case the file should be activated, but the line
603 * should NOT be changed).
604 * @param address specifies the exact address of the PC or is empty.
606 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
609 * This signal is emitted when a command that starts the inferior has
610 * been submitted to the debugger.
612 void inferiorRunning();
615 * This signal is emitted when all output from the debugger has been
616 * consumed and no more commands are in the queues.
618 void enterIdleState();
621 #endif // DBGDRIVER_H