Go back to cdw documentation
Debugging curses or ncurses programs with gdb in emacsThis article describes setup for debugging console programs that use curses/ncurses (possibly even slang) libraries with gdb debugger running in GNU emacs 23. I experienced some problems with this setup, and I decided to describe solution that worked for me. Author: Kamil Ignacak, acerion at wp dot pl Table of contents: |
IntroductionSimplest way to debug console program is to start gdb and run the program inside of gdb. Big disadvantage of this solution is that gdb commands and output are mixed with debugged program's output. If debugged program uses curses/ncurses or slang user interface library, the problem only gets worse. Solution to this problem, found in many materials on the internet and in books is to use two separate terminals: one terminal for interacting with gdb, second terminal for interacting with debugged program. This works fine as long as we work gdb + debugged program alone, but I discovered some problems when I tried to run gdb inside of emacs and debug a program from within emacs + gdb pair. These problems (and method of solving them) are described in long version of solution. This article is mostly a compilation of pieces of information found in various places on the internet. This article is about debugging with emacs + gdb pair, but instructions listed below will work both if we run gdb inside emacs and if we run "standalone" gdb. First I provide short version of solution for impatient readers, then long version with screenshots and more explanation. Tools used:
This document uses following symbols:
|
Solution - short version
That's all: emacs/gdb + external terminal combo is now ready for debugging session. If we haven't done this before, we can specify full path to debugged program now with "file" command in gdb [1]. See Cleaning up section for information on what to do after ending debugging session. Top of page |
Solution - long versionSolution most often found on the internet when searching for "debugging console programs in gdb" is to redirect input and output of debugged program to a different, separate terminal. Basics of redirecting input/output are described in paragraph 4.6 of gdb manual [2]. It turns out that in some cases this is not enough, because we need to configure our debugging environment a bit further. Steps listed below present full reasoning behind short version of procedure that you've seen above. This full reasoning includes some trials and errors, and full procedure with backtracking goes like this:
This is where most of widely available instructions end. They work pretty well for non-(n)curses program executed under gdb + emacs pair, or for ncurses program executed under gdb alone. However it is not so easy for mix of emacs+gdb+ncurses program. This is what we can see in program terminal when we follow steps listed above, running in gdb demo_forms, a sample ncurses program: This is clearly not what we expected. Other, more advanced programs using ncurses library give a hint at a source of this problem. Midnight Commander, when invoked in gdb, prints this message before exiting: "Your terminal lacks the ability to clear the screen or position the cursor.". This means that terminal mode used by gdb may be incorrect. This leads to next steps in our procedure, where we first check what is the terminal mode of program terminal, and then we check what gdb thinks about its current terminal mode:
After completing these steps and running the demo_forms sample program in gdb we can see that results are a lot better, but there are some problems with frames displayed in terminal window: Program's output looks like it doesn't quite fit into terminal, as if the debugged program thinks that terminal's size is different than it really is. Let's investigate further:
And that is all, this is how emacs, gdb and additional terminal are configured for debugging session. When you finish your debugging session you may need to clean up in your program terminal. Top of page |
Cleaning upWhen we finish debugging session we can restore control in program terminal by first closing debugged program (if we haven't done that already), and then pressing Ctrl+C to exit that "sleep 1000000" command. If debugged program didn't exit correctly it may happen that program terminal doesn't react correctly to keyboard input. We have to press Ctrl+j to restore it's state. Alternatively we can try issuing "reset" command (we may not see entered characters). Top of page |
References[1] Debugging with GDB - 18.1 Commands to Specify Files [2] Debugging with GDB - 4.6 Your Program's Input and Output [3] Using GNU's GDB Debugger / Debugging Ncurses Programs, a tutorial written by Peter Jay Salzman. Most comprehensive source on the topic described here that I found on the internet. Currently (17 Feb 2010) the site is offline, so try to find cached article. Update, 28 Dec 2010: you can find the document here: http://www3.sympatico.ca/rsquared/gdb/dnp.html, as republished by Rob Somers (thanks for the link Rob!). You may want to check whole tutorial on gdb. [4] Debugging with GDB - 4.4 Your Program's Environment A small survey about debugging techniques A discussion about emacs and gdb A discussion about gdb and ncurses libchewing-0.3.2/test/README - a small readme file from chewing library package Top of page |
Change Log
|
CopyrightPublic Domain |
Top of page Go back to cdw documentation Copyright (C) 2007 - 2016 Kamil Ignacak |