Convert parseSharedLibs() from QStrList to QStringList.
[kdbg.git] / kdbg / dbgdriver.h
blobf5f1f7dfdcf109a47ca312b14c150fccebee4a14
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 QStringList;
25 /**
26 * A type representing an address.
28 struct DbgAddr
30 QString a;
31 QString fnoffs;
32 DbgAddr() { }
33 DbgAddr(const QString& aa);
34 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
35 void operator=(const QString& aa);
36 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
37 QString asString() const;
38 bool isEmpty() const { return a.isEmpty(); }
39 protected:
40 void cleanAddr();
42 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
43 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
46 enum DbgCommand {
47 DCinitialize,
48 DCtty,
49 DCexecutable,
50 DCtargetremote,
51 DCcorefile,
52 DCattach,
53 DCinfolinemain,
54 DCinfolocals,
55 DCinforegisters,
56 DCexamine,
57 DCinfoline,
58 DCdisassemble,
59 DCsetargs,
60 DCsetenv,
61 DCunsetenv,
62 DCsetoption, /* debugger options */
63 DCcd,
64 DCbt,
65 DCrun,
66 DCcont,
67 DCstep,
68 DCstepi,
69 DCnext,
70 DCnexti,
71 DCfinish,
72 DCuntil, /* line number is zero-based! */
73 DCkill,
74 DCbreaktext,
75 DCbreakline, /* line number is zero-based! */
76 DCtbreakline, /* line number is zero-based! */
77 DCbreakaddr,
78 DCtbreakaddr,
79 DCwatchpoint,
80 DCdelete,
81 DCenable,
82 DCdisable,
83 DCprint,
84 DCprintDeref,
85 DCprintStruct,
86 DCprintQStringStruct,
87 DCframe,
88 DCfindType,
89 DCinfosharedlib,
90 DCthread,
91 DCinfothreads,
92 DCinfobreak,
93 DCcondition,
94 DCsetpc,
95 DCignore,
96 DCprintWChar,
97 DCsetvariable
100 enum RunDevNull {
101 RDNstdin = 0x1, /* redirect stdin to /dev/null */
102 RDNstdout = 0x2, /* redirect stdout to /dev/null */
103 RDNstderr = 0x4 /* redirect stderr to /dev/null */
107 * How the memory dump is formated. The lowest 4 bits define the size of
108 * the entities. The higher bits define how these are formatted. Note that
109 * not all combinations make sense.
111 enum MemoryDumpType {
112 // sizes
113 MDTbyte = 0x1,
114 MDThalfword = 0x2,
115 MDTword = 0x3,
116 MDTgiantword = 0x4,
117 MDTsizemask = 0xf,
118 // formats
119 MDThex = 0x10,
120 MDTsigned = 0x20,
121 MDTunsigned = 0x30,
122 MDToctal = 0x40,
123 MDTbinary = 0x50,
124 MDTaddress = 0x60,
125 MDTchar = 0x70,
126 MDTfloat = 0x80,
127 MDTstring = 0x90,
128 MDTinsn = 0xa0,
129 MDTformatmask = 0xf0
132 struct Breakpoint;
135 * Debugger commands are placed in a queue. Only one command at a time is
136 * sent down to the debugger. All other commands in the queue are retained
137 * until the sent command has been processed by gdb. The debugger tells us
138 * that it's done with the command by sending the prompt. The output of the
139 * debugger is parsed at that time. Then, if more commands are in the
140 * queue, the next one is sent to the debugger.
142 struct CmdQueueItem
144 DbgCommand m_cmd;
145 QString m_cmdString;
146 bool m_committed; /* just a debugging aid */
147 // remember which expression when printing an expression
148 VarTree* m_expr;
149 ExprWnd* m_exprWnd;
150 // remember file position
151 QString m_fileName;
152 int m_lineNo;
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 KProcess
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; }
294 protected:
295 QString m_runCmd;
297 enum DebuggerState {
298 DSidle, /* gdb waits for input */
299 DSinterrupted, /* a command was interrupted */
300 DSrunningLow, /* gdb is running a low-priority command */
301 DSrunning, /* gdb waits for program */
302 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
303 DScommandSentLow /* low-prioritycommand has been sent */
305 DebuggerState m_state;
307 public:
308 bool isIdle() const { return m_state == DSidle; }
310 * Tells whether a high prority command would be executed immediately.
312 bool canExecuteImmediately() const { return m_hipriCmdQueue.empty(); }
314 protected:
315 char* m_output; /* normal gdb output */
316 size_t m_outputLen; /* amount of data so far accumulated in m_output */
317 size_t m_outputAlloc; /* space available in m_output */
318 std::queue<QByteArray> m_delayedOutput; /* output colleced while we have receivedOutput */
319 /* but before signal wroteStdin arrived */
321 public:
323 * Enqueues a high-priority command. High-priority commands are
324 * executed before any low-priority commands. No user interaction is
325 * possible as long as there is a high-priority command in the queue.
327 virtual CmdQueueItem* executeCmd(DbgCommand,
328 bool clearLow = false) = 0;
329 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
330 bool clearLow = false) = 0;
331 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
332 bool clearLow = false) = 0;
333 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
334 bool clearLow = false) = 0;
335 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
336 bool clearLow = false) = 0;
337 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
338 bool clearLow = false) = 0;
340 enum QueueMode {
341 QMnormal, /* queues the command last */
342 QMoverride, /* removes an already queued command */
343 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
347 * Enqueues a low-priority command. Low-priority commands are executed
348 * after any high-priority commands.
350 virtual CmdQueueItem* queueCmd(DbgCommand,
351 QueueMode mode) = 0;
352 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
353 QueueMode mode) = 0;
354 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
355 QueueMode mode) = 0;
356 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
357 QueueMode mode) = 0;
358 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
359 QueueMode mode) = 0;
362 * Flushes the command queues.
363 * @param hipriOnly if true, only the high priority queue is flushed.
365 virtual void flushCommands(bool hipriOnly = false);
368 * Terminates the debugger process.
370 virtual void terminate() = 0;
373 * Terminates the debugger process, but also detaches any program that
374 * it has been attached to.
376 virtual void detachAndTerminate() = 0;
379 * Interrupts the debuggee.
381 virtual void interruptInferior() = 0;
384 * Specifies the command that prints the QString data.
386 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
389 * Parses the output as an array of QChars.
391 virtual ExprValue* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
394 * Parses a back-trace (the output of the DCbt command).
396 virtual void parseBackTrace(const char* output, std::list<StackFrame>& stack) = 0;
399 * Parses the output of the DCframe command;
400 * @param frameNo Returns the frame number.
401 * @param file Returns the source file name.
402 * @param lineNo The zero-based line number.
403 * @param address Returns the exact address.
404 * @return false if the frame could not be parsed successfully. The
405 * output values are undefined in this case.
407 virtual bool parseFrameChange(const char* output, int& frameNo,
408 QString& file, int& lineNo, DbgAddr& address) = 0;
411 * Parses a list of breakpoints.
412 * @param output The output of the debugger.
413 * @param brks The list of new #Breakpoint objects. The list
414 * must initially be empty.
415 * @return False if there was an error before the first breakpoint
416 * was found. Even if true is returned, #brks may be empty.
418 virtual bool parseBreakList(const char* output, std::list<Breakpoint>& brks) = 0;
421 * Parses a list of threads.
422 * @param output The output of the debugger.
423 * @return The new thread list. There is no indication if there was
424 * a parse error.
426 virtual std::list<ThreadInfo> parseThreadList(const char* output) = 0;
429 * Parses the output when the program stops to see whether this it
430 * stopped due to a breakpoint.
431 * @param output The output of the debugger.
432 * @param id Returns the breakpoint id.
433 * @param file Returns the file name in which the breakpoint is.
434 * @param lineNo Returns the zero-based line number of the breakpoint.
435 * @param address Returns the address of the breakpoint.
436 * @return False if there was no breakpoint.
438 virtual bool parseBreakpoint(const char* output, int& id,
439 QString& file, int& lineNo, QString& address) = 0;
442 * Parses the output of the DCinfolocals command.
443 * @param output The output of the debugger.
444 * @param newVars Receives the parsed variable values. The values are
445 * simply append()ed to the supplied list.
447 virtual void parseLocals(const char* output, std::list<ExprValue*>& newVars) = 0;
450 * Parses the output of a DCprint or DCprintStruct command.
451 * @param output The output of the debugger.
452 * @param wantErrorValue Specifies whether the error message should be
453 * provided as the value of a NKplain variable. If this is false,
454 * 0 is returned if the printed value is an error message.
455 * @return the parsed value. It is 0 if there was a parse error
456 * or if the output is an error message and #wantErrorValue
457 * is \c false. The returned object's text() is undefined.
459 virtual ExprValue* parsePrintExpr(const char* output, bool wantErrorValue) = 0;
462 * Parses the output of the DCcd command.
463 * @return false if the message is an error message.
465 virtual bool parseChangeWD(const char* output, QString& message) = 0;
468 * Parses the output of the DCexecutable command.
469 * @return false if an error occured.
471 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
474 * Parses the output of the DCcorefile command.
475 * @return false if the core file was not loaded successfully.
477 virtual bool parseCoreFile(const char* output) = 0;
479 enum StopFlags {
480 SFrefreshSource = 1, /* refresh of source code is needed */
481 SFrefreshBreak = 2, /* refresh breakpoints */
482 SFrefreshThreads = 4, /* refresh thread list */
483 SFprogramActive = 128 /* program remains active */
486 * Parses the output of commands that execute (a piece of) the program.
487 * @return The inclusive OR of zero or more of the StopFlags.
489 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
492 * Parses the output of the DCsharedlibs command.
494 virtual QStringList parseSharedLibs(const char* output) = 0;
497 * Parses the output of the DCfindType command.
498 * @return true if a type was found.
500 virtual bool parseFindType(const char* output, QString& type) = 0;
503 * Parses the output of the DCinforegisters command.
505 virtual std::list<RegisterInfo> parseRegisters(const char* output) = 0;
508 * Parses the output of the DCinfoline command. Returns false if the
509 * two addresses could not be found.
511 virtual bool parseInfoLine(const char* output,
512 QString& addrFrom, QString& addrTo) = 0;
515 * Parses the ouput of the DCdisassemble command.
517 virtual std::list<DisassembledCode> parseDisassemble(const char* output) = 0;
520 * Parses a memory dump. Returns an empty string if no error was found;
521 * otherwise it contains an error message.
523 virtual QString parseMemoryDump(const char* output, std::list<MemoryDump>& memdump) = 0;
526 * Parses the output of the DCsetvariable command. Returns an empty
527 * string if no error was found; otherwise it contains an error
528 * message.
530 virtual QString parseSetVariable(const char* output) = 0;
533 * Returns a value that the user can edit.
535 virtual QString editableValue(VarTree* value);
537 protected:
538 /** Removes all commands from the low-priority queue. */
539 void flushLoPriQueue();
540 /** Removes all commands from the high-priority queue. */
541 void flushHiPriQueue();
543 std::queue<CmdQueueItem*> m_hipriCmdQueue;
544 std::list<CmdQueueItem*> m_lopriCmdQueue;
546 * The active command is kept separately from other pending commands.
548 CmdQueueItem* m_activeCmd;
550 * Helper function that queues the given command string in the
551 * low-priority queue.
553 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
554 QueueMode mode);
556 * Helper function that queues the given command string in the
557 * high-priority queue.
559 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
560 bool clearLow);
561 void writeCommand();
562 virtual void commandFinished(CmdQueueItem* cmd) = 0;
564 protected:
565 /** @internal */
566 virtual int commSetupDoneC();
568 char m_prompt[10];
569 size_t m_promptMinLen;
570 char m_promptLastChar;
571 QRegExp m_promptRE;
573 // log file
574 QString m_logFileName;
575 QFile m_logFile;
577 public slots:
578 void dequeueCmdByVar(VarTree* var);
580 protected slots:
581 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
582 virtual void slotCommandRead(KProcess*);
583 virtual void slotExited(KProcess*);
585 signals:
587 * This signal is emitted when the output of a command has been fully
588 * collected and is ready to be interpreted.
590 void commandReceived(CmdQueueItem* cmd, const char* output);
593 * This signal is emitted when the debugger recognizes that a specific
594 * location in a file ought to be displayed.
596 * Gdb's --fullname option supports this for the step, next, frame, and
597 * run commands (and possibly others).
599 * @param file specifies the file; this is not necessarily a full path
600 * name, and if it is relative, you won't know relative to what, you
601 * can only guess.
602 * @param lineNo specifies the line number (0-based!) (this may be
603 * negative, in which case the file should be activated, but the line
604 * should NOT be changed).
605 * @param address specifies the exact address of the PC or is empty.
607 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
610 * This signal is emitted when a command that starts the inferior has
611 * been submitted to the debugger.
613 void inferiorRunning();
616 * This signal is emitted when all output from the debugger has been
617 * consumed and no more commands are in the queues.
619 void enterIdleState();
622 #endif // DBGDRIVER_H