3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
11 #include "pgmsettings.h"
12 #include "programconfig.h"
15 #include <qfileinfo.h>
17 #include <qstringlist.h>
20 #include <klocale.h> /* i18n */
21 #include <kmessagebox.h>
23 #include <stdlib.h> /* strtol, atoi */
25 #include <unistd.h> /* sleep(3) */
30 KDebugger::KDebugger(QWidget
* parent
,
33 QListBox
* backtrace
) :
34 QObject(parent
, "debugger"),
36 m_memoryFormat(MDTword
| MDThex
),
37 m_haveExecutable(false),
38 m_programActive(false),
39 m_programRunning(false),
40 m_sharedLibsListed(false),
44 m_localVariables(*localVars
),
45 m_watchVariables(*watchVars
),
46 m_btWindow(*backtrace
)
48 m_envVars
.setAutoDelete(true);
49 m_brkpts
.setAutoDelete(true);
51 connect(&m_localVariables
, SIGNAL(expanded(QListViewItem
*)),
52 SLOT(slotExpanding(QListViewItem
*)));
53 connect(&m_watchVariables
, SIGNAL(expanded(QListViewItem
*)),
54 SLOT(slotExpanding(QListViewItem
*)));
55 connect(&m_localVariables
, SIGNAL(editValueCommitted(VarTree
*, const QString
&)),
56 SLOT(slotValueEdited(VarTree
*, const QString
&)));
57 connect(&m_watchVariables
, SIGNAL(editValueCommitted(VarTree
*, const QString
&)),
58 SLOT(slotValueEdited(VarTree
*, const QString
&)));
60 connect(&m_btWindow
, SIGNAL(highlighted(int)), SLOT(gotoFrame(int)));
65 KDebugger::~KDebugger()
67 if (m_programConfig
!= 0) {
68 saveProgramSettings();
69 m_programConfig
->sync();
70 delete m_programConfig
;
77 void KDebugger::saveSettings(KConfig
* /*config*/)
81 void KDebugger::restoreSettings(KConfig
* /*config*/)
86 //////////////////////////////////////////////////////////////////////
89 const char GeneralGroup
[] = "General";
90 const char DebuggerCmdStr
[] = "DebuggerCmdStr";
91 const char TTYLevelEntry
[] = "TTYLevel";
92 const char KDebugger::DriverNameEntry
[] = "DriverName";
94 bool KDebugger::debugProgram(const QString
& name
,
95 DebuggerDriver
* driver
)
97 if (m_d
!= 0 && m_d
->isRunning())
99 QApplication::setOverrideCursor(waitCursor
);
103 QApplication::restoreOverrideCursor();
105 if (m_d
->isRunning() || m_haveExecutable
) {
106 /* timed out! We can't really do anything useful now */
107 TRACE("timed out while waiting for gdb to die!");
114 // wire up the driver
115 connect(driver
, SIGNAL(activateFileLine(const QString
&,int,const DbgAddr
&)),
116 this, SIGNAL(activateFileLine(const QString
&,int,const DbgAddr
&)));
117 connect(driver
, SIGNAL(processExited(KProcess
*)), SLOT(gdbExited(KProcess
*)));
118 connect(driver
, SIGNAL(commandReceived(CmdQueueItem
*,const char*)),
119 SLOT(parse(CmdQueueItem
*,const char*)));
120 connect(driver
, SIGNAL(wroteStdin(KProcess
*)), SIGNAL(updateUI()));
121 connect(driver
, SIGNAL(inferiorRunning()), SLOT(slotInferiorRunning()));
122 connect(driver
, SIGNAL(enterIdleState()), SLOT(backgroundUpdate()));
123 connect(driver
, SIGNAL(enterIdleState()), SIGNAL(updateUI()));
124 connect(&m_localVariables
, SIGNAL(removingItem(VarTree
*)),
125 driver
, SLOT(dequeueCmdByVar(VarTree
*)));
126 connect(&m_watchVariables
, SIGNAL(removingItem(VarTree
*)),
127 driver
, SLOT(dequeueCmdByVar(VarTree
*)));
129 // create the program settings object
130 openProgramConfig(name
);
132 // get debugger command from per-program settings
133 if (m_programConfig
!= 0) {
134 m_programConfig
->setGroup(GeneralGroup
);
135 m_debuggerCmd
= readDebuggerCmd();
136 // get terminal emulation level
137 m_ttyLevel
= TTYLevel(m_programConfig
->readNumEntry(TTYLevelEntry
, ttyFull
));
139 // the rest is read in later in the handler of DCexecutable
143 if (!startDriver()) {
144 TRACE("startDriver failed");
149 TRACE("before file cmd");
150 m_d
->executeCmd(DCexecutable
, name
);
154 if (!m_remoteDevice
.isEmpty()) {
155 m_d
->executeCmd(DCtargetremote
, m_remoteDevice
);
156 m_d
->queueCmd(DCbt
, DebuggerDriver::QMoverride
);
157 m_d
->queueCmd(DCframe
, 0, DebuggerDriver::QMnormal
);
158 m_programActive
= true;
159 m_haveExecutable
= true;
162 // create a type table
163 m_typeTable
= new ProgramTypeTable
;
164 m_sharedLibsListed
= false;
171 void KDebugger::shutdown()
173 // shut down debugger driver
174 if (m_d
!= 0 && m_d
->isRunning())
180 void KDebugger::useCoreFile(QString corefile
, bool batch
)
182 m_corefile
= corefile
;
184 CmdQueueItem
* cmd
= loadCoreFile();
185 cmd
->m_byUser
= true;
189 void KDebugger::setAttachPid(const QString
& pid
)
194 void KDebugger::programRun()
199 // when program is active, but not a core file, continue
200 // otherwise run the program
201 if (m_programActive
&& m_corefile
.isEmpty()) {
202 // gdb command: continue
203 m_d
->executeCmd(DCcont
, true);
206 m_d
->executeCmd(DCrun
, true);
207 m_corefile
= QString();
208 m_programActive
= true;
210 m_programRunning
= true;
213 void KDebugger::attachProgram(const QString
& pid
)
219 TRACE("Attaching to " + m_attachedPid
);
220 m_d
->executeCmd(DCattach
, m_attachedPid
);
221 m_programActive
= true;
222 m_programRunning
= true;
225 void KDebugger::programRunAgain()
227 if (canSingleStep()) {
228 m_d
->executeCmd(DCrun
, true);
229 m_corefile
= QString();
230 m_programRunning
= true;
234 void KDebugger::programStep()
236 if (canSingleStep()) {
237 m_d
->executeCmd(DCstep
, true);
238 m_programRunning
= true;
242 void KDebugger::programNext()
244 if (canSingleStep()) {
245 m_d
->executeCmd(DCnext
, true);
246 m_programRunning
= true;
250 void KDebugger::programStepi()
252 if (canSingleStep()) {
253 m_d
->executeCmd(DCstepi
, true);
254 m_programRunning
= true;
258 void KDebugger::programNexti()
260 if (canSingleStep()) {
261 m_d
->executeCmd(DCnexti
, true);
262 m_programRunning
= true;
266 void KDebugger::programFinish()
268 if (canSingleStep()) {
269 m_d
->executeCmd(DCfinish
, true);
270 m_programRunning
= true;
274 void KDebugger::programKill()
276 if (haveExecutable() && isProgramActive()) {
277 if (m_programRunning
) {
278 m_d
->interruptInferior();
280 // this is an emergency command; flush queues
281 m_d
->flushCommands(true);
282 m_d
->executeCmd(DCkill
, true);
286 bool KDebugger::runUntil(const QString
& fileName
, int lineNo
)
288 if (isReady() && m_programActive
&& !m_programRunning
) {
289 // strip off directory part of file name
290 QString file
= fileName
;
291 int offset
= file
.findRev("/");
293 file
.remove(0, offset
+1);
295 m_d
->executeCmd(DCuntil
, file
, lineNo
, true);
296 m_programRunning
= true;
303 void KDebugger::programBreak()
305 if (m_haveExecutable
&& m_programRunning
) {
306 m_d
->interruptInferior();
310 void KDebugger::programArgs(QWidget
* parent
)
312 if (m_haveExecutable
) {
313 QStringList allOptions
= m_d
->boolOptionList();
314 PgmArgs
dlg(parent
, m_executable
, m_envVars
, allOptions
);
315 dlg
.setArgs(m_programArgs
);
316 dlg
.setWd(m_programWD
);
317 dlg
.setOptions(m_boolOptions
);
319 updateProgEnvironment(dlg
.args(), dlg
.wd(),
320 dlg
.envVars(), dlg
.options());
325 void KDebugger::programSettings(QWidget
* parent
)
327 if (!m_haveExecutable
)
330 ProgramSettings
dlg(parent
, m_executable
);
332 dlg
.m_chooseDriver
.setDebuggerCmd(m_debuggerCmd
);
333 dlg
.m_output
.setTTYLevel(m_ttyLevel
);
335 if (dlg
.exec() == QDialog::Accepted
)
337 m_debuggerCmd
= dlg
.m_chooseDriver
.debuggerCmd();
338 m_ttyLevel
= TTYLevel(dlg
.m_output
.ttyLevel());
342 bool KDebugger::setBreakpoint(QString file
, int lineNo
,
343 const DbgAddr
& address
, bool temporary
)
349 Breakpoint
* bp
= breakpointByFilePos(file
, lineNo
, address
);
353 * No such breakpoint, so set a new one. If we have an address, we
354 * set the breakpoint exactly there. Otherwise we use the file name
357 Breakpoint
* bp
= new Breakpoint
;
358 bp
->temporary
= temporary
;
360 if (address
.isEmpty())
367 bp
->address
= address
;
369 setBreakpoint(bp
, false);
374 * If the breakpoint is disabled, enable it; if it's enabled,
375 * delete that breakpoint.
378 deleteBreakpoint(bp
);
380 enableDisableBreakpoint(bp
);
386 void KDebugger::setBreakpoint(Breakpoint
* bp
, bool queueOnly
)
389 if (!bp
->text
.isEmpty())
392 * The breakpoint was set using the text box in the breakpoint
393 * list. This is the only way in which watchpoints are set.
395 if (bp
->type
== Breakpoint::watchpoint
) {
396 cmd
= m_d
->executeCmd(DCwatchpoint
, bp
->text
);
398 cmd
= m_d
->executeCmd(DCbreaktext
, bp
->text
);
401 else if (bp
->address
.isEmpty())
403 // strip off directory part of file name
404 QString file
= bp
->fileName
;
405 int offset
= file
.findRev("/");
407 file
.remove(0, offset
+1);
410 cmd
= m_d
->queueCmd(bp
->temporary
? DCtbreakline
: DCbreakline
,
411 file
, bp
->lineNo
, DebuggerDriver::QMoverride
);
413 cmd
= m_d
->executeCmd(bp
->temporary
? DCtbreakline
: DCbreakline
,
420 cmd
= m_d
->queueCmd(bp
->temporary
? DCtbreakaddr
: DCbreakaddr
,
421 bp
->address
.asString(), DebuggerDriver::QMoverride
);
423 cmd
= m_d
->executeCmd(bp
->temporary
? DCtbreakaddr
: DCbreakaddr
,
424 bp
->address
.asString());
427 cmd
->m_brkpt
= bp
; // used in newBreakpoint()
430 bool KDebugger::enableDisableBreakpoint(QString file
, int lineNo
,
431 const DbgAddr
& address
)
433 Breakpoint
* bp
= breakpointByFilePos(file
, lineNo
, address
);
434 return bp
== 0 || enableDisableBreakpoint(bp
);
437 bool KDebugger::enableDisableBreakpoint(Breakpoint
* bp
)
440 * Toggle enabled/disabled state.
442 * The driver is not bothered if we are modifying an orphaned
445 if (!bp
->isOrphaned()) {
446 if (!canChangeBreakpoints()) {
449 m_d
->executeCmd(bp
->enabled
? DCdisable
: DCenable
, bp
->id
);
451 bp
->enabled
= !bp
->enabled
;
452 emit
breakpointsChanged();
457 bool KDebugger::conditionalBreakpoint(Breakpoint
* bp
,
458 const QString
& condition
,
462 * Change the condition and ignore count.
464 * The driver is not bothered if we are removing an orphaned
468 if (!bp
->isOrphaned()) {
469 if (!canChangeBreakpoints()) {
473 bool changed
= false;
475 if (bp
->condition
!= condition
) {
477 m_d
->executeCmd(DCcondition
, condition
, bp
->id
);
480 if (bp
->ignoreCount
!= ignoreCount
) {
481 // change ignore count
482 m_d
->executeCmd(DCignore
, bp
->id
, ignoreCount
);
487 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverride
);
490 bp
->condition
= condition
;
491 bp
->ignoreCount
= ignoreCount
;
492 emit
breakpointsChanged();
497 bool KDebugger::deleteBreakpoint(Breakpoint
* bp
)
500 * Remove the breakpoint.
502 * The driver is not bothered if we are removing an orphaned
505 if (!bp
->isOrphaned()) {
506 if (!canChangeBreakpoints()) {
509 m_d
->executeCmd(DCdelete
, bp
->id
);
511 // move the last entry to bp's slot and shorten the list
512 int i
= m_brkpts
.findRef(bp
);
513 m_brkpts
.insert(i
, m_brkpts
.take(m_brkpts
.size()-1));
514 m_brkpts
.resize(m_brkpts
.size()-1);
515 emit
breakpointsChanged();
520 bool KDebugger::canSingleStep()
522 return isReady() && m_programActive
&& !m_programRunning
;
525 bool KDebugger::canChangeBreakpoints()
527 return isReady() && !m_programRunning
;
530 bool KDebugger::canStart()
532 return isReady() && !m_programActive
;
535 bool KDebugger::isReady() const
537 return m_haveExecutable
&&
538 m_d
!= 0 && m_d
->canExecuteImmediately();
541 bool KDebugger::isIdle() const
543 return m_d
== 0 || m_d
->isIdle();
547 //////////////////////////////////////////////////////////
550 bool KDebugger::startDriver()
552 emit
debuggerStarting(); /* must set m_inferiorTerminal */
555 * If the per-program command string is empty, use the global setting
556 * (which might also be empty, in which case the driver uses its
559 m_explicitKill
= false;
560 if (!m_d
->startup(m_debuggerCmd
)) {
565 * If we have an output terminal, we use it. Otherwise we will run the
566 * program with input and output redirected to /dev/null. Other
567 * redirections are also necessary depending on the tty emulation
570 int redirect
= RDNstdin
|RDNstdout
|RDNstderr
; /* redirect everything */
571 if (!m_inferiorTerminal
.isEmpty()) {
572 switch (m_ttyLevel
) {
575 // redirect everything
577 case ttySimpleOutputOnly
:
585 m_d
->executeCmd(DCtty
, m_inferiorTerminal
, redirect
);
590 void KDebugger::stopDriver()
592 m_explicitKill
= true;
594 if (m_attachedPid
.isEmpty()) {
597 m_d
->detachAndTerminate();
601 * We MUST wait until the slot gdbExited() has been called. But to
602 * avoid a deadlock, we wait only for some certain maximum time. Should
603 * this timeout be reached, the only reasonable thing one could do then
606 kapp
->processEvents(1000); /* ideally, this will already shut it down */
607 int maxTime
= 20; /* about 20 seconds */
608 while (m_haveExecutable
&& maxTime
> 0) {
609 // give gdb time to die (and send a SIGCLD)
612 kapp
->processEvents(1000);
616 void KDebugger::gdbExited(KProcess
*)
619 * Save settings, but only if gdb has already processed "info line
620 * main", otherwise we would save an empty config file, because it
621 * isn't read in until then!
623 if (m_programConfig
!= 0) {
624 if (m_haveExecutable
) {
625 saveProgramSettings();
626 m_programConfig
->sync();
628 delete m_programConfig
;
636 if (m_explicitKill
) {
637 TRACE(m_d
->driverName() + " exited normally");
639 QString msg
= i18n("%1 exited unexpectedly.\n"
640 "Restart the session (e.g. with File|Executable).");
641 KMessageBox::error(parentWidget(), msg
.arg(m_d
->driverName()));
645 m_haveExecutable
= false;
647 m_programActive
= false;
648 m_programRunning
= false;
649 m_explicitKill
= false;
650 m_debuggerCmd
= QString(); /* use global setting at next start! */
651 m_attachedPid
= QString(); /* we are no longer attached to a process */
652 m_ttyLevel
= ttyFull
;
656 emit
updatePC(QString(), -1, DbgAddr(), 0);
659 QString
KDebugger::getConfigForExe(const QString
& name
)
662 QString pgmConfigFile
= fi
.dirPath(true);
663 if (!pgmConfigFile
.isEmpty()) {
664 pgmConfigFile
+= '/';
666 pgmConfigFile
+= ".kdbgrc." + fi
.fileName();
667 TRACE("program config file = " + pgmConfigFile
);
668 return pgmConfigFile
;
671 void KDebugger::openProgramConfig(const QString
& name
)
673 ASSERT(m_programConfig
== 0);
675 QString pgmConfigFile
= getConfigForExe(name
);
677 m_programConfig
= new ProgramConfig(pgmConfigFile
);
680 const char EnvironmentGroup
[] = "Environment";
681 const char WatchGroup
[] = "Watches";
682 const char FileVersion
[] = "FileVersion";
683 const char ProgramArgs
[] = "ProgramArgs";
684 const char WorkingDirectory
[] = "WorkingDirectory";
685 const char OptionsSelected
[] = "OptionsSelected";
686 const char Variable
[] = "Var%d";
687 const char Value
[] = "Value%d";
688 const char ExprFmt
[] = "Expr%d";
690 void KDebugger::saveProgramSettings()
692 ASSERT(m_programConfig
!= 0);
693 m_programConfig
->setGroup(GeneralGroup
);
694 m_programConfig
->writeEntry(FileVersion
, 1);
695 m_programConfig
->writeEntry(ProgramArgs
, m_programArgs
);
696 m_programConfig
->writeEntry(WorkingDirectory
, m_programWD
);
697 m_programConfig
->writeEntry(OptionsSelected
, m_boolOptions
);
698 m_programConfig
->writeEntry(DebuggerCmdStr
, m_debuggerCmd
);
699 m_programConfig
->writeEntry(TTYLevelEntry
, int(m_ttyLevel
));
702 driverName
= m_d
->driverName();
703 m_programConfig
->writeEntry(DriverNameEntry
, driverName
);
705 // write environment variables
706 m_programConfig
->deleteGroup(EnvironmentGroup
);
707 m_programConfig
->setGroup(EnvironmentGroup
);
708 QDictIterator
<EnvVar
> it
= m_envVars
;
712 for (int i
= 0; (var
= it
) != 0; ++it
, ++i
) {
713 varName
.sprintf(Variable
, i
);
714 varValue
.sprintf(Value
, i
);
715 m_programConfig
->writeEntry(varName
, it
.currentKey());
716 m_programConfig
->writeEntry(varValue
, var
->value
);
719 saveBreakpoints(m_programConfig
);
722 // first get rid of whatever was in this group
723 m_programConfig
->deleteGroup(WatchGroup
);
724 // then start a new group
725 m_programConfig
->setGroup(WatchGroup
);
726 VarTree
* item
= m_watchVariables
.firstChild();
728 for (; item
!= 0; item
= item
->nextSibling(), ++watchNum
) {
729 varName
.sprintf(ExprFmt
, watchNum
);
730 m_programConfig
->writeEntry(varName
, item
->getText());
733 // give others a chance
734 emit
saveProgramSpecific(m_programConfig
);
737 void KDebugger::restoreProgramSettings()
739 ASSERT(m_programConfig
!= 0);
740 m_programConfig
->setGroup(GeneralGroup
);
742 * We ignore file version for now we will use it in the future to
743 * distinguish different versions of this configuration file.
745 // m_debuggerCmd has been read in already
746 // m_ttyLevel has been read in already
747 QString pgmArgs
= m_programConfig
->readEntry(ProgramArgs
);
748 QString pgmWd
= m_programConfig
->readEntry(WorkingDirectory
);
749 QStringList boolOptions
= m_programConfig
->readListEntry(OptionsSelected
);
750 m_boolOptions
= QStringList();
752 // read environment variables
753 m_programConfig
->setGroup(EnvironmentGroup
);
755 QDict
<EnvVar
> pgmVars
;
759 for (int i
= 0;; ++i
) {
760 varName
.sprintf(Variable
, i
);
761 varValue
.sprintf(Value
, i
);
762 if (!m_programConfig
->hasKey(varName
)) {
763 /* entry not present, assume that we've hit them all */
766 QString name
= m_programConfig
->readEntry(varName
);
767 if (name
.isEmpty()) {
772 var
->value
= m_programConfig
->readEntry(varValue
);
773 var
->status
= EnvVar::EVnew
;
774 pgmVars
.insert(name
, var
);
777 updateProgEnvironment(pgmArgs
, pgmWd
, pgmVars
, boolOptions
);
779 restoreBreakpoints(m_programConfig
);
782 m_programConfig
->setGroup(WatchGroup
);
783 m_watchVariables
.clear();
784 for (int i
= 0;; ++i
) {
785 varName
.sprintf(ExprFmt
, i
);
786 if (!m_programConfig
->hasKey(varName
)) {
787 /* entry not present, assume that we've hit them all */
790 QString expr
= m_programConfig
->readEntry(varName
);
791 if (expr
.isEmpty()) {
792 // skip empty expressions
798 // give others a chance
799 emit
restoreProgramSpecific(m_programConfig
);
803 * Reads the debugger command line from the program settings. The config
804 * group must have been set by the caller.
806 QString
KDebugger::readDebuggerCmd()
808 QString debuggerCmd
= m_programConfig
->readEntry(DebuggerCmdStr
);
810 // always let the user confirm the debugger cmd if we are root
811 if (::geteuid() == 0)
813 if (!debuggerCmd
.isEmpty()) {
815 "The settings for this program specify "
816 "the following debugger command:\n%1\n"
817 "Shall this command be used?");
818 if (KMessageBox::warningYesNo(parentWidget(), msg
.arg(debuggerCmd
))
822 debuggerCmd
= QString();
830 * Breakpoints are saved one per group.
832 const char BPGroup
[] = "Breakpoint %d";
833 const char File
[] = "File";
834 const char Line
[] = "Line";
835 const char Text
[] = "Text";
836 const char Address
[] = "Address";
837 const char Temporary
[] = "Temporary";
838 const char Enabled
[] = "Enabled";
839 const char Condition
[] = "Condition";
841 void KDebugger::saveBreakpoints(ProgramConfig
* config
)
845 for (uint j
= 0; j
< m_brkpts
.size(); j
++) {
846 Breakpoint
* bp
= m_brkpts
[j
];
847 if (bp
->type
== Breakpoint::watchpoint
)
848 continue; /* don't save watchpoints */
849 groupName
.sprintf(BPGroup
, i
++);
851 /* remove remmants */
852 config
->deleteGroup(groupName
);
854 config
->setGroup(groupName
);
855 if (!bp
->text
.isEmpty()) {
857 * The breakpoint was set using the text box in the breakpoint
858 * list. We do not save the location by filename+line number,
859 * but instead honor what the user typed (a function name, for
860 * example, which could move between sessions).
862 config
->writeEntry(Text
, bp
->text
);
863 } else if (!bp
->fileName
.isEmpty()) {
864 config
->writeEntry(File
, bp
->fileName
);
865 config
->writeEntry(Line
, bp
->lineNo
);
867 * Addresses are hardly correct across sessions, so we don't
871 config
->writeEntry(Address
, bp
->address
.asString());
873 config
->writeEntry(Temporary
, bp
->temporary
);
874 config
->writeEntry(Enabled
, bp
->enabled
);
875 if (!bp
->condition
.isEmpty())
876 config
->writeEntry(Condition
, bp
->condition
);
877 // we do not save the ignore count
879 // delete remaining groups
880 // we recognize that a group is present if there is an Enabled entry
882 groupName
.sprintf(BPGroup
, i
);
883 config
->setGroup(groupName
);
884 if (!config
->hasKey(Enabled
)) {
885 /* group not present, assume that we've hit them all */
888 config
->deleteGroup(groupName
);
892 void KDebugger::restoreBreakpoints(ProgramConfig
* config
)
896 * We recognize the end of the list if there is no Enabled entry
899 for (int i
= 0;; i
++) {
900 groupName
.sprintf(BPGroup
, i
);
901 config
->setGroup(groupName
);
902 if (!config
->hasKey(Enabled
)) {
903 /* group not present, assume that we've hit them all */
906 Breakpoint
* bp
= new Breakpoint
;
907 bp
->fileName
= config
->readEntry(File
);
908 bp
->lineNo
= config
->readNumEntry(Line
, -1);
909 bp
->text
= config
->readEntry(Text
);
910 bp
->address
= config
->readEntry(Address
);
912 if ((bp
->fileName
.isEmpty() || bp
->lineNo
< 0) &&
913 bp
->text
.isEmpty() &&
914 bp
->address
.isEmpty())
919 bp
->enabled
= config
->readBoolEntry(Enabled
, true);
920 bp
->temporary
= config
->readBoolEntry(Temporary
, false);
921 bp
->condition
= config
->readEntry(Condition
);
924 * Add the breakpoint.
926 setBreakpoint(bp
, false);
927 // the new breakpoint is disabled or conditionalized later
928 // in newBreakpoint()
930 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverride
);
934 // parse output of command cmd
935 void KDebugger::parse(CmdQueueItem
* cmd
, const char* output
)
937 ASSERT(cmd
!= 0); /* queue mustn't be empty */
939 TRACE(QString(__PRETTY_FUNCTION__
) + " parsing " + output
);
941 switch (cmd
->m_cmd
) {
943 // the output (if any) is uninteresting
946 // there is no output
950 /* if value is empty, we see output, but we don't care */
953 /* display gdb's message in the status bar */
954 m_d
->parseChangeWD(output
, m_statusMessage
);
955 emit
updateStatusMessage();
960 if (m_d
->parseChangeExecutable(output
, m_statusMessage
))
962 // success; restore breakpoints etc.
963 if (m_programConfig
!= 0) {
964 restoreProgramSettings();
966 // load file containing main() or core file
967 if (!m_corefile
.isEmpty())
972 else if (!m_attachedPid
.isEmpty())
974 m_d
->queueCmd(DCattach
, m_attachedPid
, DebuggerDriver::QMoverride
);
975 m_programActive
= true;
976 m_programRunning
= true;
978 else if (!m_remoteDevice
.isEmpty())
984 m_d
->queueCmd(DCinfolinemain
, DebuggerDriver::QMnormal
);
986 if (!m_statusMessage
.isEmpty())
987 emit
updateStatusMessage();
989 QString msg
= m_d
->driverName() + ": " + m_statusMessage
;
990 KMessageBox::sorry(parentWidget(), msg
);
992 m_corefile
= ""; /* don't process core file */
993 m_haveExecutable
= false;
997 // in any event we have an executable at this point
998 m_haveExecutable
= true;
999 if (m_d
->parseCoreFile(output
)) {
1000 // loading a core is like stopping at a breakpoint
1001 m_programActive
= true;
1002 handleRunCommands(output
);
1003 // do not reset m_corefile
1006 QString msg
= m_d
->driverName() + ": " + QString(output
);
1007 KMessageBox::sorry(parentWidget(), msg
);
1009 // if core file was loaded from command line, revert to info line main
1010 if (!cmd
->m_byUser
) {
1011 m_d
->queueCmd(DCinfolinemain
, DebuggerDriver::QMnormal
);
1013 m_corefile
= QString(); /* core file not available any more */
1016 case DCinfolinemain
:
1017 // ignore the output, marked file info follows
1018 m_haveExecutable
= true;
1021 // parse local variables
1022 if (output
[0] != '\0') {
1023 handleLocals(output
);
1026 case DCinforegisters
:
1027 handleRegisters(output
);
1030 handleMemoryDump(output
);
1033 handleInfoLine(cmd
, output
);
1036 handleDisassemble(cmd
, output
);
1039 handleFrameChange(output
);
1043 handleBacktrace(output
);
1047 handlePrint(cmd
, output
);
1050 handlePrintDeref(cmd
, output
);
1053 m_haveExecutable
= true;
1064 handleRunCommands(output
);
1067 m_programRunning
= m_programActive
= false;
1069 emit
updatePC(QString(), -1, DbgAddr(), 0);
1077 newBreakpoint(cmd
, output
);
1082 // these commands need immediate response
1083 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverrideMoreEqual
);
1086 // note: this handler must not enqueue a command, since
1087 // DCinfobreak is used at various different places.
1088 updateBreakList(output
);
1091 handleFindType(cmd
, output
);
1094 case DCprintQStringStruct
:
1096 handlePrintStruct(cmd
, output
);
1098 case DCinfosharedlib
:
1099 handleSharedLibs(output
);
1103 // we are not interested in the output
1106 handleThreadList(output
);
1109 handleSetPC(output
);
1112 handleSetVariable(cmd
, output
);
1117 void KDebugger::backgroundUpdate()
1120 * If there are still expressions that need to be updated, then do so.
1122 if (m_programActive
)
1126 void KDebugger::handleRunCommands(const char* output
)
1128 uint flags
= m_d
->parseProgramStopped(output
, m_statusMessage
);
1129 emit
updateStatusMessage();
1131 m_programActive
= flags
& DebuggerDriver::SFprogramActive
;
1133 // refresh files if necessary
1134 if (flags
& DebuggerDriver::SFrefreshSource
) {
1135 TRACE("re-reading files");
1136 emit
executableUpdated();
1140 * Try to set any orphaned breakpoints now.
1142 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--) {
1143 if (m_brkpts
[i
]->isOrphaned()) {
1144 TRACE("re-trying brkpt loc: "+m_brkpts
[i
]->location
+
1145 " file: "+m_brkpts
[i
]->fileName
+
1146 QString().sprintf(" line: %d", m_brkpts
[i
]->lineNo
));
1147 setBreakpoint(m_brkpts
[i
], true);
1148 flags
|= DebuggerDriver::SFrefreshBreak
;
1153 * If we stopped at a breakpoint, we must update the breakpoint list
1154 * because the hit count changes. Also, if the breakpoint was temporary
1155 * it would go away now.
1157 if ((flags
& (DebuggerDriver::SFrefreshBreak
|DebuggerDriver::SFrefreshSource
)) ||
1158 stopMayChangeBreakList())
1160 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverride
);
1164 * If we haven't listed the shared libraries yet, do so. We must do
1165 * this before we emit any commands that list variables, since the type
1166 * libraries depend on the shared libraries.
1168 if (!m_sharedLibsListed
) {
1169 // must be a high-priority command!
1170 m_d
->executeCmd(DCinfosharedlib
);
1173 // get the backtrace if the program is running
1174 if (m_programActive
) {
1175 m_d
->queueCmd(DCbt
, DebuggerDriver::QMoverride
);
1177 // program finished: erase PC
1178 emit
updatePC(QString(), -1, DbgAddr(), 0);
1179 // dequeue any commands in the queues
1180 m_d
->flushCommands();
1183 /* Update threads list */
1184 if (m_programActive
&& (flags
& DebuggerDriver::SFrefreshThreads
)) {
1185 m_d
->queueCmd(DCinfothreads
, DebuggerDriver::QMoverride
);
1188 m_programRunning
= false;
1189 emit
programStopped();
1192 void KDebugger::slotInferiorRunning()
1194 m_programRunning
= true;
1197 void KDebugger::updateAllExprs()
1199 if (!m_programActive
)
1202 // retrieve local variables
1203 m_d
->queueCmd(DCinfolocals
, DebuggerDriver::QMoverride
);
1205 // retrieve registers
1206 m_d
->queueCmd(DCinforegisters
, DebuggerDriver::QMoverride
);
1208 // get new memory dump
1209 if (!m_memoryExpression
.isEmpty()) {
1210 queueMemoryDump(false);
1213 // update watch expressions
1214 VarTree
* item
= m_watchVariables
.firstChild();
1215 for (; item
!= 0; item
= item
->nextSibling()) {
1216 m_watchEvalExpr
.push_back(item
->getText());
1220 void KDebugger::updateProgEnvironment(const QString
& args
, const QString
& wd
,
1221 const QDict
<EnvVar
>& newVars
,
1222 const QStringList
& newOptions
)
1224 m_programArgs
= args
;
1225 m_d
->executeCmd(DCsetargs
, m_programArgs
);
1226 TRACE("new pgm args: " + m_programArgs
+ "\n");
1228 m_programWD
= wd
.stripWhiteSpace();
1229 if (!m_programWD
.isEmpty()) {
1230 m_d
->executeCmd(DCcd
, m_programWD
);
1231 TRACE("new wd: " + m_programWD
+ "\n");
1234 // update environment variables
1235 QDictIterator
<EnvVar
> it
= newVars
;
1237 for (; (val
= it
) != 0; ++it
) {
1238 QString var
= it
.currentKey();
1239 switch (val
->status
) {
1241 m_envVars
.insert(var
, val
);
1243 case EnvVar::EVdirty
:
1244 // the value must be in our list
1245 ASSERT(m_envVars
[var
] == val
);
1247 m_d
->executeCmd(DCsetenv
, var
, val
->value
);
1249 case EnvVar::EVdeleted
:
1250 // must be in our list
1251 ASSERT(m_envVars
[var
] == val
);
1253 m_d
->executeCmd(DCunsetenv
, var
);
1254 m_envVars
.remove(var
);
1258 case EnvVar::EVclean
:
1259 // variable not changed
1265 QStringList::ConstIterator oi
;
1266 for (oi
= newOptions
.begin(); oi
!= newOptions
.end(); ++oi
)
1268 if (m_boolOptions
.findIndex(*oi
) < 0) {
1269 // the options is currently not set, so set it
1270 m_d
->executeCmd(DCsetoption
, *oi
, 1);
1272 // option is set, no action required, but move it to the end
1273 m_boolOptions
.remove(*oi
);
1275 m_boolOptions
.append(*oi
);
1278 * Now all options that should be set are at the end of m_boolOptions.
1279 * If some options need to be unset, they are at the front of the list.
1280 * Here we unset and remove them.
1282 while (m_boolOptions
.count() > newOptions
.count()) {
1283 m_d
->executeCmd(DCsetoption
, m_boolOptions
.first(), 0);
1284 m_boolOptions
.remove(m_boolOptions
.begin());
1288 void KDebugger::handleLocals(const char* output
)
1290 // retrieve old list of local variables
1292 m_localVariables
.exprList(oldVars
);
1295 * Get local variables.
1297 QList
<ExprValue
> newVars
;
1298 parseLocals(output
, newVars
);
1301 * Clear any old VarTree item pointers, so that later we don't access
1302 * dangling pointers.
1304 m_localVariables
.clearPendingUpdates();
1307 * Match old variables against new ones.
1309 for (const char* n
= oldVars
.first(); n
!= 0; n
= oldVars
.next()) {
1310 // lookup this variable in the list of new variables
1311 ExprValue
* v
= newVars
.first();
1312 while (v
!= 0 && v
->m_name
!= n
) {
1316 // old variable not in the new variables
1317 TRACE(QString("old var deleted: ") + n
);
1318 VarTree
* v
= m_localVariables
.topLevelExprByName(n
);
1320 m_localVariables
.removeExpr(v
);
1323 // variable in both old and new lists: update
1324 TRACE(QString("update var: ") + n
);
1325 m_localVariables
.updateExpr(newVars
.current(), *m_typeTable
);
1326 // remove the new variable from the list
1331 // insert all remaining new variables
1332 while (!newVars
.isEmpty())
1334 ExprValue
* v
= newVars
.take(0);
1335 TRACE("new var: " + v
->m_name
);
1336 m_localVariables
.insertExpr(v
, *m_typeTable
);
1341 void KDebugger::parseLocals(const char* output
, QList
<ExprValue
>& newVars
)
1343 QList
<ExprValue
> vars
;
1344 m_d
->parseLocals(output
, vars
);
1346 QString origName
; /* used in renaming variables */
1347 while (vars
.count() > 0)
1349 ExprValue
* variable
= vars
.take(0);
1351 * When gdb prints local variables, those from the innermost block
1352 * come first. We run through the list of already parsed variables
1353 * to find duplicates (ie. variables that hide local variables from
1354 * a surrounding block). We keep the name of the inner variable, but
1355 * rename those from the outer block so that, when the value is
1356 * updated in the window, the value of the variable that is
1357 * _visible_ changes the color!
1360 origName
= variable
->m_name
;
1361 for (ExprValue
* v
= newVars
.first(); v
!= 0; v
= newVars
.next()) {
1362 if (variable
->m_name
== v
->m_name
) {
1363 // we found a duplicate, change name
1365 QString newName
= origName
+ " (" + QString().setNum(block
) + ")";
1366 variable
->m_name
= newName
;
1369 newVars
.append(variable
);
1373 bool KDebugger::handlePrint(CmdQueueItem
* cmd
, const char* output
)
1375 ASSERT(cmd
->m_expr
!= 0);
1377 ExprValue
* variable
= m_d
->parsePrintExpr(output
, true);
1381 // set expression "name"
1382 variable
->m_name
= cmd
->m_expr
->getText();
1385 TRACE("update expr: " + cmd
->m_expr
->getText());
1386 cmd
->m_exprWnd
->updateExpr(cmd
->m_expr
, variable
, *m_typeTable
);
1390 evalExpressions(); /* enqueue dereferenced pointers */
1395 bool KDebugger::handlePrintDeref(CmdQueueItem
* cmd
, const char* output
)
1397 ASSERT(cmd
->m_expr
!= 0);
1399 ExprValue
* variable
= m_d
->parsePrintExpr(output
, true);
1403 // set expression "name"
1404 variable
->m_name
= cmd
->m_expr
->getText();
1408 * We must insert a dummy parent, because otherwise variable's value
1409 * would overwrite cmd->m_expr's value.
1411 ExprValue
* dummyParent
= new ExprValue(variable
->m_name
, VarTree::NKplain
);
1412 dummyParent
->m_varKind
= VarTree::VKdummy
;
1413 // the name of the parsed variable is the address of the pointer
1414 QString addr
= "*" + cmd
->m_expr
->value();
1415 variable
->m_name
= addr
;
1416 variable
->m_nameKind
= VarTree::NKaddress
;
1418 dummyParent
->m_child
= variable
;
1419 // expand the first level for convenience
1420 variable
->m_initiallyExpanded
= true;
1421 TRACE("update ptr: " + cmd
->m_expr
->getText());
1422 cmd
->m_exprWnd
->updateExpr(cmd
->m_expr
, dummyParent
, *m_typeTable
);
1426 evalExpressions(); /* enqueue dereferenced pointers */
1431 // parse the output of bt
1432 void KDebugger::handleBacktrace(const char* output
)
1435 m_btWindow
.setAutoUpdate(false);
1439 QList
<StackFrame
> stack
;
1440 m_d
->parseBackTrace(output
, stack
);
1442 if (stack
.count() > 0) {
1443 StackFrame
* frm
= stack
.take(0);
1444 // first frame must set PC
1445 // note: frm->lineNo is zero-based
1446 emit
updatePC(frm
->fileName
, frm
->lineNo
, frm
->address
, frm
->frameNo
);
1451 func
= frm
->var
->m_name
;
1453 func
= frm
->fileName
+ ":" + QString().setNum(frm
->lineNo
+1);
1454 m_btWindow
.insertItem(func
);
1455 TRACE("frame " + func
+ " (" + frm
->fileName
+ ":" +
1456 QString().setNum(frm
->lineNo
+1) + ")");
1459 while ((frm
= stack
.take()) != 0);
1462 m_btWindow
.setAutoUpdate(true);
1463 m_btWindow
.repaint();
1466 void KDebugger::gotoFrame(int frame
)
1468 m_d
->executeCmd(DCframe
, frame
);
1471 void KDebugger::handleFrameChange(const char* output
)
1477 if (m_d
->parseFrameChange(output
, frameNo
, fileName
, lineNo
, address
)) {
1478 /* lineNo can be negative here if we can't find a file name */
1479 emit
updatePC(fileName
, lineNo
, address
, frameNo
);
1481 emit
updatePC(fileName
, -1, address
, frameNo
);
1485 void KDebugger::evalExpressions()
1487 // evaluate expressions in the following order:
1488 // watch expressions
1489 // pointers in local variables
1490 // pointers in watch expressions
1491 // types in local variables
1492 // types in watch expressions
1493 // struct members in local variables
1494 // struct members in watch expressions
1495 VarTree
* exprItem
= 0;
1496 if (!m_watchEvalExpr
.empty())
1498 QString expr
= m_watchEvalExpr
.front();
1499 m_watchEvalExpr
.pop_front();
1500 exprItem
= m_watchVariables
.topLevelExprByName(expr
);
1502 if (exprItem
!= 0) {
1503 CmdQueueItem
* cmd
= m_d
->queueCmd(DCprint
, exprItem
->getText(), DebuggerDriver::QMoverride
);
1504 // remember which expr this was
1505 cmd
->m_expr
= exprItem
;
1506 cmd
->m_exprWnd
= &m_watchVariables
;
1509 #define POINTER(widget) \
1511 exprItem = widget.nextUpdatePtr(); \
1512 if (exprItem != 0) goto pointer
1513 #define STRUCT(widget) \
1515 exprItem = widget.nextUpdateStruct(); \
1516 if (exprItem != 0) goto ustruct
1517 #define TYPE(widget) \
1519 exprItem = widget.nextUpdateType(); \
1520 if (exprItem != 0) goto type
1522 POINTER(m_localVariables
);
1523 POINTER(m_watchVariables
);
1524 STRUCT(m_localVariables
);
1525 STRUCT(m_watchVariables
);
1526 TYPE(m_localVariables
);
1527 TYPE(m_watchVariables
);
1534 // we have an expression to send
1535 dereferencePointer(wnd
, exprItem
, false);
1540 if (exprItem
->m_type
== 0 || exprItem
->m_type
== TypeInfo::unknownType())
1542 evalInitialStructExpression(exprItem
, wnd
, false);
1547 * Sometimes a VarTree gets registered twice for a type update. So
1548 * it may happen that it has already been updated. Hence, we ignore
1549 * it here and go on to the next task.
1551 if (exprItem
->m_type
!= 0)
1553 determineType(wnd
, exprItem
);
1557 void KDebugger::dereferencePointer(ExprWnd
* wnd
, VarTree
* exprItem
,
1560 ASSERT(exprItem
->m_varKind
== VarTree::VKpointer
);
1562 QString expr
= exprItem
->computeExpr();
1563 TRACE("dereferencing pointer: " + expr
);
1566 cmd
= m_d
->queueCmd(DCprintDeref
, expr
, DebuggerDriver::QMoverrideMoreEqual
);
1568 cmd
= m_d
->queueCmd(DCprintDeref
, expr
, DebuggerDriver::QMoverride
);
1570 // remember which expr this was
1571 cmd
->m_expr
= exprItem
;
1572 cmd
->m_exprWnd
= wnd
;
1575 void KDebugger::determineType(ExprWnd
* wnd
, VarTree
* exprItem
)
1577 ASSERT(exprItem
->m_varKind
== VarTree::VKstruct
);
1579 QString expr
= exprItem
->computeExpr();
1580 TRACE("get type of: " + expr
);
1582 cmd
= m_d
->queueCmd(DCfindType
, expr
, DebuggerDriver::QMoverride
);
1584 // remember which expr this was
1585 cmd
->m_expr
= exprItem
;
1586 cmd
->m_exprWnd
= wnd
;
1589 void KDebugger::handleFindType(CmdQueueItem
* cmd
, const char* output
)
1592 if (m_d
->parseFindType(output
, type
))
1594 ASSERT(cmd
!= 0 && cmd
->m_expr
!= 0);
1596 TypeInfo
* info
= m_typeTable
->lookup(type
);
1600 * We've asked gdb for the type of the expression in
1601 * cmd->m_expr, but it returned a name we don't know. The base
1602 * class (and member) types have been checked already (at the
1603 * time when we parsed that particular expression). Now it's
1604 * time to derive the type from the base classes as a last
1607 info
= cmd
->m_expr
->inferTypeFromBaseClass();
1608 // if we found a type through this method, register an alias
1610 TRACE("infered alias: " + type
);
1611 m_typeTable
->registerAlias(type
, info
);
1615 TRACE("unknown type "+type
);
1616 cmd
->m_expr
->m_type
= TypeInfo::unknownType();
1618 cmd
->m_expr
->m_type
= info
;
1619 /* since this node has a new type, we get its value immediately */
1620 evalInitialStructExpression(cmd
->m_expr
, cmd
->m_exprWnd
, false);
1625 evalExpressions(); /* queue more of them */
1628 void KDebugger::handlePrintStruct(CmdQueueItem
* cmd
, const char* output
)
1630 VarTree
* var
= cmd
->m_expr
;
1632 ASSERT(var
->m_varKind
== VarTree::VKstruct
);
1634 ExprValue
* partExpr
;
1635 if (cmd
->m_cmd
== DCprintQStringStruct
) {
1636 partExpr
= m_d
->parseQCharArray(output
, false, m_typeTable
->qCharIsShort());
1637 } else if (cmd
->m_cmd
== DCprintWChar
) {
1638 partExpr
= m_d
->parseQCharArray(output
, false, true);
1640 partExpr
= m_d
->parsePrintExpr(output
, false);
1644 /* we only allow simple values at the moment */
1645 partExpr
->m_child
!= 0;
1650 partValue
= "?""?""?"; // 2 question marks in a row would be a trigraph
1652 partValue
= partExpr
->m_value
;
1658 * Updating a struct value works like this: var->m_partialValue holds
1659 * the value that we have gathered so far (it's been initialized with
1660 * var->m_type->m_displayString[0] earlier). Each time we arrive here,
1661 * we append the printed result followed by the next
1662 * var->m_type->m_displayString to var->m_partialValue.
1664 * If the expression we just evaluated was a guard expression, and it
1665 * resulted in an error, we must not evaluate the real expression, but
1666 * go on to the next index. (We must still add the question marks to
1669 * Next, if this was the length expression, we still have not seen the
1670 * real expression, but the length of a QString.
1672 ASSERT(var
->m_exprIndex
>= 0 && var
->m_exprIndex
<= typeInfoMaxExpr
);
1674 if (errorValue
|| !var
->m_exprIndexUseGuard
)
1676 // add current partValue (which might be the question marks)
1677 var
->m_partialValue
+= partValue
;
1678 var
->m_exprIndex
++; /* next part */
1679 var
->m_exprIndexUseGuard
= true;
1680 var
->m_partialValue
+= var
->m_type
->m_displayString
[var
->m_exprIndex
];
1684 // this was a guard expression that succeeded
1685 // go for the real expression
1686 var
->m_exprIndexUseGuard
= false;
1689 /* go for more sub-expressions if needed */
1690 if (var
->m_exprIndex
< var
->m_type
->m_numExprs
) {
1691 /* queue a new print command with quite high priority */
1692 evalStructExpression(var
, cmd
->m_exprWnd
, true);
1696 cmd
->m_exprWnd
->updateStructValue(var
);
1698 evalExpressions(); /* enqueue dereferenced pointers */
1701 /* queues the first printStruct command for a struct */
1702 void KDebugger::evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
)
1704 var
->m_exprIndex
= 0;
1705 if (var
->m_type
!= TypeInfo::wchartType())
1707 var
->m_exprIndexUseGuard
= true;
1708 var
->m_partialValue
= var
->m_type
->m_displayString
[0];
1709 evalStructExpression(var
, wnd
, immediate
);
1713 var
->m_exprIndexUseGuard
= false;
1714 QString expr
= var
->computeExpr();
1715 CmdQueueItem
* cmd
= m_d
->queueCmd(DCprintWChar
, expr
,
1716 immediate
? DebuggerDriver::QMoverrideMoreEqual
1717 : DebuggerDriver::QMoverride
);
1718 // remember which expression this was
1720 cmd
->m_exprWnd
= wnd
;
1724 /* queues a printStruct command; var must have been initialized correctly */
1725 void KDebugger::evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
)
1727 QString base
= var
->computeExpr();
1729 if (var
->m_exprIndexUseGuard
) {
1730 exprFmt
= var
->m_type
->m_guardStrings
[var
->m_exprIndex
];
1731 if (exprFmt
.isEmpty()) {
1732 // no guard, omit it and go to expression
1733 var
->m_exprIndexUseGuard
= false;
1736 if (!var
->m_exprIndexUseGuard
) {
1737 exprFmt
= var
->m_type
->m_exprStrings
[var
->m_exprIndex
];
1741 expr
.sprintf(exprFmt
, base
.data());
1743 DbgCommand dbgCmd
= DCprintStruct
;
1744 // check if this is a QString::Data
1745 if (strncmp(expr
, "/QString::Data ", 15) == 0)
1747 if (m_typeTable
->parseQt2QStrings())
1749 expr
= expr
.mid(15, expr
.length()); /* strip off /QString::Data */
1750 dbgCmd
= DCprintQStringStruct
;
1753 * This should not happen: the type libraries should be set up
1754 * in a way that this can't happen. If this happens
1755 * nevertheless it means that, eg., kdecore was loaded but qt2
1756 * was not (only qt2 enables the QString feature).
1758 // TODO: remove this "print"; queue the next printStruct instead
1764 TRACE("evalStruct: " + expr
+ (var
->m_exprIndexUseGuard
? " // guard" : " // real"));
1765 CmdQueueItem
* cmd
= m_d
->queueCmd(dbgCmd
, expr
,
1766 immediate
? DebuggerDriver::QMoverrideMoreEqual
1767 : DebuggerDriver::QMnormal
);
1769 // remember which expression this was
1771 cmd
->m_exprWnd
= wnd
;
1774 void KDebugger::handleSharedLibs(const char* output
)
1776 // delete all known libraries
1777 m_sharedLibs
.clear();
1779 // parse the table of shared libraries
1780 m_d
->parseSharedLibs(output
, m_sharedLibs
);
1781 m_sharedLibsListed
= true;
1783 // get type libraries
1784 m_typeTable
->loadLibTypes(m_sharedLibs
);
1786 // hand over the QString data cmd
1787 m_d
->setPrintQStringDataCmd(m_typeTable
->printQStringDataCmd());
1790 CmdQueueItem
* KDebugger::loadCoreFile()
1792 return m_d
->queueCmd(DCcorefile
, m_corefile
, DebuggerDriver::QMoverride
);
1795 void KDebugger::slotExpanding(QListViewItem
* item
)
1797 VarTree
* exprItem
= static_cast<VarTree
*>(item
);
1798 if (exprItem
->m_varKind
!= VarTree::VKpointer
) {
1801 ExprWnd
* wnd
= static_cast<ExprWnd
*>(item
->listView());
1802 dereferencePointer(wnd
, exprItem
, true);
1805 // add the expression in the edit field to the watch expressions
1806 void KDebugger::addWatch(const QString
& t
)
1808 QString expr
= t
.stripWhiteSpace();
1809 // don't add a watched expression again
1810 if (expr
.isEmpty() || m_watchVariables
.topLevelExprByName(expr
) != 0)
1812 ExprValue
e(expr
, VarTree::NKplain
);
1813 m_watchVariables
.insertExpr(&e
, *m_typeTable
);
1815 // if we are boring ourselves, send down the command
1816 if (m_programActive
) {
1817 m_watchEvalExpr
.push_back(expr
);
1818 if (m_d
->isIdle()) {
1824 // delete a toplevel watch expression
1825 void KDebugger::slotDeleteWatch()
1827 // delete only allowed while debugger is idle; or else we might delete
1828 // the very expression the debugger is currently working on...
1832 VarTree
* item
= m_watchVariables
.currentItem();
1833 if (item
== 0 || !item
->isToplevelExpr())
1836 // remove the variable from the list to evaluate
1837 QStringList::iterator i
= m_watchEvalExpr
.find(item
->getText());
1838 if (i
!= m_watchEvalExpr
.end()) {
1839 m_watchEvalExpr
.erase(i
);
1841 m_watchVariables
.removeExpr(item
);
1842 // item is invalid at this point!
1845 void KDebugger::handleRegisters(const char* output
)
1847 QList
<RegisterInfo
> regs
;
1848 m_d
->parseRegisters(output
, regs
);
1850 emit
registersChanged(regs
);
1853 regs
.setAutoDelete(true);
1857 * The output of the DCbreak* commands has more accurate information about
1858 * the file and the line number.
1860 * All newly set breakpoints are inserted in the m_brkpts, even those that
1861 * were not set sucessfully. The unsuccessful breakpoints ("orphaned
1862 * breakpoints") are assigned negative ids, and they are tried to set later
1863 * when the program stops again at a breakpoint.
1865 void KDebugger::newBreakpoint(CmdQueueItem
* cmd
, const char* output
)
1867 Breakpoint
* bp
= cmd
->m_brkpt
;
1872 // if this is a new breakpoint, put it in the list
1873 bool isNew
= !m_brkpts
.contains(bp
);
1875 assert(bp
->id
== 0);
1876 int n
= m_brkpts
.size();
1877 m_brkpts
.resize(n
+1);
1878 m_brkpts
.insert(n
, bp
);
1881 // parse the output to determine success or failure
1886 if (!m_d
->parseBreakpoint(output
, id
, file
, lineNo
, address
))
1889 * Failure, the breakpoint could not be set. If this is a new
1890 * breakpoint, assign it a negative id. We look for the minimal id
1891 * of all breakpoints (that are already in the list) to get the new
1896 assert(bp
->id
== 0);
1897 for (int i
= m_brkpts
.size()-2; i
>= 0; i
--) {
1898 if (m_brkpts
[i
]->id
< bp
->id
) {
1899 bp
->id
= m_brkpts
[i
]->id
;
1908 // The breakpoint was successfully set.
1911 // this is a new or orphaned breakpoint:
1912 // set the remaining properties
1913 if (!cmd
->m_brkpt
->enabled
) {
1914 m_d
->executeCmd(DCdisable
, id
);
1916 if (!cmd
->m_brkpt
->condition
.isEmpty()) {
1917 m_d
->executeCmd(DCcondition
, cmd
->m_brkpt
->condition
, id
);
1922 bp
->fileName
= file
;
1923 bp
->lineNo
= lineNo
;
1924 if (!address
.isEmpty())
1925 bp
->address
= address
;
1928 void KDebugger::updateBreakList(const char* output
)
1931 QList
<Breakpoint
> brks
;
1932 brks
.setAutoDelete(false);
1933 m_d
->parseBreakList(output
, brks
);
1935 // merge new information into existing breakpoints
1937 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--) // decrement!
1939 // skip orphaned breakpoints
1940 if (m_brkpts
[i
]->id
< 0)
1943 for (Breakpoint
* bp
= brks
.first(); bp
!= 0; bp
= brks
.next())
1945 if (bp
->id
== m_brkpts
[i
]->id
) {
1946 // keep accurate location
1947 // except that xsldbg doesn't have a location in
1948 // the old breakpoint if it's just been set
1949 bp
->text
= m_brkpts
[i
]->text
;
1950 if (!m_brkpts
[i
]->fileName
.isEmpty()) {
1951 bp
->fileName
= m_brkpts
[i
]->fileName
;
1952 bp
->lineNo
= m_brkpts
[i
]->lineNo
;
1954 m_brkpts
.insert(i
, bp
); // old object is deleted
1959 * If we get here, this breakpoint is no longer present.
1961 * To delete the breakpoint at i, we place the last breakpoint in
1962 * the list into the slot i. This will delete the old object at i.
1963 * Then we shorten the list by one.
1965 m_brkpts
.insert(i
, m_brkpts
.take(m_brkpts
.size()-1));
1966 m_brkpts
.resize(m_brkpts
.size()-1);
1967 TRACE(QString().sprintf("deleted brkpt %d, have now %d brkpts", i
, m_brkpts
.size()));
1972 // brks may contain new breakpoints not already in m_brkpts
1973 for (const Breakpoint
* bp
= brks
.first(); bp
!= 0; bp
= brks
.next())
1976 for (uint i
= 0; i
< m_brkpts
.size(); i
++) {
1977 if (bp
->id
== m_brkpts
[i
]->id
) {
1983 int n
= m_brkpts
.size();
1984 m_brkpts
.resize(n
+1);
1985 m_brkpts
.insert(n
, bp
);
1989 emit
breakpointsChanged();
1992 // look if there is at least one temporary breakpoint
1994 bool KDebugger::stopMayChangeBreakList() const
1996 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--) {
1997 Breakpoint
* bp
= m_brkpts
[i
];
1998 if (bp
->temporary
|| bp
->type
== Breakpoint::watchpoint
)
2004 Breakpoint
* KDebugger::breakpointByFilePos(QString file
, int lineNo
,
2005 const DbgAddr
& address
)
2007 // look for exact file name match
2009 for (i
= m_brkpts
.size()-1; i
>= 0; i
--) {
2010 if (m_brkpts
[i
]->lineNo
== lineNo
&&
2011 m_brkpts
[i
]->fileName
== file
&&
2012 (address
.isEmpty() || m_brkpts
[i
]->address
== address
))
2017 // not found, so try basename
2018 // strip off directory part of file name
2019 int offset
= file
.findRev("/");
2020 file
.remove(0, offset
+1);
2022 for (i
= m_brkpts
.size()-1; i
>= 0; i
--) {
2023 // get base name of breakpoint's file
2024 QString basename
= m_brkpts
[i
]->fileName
;
2025 int offset
= basename
.findRev("/");
2027 basename
.remove(0, offset
+1);
2030 if (m_brkpts
[i
]->lineNo
== lineNo
&&
2032 (address
.isEmpty() || m_brkpts
[i
]->address
== address
))
2042 Breakpoint
* KDebugger::breakpointById(int id
)
2044 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--)
2046 if (m_brkpts
[i
]->id
== id
) {
2054 void KDebugger::slotValuePopup(const QString
& expr
)
2056 // search the local variables for a match
2057 VarTree
* v
= m_localVariables
.topLevelExprByName(expr
);
2059 // not found, check watch expressions
2060 v
= m_watchVariables
.topLevelExprByName(expr
);
2062 // try a member of 'this'
2063 v
= m_localVariables
.topLevelExprByName("this");
2065 v
= ExprWnd::ptrMemberByName(v
, expr
);
2067 // nothing found; do nothing
2073 // construct the tip
2074 QString tip
= v
->getText() + " = ";
2075 if (!v
->value().isEmpty())
2081 // no value: we use some hint
2082 switch (v
->m_varKind
) {
2083 case VarTree::VKstruct
:
2086 case VarTree::VKarray
:
2090 tip
+= "?""?""?"; // 2 question marks in a row would be a trigraph
2094 emit
valuePopup(tip
);
2097 void KDebugger::slotDisassemble(const QString
& fileName
, int lineNo
)
2099 CmdQueueItem
* cmd
= m_d
->queueCmd(DCinfoline
, fileName
, lineNo
,
2100 DebuggerDriver::QMoverrideMoreEqual
);
2101 cmd
->m_fileName
= fileName
;
2102 cmd
->m_lineNo
= lineNo
;
2105 void KDebugger::handleInfoLine(CmdQueueItem
* cmd
, const char* output
)
2107 QString addrFrom
, addrTo
;
2108 if (cmd
->m_lineNo
>= 0) {
2110 if (m_d
->parseInfoLine(output
, addrFrom
, addrTo
)) {
2111 // got the address range, now get the real code
2112 CmdQueueItem
* c
= m_d
->queueCmd(DCdisassemble
, addrFrom
, addrTo
,
2113 DebuggerDriver::QMoverrideMoreEqual
);
2114 c
->m_fileName
= cmd
->m_fileName
;
2115 c
->m_lineNo
= cmd
->m_lineNo
;
2118 QList
<DisassembledCode
> empty
;
2119 emit
disassembled(cmd
->m_fileName
, cmd
->m_lineNo
, empty
);
2122 // set program counter
2123 if (m_d
->parseInfoLine(output
, addrFrom
, addrTo
)) {
2124 // move the program counter to the start address
2125 m_d
->executeCmd(DCsetpc
, addrFrom
);
2130 void KDebugger::handleDisassemble(CmdQueueItem
* cmd
, const char* output
)
2132 QList
<DisassembledCode
> code
;
2133 code
.setAutoDelete(true);
2134 m_d
->parseDisassemble(output
, code
);
2135 emit
disassembled(cmd
->m_fileName
, cmd
->m_lineNo
, code
);
2138 void KDebugger::handleThreadList(const char* output
)
2140 QList
<ThreadInfo
> threads
;
2141 threads
.setAutoDelete(true);
2142 m_d
->parseThreadList(output
, threads
);
2143 emit
threadsChanged(threads
);
2146 void KDebugger::setThread(int id
)
2148 m_d
->queueCmd(DCthread
, id
, DebuggerDriver::QMoverrideMoreEqual
);
2151 void KDebugger::setMemoryExpression(const QString
& memexpr
)
2153 m_memoryExpression
= memexpr
;
2155 // queue the new expression
2156 if (!m_memoryExpression
.isEmpty() &&
2157 isProgramActive() &&
2158 !isProgramRunning())
2160 queueMemoryDump(true);
2164 void KDebugger::queueMemoryDump(bool immediate
)
2166 m_d
->queueCmd(DCexamine
, m_memoryExpression
, m_memoryFormat
,
2167 immediate
? DebuggerDriver::QMoverrideMoreEqual
:
2168 DebuggerDriver::QMoverride
);
2171 void KDebugger::handleMemoryDump(const char* output
)
2173 QList
<MemoryDump
> memdump
;
2174 memdump
.setAutoDelete(true);
2175 QString msg
= m_d
->parseMemoryDump(output
, memdump
);
2176 emit
memoryDumpChanged(msg
, memdump
);
2179 void KDebugger::setProgramCounter(const QString
& file
, int line
, const DbgAddr
& addr
)
2181 if (addr
.isEmpty()) {
2182 // find address of the specified line
2183 CmdQueueItem
* cmd
= m_d
->executeCmd(DCinfoline
, file
, line
);
2184 cmd
->m_lineNo
= -1; /* indicates "Set PC" UI command */
2186 // move the program counter to that address
2187 m_d
->executeCmd(DCsetpc
, addr
.asString());
2191 void KDebugger::handleSetPC(const char* /*output*/)
2193 // TODO: handle errors
2195 // now go to the top-most frame
2196 // this also modifies the program counter indicator in the UI
2200 void KDebugger::slotValueEdited(VarTree
* expr
, const QString
& text
)
2202 if (text
.simplifyWhiteSpace().isEmpty())
2203 return; /* no text entered: ignore request */
2205 ExprWnd
* wnd
= static_cast<ExprWnd
*>(expr
->listView());
2206 TRACE(QString().sprintf("Changing %s to ",
2207 wnd
->name()) + text
);
2209 // determine the lvalue to edit
2210 QString lvalue
= expr
->computeExpr();
2211 CmdQueueItem
* cmd
= m_d
->executeCmd(DCsetvariable
, lvalue
, text
);
2213 cmd
->m_exprWnd
= wnd
;
2216 void KDebugger::handleSetVariable(CmdQueueItem
* cmd
, const char* output
)
2218 QString msg
= m_d
->parseSetVariable(output
);
2221 // there was an error; display it in the status bar
2222 m_statusMessage
= msg
;
2223 emit
updateStatusMessage();
2227 // get the new value
2228 QString expr
= cmd
->m_expr
->computeExpr();
2229 CmdQueueItem
* printCmd
=
2230 m_d
->queueCmd(DCprint
, expr
, DebuggerDriver::QMoverrideMoreEqual
);
2231 printCmd
->m_expr
= cmd
->m_expr
;
2232 printCmd
->m_exprWnd
= cmd
->m_exprWnd
;
2236 #include "debugger.moc"