3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
21 * A type representing an address.
27 DbgAddr(const QString
& aa
);
28 DbgAddr(const DbgAddr
& src
) : a(src
.a
) { }
29 void operator=(const QString
& aa
);
30 void operator=(const DbgAddr
& src
) { a
= src
.a
; }
31 QString
asString() const;
32 bool isEmpty() const { return a
.isEmpty(); }
36 bool operator==(const DbgAddr
& a1
, const DbgAddr
& a2
);
37 bool operator>(const DbgAddr
& a1
, const DbgAddr
& a2
);
62 DCuntil
, /* line number is zero-based! */
65 DCbreakline
, /* line number is zero-based! */
66 DCtbreakline
, /* line number is zero-based! */
86 RDNstdin
= 0x1, /* redirect stdin to /dev/null */
87 RDNstdout
= 0x2, /* redirect stdout to /dev/null */
88 RDNstderr
= 0x4 /* redirect stderr to /dev/null */
92 * Debugger commands are placed in a queue. Only one command at a time is
93 * sent down to the debugger. All other commands in the queue are retained
94 * until the sent command has been processed by gdb. The debugger tells us
95 * that it's done with the command by sending the prompt. The output of the
96 * debugger is parsed at that time. Then, if more commands are in the
97 * queue, the next one is sent to the debugger.
103 bool m_committed
; /* just a debugging aid */
104 // remember which expression when printing an expression
107 // remember file position
110 // whether command was emitted due to direct user request (only set when relevant)
113 CmdQueueItem(DbgCommand cmd
, const QString
& str
) :
125 * The information about a breakpoint that is parsed from the list of
130 int id
; /* gdb's number */
134 DbgAddr address
; /* exact address of breakpoint */
135 QString condition
; /* condition as printed by gdb */
136 int ignoreCount
; /* ignore next that may hits */
137 int hitCount
; /* as reported by gdb */
138 // the following items repeat the location, but in a better usable way
140 int lineNo
; /* zero-based line number */
144 * Information about a stack frame.
149 int lineNo
; /* zero-based line number */
150 DbgAddr address
; /* exact address of PC */
154 * The information about a stack frame as parsed from the backtrace.
156 struct StackFrame
: FrameInfo
159 VarTree
* var
; /* more information if non-zero */
160 StackFrame() : var(0) { }
165 * The information about a thread as parsed from the threads list.
167 struct ThreadInfo
: FrameInfo
169 int id
; /* gdb's number */
170 QString threadName
; /* the SYSTAG */
171 QString function
; /* where thread is halted */
172 bool hasFocus
; /* the thread whose stack we are watching */
176 * Register information
182 QString cookedValue
; /* may be empty */
188 struct DisassembledCode
195 * This is an abstract base class for debugger process.
197 * This class represents the debugger program. It provides the low-level
198 * interface to the commandline debugger. As such it implements the
199 * commands and parses the output.
201 class DebuggerDriver
: public KProcess
206 virtual ~DebuggerDriver() = 0;
208 virtual QString
driverName() const = 0;
210 * Returns the default command string to invoke the debugger driver.
212 virtual QString
defaultInvocation() const = 0;
214 virtual bool startup(QString cmdStr
);
215 void dequeueCmdByVar(VarTree
* var
);
216 void setLogFileName(const QString
& fname
) { m_logFileName
= fname
; }
222 DSidle
, /* gdb waits for input */
223 DSinterrupted
, /* a command was interrupted */
224 DSrunningLow
, /* gdb is running a low-priority command */
225 DSrunning
, /* gdb waits for program */
226 DScommandSent
, /* command has been sent, we wait for wroteStdin signal */
227 DScommandSentLow
/* low-prioritycommand has been sent */
229 DebuggerState m_state
;
232 bool isIdle() const { return m_state
== DSidle
; }
234 * Tells whether a high prority command would be executed immediately.
236 bool canExecuteImmediately() const { return m_hipriCmdQueue
.isEmpty(); }
239 char* m_output
; /* normal gdb output */
240 int m_outputLen
; /* current accumulated output */
241 int m_outputAlloc
; /* space available in m_gdbOutput */
243 typedef QString DelayedStr
;
245 typedef QCString DelayedStr
;
247 QQueue
<DelayedStr
> m_delayedOutput
; /* output colleced while we have receivedOutput */
248 /* but before signal wroteStdin arrived */
252 * Enqueues a high-priority command. High-priority commands are
253 * executed before any low-priority commands. No user interaction is
254 * possible as long as there is a high-priority command in the queue.
256 virtual CmdQueueItem
* executeCmd(DbgCommand
,
257 bool clearLow
= false) = 0;
258 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
,
259 bool clearLow
= false) = 0;
260 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg
,
261 bool clearLow
= false) = 0;
262 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
, int intArg
,
263 bool clearLow
= false) = 0;
264 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg1
, QString strArg2
,
265 bool clearLow
= false) = 0;
266 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg1
, int intArg2
,
267 bool clearLow
= false) = 0;
270 QMnormal
, /* queues the command last */
271 QMoverride
, /* removes an already queued command */
272 QMoverrideMoreEqual
/* ditto, also puts the command first in the queue */
276 * Enqueues a low-priority command. Low-priority commands are executed
277 * after any high-priority commands.
279 virtual CmdQueueItem
* queueCmd(DbgCommand
,
281 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
,
283 virtual CmdQueueItem
* queueCmd(DbgCommand
, int intArg
,
285 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
, int intArg
,
287 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg1
, QString strArg2
,
291 * Flushes the command queues.
292 * @param hipriOnly if true, only the high priority queue is flushed.
294 virtual void flushCommands(bool hipriOnly
= false);
297 * Terminates the debugger process.
299 virtual void terminate() = 0;
302 * Terminates the debugger process, but also detaches any program that
303 * it has been attached to.
305 virtual void detachAndTerminate() = 0;
308 * Interrupts the debuggee.
310 virtual void interruptInferior() = 0;
313 * Parses the output as an array of QChars.
315 virtual VarTree
* parseQCharArray(const char* output
, bool wantErrorValue
) = 0;
318 * Parses a back-trace (the output of the DCbt command).
320 virtual void parseBackTrace(const char* output
, QList
<StackFrame
>& stack
) = 0;
323 * Parses the output of the DCframe command;
324 * @param frameNo Returns the frame number.
325 * @param file Returns the source file name.
326 * @param lineNo The zero-based line number.
327 * @param address Returns the exact address.
328 * @return false if the frame could not be parsed successfully. The
329 * output values are undefined in this case.
331 virtual bool parseFrameChange(const char* output
, int& frameNo
,
332 QString
& file
, int& lineNo
, DbgAddr
& address
) = 0;
335 * Parses a list of breakpoints.
336 * @param output The output of the debugger.
337 * @param brks The list of new #Breakpoint objects. The list
338 * must initially be empty.
339 * @return False if there was an error before the first breakpoint
340 * was found. Even if true is returned, #brks may be empty.
342 virtual bool parseBreakList(const char* output
, QList
<Breakpoint
>& brks
) = 0;
345 * Parses a list of threads.
346 * @param output The output of the debugger.
347 * @param threads The list of new #ThreadInfo objects. The list
348 * must initially be empty.
349 * @return False if there was an error before the first thread entry
350 * was found. Even if true is returned, #threads may be empty.
352 virtual bool parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
) = 0;
355 * Parses the output when the program stops to see whether this it
356 * stopped due to a breakpoint.
357 * @param output The output of the debugger.
358 * @param id Returns the breakpoint id.
359 * @param file Returns the file name in which the breakpoint is.
360 * @param lineNo Returns he zero-based line number of the breakpoint.
361 * @return False if there was no breakpoint.
363 virtual bool parseBreakpoint(const char* output
, int& id
,
364 QString
& file
, int& lineNo
) = 0;
367 * Parses the output of the DCinfolocals command.
368 * @param output The output of the debugger.
369 * @param newVars Receives the parsed variable values. The values are
370 * simply append()ed to the supplied list.
372 virtual void parseLocals(const char* output
, QList
<VarTree
>& newVars
) = 0;
375 * Parses the output of a DCprint or DCprintStruct command.
376 * @param output The output of the debugger.
377 * @param wantErrorValue Specifies whether the error message should be
378 * provided as the value of a NKplain variable. If this is false,
379 * #var will be 0 if the printed value is an error message.
380 * @param var Returns the variable value. It is set to 0 if there was
381 * a parse error or if the output is an error message and wantErrorValue
382 * is false. If it is not 0, #var->text() will return junk and must be
383 * set using #var->setText().
384 * @return false if the output is an error message. Even if true is
385 * returned, #var might still be 0 (due to a parse error).
387 virtual bool parsePrintExpr(const char* output
, bool wantErrorValue
,
391 * Parses the output of the DCcd command.
392 * @return false if the message is an error message.
394 virtual bool parseChangeWD(const char* output
, QString
& message
) = 0;
397 * Parses the output of the DCexecutable command.
398 * @return false if an error occured.
400 virtual bool parseChangeExecutable(const char* output
, QString
& message
) = 0;
403 * Parses the output of the DCcorefile command.
404 * @return false if the core file was not loaded successfully.
406 virtual bool parseCoreFile(const char* output
) = 0;
409 SFrefreshSource
= 1, /* refresh of source code is needed */
410 SFrefreshBreak
= 2, /* refresh breakpoints */
411 SFrefreshThreads
= 4, /* refresh thread list */
412 SFprogramActive
= 128 /* program remains active */
415 * Parses the output of commands that execute (a piece of) the program.
416 * @return The inclusive OR of zero or more of the StopFlags.
418 virtual uint
parseProgramStopped(const char* output
, QString
& message
) = 0;
421 * Parses the output of the DCsharedlibs command.
423 virtual void parseSharedLibs(const char* output
, QStrList
& shlibs
) = 0;
426 * Parses the output of the DCfindType command.
427 * @return true if a type was found.
429 virtual bool parseFindType(const char* output
, QString
& type
) = 0;
432 * Parses the output of the DCinforegisters command.
434 virtual void parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
) = 0;
437 * Parses the output of the DCinfoline command. Returns false if the
438 * two addresses could not be found.
440 virtual bool parseInfoLine(const char* output
,
441 QString
& addrFrom
, QString
& addrTo
) = 0;
444 * Parses the ouput of the DCdisassemble command.
446 virtual void parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
) = 0;
449 /** Removes all commands from the low-priority queue. */
450 void flushLoPriQueue();
451 /** Removes all commands from the high-priority queue. */
452 void flushHiPriQueue();
454 QQueue
<CmdQueueItem
> m_hipriCmdQueue
;
455 QList
<CmdQueueItem
> m_lopriCmdQueue
;
457 * The active command is kept separately from other pending commands.
459 CmdQueueItem
* m_activeCmd
;
461 * Helper function that queues the given command string in the
462 * low-priority queue.
464 CmdQueueItem
* queueCmdString(DbgCommand cmd
, QString cmdString
,
467 * Helper function that queues the given command string in the
468 * high-priority queue.
470 CmdQueueItem
* executeCmdString(DbgCommand cmd
, QString cmdString
,
473 virtual void commandFinished(CmdQueueItem
* cmd
) = 0;
477 virtual int commSetupDoneC();
481 char m_promptLastChar
;
484 QString m_logFileName
;
488 void slotReceiveOutput(KProcess
*, char* buffer
, int buflen
);
489 void slotCommandRead(KProcess
*);
490 void slotExited(KProcess
*);
494 * This signal is emitted when the output of a command has been fully
495 * collected and is ready to be interpreted.
497 void commandReceived(CmdQueueItem
* cmd
, const char* output
);
500 * This signal is emitted when the debugger recognizes that a specific
501 * location in a file ought to be displayed.
503 * Gdb's --fullname option supports this for the step, next, frame, and
504 * run commands (and possibly others).
506 * @param file specifies the file; this is not necessarily a full path
507 * name, and if it is relative, you won't know relative to what, you
509 * @param lineNo specifies the line number (0-based!) (this may be
510 * negative, in which case the file should be activated, but the line
511 * should NOT be changed).
512 * @param address specifies the exact address of the PC or is empty.
514 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
517 * This signal is emitted when a command that starts the inferior has
518 * been submitted to the debugger.
520 void inferiorRunning();
523 * This signal is emitted when all output from the debugger has been
524 * consumed and no more commands are in the queues.
526 void enterIdleState();
529 #endif // DBGDRIVER_H