Replace a Q[Ptr]List by std::list in the debugger driver.
[kdbg.git] / kdbg / dbgdriver.h
blobcf3e2eab6a16f0b51619965a43d5394710705a3a
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.h>
11 #include <qregexp.h>
12 #include <qcstring.h>
13 #include <kprocess.h>
14 #include <queue>
15 #include <list>
18 class VarTree;
19 class ExprValue;
20 class ExprWnd;
21 class KDebugger;
22 class QStrList;
23 class QStringList;
26 /**
27 * A type representing an address.
29 struct DbgAddr
31 QString a;
32 QString fnoffs;
33 DbgAddr() { }
34 DbgAddr(const QString& aa);
35 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
36 void operator=(const QString& aa);
37 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
38 QString asString() const;
39 bool isEmpty() const { return a.isEmpty(); }
40 protected:
41 void cleanAddr();
43 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
44 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
47 enum DbgCommand {
48 DCinitialize,
49 DCtty,
50 DCexecutable,
51 DCtargetremote,
52 DCcorefile,
53 DCattach,
54 DCinfolinemain,
55 DCinfolocals,
56 DCinforegisters,
57 DCexamine,
58 DCinfoline,
59 DCdisassemble,
60 DCsetargs,
61 DCsetenv,
62 DCunsetenv,
63 DCsetoption, /* debugger options */
64 DCcd,
65 DCbt,
66 DCrun,
67 DCcont,
68 DCstep,
69 DCstepi,
70 DCnext,
71 DCnexti,
72 DCfinish,
73 DCuntil, /* line number is zero-based! */
74 DCkill,
75 DCbreaktext,
76 DCbreakline, /* line number is zero-based! */
77 DCtbreakline, /* line number is zero-based! */
78 DCbreakaddr,
79 DCtbreakaddr,
80 DCwatchpoint,
81 DCdelete,
82 DCenable,
83 DCdisable,
84 DCprint,
85 DCprintDeref,
86 DCprintStruct,
87 DCprintQStringStruct,
88 DCframe,
89 DCfindType,
90 DCinfosharedlib,
91 DCthread,
92 DCinfothreads,
93 DCinfobreak,
94 DCcondition,
95 DCsetpc,
96 DCignore,
97 DCprintWChar,
98 DCsetvariable
101 enum RunDevNull {
102 RDNstdin = 0x1, /* redirect stdin to /dev/null */
103 RDNstdout = 0x2, /* redirect stdout to /dev/null */
104 RDNstderr = 0x4 /* redirect stderr to /dev/null */
108 * How the memory dump is formated. The lowest 4 bits define the size of
109 * the entities. The higher bits define how these are formatted. Note that
110 * not all combinations make sense.
112 enum MemoryDumpType {
113 // sizes
114 MDTbyte = 0x1,
115 MDThalfword = 0x2,
116 MDTword = 0x3,
117 MDTgiantword = 0x4,
118 MDTsizemask = 0xf,
119 // formats
120 MDThex = 0x10,
121 MDTsigned = 0x20,
122 MDTunsigned = 0x30,
123 MDToctal = 0x40,
124 MDTbinary = 0x50,
125 MDTaddress = 0x60,
126 MDTchar = 0x70,
127 MDTfloat = 0x80,
128 MDTstring = 0x90,
129 MDTinsn = 0xa0,
130 MDTformatmask = 0xf0
133 struct Breakpoint;
136 * Debugger commands are placed in a queue. Only one command at a time is
137 * sent down to the debugger. All other commands in the queue are retained
138 * until the sent command has been processed by gdb. The debugger tells us
139 * that it's done with the command by sending the prompt. The output of the
140 * debugger is parsed at that time. Then, if more commands are in the
141 * queue, the next one is sent to the debugger.
143 struct CmdQueueItem
145 DbgCommand m_cmd;
146 QString m_cmdString;
147 bool m_committed; /* just a debugging aid */
148 // remember which expression when printing an expression
149 VarTree* m_expr;
150 ExprWnd* m_exprWnd;
151 // remember file position
152 QString m_fileName;
153 int m_lineNo;
154 // the breakpoint info
155 Breakpoint* m_brkpt;
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_byUser(false)
170 struct IsEqualCmd
172 IsEqualCmd(DbgCommand cmd, const QString& str) : m_cmd(cmd), m_str(str) { }
173 bool operator()(CmdQueueItem*) const;
174 DbgCommand m_cmd;
175 const QString& m_str;
180 * The information about a breakpoint that is parsed from the list of
181 * breakpoints.
183 struct Breakpoint
185 int id; /* gdb's number */
186 enum Type {
187 breakpoint, watchpoint
188 } type;
189 bool temporary;
190 bool enabled;
191 QString location;
192 QString text; /* text if set using DCbreaktext */
193 DbgAddr address; /* exact address of breakpoint */
194 QString condition; /* condition as printed by gdb */
195 int ignoreCount; /* ignore next that may hits */
196 int hitCount; /* as reported by gdb */
197 // the following items repeat the location, but in a better usable way
198 QString fileName;
199 int lineNo; /* zero-based line number */
200 Breakpoint();
201 bool isOrphaned() const { return id < 0; }
205 * Information about a stack frame.
207 struct FrameInfo
209 QString fileName;
210 int lineNo; /* zero-based line number */
211 DbgAddr address; /* exact address of PC */
215 * The information about a stack frame as parsed from the backtrace.
217 struct StackFrame : FrameInfo
219 int frameNo;
220 ExprValue* var; /* more information if non-zero */
221 StackFrame() : var(0) { }
222 ~StackFrame();
226 * The information about a thread as parsed from the threads list.
228 struct ThreadInfo : FrameInfo
230 int id; /* gdb's number */
231 QString threadName; /* the SYSTAG */
232 QString function; /* where thread is halted */
233 bool hasFocus; /* the thread whose stack we are watching */
237 * Register information
239 struct RegisterInfo
241 QString regName;
242 QString rawValue;
243 QString cookedValue; /* may be empty */
244 QString type; /* of vector register if not empty */
248 * Disassembled code
250 struct DisassembledCode
252 DbgAddr address;
253 QString code;
257 * Memory contents
259 struct MemoryDump
261 DbgAddr address;
262 QString dump;
266 * This is an abstract base class for debugger process.
268 * This class represents the debugger program. It provides the low-level
269 * interface to the commandline debugger. As such it implements the
270 * commands and parses the output.
272 class DebuggerDriver : public KProcess
274 Q_OBJECT
275 public:
276 DebuggerDriver();
277 virtual ~DebuggerDriver() = 0;
279 virtual QString driverName() const = 0;
281 * Returns the default command string to invoke the debugger driver.
283 virtual QString defaultInvocation() const = 0;
286 * Returns a list of options that can be turned on and off.
288 virtual QStringList boolOptionList() const = 0;
290 virtual bool startup(QString cmdStr);
291 void setLogFileName(const QString& fname) { m_logFileName = fname; }
293 protected:
294 QString m_runCmd;
296 enum DebuggerState {
297 DSidle, /* gdb waits for input */
298 DSinterrupted, /* a command was interrupted */
299 DSrunningLow, /* gdb is running a low-priority command */
300 DSrunning, /* gdb waits for program */
301 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
302 DScommandSentLow /* low-prioritycommand has been sent */
304 DebuggerState m_state;
306 public:
307 bool isIdle() const { return m_state == DSidle; }
309 * Tells whether a high prority command would be executed immediately.
311 bool canExecuteImmediately() const { return m_hipriCmdQueue.empty(); }
313 protected:
314 char* m_output; /* normal gdb output */
315 size_t m_outputLen; /* amount of data so far accumulated in m_output */
316 size_t m_outputAlloc; /* space available in m_output */
317 std::queue<QByteArray> m_delayedOutput; /* output colleced while we have receivedOutput */
318 /* but before signal wroteStdin arrived */
320 public:
322 * Enqueues a high-priority command. High-priority commands are
323 * executed before any low-priority commands. No user interaction is
324 * possible as long as there is a high-priority command in the queue.
326 virtual CmdQueueItem* executeCmd(DbgCommand,
327 bool clearLow = false) = 0;
328 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
329 bool clearLow = false) = 0;
330 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
331 bool clearLow = false) = 0;
332 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
333 bool clearLow = false) = 0;
334 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
335 bool clearLow = false) = 0;
336 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
337 bool clearLow = false) = 0;
339 enum QueueMode {
340 QMnormal, /* queues the command last */
341 QMoverride, /* removes an already queued command */
342 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
346 * Enqueues a low-priority command. Low-priority commands are executed
347 * after any high-priority commands.
349 virtual CmdQueueItem* queueCmd(DbgCommand,
350 QueueMode mode) = 0;
351 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
352 QueueMode mode) = 0;
353 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
354 QueueMode mode) = 0;
355 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
356 QueueMode mode) = 0;
357 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
358 QueueMode mode) = 0;
361 * Flushes the command queues.
362 * @param hipriOnly if true, only the high priority queue is flushed.
364 virtual void flushCommands(bool hipriOnly = false);
367 * Terminates the debugger process.
369 virtual void terminate() = 0;
372 * Terminates the debugger process, but also detaches any program that
373 * it has been attached to.
375 virtual void detachAndTerminate() = 0;
378 * Interrupts the debuggee.
380 virtual void interruptInferior() = 0;
383 * Specifies the command that prints the QString data.
385 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
388 * Parses the output as an array of QChars.
390 virtual ExprValue* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
393 * Parses a back-trace (the output of the DCbt command).
395 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
398 * Parses the output of the DCframe command;
399 * @param frameNo Returns the frame number.
400 * @param file Returns the source file name.
401 * @param lineNo The zero-based line number.
402 * @param address Returns the exact address.
403 * @return false if the frame could not be parsed successfully. The
404 * output values are undefined in this case.
406 virtual bool parseFrameChange(const char* output, int& frameNo,
407 QString& file, int& lineNo, DbgAddr& address) = 0;
410 * Parses a list of breakpoints.
411 * @param output The output of the debugger.
412 * @param brks The list of new #Breakpoint objects. The list
413 * must initially be empty.
414 * @return False if there was an error before the first breakpoint
415 * was found. Even if true is returned, #brks may be empty.
417 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
420 * Parses a list of threads.
421 * @param output The output of the debugger.
422 * @param threads The list of new #ThreadInfo objects. The list
423 * must initially be empty.
424 * @return False if there was an error before the first thread entry
425 * was found. Even if true is returned, #threads may be empty.
427 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
430 * Parses the output when the program stops to see whether this it
431 * stopped due to a breakpoint.
432 * @param output The output of the debugger.
433 * @param id Returns the breakpoint id.
434 * @param file Returns the file name in which the breakpoint is.
435 * @param lineNo Returns the zero-based line number of the breakpoint.
436 * @param address Returns the address of the breakpoint.
437 * @return False if there was no breakpoint.
439 virtual bool parseBreakpoint(const char* output, int& id,
440 QString& file, int& lineNo, QString& address) = 0;
443 * Parses the output of the DCinfolocals command.
444 * @param output The output of the debugger.
445 * @param newVars Receives the parsed variable values. The values are
446 * simply append()ed to the supplied list.
448 virtual void parseLocals(const char* output, QList<ExprValue>& newVars) = 0;
451 * Parses the output of a DCprint or DCprintStruct command.
452 * @param output The output of the debugger.
453 * @param wantErrorValue Specifies whether the error message should be
454 * provided as the value of a NKplain variable. If this is false,
455 * 0 is returned if the printed value is an error message.
456 * @return the parsed value. It is 0 if there was a parse error
457 * or if the output is an error message and #wantErrorValue
458 * is \c false. The returned object's text() is undefined.
460 virtual ExprValue* parsePrintExpr(const char* output, bool wantErrorValue) = 0;
463 * Parses the output of the DCcd command.
464 * @return false if the message is an error message.
466 virtual bool parseChangeWD(const char* output, QString& message) = 0;
469 * Parses the output of the DCexecutable command.
470 * @return false if an error occured.
472 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
475 * Parses the output of the DCcorefile command.
476 * @return false if the core file was not loaded successfully.
478 virtual bool parseCoreFile(const char* output) = 0;
480 enum StopFlags {
481 SFrefreshSource = 1, /* refresh of source code is needed */
482 SFrefreshBreak = 2, /* refresh breakpoints */
483 SFrefreshThreads = 4, /* refresh thread list */
484 SFprogramActive = 128 /* program remains active */
487 * Parses the output of commands that execute (a piece of) the program.
488 * @return The inclusive OR of zero or more of the StopFlags.
490 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
493 * Parses the output of the DCsharedlibs command.
495 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
498 * Parses the output of the DCfindType command.
499 * @return true if a type was found.
501 virtual bool parseFindType(const char* output, QString& type) = 0;
504 * Parses the output of the DCinforegisters command.
506 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
509 * Parses the output of the DCinfoline command. Returns false if the
510 * two addresses could not be found.
512 virtual bool parseInfoLine(const char* output,
513 QString& addrFrom, QString& addrTo) = 0;
516 * Parses the ouput of the DCdisassemble command.
518 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
521 * Parses a memory dump. Returns an empty string if no error was found;
522 * otherwise it contains an error message.
524 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
527 * Parses the output of the DCsetvariable command. Returns an empty
528 * string if no error was found; otherwise it contains an error
529 * message.
531 virtual QString parseSetVariable(const char* output) = 0;
534 * Returns a value that the user can edit.
536 virtual QString editableValue(VarTree* value);
538 protected:
539 /** Removes all commands from the low-priority queue. */
540 void flushLoPriQueue();
541 /** Removes all commands from the high-priority queue. */
542 void flushHiPriQueue();
544 std::queue<CmdQueueItem*> m_hipriCmdQueue;
545 std::list<CmdQueueItem*> m_lopriCmdQueue;
547 * The active command is kept separately from other pending commands.
549 CmdQueueItem* m_activeCmd;
551 * Helper function that queues the given command string in the
552 * low-priority queue.
554 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
555 QueueMode mode);
557 * Helper function that queues the given command string in the
558 * high-priority queue.
560 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
561 bool clearLow);
562 void writeCommand();
563 virtual void commandFinished(CmdQueueItem* cmd) = 0;
565 protected:
566 /** @internal */
567 virtual int commSetupDoneC();
569 char m_prompt[10];
570 size_t m_promptMinLen;
571 char m_promptLastChar;
572 QRegExp m_promptRE;
574 // log file
575 QString m_logFileName;
576 QFile m_logFile;
578 public slots:
579 void dequeueCmdByVar(VarTree* var);
581 protected slots:
582 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
583 virtual void slotCommandRead(KProcess*);
584 virtual void slotExited(KProcess*);
586 signals:
588 * This signal is emitted when the output of a command has been fully
589 * collected and is ready to be interpreted.
591 void commandReceived(CmdQueueItem* cmd, const char* output);
594 * This signal is emitted when the debugger recognizes that a specific
595 * location in a file ought to be displayed.
597 * Gdb's --fullname option supports this for the step, next, frame, and
598 * run commands (and possibly others).
600 * @param file specifies the file; this is not necessarily a full path
601 * name, and if it is relative, you won't know relative to what, you
602 * can only guess.
603 * @param lineNo specifies the line number (0-based!) (this may be
604 * negative, in which case the file should be activated, but the line
605 * should NOT be changed).
606 * @param address specifies the exact address of the PC or is empty.
608 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
611 * This signal is emitted when a command that starts the inferior has
612 * been submitted to the debugger.
614 void inferiorRunning();
617 * This signal is emitted when all output from the debugger has been
618 * consumed and no more commands are in the queues.
620 void enterIdleState();
623 #endif // DBGDRIVER_H