3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
7 #include <klocale.h> /* i18n */
9 #include <kmessagebox.h>
10 #include <kiconloader.h>
11 #include <kstatusbar.h>
13 #include <kfiledialog.h>
15 #include <qtabdialog.h>
17 #include "mainwndbase.h"
19 #include "gdbdriver.h"
20 #include "xsldbgdriver.h"
21 #include "prefdebugger.h"
24 #include "commandids.h"
30 #ifdef HAVE_SYS_STAT_H
31 #include <sys/stat.h> /* mknod(2) */
34 #include <fcntl.h> /* open(2) */
37 #include <unistd.h> /* getpid, unlink etc. */
40 #define MAX_RECENT_FILES 4
42 WatchWindow::WatchWindow(QWidget
* parent
, const char* name
, WFlags f
) :
43 QWidget(parent
, name
, f
),
44 m_watchEdit(this, "watch_edit"),
45 m_watchAdd(i18n(" Add "), this, "watch_add"),
46 m_watchDelete(i18n(" Del "), this, "watch_delete"),
47 m_watchVariables(this, "watch_variables"),
52 m_watchAdd
.setMinimumSize(m_watchAdd
.sizeHint());
53 m_watchDelete
.setMinimumSize(m_watchDelete
.sizeHint());
54 m_watchV
.addLayout(&m_watchH
, 0);
55 m_watchV
.addWidget(&m_watchVariables
, 10);
56 m_watchH
.addWidget(&m_watchEdit
, 10);
57 m_watchH
.addWidget(&m_watchAdd
, 0);
58 m_watchH
.addWidget(&m_watchDelete
, 0);
60 connect(&m_watchEdit
, SIGNAL(returnPressed()), SIGNAL(addWatch()));
61 connect(&m_watchAdd
, SIGNAL(clicked()), SIGNAL(addWatch()));
62 connect(&m_watchDelete
, SIGNAL(clicked()), SIGNAL(deleteWatch()));
63 connect(&m_watchVariables
, SIGNAL(highlighted(int)), SLOT(slotWatchHighlighted(int)));
65 m_watchVariables
.setMoveCurrentToSibling(true);
66 m_watchVariables
.installEventFilter(this);
69 WatchWindow::~WatchWindow()
73 bool WatchWindow::eventFilter(QObject
*, QEvent
* ev
)
75 if (ev
->type() == QEvent::KeyPress
)
77 QKeyEvent
* kev
= static_cast<QKeyEvent
*>(ev
);
78 if (kev
->key() == Key_Delete
) {
87 // place the text of the hightlighted watch expr in the edit field
88 void WatchWindow::slotWatchHighlighted(int idx
)
90 QString text
= m_watchVariables
.exprStringAt(idx
);
91 m_watchEdit
.setText(text
);
95 static void splitCmdStr(const QString
& cmd
, ValArray
<QString
>& parts
)
97 QString str
= cmd
.simplifyWhiteSpace();
100 while ((end
= str
.find(' ', start
)) >= 0) {
101 parts
.append(str
.mid(start
, end
-start
));
104 parts
.append(str
.mid(start
, str
.length()-start
));
108 const char defaultTermCmdStr
[] = "xterm -name kdbgio -title %T -e sh -c %C";
109 const char defaultSourceFilter
[] = "*.c *.cc *.cpp *.c++ *.C *.CC";
110 const char defaultHeaderFilter
[] = "*.h *.hh *.hpp *.h++";
113 DebuggerMainWndBase::DebuggerMainWndBase() :
114 m_animationCounter(0),
115 m_outputTermCmdStr(defaultTermCmdStr
),
117 m_ttyLevel(-1), /* no tty yet */
118 #ifdef GDB_TRANSCRIPT
119 m_transcriptFile(GDB_TRANSCRIPT
),
121 m_popForeground(false),
124 m_sourceFilter(defaultSourceFilter
),
125 m_headerFilter(defaultHeaderFilter
),
128 m_statusActive
= i18n("active");
131 DebuggerMainWndBase::~DebuggerMainWndBase()
135 // if the output window is open, close it
136 if (m_outputTermProc
!= 0) {
137 m_outputTermProc
->disconnect(); /* ignore signals */
138 m_outputTermProc
->kill();
139 shutdownTermWindow();
143 void DebuggerMainWndBase::setupDebugger(QWidget
* parent
,
148 m_debugger
= new KDebugger(parent
, localVars
, watchVars
, backtrace
);
150 QObject::connect(m_debugger
, SIGNAL(updateStatusMessage()),
151 parent
, SLOT(slotNewStatusMsg()));
152 QObject::connect(m_debugger
, SIGNAL(updateUI()),
153 parent
, SLOT(updateUI()));
155 QObject::connect(m_debugger
, SIGNAL(breakpointsChanged()),
156 parent
, SLOT(updateLineItems()));
158 QObject::connect(m_debugger
, SIGNAL(animationTimeout()),
159 parent
, SLOT(slotAnimationTimeout()));
160 QObject::connect(m_debugger
, SIGNAL(debuggerStarting()),
161 parent
, SLOT(slotDebuggerStarting()));
165 void DebuggerMainWndBase::setCoreFile(const QString
& corefile
)
167 assert(m_debugger
!= 0);
168 m_debugger
->useCoreFile(corefile
, true);
171 void DebuggerMainWndBase::setRemoteDevice(const QString
& remoteDevice
)
173 if (m_debugger
!= 0) {
174 m_debugger
->setRemoteDevice(remoteDevice
);
178 void DebuggerMainWndBase::setTranscript(const char* name
)
180 m_transcriptFile
= name
;
181 if (m_debugger
!= 0 && m_debugger
->driver() != 0)
182 m_debugger
->driver()->setLogFileName(m_transcriptFile
);
185 const char OutputWindowGroup
[] = "OutputWindow";
186 const char TermCmdStr
[] = "TermCmdStr";
187 const char KeepScript
[] = "KeepScript";
188 const char DebuggerGroup
[] = "Debugger";
189 const char DebuggerCmdStr
[] = "DebuggerCmdStr";
190 const char PreferencesGroup
[] = "Preferences";
191 const char PopForeground
[] = "PopForeground";
192 const char BackTimeout
[] = "BackTimeout";
193 const char TabWidth
[] = "TabWidth";
194 const char FilesGroup
[] = "Files";
195 const char SourceFileFilter
[] = "SourceFileFilter";
196 const char HeaderFileFilter
[] = "HeaderFileFilter";
197 const char GeneralGroup
[] = "General";
199 void DebuggerMainWndBase::saveSettings(KConfig
* config
)
201 if (m_debugger
!= 0) {
202 m_debugger
->saveSettings(config
);
205 KConfigGroupSaver
g(config
, OutputWindowGroup
);
206 config
->writeEntry(TermCmdStr
, m_outputTermCmdStr
);
208 config
->setGroup(DebuggerGroup
);
209 config
->writeEntry(DebuggerCmdStr
, m_debuggerCmdStr
);
211 config
->setGroup(PreferencesGroup
);
212 config
->writeEntry(PopForeground
, m_popForeground
);
213 config
->writeEntry(BackTimeout
, m_backTimeout
);
214 config
->writeEntry(TabWidth
, m_tabWidth
);
215 config
->writeEntry(SourceFileFilter
, m_sourceFilter
);
216 config
->writeEntry(HeaderFileFilter
, m_headerFilter
);
219 void DebuggerMainWndBase::restoreSettings(KConfig
* config
)
221 if (m_debugger
!= 0) {
222 m_debugger
->restoreSettings(config
);
225 KConfigGroupSaver
g(config
, OutputWindowGroup
);
227 * For debugging and emergency purposes, let the config file override
228 * the shell script that is used to keep the output window open. This
229 * string must have EXACTLY 1 %s sequence in it.
231 setTerminalCmd(config
->readEntry(TermCmdStr
, defaultTermCmdStr
));
232 m_outputTermKeepScript
= config
->readEntry(KeepScript
);
234 config
->setGroup(DebuggerGroup
);
235 setDebuggerCmdStr(config
->readEntry(DebuggerCmdStr
));
237 config
->setGroup(PreferencesGroup
);
238 m_popForeground
= config
->readBoolEntry(PopForeground
, false);
239 m_backTimeout
= config
->readNumEntry(BackTimeout
, 1000);
240 m_tabWidth
= config
->readNumEntry(TabWidth
, 0);
241 m_sourceFilter
= config
->readEntry(SourceFileFilter
, m_sourceFilter
);
242 m_headerFilter
= config
->readEntry(HeaderFileFilter
, m_headerFilter
);
245 void DebuggerMainWndBase::setAttachPid(const QString
& pid
)
247 assert(m_debugger
!= 0);
248 m_debugger
->setAttachPid(pid
);
251 bool DebuggerMainWndBase::debugProgram(const QString
& executable
,
252 QCString lang
, QWidget
* parent
)
254 assert(m_debugger
!= 0);
256 TRACE(QString().sprintf("trying language '%s'...", lang
.data()));
257 DebuggerDriver
* driver
= driverFromLang(lang
);
261 // see if there is a language in the per-program config file
262 QString configName
= m_debugger
->getConfigForExe(executable
);
263 if (QFile::exists(configName
))
265 KSimpleConfig
c(configName
, true); // read-only
266 c
.setGroup(GeneralGroup
);
268 // Using "GDB" as default here is for backwards compatibility:
269 // The config file exists but doesn't have an entry,
270 // so it must have been created by an old version of KDbg
271 // that had only the GDB driver.
272 lang
= c
.readEntry(KDebugger::DriverNameEntry
, "GDB").latin1();
274 TRACE(QString().sprintf("...bad, trying config driver %s...",
276 driver
= driverFromLang(lang
);
282 QCString name
= driverNameFromFile(executable
);
284 TRACE(QString().sprintf("...no luck, trying %s derived"
285 " from file contents", name
.data()));
286 driver
= driverFromLang(name
);
291 QString msg
= i18n("Don't know how to debug language `%1'");
292 KMessageBox::sorry(parent
, msg
.arg(lang
));
296 driver
->setLogFileName(m_transcriptFile
);
298 bool success
= m_debugger
->debugProgram(executable
, driver
);
304 QString msg
= i18n("Could not start the debugger process.\n"
305 "Please shut down KDbg and resolve the problem.");
306 KMessageBox::sorry(parent
, msg
);
312 // derive driver from language
313 DebuggerDriver
* DebuggerMainWndBase::driverFromLang(QCString lang
)
315 // lang is needed in all lowercase
318 // The following table relates languages and debugger drivers
319 static const struct L
{
320 const char* shortest
; // abbreviated to this is still unique
321 const char* full
; // full name of language
325 { "f", "fortran", 1 },
326 { "p", "python", 3 },
328 // the following are actually driver names
330 { "xsldbg", "xsldbg", 2 },
332 const int N
= sizeof(langs
)/sizeof(langs
[0]);
334 // lookup the language name
336 for (int i
= 0; i
< N
; i
++)
338 const L
& l
= langs
[i
];
340 // shortest must match
341 if (strncmp(l
.shortest
, lang
, strlen(l
.shortest
)) != 0)
344 // lang must not be longer than the full name, and it must match
345 if (lang
.length() <= strlen(l
.full
) &&
346 strncmp(l
.full
, lang
, lang
.length()) == 0)
352 DebuggerDriver
* driver
= 0;
356 GdbDriver
* gdb
= new GdbDriver
;
357 gdb
->setDefaultInvocation(m_debuggerCmdStr
);
362 driver
= new XsldbgDriver
;
372 * Try to guess the language to use from the contents of the file.
374 QCString
DebuggerMainWndBase::driverNameFromFile(const QString
& exe
)
376 /* Inprecise but simple test to see if file is in XSLT language */
377 if (exe
.right(4).lower() == ".xsl")
383 // helper that gets a file name (it only differs in the caption of the dialog)
384 QString
DebuggerMainWndBase::myGetFileName(QString caption
,
385 QString dir
, QString filter
,
389 KFileDialog
dlg(dir
, filter
, parent
, "filedialog", true);
391 dlg
.setCaption(caption
);
393 if (dlg
.exec() == QDialog::Accepted
)
394 filename
= dlg
.selectedFile();
399 void DebuggerMainWndBase::initAnimation(KToolBar
* toolbar
)
401 QPixmap pixmap
= BarIcon("kde1");
404 toolbar
->insertButton(pixmap
, ID_STATUS_BUSY
);
405 toolbar
->alignItemRight(ID_STATUS_BUSY
, true);
407 // Load animated logo
408 m_animation
.setAutoDelete(true);
410 for (int i
= 1; i
<= numPix
; i
++) {
411 n
.sprintf("kde%d", i
);
412 QPixmap
* p
= new QPixmap(BarIcon(n
));
414 m_animation
.append(p
);
419 // safeguard: if we did not find a single icon, add a dummy
420 if (m_animation
.count() == 0) {
421 QPixmap
* pix
= new QPixmap(2,2);
423 p
.fillRect(0,0,2,2,QBrush(Qt::white
));
424 m_animation
.append(pix
);
428 void DebuggerMainWndBase::nextAnimationFrame(KToolBar
* toolbar
)
430 assert(m_animation
.count() > 0); /* must have been initialized */
431 m_animationCounter
++;
432 if (m_animationCounter
== m_animation
.count())
433 m_animationCounter
= 0;
434 toolbar
->setButtonPixmap(ID_STATUS_BUSY
,
435 *m_animation
.at(m_animationCounter
));
438 void DebuggerMainWndBase::newStatusMsg(KStatusBar
* statusbar
)
440 QString msg
= m_debugger
->statusMessage();
441 statusbar
->changeItem(msg
, ID_STATUS_MSG
);
444 void DebuggerMainWndBase::doGlobalOptions(QWidget
* parent
)
446 QTabDialog
dlg(parent
, "global_options", true);
447 QString title
= kapp
->caption();
448 title
+= i18n(": Global options");
449 dlg
.setCaption(title
);
450 dlg
.setCancelButton(i18n("Cancel"));
451 dlg
.setOKButton(i18n("OK"));
453 PrefDebugger
prefDebugger(&dlg
);
454 prefDebugger
.setDebuggerCmd(m_debuggerCmdStr
.isEmpty() ?
455 GdbDriver::defaultGdb() : m_debuggerCmdStr
);
456 prefDebugger
.setTerminal(m_outputTermCmdStr
);
458 PrefMisc
prefMisc(&dlg
);
459 prefMisc
.setPopIntoForeground(m_popForeground
);
460 prefMisc
.setBackTimeout(m_backTimeout
);
461 prefMisc
.setTabWidth(m_tabWidth
);
462 prefMisc
.setSourceFilter(m_sourceFilter
);
463 prefMisc
.setHeaderFilter(m_headerFilter
);
465 dlg
.addTab(&prefDebugger
, i18n("&Debugger"));
466 dlg
.addTab(&prefMisc
, i18n("&Miscellaneous"));
467 if (dlg
.exec() == QDialog::Accepted
)
469 setDebuggerCmdStr(prefDebugger
.debuggerCmd());
470 setTerminalCmd(prefDebugger
.terminal());
471 m_popForeground
= prefMisc
.popIntoForeground();
472 m_backTimeout
= prefMisc
.backTimeout();
473 m_tabWidth
= prefMisc
.tabWidth();
474 m_sourceFilter
= prefMisc
.sourceFilter();
475 if (m_sourceFilter
.isEmpty())
476 m_sourceFilter
= defaultSourceFilter
;
477 m_headerFilter
= prefMisc
.headerFilter();
478 if (m_headerFilter
.isEmpty())
479 m_headerFilter
= defaultHeaderFilter
;
483 const char fifoNameBase
[] = "/tmp/kdbgttywin%05d";
486 * We use the scope operator :: in this function, so that we don't
487 * accidentally use the wrong close() function (I've been bitten ;-),
488 * outch!) (We use it for all the libc functions, to be consistent...)
490 QString
DebuggerMainWndBase::createOutputWindow()
492 // create a name for a fifo
494 fifoName
.sprintf(fifoNameBase
, ::getpid());
496 // create a fifo that will pass in the tty name
497 ::unlink(fifoName
); /* remove remnants */
499 if (::mkfifo(fifoName
, S_IRUSR
|S_IWUSR
) < 0) {
501 TRACE("mkfifo " + fifoName
+ " failed");
505 if (::mknod(fifoName
, S_IFIFO
| S_IRUSR
|S_IWUSR
, 0) < 0) {
507 TRACE("mknod " + fifoName
+ " failed");
512 m_outputTermProc
= new KProcess
;
516 * Spawn an xterm that in turn runs a shell script that passes us
517 * back the terminal name and then only sits and waits.
519 static const char shellScriptFmt
[] =
521 "trap \"\" INT QUIT TSTP;" /* ignore various signals */
522 "exec<&-;exec>&-;" /* close stdin and stdout */
523 "while :;do sleep 3600;done";
524 // let config file override this script
525 const char* fmt
= shellScriptFmt
;
526 if (m_outputTermKeepScript
.length() != 0) {
527 fmt
= m_outputTermKeepScript
.data();
531 shellScript
.sprintf(fmt
, fifoName
.data());
532 TRACE("output window script is " + shellScript
);
534 QString title
= kapp
->caption();
535 title
+= i18n(": Program output");
537 // parse the command line specified in the preferences
538 ValArray
<QString
> cmdParts
;
539 splitCmdStr(m_outputTermCmdStr
, cmdParts
);
542 * Build the argv array. Thereby substitute special sequences:
549 { "%C", shellScript
}
552 for (int i
= 0; i
< cmdParts
.size(); i
++) {
553 QString
& str
= cmdParts
[i
];
554 for (int j
= sizeof(substitute
)/sizeof(substitute
[0])-1; j
>= 0; j
--) {
555 int pos
= str
.find(substitute
[j
].seq
);
557 str
.replace(pos
, 2, substitute
[j
].replace
);
558 break; /* substitute only one sequence */
561 *m_outputTermProc
<< str
;
566 if (m_outputTermProc
->start())
568 // read the ttyname from the fifo
569 int f
= ::open(fifoName
, O_RDONLY
);
577 int n
= ::read(f
, ttyname
, sizeof(ttyname
)-sizeof(char)); /* leave space for '\0' */
589 QString tty
= QString(ttyname
).stripWhiteSpace();
595 // error, could not start xterm
596 TRACE("fork failed for fifo " + fifoName
);
598 shutdownTermWindow();
603 void DebuggerMainWndBase::shutdownTermWindow()
605 delete m_outputTermProc
;
606 m_outputTermProc
= 0;
609 void DebuggerMainWndBase::setTerminalCmd(const QString
& cmd
)
611 m_outputTermCmdStr
= cmd
;
612 // revert to default if empty
613 if (m_outputTermCmdStr
.isEmpty()) {
614 m_outputTermCmdStr
= defaultTermCmdStr
;
618 void DebuggerMainWndBase::slotDebuggerStarting()
620 if (m_debugger
== 0) /* paranoia check */
623 if (m_ttyLevel
== m_debugger
->ttyLevel())
628 // shut down terminal emulations we will not need
629 switch (m_ttyLevel
) {
630 case KDebugger::ttySimpleOutputOnly
:
631 ttyWindow()->deactivate();
633 case KDebugger::ttyFull
:
634 if (m_outputTermProc
!= 0) {
635 m_outputTermProc
->kill();
636 // will be deleted in slot
642 m_ttyLevel
= m_debugger
->ttyLevel();
645 switch (m_ttyLevel
) {
646 case KDebugger::ttySimpleOutputOnly
:
647 ttyName
= ttyWindow()->activate();
649 case KDebugger::ttyFull
:
650 if (m_outputTermProc
== 0) {
651 // create an output window
652 ttyName
= createOutputWindow();
653 TRACE(ttyName
.isEmpty() ?
654 "createOuputWindow failed" : "successfully created output window");
660 m_debugger
->setTerminal(ttyName
);
664 void DebuggerMainWndBase::setDebuggerCmdStr(const QString
& cmd
)
666 m_debuggerCmdStr
= cmd
;
667 // make empty if it is the default
668 if (m_debuggerCmdStr
== GdbDriver::defaultGdb()) {
669 m_debuggerCmdStr
= QString();
674 #include "mainwndbase.moc"