Update backtrace window from deprecated Q3ListBox widget to QListWidget.
[kdbg.git] / kdbg / dbgmainwnd.cpp
bloba67fb43a5ca0e69ef0935385442f5e048bbab47c
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include <kapplication.h>
8 #include <klocale.h> /* i18n */
9 #include <kmessagebox.h>
10 #include <kconfig.h>
11 #include <kstatusbar.h>
12 #include <kicon.h>
13 #include <kstandardaction.h>
14 #include <kstandardshortcut.h>
15 #include <kaction.h>
16 #include <kactioncollection.h>
17 #include <krecentfilesaction.h>
18 #include <ktoggleaction.h>
19 #include <kfiledialog.h>
20 #include <k3process.h>
21 #include <kshortcutsdialog.h>
22 #include <kanimatedbutton.h>
23 #include <kwindowsystem.h>
24 #include <ktoolbar.h>
25 #include <kurl.h>
26 #include <kxmlguifactory.h>
27 #include <KPageDialog>
28 #include <QListWidget>
29 #include <QFile>
30 #include <QFileInfo>
31 #include <QList>
32 #include <Q3PopupMenu>
33 #include <Q3ListViewItem>
34 #include <QDockWidget>
35 #include "dbgmainwnd.h"
36 #include "debugger.h"
37 #include "commandids.h"
38 #include "winstack.h"
39 #include "brkpt.h"
40 #include "threadlist.h"
41 #include "memwindow.h"
42 #include "ttywnd.h"
43 #include "watchwindow.h"
44 #include "procattach.h"
45 #include "prefdebugger.h"
46 #include "prefmisc.h"
47 #include "gdbdriver.h"
48 #include "xsldbgdriver.h"
49 #include "mydebug.h"
50 #include <sys/stat.h> /* mknod(2) */
51 #include <unistd.h> /* getpid */
54 static const char defaultTermCmdStr[] = "xterm -name kdbgio -title %T -e sh -c %C";
55 static const char defaultSourceFilter[] = "*.c *.cc *.cpp *.c++ *.C *.CC";
56 static const char defaultHeaderFilter[] = "*.h *.hh *.hpp *.h++";
58 DebuggerMainWnd::DebuggerMainWnd() :
59 KXmlGuiWindow(),
60 m_debugger(0),
61 #ifdef GDB_TRANSCRIPT
62 m_transcriptFile(GDB_TRANSCRIPT),
63 #endif
64 m_outputTermCmdStr(defaultTermCmdStr),
65 m_outputTermProc(0),
66 m_ttyLevel(-1), /* no tty yet */
67 m_popForeground(false),
68 m_backTimeout(1000),
69 m_tabWidth(0),
70 m_sourceFilter(defaultSourceFilter),
71 m_headerFilter(defaultHeaderFilter),
72 m_animation(0),
73 m_statusActive(i18n("active"))
75 m_filesWindow = new WinStack(this);
76 setCentralWidget(m_filesWindow);
78 QDockWidget* dw1 = createDockWidget("Stack", i18n("Stack"));
79 m_btWindow = new QListWidget(dw1);
80 dw1->setWidget(m_btWindow);
81 QDockWidget* dw2 = createDockWidget("Locals", i18n("Locals"));
82 m_localVariables = new ExprWnd(dw2, i18n("Variable"));
83 dw2->setWidget(m_localVariables);
84 QDockWidget* dw3 = createDockWidget("Watches", i18n("Watches"));
85 m_watches = new WatchWindow(dw3);
86 dw3->setWidget(m_watches);
87 QDockWidget* dw4 = createDockWidget("Registers", i18n("Registers"));
88 m_registers = new RegisterView(dw4);
89 dw4->setWidget(m_registers);
90 QDockWidget* dw5 = createDockWidget("Breakpoints", i18n("Breakpoints"));
91 m_bpTable = new BreakpointTable(dw5);
92 dw5->setWidget(m_bpTable);
93 QDockWidget* dw6 = createDockWidget("Output", i18n("Output"));
94 m_ttyWindow = new TTYWindow(dw6);
95 dw6->setWidget(m_ttyWindow);
96 QDockWidget* dw7 = createDockWidget("Threads", i18n("Threads"));
97 m_threads = new ThreadList(dw7);
98 dw7->setWidget(m_threads);
99 QDockWidget* dw8 = createDockWidget("Memory", i18n("Memory"));
100 m_memoryWindow = new MemoryWindow(dw8);
101 dw8->setWidget(m_memoryWindow);
103 m_debugger = new KDebugger(this, m_localVariables, m_watches->watchVariables(), m_btWindow);
105 connect(m_debugger, SIGNAL(updateStatusMessage()), SLOT(slotNewStatusMsg()));
106 connect(m_debugger, SIGNAL(updateUI()), SLOT(updateUI()));
107 connect(m_debugger, SIGNAL(breakpointsChanged()), SLOT(updateLineItems()));
108 connect(m_debugger, SIGNAL(debuggerStarting()), SLOT(slotDebuggerStarting()));
109 m_bpTable->setDebugger(m_debugger);
110 m_memoryWindow->setDebugger(m_debugger);
112 setStandardToolBarMenuEnabled(true);
113 initKAction();
114 initToolbar(); // kind of obsolete?
116 connect(m_watches, SIGNAL(addWatch()), SLOT(slotAddWatch()));
117 connect(m_watches, SIGNAL(deleteWatch()), m_debugger, SLOT(slotDeleteWatch()));
118 connect(m_watches, SIGNAL(textDropped(const QString&)), SLOT(slotAddWatch(const QString&)));
120 connect(&m_filesWindow->m_findDlg, SIGNAL(closed()), SLOT(updateUI()));
121 connect(m_filesWindow, SIGNAL(newFileLoaded()),
122 SLOT(slotNewFileLoaded()));
123 connect(m_filesWindow, SIGNAL(toggleBreak(const QString&,int,const DbgAddr&,bool)),
124 this, SLOT(slotToggleBreak(const QString&,int,const DbgAddr&,bool)));
125 connect(m_filesWindow, SIGNAL(enadisBreak(const QString&,int,const DbgAddr&)),
126 this, SLOT(slotEnaDisBreak(const QString&,int,const DbgAddr&)));
127 connect(m_debugger, SIGNAL(activateFileLine(const QString&,int,const DbgAddr&)),
128 m_filesWindow, SLOT(activate(const QString&,int,const DbgAddr&)));
129 connect(m_debugger, SIGNAL(executableUpdated()),
130 m_filesWindow, SLOT(reloadAllFiles()));
131 connect(m_debugger, SIGNAL(updatePC(const QString&,int,const DbgAddr&,int)),
132 m_filesWindow, SLOT(updatePC(const QString&,int,const DbgAddr&,int)));
133 // value popup communication
134 connect(m_filesWindow, SIGNAL(initiateValuePopup(const QString&)),
135 m_debugger, SLOT(slotValuePopup(const QString&)));
136 connect(m_debugger, SIGNAL(valuePopup(const QString&)),
137 m_filesWindow, SLOT(slotShowValueTip(const QString&)));
138 // disassembling
139 connect(m_filesWindow, SIGNAL(disassemble(const QString&, int)),
140 m_debugger, SLOT(slotDisassemble(const QString&, int)));
141 connect(m_debugger, SIGNAL(disassembled(const QString&,int,const std::list<DisassembledCode>&)),
142 m_filesWindow, SLOT(slotDisassembled(const QString&,int,const std::list<DisassembledCode>&)));
143 connect(m_filesWindow, SIGNAL(moveProgramCounter(const QString&,int,const DbgAddr&)),
144 m_debugger, SLOT(setProgramCounter(const QString&,int,const DbgAddr&)));
145 // program stopped
146 connect(m_debugger, SIGNAL(programStopped()), SLOT(slotProgramStopped()));
147 connect(&m_backTimer, SIGNAL(timeout()), SLOT(slotBackTimer()));
148 // tab width
149 connect(this, SIGNAL(setTabWidth(int)), m_filesWindow, SIGNAL(setTabWidth(int)));
151 // connect breakpoint table
152 connect(m_bpTable, SIGNAL(activateFileLine(const QString&,int,const DbgAddr&)),
153 m_filesWindow, SLOT(activate(const QString&,int,const DbgAddr&)));
154 connect(m_debugger, SIGNAL(updateUI()), m_bpTable, SLOT(updateUI()));
155 connect(m_debugger, SIGNAL(breakpointsChanged()), m_bpTable, SLOT(updateBreakList()));
156 connect(m_debugger, SIGNAL(breakpointsChanged()), m_bpTable, SLOT(updateUI()));
158 connect(m_debugger, SIGNAL(registersChanged(const std::list<RegisterInfo>&)),
159 m_registers, SLOT(updateRegisters(const std::list<RegisterInfo>&)));
161 connect(m_debugger, SIGNAL(memoryDumpChanged(const QString&, const std::list<MemoryDump>&)),
162 m_memoryWindow, SLOT(slotNewMemoryDump(const QString&, const std::list<MemoryDump>&)));
163 connect(m_debugger, SIGNAL(saveProgramSpecific(KConfigBase*)),
164 m_memoryWindow, SLOT(saveProgramSpecific(KConfigBase*)));
165 connect(m_debugger, SIGNAL(restoreProgramSpecific(KConfigBase*)),
166 m_memoryWindow, SLOT(restoreProgramSpecific(KConfigBase*)));
168 // thread window
169 connect(m_debugger, SIGNAL(threadsChanged(const std::list<ThreadInfo>&)),
170 m_threads, SLOT(updateThreads(const std::list<ThreadInfo>&)));
171 connect(m_threads, SIGNAL(setThread(int)),
172 m_debugger, SLOT(setThread(int)));
174 // popup menu of the local variables window
175 connect(m_localVariables, SIGNAL(contextMenuRequested(Q3ListViewItem*, const QPoint&, int)),
176 this, SLOT(slotLocalsPopup(Q3ListViewItem*, const QPoint&)));
178 restoreSettings(KGlobal::config());
180 updateUI();
181 m_bpTable->updateUI();
184 DebuggerMainWnd::~DebuggerMainWnd()
186 saveSettings(KGlobal::config());
187 // must delete m_debugger early since it references our windows
188 delete m_debugger;
189 m_debugger = 0;
191 delete m_memoryWindow;
192 delete m_threads;
193 delete m_ttyWindow;
194 delete m_bpTable;
195 delete m_registers;
196 delete m_watches;
197 delete m_localVariables;
198 delete m_btWindow;
199 delete m_filesWindow;
201 // if the output window is open, close it
202 if (m_outputTermProc != 0) {
203 m_outputTermProc->disconnect(); /* ignore signals */
204 m_outputTermProc->kill();
205 shutdownTermWindow();
209 QDockWidget* DebuggerMainWnd::createDockWidget(const char* name, const QString& title)
211 QDockWidget* w = new QDockWidget(title, this);
212 w->setObjectName(name);
213 // view menu changes when docking state changes
214 connect(w, SIGNAL(visibilityChanged(bool)), SLOT(updateUI()));
215 return w;
218 QAction* DebuggerMainWnd::createAction(const QString& text, const char* icon,
219 int shortcut, const QObject* receiver,
220 const char* slot, const char* name)
222 KAction* a = actionCollection()->addAction(name);
223 a->setText(text);
224 a->setIcon(KIcon(icon));
225 if (shortcut)
226 a->setShortcut(KShortcut(shortcut));
227 connect(a, SIGNAL(triggered()), receiver, slot);
228 return a;
231 QAction* DebuggerMainWnd::createAction(const QString& text,
232 int shortcut, const QObject* receiver,
233 const char* slot, const char* name)
235 KAction* a = actionCollection()->addAction(name);
236 a->setText(text);
237 if (shortcut)
238 a->setShortcut(KShortcut(shortcut));
239 connect(a, SIGNAL(triggered()), receiver, slot);
240 return a;
244 void DebuggerMainWnd::initKAction()
246 // file menu
247 KAction* open = KStandardAction::open(this, SLOT(slotFileOpen()),
248 actionCollection());
249 open->setText(i18n("&Open Source..."));
250 m_closeAction = KStandardAction::close(m_filesWindow, SLOT(slotClose()), actionCollection());
251 m_reloadAction = createAction(i18n("&Reload Source"), "view-refresh", 0,
252 m_filesWindow, SLOT(slotFileReload()), "file_reload");
253 m_fileExecAction = createAction(i18n("&Executable..."), "execopen", 0,
254 this, SLOT(slotFileExe()), "file_executable");
255 m_recentExecAction = KStandardAction::openRecent(this, SLOT(slotRecentExec(const KUrl&)),
256 actionCollection());
257 m_recentExecAction->setObjectName("file_executable_recent");
258 m_recentExecAction->setText(i18n("Recent E&xecutables"));
259 m_coreDumpAction = createAction(i18n("&Core dump..."), 0,
260 this, SLOT(slotFileCore()), "file_core_dump");
261 KStandardAction::quit(kapp, SLOT(closeAllWindows()), actionCollection());
263 // settings menu
264 m_settingsAction = createAction(i18n("This &Program..."), 0,
265 this, SLOT(slotFileProgSettings()), "settings_program");
266 createAction(i18n("&Global Options..."), 0,
267 this, SLOT(slotFileGlobalSettings()), "settings_global");
268 KStandardAction::keyBindings(this, SLOT(slotConfigureKeys()), actionCollection());
269 KStandardAction::showStatusbar(this, SLOT(slotViewStatusbar()), actionCollection());
271 // view menu
272 m_findAction = KStandardAction::find(m_filesWindow, SLOT(slotViewFind()), actionCollection());
273 KStandardAction::findNext(m_filesWindow, SLOT(slotFindForward()), actionCollection());
274 KStandardAction::findPrev(m_filesWindow, SLOT(slotFindBackward()), actionCollection());
276 i18n("Source &code");
277 struct { QString text; QWidget* w; QString id; QAction** act; } dw[] = {
278 { i18n("Stac&k"), m_btWindow, "view_stack", &m_btWindowAction },
279 { i18n("&Locals"), m_localVariables, "view_locals", &m_localVariablesAction },
280 { i18n("&Watched expressions"), m_watches, "view_watched_expressions", &m_watchesAction },
281 { i18n("&Registers"), m_registers, "view_registers", &m_registersAction },
282 { i18n("&Breakpoints"), m_bpTable, "view_breakpoints", &m_bpTableAction },
283 { i18n("T&hreads"), m_threads, "view_threads", &m_threadsAction },
284 { i18n("&Output"), m_ttyWindow, "view_output", &m_ttyWindowAction },
285 { i18n("&Memory"), m_memoryWindow, "view_memory", &m_memoryWindowAction }
287 for (unsigned i = 0; i < sizeof(dw)/sizeof(dw[0]); i++) {
288 QDockWidget* d = dockParent(dw[i].w);
289 *dw[i].act = new KToggleAction(dw[i].text, actionCollection());
290 actionCollection()->addAction(dw[i].id, *dw[i].act);
291 connect(*dw[i].act, SIGNAL(triggered()), d, SLOT(show()));
294 // execution menu
295 m_runAction = createAction(i18n("&Run"), "pgmrun", Qt::Key_F5,
296 m_debugger, SLOT(programRun()), "exec_run");
297 connect(m_runAction, SIGNAL(activated()), this, SLOT(intoBackground()));
298 m_stepIntoAction = createAction(i18n("Step &into"), "pgmstep", Qt::Key_F8,
299 m_debugger, SLOT(programStep()), "exec_step_into");
300 connect(m_stepIntoAction, SIGNAL(activated()), this, SLOT(intoBackground()));
301 m_stepOverAction = createAction(i18n("Step &over"), "pgmnext", Qt::Key_F10,
302 m_debugger, SLOT(programNext()), "exec_step_over");
303 connect(m_stepOverAction, SIGNAL(activated()), this, SLOT(intoBackground()));
304 m_stepOutAction = createAction(i18n("Step o&ut"), "pgmfinish", Qt::Key_F6,
305 m_debugger, SLOT(programFinish()), "exec_step_out");
306 connect(m_stepOutAction, SIGNAL(activated()), this, SLOT(intoBackground()));
307 m_toCursorAction = createAction(i18n("Run to &cursor"), Qt::Key_F7,
308 this, SLOT(slotExecUntil()), "exec_run_to_cursor");
309 connect(m_toCursorAction, SIGNAL(activated()), this, SLOT(intoBackground()));
310 m_stepIntoIAction = createAction(i18n("Step i&nto by instruction"), "pgmstepi", Qt::SHIFT+Qt::Key_F8,
311 m_debugger, SLOT(programStepi()), "exec_step_into_by_insn");
312 connect(m_stepIntoIAction, SIGNAL(activated()), this, SLOT(intoBackground()));
313 m_stepOverIAction = createAction(i18n("Step o&ver by instruction"), "pgmnexti", Qt::SHIFT+Qt::Key_F10,
314 m_debugger, SLOT(programNexti()), "exec_step_over_by_insn");
315 connect(m_stepOverIAction, SIGNAL(activated()), this, SLOT(intoBackground()));
316 m_execMovePCAction = createAction(i18n("&Program counter to current line"), 0,
317 m_filesWindow, SLOT(slotMoveProgramCounter()), "exec_movepc");
318 m_breakAction = createAction(i18n("&Break"), 0,
319 m_debugger, SLOT(programBreak()), "exec_break");
320 m_killAction = createAction(i18n("&Kill"), 0,
321 m_debugger, SLOT(programKill()), "exec_kill");
322 m_restartAction = createAction(i18n("Re&start"), 0,
323 m_debugger, SLOT(programRunAgain()), "exec_restart");
324 m_attachAction = createAction(i18n("A&ttach..."), 0,
325 this, SLOT(slotExecAttach()), "exec_attach");
326 m_argumentsAction = createAction(i18n("&Arguments..."), 0,
327 this, SLOT(slotExecArgs()), "exec_arguments");
329 // breakpoint menu
330 m_bpSetAction = createAction(i18n("Set/Clear &breakpoint"), "brkpt", Qt::Key_F9,
331 m_filesWindow, SLOT(slotBrkptSet()), "breakpoint_set");
332 m_bpSetTempAction = createAction(i18n("Set &temporary breakpoint"), Qt::SHIFT+Qt::Key_F9,
333 m_filesWindow, SLOT(slotBrkptSetTemp()), "breakpoint_set_temporary");
334 m_bpEnableAction = createAction(i18n("&Enable/Disable breakpoint"), Qt::CTRL+Qt::Key_F9,
335 m_filesWindow, SLOT(slotBrkptEnable()), "breakpoint_enable");
337 // only in popup menus
338 createAction(i18n("Watch Expression"), 0,
339 this, SLOT(slotLocalsToWatch()), "watch_expression");
340 m_editValueAction = createAction(i18n("Edit Value"), Qt::Key_F2,
341 this, SLOT(slotEditValue()), "edit_value");
343 // all actions force an UI update
344 QList<QAction*> actions = actionCollection()->actions();
345 foreach(QAction* action, actions) {
346 connect(action, SIGNAL(activated()), this, SLOT(updateUI()));
349 createGUI("kdbgui.rc");
352 void DebuggerMainWnd::initToolbar()
354 KToolBar* toolbar = toolBar("mainToolBar");
355 m_animation = new KAnimatedButton(toolbar);
356 toolbar->addWidget(m_animation);
357 m_animation->setIcons("pulse");
358 connect(m_animation, SIGNAL(triggered(QAction*)), m_debugger, SLOT(programBreak()));
359 m_animRunning = false;
361 KStatusBar* statusbar = statusBar();
362 statusbar->insertItem(m_statusActive, ID_STATUS_ACTIVE);
363 m_lastActiveStatusText = m_statusActive;
364 statusbar->insertItem("", ID_STATUS_MSG); /* message pane */
366 // reserve some translations
367 i18n("Restart");
368 i18n("Core dump");
371 bool DebuggerMainWnd::queryClose()
373 if (m_debugger != 0) {
374 m_debugger->shutdown();
376 return true;
380 // instance properties
381 void DebuggerMainWnd::saveProperties(KConfigGroup& cg)
383 // session management
384 QString executable = "";
385 if (m_debugger != 0) {
386 executable = m_debugger->executable();
388 cg.writeEntry("executable", executable);
391 void DebuggerMainWnd::readProperties(const KConfigGroup& cg)
393 // session management
394 QString execName = cg.readEntry("executable");
396 TRACE("readProperties: executable=" + execName);
397 if (!execName.isEmpty()) {
398 debugProgram(execName, "");
402 static const char WindowGroup[] = "Windows";
403 static const char ToolbarGroup[] = "ToolbarSettings";
404 static const char RecentExecutables[] = "RecentExecutables";
405 static const char LastSession[] = "LastSession";
406 static const char OutputWindowGroup[] = "OutputWindow";
407 static const char TermCmdStr[] = "TermCmdStr";
408 static const char KeepScript[] = "KeepScript";
409 static const char DebuggerGroup[] = "Debugger";
410 static const char DebuggerCmdStr[] = "DebuggerCmdStr";
411 static const char PreferencesGroup[] = "Preferences";
412 static const char PopForeground[] = "PopForeground";
413 static const char BackTimeout[] = "BackTimeout";
414 static const char TabWidth[] = "TabWidth";
415 static const char SourceFileFilter[] = "SourceFileFilter";
416 static const char HeaderFileFilter[] = "HeaderFileFilter";
418 void DebuggerMainWnd::saveSettings(KSharedConfigPtr config)
420 KConfigGroup g = config->group(WindowGroup);
422 QByteArray layout = saveState();
423 g.writeEntry("Layout", layout);
424 g.writeEntry("Geometry", geometry());
426 KConfigGroup tg = config->group(ToolbarGroup);
427 toolBar("mainToolBar")->saveSettings(tg);
429 m_recentExecAction->saveEntries(config->group(RecentExecutables));
431 KConfigGroup lg = config->group(LastSession);
432 lg.writeEntry("Width0Locals", m_localVariables->columnWidth(0));
433 lg.writeEntry("Width0Watches", m_watches->columnWidth(0));
435 if (m_debugger != 0) {
436 m_debugger->saveSettings(config.data());
439 config->group(OutputWindowGroup).writeEntry(TermCmdStr, m_outputTermCmdStr);
440 config->group(DebuggerGroup).writeEntry(DebuggerCmdStr, m_debuggerCmdStr);
442 KConfigGroup pg(config->group(PreferencesGroup));
443 pg.writeEntry(PopForeground, m_popForeground);
444 pg.writeEntry(BackTimeout, m_backTimeout);
445 pg.writeEntry(TabWidth, m_tabWidth);
446 pg.writeEntry(SourceFileFilter, m_sourceFilter);
447 pg.writeEntry(HeaderFileFilter, m_headerFilter);
450 void DebuggerMainWnd::restoreSettings(KSharedConfigPtr config)
452 KConfigGroup g = config->group(WindowGroup);
454 QByteArray layout = g.readEntry("Layout", QByteArray());
455 if (!restoreState(layout))
456 makeDefaultLayout();
457 QRect r = g.readEntry("Geometry", QRect());
458 if (!r.isEmpty())
459 setGeometry(r);
461 toolBar("mainToolBar")->applySettings(config->group(ToolbarGroup));
463 m_recentExecAction->loadEntries(config->group(RecentExecutables));
465 KConfigGroup lg = config->group(LastSession);
466 int w;
467 w = lg.readEntry("Width0Locals", -1);
468 if (w >= 0 && w < 30000)
469 m_localVariables->setColumnWidth(0, w);
470 w = lg.readEntry("Width0Watches", -1);
471 if (w >= 0 && w < 30000)
472 m_watches->setColumnWidth(0, w);
474 if (m_debugger != 0) {
475 m_debugger->restoreSettings(config.data());
478 KConfigGroup og(config->group(OutputWindowGroup));
480 * For debugging and emergency purposes, let the config file override
481 * the shell script that is used to keep the output window open. This
482 * string must have EXACTLY 1 %s sequence in it.
484 setTerminalCmd(og.readEntry(TermCmdStr, defaultTermCmdStr));
485 m_outputTermKeepScript = og.readEntry(KeepScript);
487 setDebuggerCmdStr(config->group(DebuggerGroup).readEntry(DebuggerCmdStr));
489 KConfigGroup pg(config->group(PreferencesGroup));
490 m_popForeground = pg.readEntry(PopForeground, false);
491 m_backTimeout = pg.readEntry(BackTimeout, 1000);
492 m_tabWidth = pg.readEntry(TabWidth, 0);
493 m_sourceFilter = pg.readEntry(SourceFileFilter, m_sourceFilter);
494 m_headerFilter = pg.readEntry(HeaderFileFilter, m_headerFilter);
496 emit setTabWidth(m_tabWidth);
499 void DebuggerMainWnd::updateUI()
501 m_findAction->setChecked(m_filesWindow->m_findDlg.isVisible());
502 m_findAction->setEnabled(m_filesWindow->hasWindows());
503 m_bpSetAction->setEnabled(m_debugger->canChangeBreakpoints());
504 m_bpSetTempAction->setEnabled(m_debugger->canChangeBreakpoints());
505 m_bpEnableAction->setEnabled(m_debugger->canChangeBreakpoints());
506 m_bpTableAction->setChecked(isDockVisible(m_bpTable));
507 m_btWindowAction->setChecked(isDockVisible(m_btWindow));
508 m_localVariablesAction->setChecked(isDockVisible(m_localVariables));
509 m_watchesAction->setChecked(isDockVisible(m_watches));
510 m_registersAction->setChecked(isDockVisible(m_registers));
511 m_threadsAction->setChecked(isDockVisible(m_threads));
512 m_memoryWindowAction->setChecked(isDockVisible(m_memoryWindow));
513 m_ttyWindowAction->setChecked(isDockVisible(m_ttyWindow));
515 m_fileExecAction->setEnabled(m_debugger->isIdle());
516 m_settingsAction->setEnabled(m_debugger->haveExecutable());
517 m_coreDumpAction->setEnabled(m_debugger->canStart());
518 m_closeAction->setEnabled(m_filesWindow->hasWindows());
519 m_reloadAction->setEnabled(m_filesWindow->hasWindows());
520 m_stepIntoAction->setEnabled(m_debugger->canSingleStep());
521 m_stepIntoIAction->setEnabled(m_debugger->canSingleStep());
522 m_stepOverAction->setEnabled(m_debugger->canSingleStep());
523 m_stepOverIAction->setEnabled(m_debugger->canSingleStep());
524 m_stepOutAction->setEnabled(m_debugger->canSingleStep());
525 m_toCursorAction->setEnabled(m_debugger->canSingleStep());
526 m_execMovePCAction->setEnabled(m_debugger->canSingleStep());
527 m_restartAction->setEnabled(m_debugger->canSingleStep());
528 m_attachAction->setEnabled(m_debugger->isReady());
529 m_runAction->setEnabled(m_debugger->canStart() || m_debugger->canSingleStep());
530 m_killAction->setEnabled(m_debugger->haveExecutable() && m_debugger->isProgramActive());
531 m_breakAction->setEnabled(m_debugger->isProgramRunning());
532 m_argumentsAction->setEnabled(m_debugger->haveExecutable());
533 m_editValueAction->setEnabled(m_debugger->canSingleStep());
535 // animation
536 if (m_debugger->isIdle()) {
537 if (m_animRunning) {
538 m_animation->stop();
539 m_animRunning = false;
541 } else {
542 if (!m_animRunning) {
543 m_animation->start();
544 m_animRunning = true;
548 // update statusbar
549 QString newStatus;
550 if (m_debugger->isProgramActive())
551 newStatus = m_statusActive;
552 if (newStatus != m_lastActiveStatusText) {
553 statusBar()->changeItem(newStatus, ID_STATUS_ACTIVE);
554 m_lastActiveStatusText = newStatus;
558 void DebuggerMainWnd::updateLineItems()
560 m_filesWindow->updateLineItems(m_debugger);
563 void DebuggerMainWnd::slotAddWatch()
565 if (m_debugger != 0) {
566 QString t = m_watches->watchText();
567 m_debugger->addWatch(t);
571 void DebuggerMainWnd::slotAddWatch(const QString& text)
573 if (m_debugger != 0) {
574 m_debugger->addWatch(text);
578 void DebuggerMainWnd::slotNewFileLoaded()
580 // updates program counter in the new file
581 if (m_debugger != 0)
582 m_filesWindow->updateLineItems(m_debugger);
585 QDockWidget* DebuggerMainWnd::dockParent(QWidget* w)
587 while ((w = w->parentWidget()) != 0) {
588 if (w->isA("QDockWidget"))
589 return static_cast<QDockWidget*>(w);
591 return 0;
594 bool DebuggerMainWnd::isDockVisible(QWidget* w)
596 QDockWidget* d = dockParent(w);
597 return d != 0 && d->isVisible();
600 void DebuggerMainWnd::makeDefaultLayout()
602 // +---------------+---------+
603 // | Source | Locals |
604 // | | |
605 // |---------------+---------+
606 // |Stack, Brkpts, | Watches |
607 // |Output,... | |
608 // +---------------+---------+
610 addDockWidget(Qt::RightDockWidgetArea, dockParent(m_localVariables));
611 addDockWidget(Qt::BottomDockWidgetArea, dockParent(m_memoryWindow));
612 splitDockWidget(dockParent(m_memoryWindow), dockParent(m_threads), Qt::Horizontal);
613 tabifyDockWidget(dockParent(m_memoryWindow), dockParent(m_registers));
614 tabifyDockWidget(dockParent(m_registers), dockParent(m_bpTable));
615 tabifyDockWidget(dockParent(m_bpTable), dockParent(m_ttyWindow));
616 tabifyDockWidget(dockParent(m_ttyWindow), dockParent(m_btWindow));
617 tabifyDockWidget(dockParent(m_threads), dockParent(m_watches));
618 dockParent(m_localVariables)->setVisible(true);
619 dockParent(m_ttyWindow)->setVisible(true);
620 dockParent(m_watches)->setVisible(true);
621 dockParent(m_btWindow)->setVisible(true);
622 dockParent(m_bpTable)->setVisible(true);
625 bool DebuggerMainWnd::debugProgram(const QString& exe, const QString& lang)
627 // check the file name
628 QFileInfo fi(exe);
630 bool success = fi.isFile();
631 if (!success)
633 QString msg = i18n("`%1' is not a file or does not exist");
634 KMessageBox::sorry(this, msg.arg(exe));
636 else
638 success = startDriver(fi.absFilePath(), lang);
641 if (success)
643 m_recentExecAction->addUrl(KUrl(fi.absFilePath()));
645 // keep the directory
646 m_lastDirectory = fi.dirPath(true);
647 m_filesWindow->setExtraDirectory(m_lastDirectory);
649 // set caption to basename part of executable
650 QString caption = fi.fileName();
651 setCaption(caption);
653 else
655 m_recentExecAction->removeUrl(KUrl(fi.absFilePath()));
658 return success;
661 static const char GeneralGroup[] = "General";
663 bool DebuggerMainWnd::startDriver(const QString& executable, QString lang)
665 assert(m_debugger != 0);
667 TRACE(QString("trying language '%1'...").arg(lang));
668 DebuggerDriver* driver = driverFromLang(lang);
670 if (driver == 0)
672 // see if there is a language in the per-program config file
673 QString configName = m_debugger->getConfigForExe(executable);
674 if (QFile::exists(configName))
676 KConfig c(configName, KConfig::SimpleConfig);
678 // Using "GDB" as default here is for backwards compatibility:
679 // The config file exists but doesn't have an entry,
680 // so it must have been created by an old version of KDbg
681 // that had only the GDB driver.
682 lang = c.group(GeneralGroup)
683 .readEntry(KDebugger::DriverNameEntry, "GDB");
685 TRACE(QString("...bad, trying config driver %1...").arg(lang));
686 driver = driverFromLang(lang);
690 if (driver == 0)
692 QString name = driverNameFromFile(executable);
694 TRACE(QString("...no luck, trying %1 derived"
695 " from file contents").arg(name));
696 driver = driverFromLang(name);
698 if (driver == 0)
700 // oops
701 QString msg = i18n("Don't know how to debug language `%1'");
702 KMessageBox::sorry(this, msg.arg(lang));
703 return false;
706 driver->setLogFileName(m_transcriptFile);
708 bool success = m_debugger->debugProgram(executable, driver);
710 if (!success)
712 delete driver;
714 QString msg = i18n("Could not start the debugger process.\n"
715 "Please shut down KDbg and resolve the problem.");
716 KMessageBox::sorry(this, msg);
719 return success;
722 // derive driver from language
723 DebuggerDriver* DebuggerMainWnd::driverFromLang(QString lang)
725 // lang is needed in all lowercase
726 lang = lang.lower();
728 // The following table relates languages and debugger drivers
729 static const struct L {
730 const char* shortest; // abbreviated to this is still unique
731 const char* full; // full name of language
732 int driver;
733 } langs[] = {
734 { "c", "c++", 1 },
735 { "f", "fortran", 1 },
736 { "p", "python", 3 },
737 { "x", "xslt", 2 },
738 // the following are actually driver names
739 { "gdb", "gdb", 1 },
740 { "xsldbg", "xsldbg", 2 },
742 const int N = sizeof(langs)/sizeof(langs[0]);
744 // lookup the language name
745 int driverID = 0;
746 for (int i = 0; i < N; i++)
748 const L& l = langs[i];
750 // shortest must match
751 if (!lang.startsWith(l.shortest))
752 continue;
754 // lang must not be longer than the full name, and it must match
755 if (QString(l.full).startsWith(lang))
757 driverID = l.driver;
758 break;
761 DebuggerDriver* driver = 0;
762 switch (driverID) {
763 case 1:
765 GdbDriver* gdb = new GdbDriver;
766 gdb->setDefaultInvocation(m_debuggerCmdStr);
767 driver = gdb;
769 break;
770 case 2:
771 driver = new XsldbgDriver;
772 break;
773 default:
774 // unknown language
775 break;
777 return driver;
781 * Try to guess the language to use from the contents of the file.
783 QString DebuggerMainWnd::driverNameFromFile(const QString& exe)
785 /* Inprecise but simple test to see if file is in XSLT language */
786 if (exe.right(4).lower() == ".xsl")
787 return "XSLT";
789 return "GDB";
792 void DebuggerMainWnd::setCoreFile(const QString& corefile)
794 assert(m_debugger != 0);
795 m_debugger->useCoreFile(corefile, true);
798 void DebuggerMainWnd::setRemoteDevice(const QString& remoteDevice)
800 if (m_debugger != 0) {
801 m_debugger->setRemoteDevice(remoteDevice);
805 void DebuggerMainWnd::overrideProgramArguments(const QString& args)
807 assert(m_debugger != 0);
808 m_debugger->overrideProgramArguments(args);
811 void DebuggerMainWnd::setTranscript(const QString& name)
813 m_transcriptFile = name;
814 if (m_debugger != 0 && m_debugger->driver() != 0)
815 m_debugger->driver()->setLogFileName(m_transcriptFile);
818 void DebuggerMainWnd::setAttachPid(const QString& pid)
820 assert(m_debugger != 0);
821 m_debugger->setAttachPid(pid);
824 void DebuggerMainWnd::slotNewStatusMsg()
826 QString msg = m_debugger->statusMessage();
827 statusBar()->changeItem(msg, ID_STATUS_MSG);
830 void DebuggerMainWnd::slotFileGlobalSettings()
832 int oldTabWidth = m_tabWidth;
834 KPageDialog dlg(this);
835 QString title = KGlobal::caption();
836 title += i18n(": Global options");
837 dlg.setCaption(title);
839 PrefDebugger prefDebugger(&dlg);
840 prefDebugger.setDebuggerCmd(m_debuggerCmdStr.isEmpty() ?
841 GdbDriver::defaultGdb() : m_debuggerCmdStr);
842 prefDebugger.setTerminal(m_outputTermCmdStr);
844 PrefMisc prefMisc(&dlg);
845 prefMisc.setPopIntoForeground(m_popForeground);
846 prefMisc.setBackTimeout(m_backTimeout);
847 prefMisc.setTabWidth(m_tabWidth);
848 prefMisc.setSourceFilter(m_sourceFilter);
849 prefMisc.setHeaderFilter(m_headerFilter);
851 dlg.addPage(&prefDebugger, i18n("Debugger"));
852 dlg.addPage(&prefMisc, i18n("Miscellaneous"));
853 if (dlg.exec() == QDialog::Accepted)
855 setDebuggerCmdStr(prefDebugger.debuggerCmd());
856 setTerminalCmd(prefDebugger.terminal());
857 m_popForeground = prefMisc.popIntoForeground();
858 m_backTimeout = prefMisc.backTimeout();
859 m_tabWidth = prefMisc.tabWidth();
860 m_sourceFilter = prefMisc.sourceFilter();
861 if (m_sourceFilter.isEmpty())
862 m_sourceFilter = defaultSourceFilter;
863 m_headerFilter = prefMisc.headerFilter();
864 if (m_headerFilter.isEmpty())
865 m_headerFilter = defaultHeaderFilter;
868 if (m_tabWidth != oldTabWidth) {
869 emit setTabWidth(m_tabWidth);
873 void DebuggerMainWnd::setTerminalCmd(const QString& cmd)
875 m_outputTermCmdStr = cmd;
876 // revert to default if empty
877 if (m_outputTermCmdStr.isEmpty()) {
878 m_outputTermCmdStr = defaultTermCmdStr;
882 void DebuggerMainWnd::setDebuggerCmdStr(const QString& cmd)
884 m_debuggerCmdStr = cmd;
885 // make empty if it is the default
886 if (m_debuggerCmdStr == GdbDriver::defaultGdb()) {
887 m_debuggerCmdStr = QString();
891 void DebuggerMainWnd::slotDebuggerStarting()
893 if (m_debugger == 0) /* paranoia check */
894 return;
896 if (m_ttyLevel == m_debugger->ttyLevel())
897 return;
899 // shut down terminal emulations we will not need
900 switch (m_ttyLevel) {
901 case KDebugger::ttySimpleOutputOnly:
902 m_ttyWindow->deactivate();
903 break;
904 case KDebugger::ttyFull:
905 if (m_outputTermProc != 0) {
906 m_outputTermProc->kill();
907 // will be deleted in slot
909 break;
910 default: break;
913 m_ttyLevel = m_debugger->ttyLevel();
915 QString ttyName;
916 switch (m_ttyLevel) {
917 case KDebugger::ttySimpleOutputOnly:
918 ttyName = m_ttyWindow->activate();
919 break;
920 case KDebugger::ttyFull:
921 if (m_outputTermProc == 0) {
922 // create an output window
923 ttyName = createOutputWindow();
924 TRACE(ttyName.isEmpty() ?
925 "createOuputWindow failed" : "successfully created output window");
927 break;
928 default: break;
931 m_debugger->setTerminal(ttyName);
934 void DebuggerMainWnd::slotToggleBreak(const QString& fileName, int lineNo,
935 const DbgAddr& address, bool temp)
937 // lineNo is zero-based
938 if (m_debugger != 0) {
939 m_debugger->setBreakpoint(fileName, lineNo, address, temp);
943 void DebuggerMainWnd::slotEnaDisBreak(const QString& fileName, int lineNo,
944 const DbgAddr& address)
946 // lineNo is zero-based
947 if (m_debugger != 0) {
948 m_debugger->enableDisableBreakpoint(fileName, lineNo, address);
952 QString DebuggerMainWnd::createOutputWindow()
954 // create a name for a fifo
955 QString fifoName;
956 fifoName.sprintf("/tmp/kdbgttywin%05d", ::getpid());
958 // create a fifo that will pass in the tty name
959 QFile::remove(fifoName); // remove remnants
960 #ifdef HAVE_MKFIFO
961 if (::mkfifo(fifoName.local8Bit(), S_IRUSR|S_IWUSR) < 0) {
962 // failed
963 TRACE("mkfifo " + fifoName + " failed");
964 return QString();
966 #else
967 if (::mknod(fifoName.local8Bit(), S_IFIFO | S_IRUSR|S_IWUSR, 0) < 0) {
968 // failed
969 TRACE("mknod " + fifoName + " failed");
970 return QString();
972 #endif
974 m_outputTermProc = new K3Process;
977 * Spawn an xterm that in turn runs a shell script that passes us
978 * back the terminal name and then only sits and waits.
980 static const char shellScriptFmt[] =
981 "tty>%s;"
982 "trap \"\" INT QUIT TSTP;" /* ignore various signals */
983 "exec<&-;exec>&-;" /* close stdin and stdout */
984 "while :;do sleep 3600;done";
985 // let config file override this script
986 QString shellScript;
987 if (!m_outputTermKeepScript.isEmpty()) {
988 shellScript = m_outputTermKeepScript;
989 } else {
990 shellScript = shellScriptFmt;
993 shellScript.replace("%s", fifoName);
994 TRACE("output window script is " + shellScript);
996 QString title = KGlobal::caption();
997 title += i18n(": Program output");
999 // parse the command line specified in the preferences
1000 QStringList cmdParts = QStringList::split(' ', m_outputTermCmdStr);
1003 * Build the argv array. Thereby substitute special sequences:
1005 struct {
1006 char seq[4];
1007 QString replace;
1008 } substitute[] = {
1009 { "%T", title },
1010 { "%C", shellScript }
1013 for (QStringList::iterator i = cmdParts.begin(); i != cmdParts.end(); ++i)
1015 QString& str = *i;
1016 for (int j = sizeof(substitute)/sizeof(substitute[0])-1; j >= 0; j--) {
1017 int pos = str.find(substitute[j].seq);
1018 if (pos >= 0) {
1019 str.replace(pos, 2, substitute[j].replace);
1020 break; /* substitute only one sequence */
1023 *m_outputTermProc << str;
1026 QString tty;
1028 if (m_outputTermProc->start())
1030 // read the ttyname from the fifo
1031 QFile f(fifoName);
1032 if (f.open(QIODevice::ReadOnly))
1034 QByteArray t = f.readAll();
1035 tty = QString::fromLocal8Bit(t, t.size());
1036 f.close();
1038 f.remove();
1040 // remove whitespace
1041 tty = tty.stripWhiteSpace();
1042 TRACE("tty=" + tty);
1044 connect(m_outputTermProc, SIGNAL(processExited(K3Process*)),
1045 SLOT(slotTermEmuExited()));
1047 else
1049 // error, could not start xterm
1050 TRACE("fork failed for fifo " + fifoName);
1051 QFile::remove(fifoName);
1052 shutdownTermWindow();
1055 return tty;
1058 void DebuggerMainWnd::slotTermEmuExited()
1060 shutdownTermWindow();
1063 void DebuggerMainWnd::shutdownTermWindow()
1065 delete m_outputTermProc;
1066 m_outputTermProc = 0;
1069 void DebuggerMainWnd::slotProgramStopped()
1071 // when the program stopped, move the window to the foreground
1072 if (m_popForeground) {
1073 // unfortunately, this requires quite some force to work :-(
1074 KWindowSystem::raiseWindow(winId());
1075 KWindowSystem::forceActiveWindow(winId());
1077 m_backTimer.stop();
1080 void DebuggerMainWnd::intoBackground()
1082 if (m_popForeground) {
1083 m_backTimer.start(m_backTimeout, true); /* single-shot */
1087 void DebuggerMainWnd::slotBackTimer()
1089 lower();
1092 void DebuggerMainWnd::slotRecentExec(const KUrl& url)
1094 QString exe = url.path();
1095 debugProgram(exe, "");
1098 QString DebuggerMainWnd::makeSourceFilter()
1100 QString f;
1101 f = m_sourceFilter + " " + m_headerFilter + i18n("|All source files\n");
1102 f += m_sourceFilter + i18n("|Source files\n");
1103 f += m_headerFilter + i18n("|Header files\n");
1104 f += i18n("*|All files");
1105 return f;
1109 * Pop up the context menu in the locals window
1111 void DebuggerMainWnd::slotLocalsPopup(Q3ListViewItem*, const QPoint& pt)
1113 Q3PopupMenu* popup =
1114 static_cast<Q3PopupMenu*>(factory()->container("popup_locals", this));
1115 if (popup == 0) {
1116 return;
1118 if (popup->isVisible()) {
1119 popup->hide();
1120 } else {
1121 popup->popup(pt);
1126 * Copies the currently selected item to the watch window.
1128 void DebuggerMainWnd::slotLocalsToWatch()
1130 VarTree* item = m_localVariables->selectedItem();
1132 if (item != 0 && m_debugger != 0) {
1133 QString text = item->computeExpr();
1134 m_debugger->addWatch(text);
1139 * Starts editing a value in a value display
1141 void DebuggerMainWnd::slotEditValue()
1143 // does one of the value trees have the focus
1144 QWidget* f = kapp->focusWidget();
1145 ExprWnd* wnd;
1146 if (f == m_localVariables) {
1147 wnd = m_localVariables;
1148 } else if (f == m_watches->watchVariables()) {
1149 wnd = m_watches->watchVariables();
1150 } else {
1151 return;
1154 if (m_localVariables->isEditing() ||
1155 m_watches->watchVariables()->isEditing())
1157 return; /* don't edit twice */
1160 VarTree* expr = wnd->currentItem();
1161 if (expr != 0 && m_debugger != 0 && m_debugger->canSingleStep())
1163 TRACE("edit value");
1164 // determine the text to edit
1165 QString text = m_debugger->driver()->editableValue(expr);
1166 wnd->editValue(expr, text);
1170 // helper that gets a file name (it only differs in the caption of the dialog)
1171 static QString myGetFileName(QString caption,
1172 QString dir, QString filter,
1173 QWidget* parent)
1175 QString filename;
1176 KFileDialog dlg(dir, filter, parent);
1178 dlg.setCaption(caption);
1180 if (dlg.exec() == QDialog::Accepted)
1181 filename = dlg.selectedFile();
1183 return filename;
1186 void DebuggerMainWnd::slotFileOpen()
1188 // start browsing in the active file's directory
1189 // fall back to last used directory (executable)
1190 QString dir = m_lastDirectory;
1191 QString fileName = m_filesWindow->activeFileName();
1192 if (!fileName.isEmpty()) {
1193 QFileInfo fi(fileName);
1194 dir = fi.dirPath();
1197 fileName = myGetFileName(i18n("Open"),
1198 dir,
1199 makeSourceFilter(), this);
1201 if (!fileName.isEmpty())
1203 QFileInfo fi(fileName);
1204 m_lastDirectory = fi.dirPath();
1205 m_filesWindow->setExtraDirectory(m_lastDirectory);
1206 m_filesWindow->activateFile(fileName);
1210 void DebuggerMainWnd::slotFileExe()
1212 if (m_debugger->isIdle())
1214 // open a new executable
1215 QString executable = myGetFileName(i18n("Select the executable to debug"),
1216 m_lastDirectory, 0, this);
1217 if (executable.isEmpty())
1218 return;
1220 debugProgram(executable, "");
1224 void DebuggerMainWnd::slotFileCore()
1226 if (m_debugger->canStart())
1228 QString corefile = myGetFileName(i18n("Select core dump"),
1229 m_lastDirectory, 0, this);
1230 if (!corefile.isEmpty()) {
1231 m_debugger->useCoreFile(corefile, false);
1236 void DebuggerMainWnd::slotFileProgSettings()
1238 if (m_debugger != 0) {
1239 m_debugger->programSettings(this);
1243 void DebuggerMainWnd::slotViewStatusbar()
1245 if (statusBar()->isVisible())
1246 statusBar()->hide();
1247 else
1248 statusBar()->show();
1249 setSettingsDirty();
1252 void DebuggerMainWnd::slotExecUntil()
1254 if (m_debugger != 0)
1256 QString file;
1257 int lineNo;
1258 if (m_filesWindow->activeLine(file, lineNo))
1259 m_debugger->runUntil(file, lineNo);
1263 void DebuggerMainWnd::slotExecAttach()
1265 #ifdef PS_COMMAND
1266 ProcAttachPS dlg(this);
1267 // seed filter with executable name
1268 QFileInfo fi = m_debugger->executable();
1269 dlg.setFilterText(fi.fileName());
1270 #else
1271 ProcAttach dlg(this);
1272 dlg.setText(m_debugger->attachedPid());
1273 #endif
1274 if (dlg.exec()) {
1275 m_debugger->attachProgram(dlg.text());
1279 void DebuggerMainWnd::slotExecArgs()
1281 if (m_debugger != 0) {
1282 m_debugger->programArgs(this);
1286 void DebuggerMainWnd::slotConfigureKeys()
1288 KShortcutsDialog::configure(actionCollection());
1291 #include "dbgmainwnd.moc"