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.
9 #include <qstringlist.h>
11 #include <stdlib.h> /* strtol, atoi */
20 DebuggerDriver::DebuggerDriver() :
29 m_output
= new char[m_outputAlloc
];
32 connect(this, SIGNAL(receivedStdout(KProcess
*,char*,int)),
33 SLOT(slotReceiveOutput(KProcess
*,char*,int)));
34 connect(this, SIGNAL(wroteStdin(KProcess
*)), SLOT(slotCommandRead(KProcess
*)));
35 connect(this, SIGNAL(processExited(KProcess
*)), SLOT(slotExited(KProcess
*)));
38 DebuggerDriver::~DebuggerDriver()
45 int DebuggerDriver::commSetupDoneC()
47 TRACE(__PRETTY_FUNCTION__
);
49 if (!KProcess::commSetupDoneC())
53 return dup2(STDOUT_FILENO
, STDERR_FILENO
) != -1;
57 bool DebuggerDriver::startup(QString cmdStr
)
59 // clear command queues
66 // debugger executable
68 cmdStr
= defaultInvocation();
70 QStringList cmd
= QStringList::split(' ', cmdStr
);
72 for (QStringList::iterator i
= cmd
.begin(); i
!= cmd
.end(); ++i
) {
76 if (!start(KProcess::NotifyOnExit
,
77 KProcess::Communication(KProcess::Stdin
|KProcess::Stdout
))) {
82 if (!m_logFile
.isOpen() && !m_logFileName
.isEmpty()) {
83 m_logFile
.setName(m_logFileName
);
84 m_logFile
.open(IO_WriteOnly
);
90 void DebuggerDriver::slotExited(KProcess
*)
92 static const char txt
[] = "\n====== debugger exited ======\n";
93 if (m_logFile
.isOpen()) {
94 m_logFile
.writeBlock(txt
,sizeof(txt
)-1);
105 CmdQueueItem
* DebuggerDriver::executeCmdString(DbgCommand cmd
,
106 QString cmdString
, bool clearLow
)
108 // place a new command into the high-priority queue
109 CmdQueueItem
* cmdItem
= new CmdQueueItem(cmd
, cmdString
);
110 m_hipriCmdQueue
.push(cmdItem
);
113 if (m_state
== DSrunningLow
) {
114 // take the liberty to interrupt the running command
115 m_state
= DSinterrupted
;
117 ASSERT(m_activeCmd
!= 0);
118 TRACE(QString().sprintf("interrupted the command %d",
119 (m_activeCmd
? m_activeCmd
->m_cmd
: -1)));
125 // if gdb is idle, send it the command
126 if (m_state
== DSidle
) {
127 ASSERT(m_activeCmd
== 0);
134 bool CmdQueueItem::IsEqualCmd::operator()(CmdQueueItem
* cmd
) const
136 return cmd
->m_cmd
== m_cmd
&& cmd
->m_cmdString
== m_str
;
139 CmdQueueItem
* DebuggerDriver::queueCmdString(DbgCommand cmd
,
140 QString cmdString
, QueueMode mode
)
142 // place a new command into the low-priority queue
143 std::list
<CmdQueueItem
*>::iterator i
;
144 CmdQueueItem
* cmdItem
= 0;
146 case QMoverrideMoreEqual
:
148 // check whether gdb is currently processing this command
149 if (m_activeCmd
!= 0 &&
150 m_activeCmd
->m_cmd
== cmd
&& m_activeCmd
->m_cmdString
== cmdString
)
154 // check whether there is already the same command in the queue
155 i
= find_if(m_lopriCmdQueue
.begin(), m_lopriCmdQueue
.end(), CmdQueueItem::IsEqualCmd(cmd
, cmdString
));
156 if (i
!= m_lopriCmdQueue
.end()) {
159 if (mode
== QMoverrideMoreEqual
) {
160 // All commands are equal, but some are more equal than others...
161 // put this command in front of all others
162 m_lopriCmdQueue
.erase(i
);
163 m_lopriCmdQueue
.push_front(cmdItem
);
166 } // else none found, so add it
169 cmdItem
= new CmdQueueItem(cmd
, cmdString
);
170 m_lopriCmdQueue
.push_back(cmdItem
);
173 // if gdb is idle, send it the command
174 if (m_state
== DSidle
) {
175 ASSERT(m_activeCmd
== 0);
182 // dequeue a pending command, make it the active one and send it to gdb
183 void DebuggerDriver::writeCommand()
185 // ASSERT(m_activeCmd == 0);
186 assert(m_activeCmd
== 0);
188 // first check the high-priority queue - only if it is empty
189 // use a low-priority command.
191 DebuggerState newState
= DScommandSent
;
192 if (!m_hipriCmdQueue
.empty()) {
193 cmd
= m_hipriCmdQueue
.front();
194 m_hipriCmdQueue
.pop();
195 } else if (!m_lopriCmdQueue
.empty()) {
196 cmd
= m_lopriCmdQueue
.front();
197 m_lopriCmdQueue
.pop_front();
198 newState
= DScommandSentLow
;
201 m_state
= DSidle
; /* is necessary if command was interrupted earlier */
206 TRACE("in writeCommand: " + cmd
->m_cmdString
);
208 const char* str
= cmd
->m_cmdString
;
209 writeStdin(const_cast<char*>(str
), cmd
->m_cmdString
.length());
211 // write also to log file
212 if (m_logFile
.isOpen()) {
213 m_logFile
.writeBlock(str
, cmd
->m_cmdString
.length());
220 void DebuggerDriver::flushLoPriQueue()
222 while (!m_lopriCmdQueue
.empty()) {
223 delete m_lopriCmdQueue
.back();
224 m_lopriCmdQueue
.pop_back();
228 void DebuggerDriver::flushHiPriQueue()
230 while (!m_hipriCmdQueue
.empty()) {
231 delete m_hipriCmdQueue
.front();
232 m_hipriCmdQueue
.pop();
236 void DebuggerDriver::flushCommands(bool hipriOnly
)
244 void DebuggerDriver::slotCommandRead(KProcess
*)
246 TRACE(__PRETTY_FUNCTION__
);
248 // there must be an active command which is not yet commited
249 ASSERT(m_state
== DScommandSent
|| m_state
== DScommandSentLow
);
250 ASSERT(m_activeCmd
!= 0);
251 ASSERT(!m_activeCmd
->m_committed
);
253 // commit the command
254 m_activeCmd
->m_committed
= true;
256 // now the debugger is officially working on the command
257 m_state
= m_state
== DScommandSent
? DSrunning
: DSrunningLow
;
259 // set the flag that reflects whether the program is really running
260 switch (m_activeCmd
->m_cmd
) {
261 case DCrun
: case DCcont
: case DCnext
: case DCstep
: case DCfinish
: case DCuntil
:
262 emit
inferiorRunning();
268 // re-receive delayed output
269 while (!m_delayedOutput
.empty()) {
270 QByteArray delayed
= m_delayedOutput
.front();
271 m_delayedOutput
.pop();
272 slotReceiveOutput(0, const_cast<char*>(delayed
.data()), delayed
.size());
276 void DebuggerDriver::slotReceiveOutput(KProcess
*, char* buffer
, int buflen
)
279 * The debugger should be running (processing a command) at this point.
280 * If it is not, it is still idle because we haven't received the
281 * wroteStdin signal yet, in which case there must be an active command
282 * which is not commited.
284 if (m_state
== DScommandSent
|| m_state
== DScommandSentLow
) {
285 ASSERT(m_activeCmd
!= 0);
286 ASSERT(!m_activeCmd
->m_committed
);
288 * We received output before we got signal wroteStdin. Collect this
289 * output, it will be re-sent by commandRead when it gets the
290 * acknowledgment for the uncommitted command.
292 m_delayedOutput
.push(QByteArray().duplicate(buffer
, buflen
));
295 // write to log file (do not log delayed output - it would appear twice)
296 if (m_logFile
.isOpen()) {
297 m_logFile
.writeBlock(buffer
, buflen
);
302 * gdb sometimes produces stray output while it's idle. This happens if
303 * it receives a signal, most prominently a SIGCONT after a SIGTSTP:
304 * The user haltet kdbg with Ctrl-Z, then continues it with "fg", which
305 * also continues gdb, which repeats the prompt!
307 if (m_activeCmd
== 0 && m_state
!= DSinterrupted
) {
309 TRACE("ignoring stray output: " + QString::fromLatin1(buffer
, buflen
));
312 ASSERT(m_state
== DSrunning
|| m_state
== DSrunningLow
|| m_state
== DSinterrupted
);
313 ASSERT(m_activeCmd
!= 0 || m_state
== DSinterrupted
);
315 // collect output until next prompt string is found
318 if (m_outputLen
+ buflen
>= m_outputAlloc
) {
320 * Must enlarge m_output: double it. Note: That particular
321 * sequence of commandes here ensures exception safety.
323 int newSize
= m_outputAlloc
* 2;
324 char* newBuf
= new char[newSize
];
325 memcpy(newBuf
, m_output
, m_outputLen
);
328 m_outputAlloc
= newSize
;
330 memcpy(m_output
+m_outputLen
, buffer
, buflen
);
331 m_outputLen
+= buflen
;
332 m_output
[m_outputLen
] = '\0';
334 // check for a prompt
335 int promptStart
= findPrompt(m_output
, m_outputLen
);
336 if (promptStart
>= 0)
340 // terminate output before the prompt
341 m_output
[promptStart
] = '\0';
344 * We've got output for the active command. But if it was
345 * interrupted, ignore it.
347 if (m_state
!= DSinterrupted
) {
349 * m_state shouldn't be DSidle while we are parsing the output
350 * so that all commands produced by parse() go into the queue
351 * instead of being written to gdb immediately.
353 ASSERT(m_state
!= DSidle
);
354 CmdQueueItem
* cmd
= m_activeCmd
;
356 commandFinished(cmd
);
363 // also clear delayed output if interrupted
364 if (m_state
== DSinterrupted
) {
365 m_delayedOutput
= std::queue
<QByteArray
>();
369 * We parsed some output successfully. Unless there's more delayed
370 * output, the debugger must be idle now, so send down the next
373 if (m_delayedOutput
.empty()) {
374 if (m_hipriCmdQueue
.empty() && m_lopriCmdQueue
.empty()) {
375 // no pending commands
377 emit
enterIdleState();
385 void DebuggerDriver::dequeueCmdByVar(VarTree
* var
)
390 std::list
<CmdQueueItem
*>::iterator i
= m_lopriCmdQueue
.begin();
391 while (i
!= m_lopriCmdQueue
.end()) {
392 if ((*i
)->m_expr
!= 0 && var
->isAncestorEq((*i
)->m_expr
)) {
393 // this is indeed a critical command; delete it
394 TRACE("removing critical lopri-cmd: " + (*i
)->m_cmdString
);
396 m_lopriCmdQueue
.erase(i
++);
403 QString
DebuggerDriver::editableValue(VarTree
* value
)
405 // by default, let the user edit what is visible
406 return value
->value();
410 StackFrame::~StackFrame()
416 DbgAddr::DbgAddr(const QString
& aa
) :
423 * We strip off the leading 0x and any leading zeros.
425 void DbgAddr::cleanAddr()
430 while (a
[0] == '0' || a
[0] == 'x') {
435 void DbgAddr::operator=(const QString
& aa
)
442 /* Re-attach 0x in front of the address */
443 QString
DbgAddr::asString() const
451 bool operator==(const DbgAddr
& a1
, const DbgAddr
& a2
)
453 return QString::compare(a1
.a
, a2
.a
) == 0;
456 bool operator>(const DbgAddr
& a1
, const DbgAddr
& a2
)
458 if (a1
.a
.length() > a2
.a
.length())
460 if (a1
.a
.length() < a2
.a
.length())
462 return QString::compare(a1
.a
, a2
.a
) > 0;
466 Breakpoint::Breakpoint() :
476 #include "dbgdriver.moc"