Non-VisualC++ debuggers?

Status
Not open for further replies.
Heya :)

I'm kinda new to programming. Here at my university we compile with g++ on the university's server, by remotely logging into our account etc etc.

I write my code using UltraEdit, which is pretty cool. However, it would be nice if there would be something to help me when I'm stuck with bugs.

So is there some kind of program out there that helps to find syntax/logic errors? Apart from the compiler of course ;)
 
gdb!

The de-facto unix debugger. If you are used to the point-and-click style of VC++, you will be initially in a state of shock. Don't worry, it'll pass. While graphical debuggers are nice, there really isn't anything they can do that can't be done from command line.

To use gdb, all you do is compile your executable with debug symbols. This involves calling your compiler with a -g file. For instance,
code:<HR>
Code:
gcc -g myfile.c -o myfile
After that, just type gdb myfile, open another window for help (man pages for gdb are sort of useless, I prefer the info documentation - info gdb, if it's available on your system). You won't be immediately productive, but learning to use gdb is a worthwhile investment.

PO
 

DaisyMan

Ars Tribunus Militum
2,135
there really isn't anything they can do that can't be done from command line.
OK, I'll bite: how does one step through the code using a command-line debugger? set a breakpoint? add a variable watch? jump around in your code while the program is running?

I can't see not having a UI of *some* sort for that. It could be a yucky DOS-like pseudo-GUI, but don't you need some GUI for that kind of easy interaction?
 

w_mute

Ars Scholae Palatinae
1,139
Subscriptor++
OK, I'll bite: how does one step through the code using a command-line debugger? set a breakpoint? add a variable watch? jump around in your code while the program is running?

I can't see not having a UI of *some* sort for that. It could be a yucky DOS-like pseudo-GUI, but don't you need some GUI for that kind of easy interaction?

gdb would be considered a command line debugger. Basically its like its own shell (think DOS prompt or whatever you like). You can set watches, have values of variables displayed after each execution step, examine memory, etc etc. An example is that DDD is just a GUI for gdb.

So for gdb:
Step Into: = 's or step'
Next: = 'n or next'
Breakpoint: = b linenum
or: b filename.C:linenum
Variable Watch: I forget and am too lazy to look it up, but its there :D.

Some versions of gdb don't let you abbreviate commands so you might have to spell out step, next, breakpoint or whatever. Its got a decent built in-help/command reference.

As far as jumping around, did you mean just looking at different part of source? If so its pretty easy: 'list x' will list a set of lines (10 I think?) from the current source file starting at line 'x'. If you omit x I think it lists from the current position in code. 'list blah.c:x' works similarly.

Yes, but can it be done just as fast? I have strong doubts (I've never used a command line debugger though).

In general yes. Due to textmode limits sometimes watching lots of variables can be a pain...and using an older version of gdb can also be slower (no command abbreviation, other UI annoyances). Again, I point to DDD as an illustration of gdb's power. Once you learn GDB its quite fast in general.

gdb's main problem arises when handling multiprocess programs. The only way to debug a child process is an ugly workaround on most systems.

-Greg
 
Awww damnit,

When I replied to this fellow's post, the LAST thing I wanted was to start yet another GUI vs. CLI flamewar. Come on people, don't bite when it's obvious I wasn't trolling. I like graphical debuggers, but to suggest that they are the only way to do things is preposterous. And when you develop software for unix, a graphical debugger is oftentime a productivity hidrance rather than improvement. Which doesn't mean, of course, that there are no graphical debuggers for UNIX - I myself have used DDD (which is a GUI front-end for gdb, with some nifty graphing features on top), and I know there are countless others. Right tool for the right job, correct?

PO
 

DaisyMan

Ars Tribunus Militum
2,135
w_mute:

I'll confess I've never seen it, so maybe my mental image is flawed, but that doesn't sound very convenient...

What if I want to keep a watch on five variables at once - as I step through each line, will it print out all five variables again? Or ten, or twenty? That sounds rather messy.

With regard to scanning over the source (yes, I meant what you described) and setting breakpoints, how do you know what line number you want? I guess its OK for a little program, but for larger ones I would think I'd be switching back and forth between some other window with the source in it, almost constantly, to figure out which line I want to move to. Do you really keep all the line numbers in your head? And mentally update your numbers whenever you edit code, possibly code two hundred lines prior? :eek:

Psycho: not trying to start a flame-war, I've just never really thought of debugging in anything *but* a GUI. I'm honestly curious as to how they arrange such a thing. All in good fun! :)
 

Liam

Ars Scholae Palatinae
618
Jubai, if you're _telnetting_ into your University's server, you're going to be stuck with good ol' commandline gdb. If you use Emacs to edit your source, it has a relatively nice (or so I've seen) interface with gdb that will let you step pseudo-graphically through your code (by highlighting the current line in the edit window; breakpoints, watches, etc. are still set through the commandline). It looks decent, although I don't use Emacs, so I can't comment seriously on it's usefulness. If you're opening an X session, then you could worry about the linux GUI tools mentioned.

My personal advice, if you've got a computer you can install software on (i.e. not a lab somewhere), is to download the Cygnus Cygwin development tools. It's the bash shell plus all your favourite (or not) unix tools, ported to Windows. XFree86 even compiles for it out of the box. Then do your development locally, confident that porting from your Windows g++ to your University's UNIX g++ will be a LOT easier than porting from, say, Visual C++. The Cygwin port of the gdb has a NICE GUI frontend that does rather everything you'd expect. Very much like Visual Studio's IDE, as far as functionality. PLUS, you can whip out the commandline. When working on programs for school, I use g++ locally and port to their system. They're using an older version of g++ (it's an HP-UX system), so I run into some stuff, but not too much. If your University is smart enough to have a more recent g++ (say on a Linux box), it should be almost painless.

Also, I might suggest not to be afraid of "printf-debugging." Throw copius cout << "Got here." << endl statements when you've got seemingly endless loops who-knows-where. This really helps you figure out what the control flow is. Of course, don't make EVERY message say "got here," and print out the values of key variables, etc., so you can see what's going on. You don't _really_ need a debugger to debug, although I'll admit that it is nice.

I doubt I have to mention to be sure to use -Wall to catch extra errors, and -g to enable debugging symbols (gdb style) to be included in your program.
 
Word of advice to those debugging without using a debugger: when you output something, make sure the output stream/fd is opened in UNBUFFERED mode. Otherwise you will quickly find yourself in a situation where your code crashes shortly after the last printf/cout/whatever, but since it was not open in unbuffered mode, the last message (or two!) never got printed, and then you spend hours looking for a crash in the wrong place. I usually just send all output to stderr, which is guaranteed to be opened in unbuffered mode by default. I think C++'s cerr works the same way. I might be supersticious, but I always try to use C-style IO for debugging info - I guess C++'s stream stuff just feels too much like black magic :)
 

Liam

Ars Scholae Palatinae
618
Agreed. Make sure you include "<<endl" after any and all debugging statements that go to any streams anywhere. Not only does it append a newline, it flushes the stream. When doing printf()s, don't forget your fflush()s!

Also (don't laugh, I saw this happen): if you're writing debugging info to a file, make SURE you only have ONE copy of the file opened at any given time. If you open it in your program several times in append mode, all sorts of bizarre things will happen, and your debugging info will be rendered mostly useless.

PO: yes, you sound overly superstitious. But there's a fair amount of magic that happens in that box, and a little superstition never hurt anybody! ;)
 

gumbo

Ars Tribunus Angusticlavius
6,107
Originally posted by bugbum1:
Quote: "Cygnus has a graphical version of gdb called Insight for Cygwin - "

erm, we tried using Insight and it's broken. Red Hat will fix it for as a custom job for $25,000 according to their rep... now that's some expensive "free" software.

Damn. I am going to see if I can get DDD to compile - others have done it. I want a good visual mingw debugger - I hate the cygwin dll restriction.

gubmo

[This message was edited by gubmo on December 28, 2001 at 09:15.]
 
I haven't seen this mentioned yet, so here goes. If you use gdb under Emacs, Emacs will split your screen with one half the gdb command line and in the other half the appropriate source file with current line marked with an arrow. For me, that's the single most important feature of a debugger, being able to match the debugger's output to my source code.

As for watch variables, I believe "watch" is the command under gdb, and gdb will print a message to indicate when the value has changed and what the new value is.
 
Daisyman, why don't you just try it? Pretty much every recent unix installation in the world will have gcc and gdb installed. Write a small program, compile it with symbols, get a cup of strong coffee, and waste a few hours playing around with gdb (mostly reading the info/manual pages). Who knows, you might dig it.

When I write code for windows, VC++'s excellent debugger is my lifesaver. But when I get down and dirty in unix, there is no way I can be nearly as productive using a point-and-click GUI debugger. Let me give you an example. I once wrote a small remote file access library for unix - the idea was that in any C program you could just include my header file, and then instead of saying open("/path/to/myfile.txt", flags); you could say open("computer@port:/path/to/myfile.txt", flags). This was really an excersize in writing unix networking code, so I added no security features at all - but I did have to write a client and a server. Anyway, towards the end of development - when all the nasty bugs appear - I would quite often have several debugging windows open: for example, I would run the debugger on the client on local machine, and then I would ssh into remote machine and run the server in the debugger, and then I would have yet another window showing the tcpdump output. I shudder to think of how time-consuming it'd be to simultaneously debug two processes (actually, more, because the server process was a daemon that'd fork for each incoming connection, so make it at least 3) on two separate machines in VC++ - yes, the facilities are there, but sometimes the down-and-dirty command line approach just makes more sense. To me, anyhow.
 

w_mute

Ars Scholae Palatinae
1,139
Subscriptor++
What if I want to keep a watch on five variables at once - as I step through each line, will it print out all five variables again? Or ten, or twenty?
If you were to issue:
   display var1
   display var2

It would print something like:
   var1 = ...
   var2 = ...

After each execution step (before each command prompt)
I believe there is an option to have it only re-display the var if its value has changed as well. Displays will get hidden (or truncated) when leaving scopes or if the variable has some invalid (or huge) value.

With regard to scanning over the source (yes, I meant what you described) and setting breakpoints, how do you know what line number you want?
I forgot to mention that when you list any code it prepends the line with a line number. When executing (if you're at a prompt after say a breakpoint or other step) it will print the line of code about to be run too.

Basically yes, gdb would be kind of a pain for larger projects. Programs like ddd attempt to make a nice wrapper for gdb which give you the more modern multiple windows with source and watch lists, visual representation of pointers, etc.

If you say, recompile the program there is a command you can issue to reload it I believe.

I guess imagine a gdb session as such (this is from memory so some details are probably off):
code:<HR>
Code:
gdb myprogram
..gdb info and status prints...<P>(gdb) b 15
(gdb) run
 Breakpoint 1 in myproc.c:15
   15  if(x<10) {
(gdb) print x
   x = 5
(gdb) display x
   x = 5
(gdb) n
   x = 5
   16 printf("Bugger");
(gdb) s
  ...this would step into the printf function where n
     would just run it...
   x = 5
   17 --x;
(gdb) c
  ... c means continue execution until end/break/other
    input needed..<P>   Segmentation fault
(gdb) where
  ..this would print out the program's position, the
     functions that got you to where you
     were (like main():65->sub1():10->sub2():30)...
(gdb) run
Program terminated.  Restart execution from begining?

So yeah, there's a semi accurate picture of gdb.

-Greg
 
One very cool thing about gdb is i's ability to revive programs from the dead by loading core dump files. VC++ does not have a post-mortem debugger, and only recently did PeterB clued me in on the existance of a free post-mortem debugger for windows. Debugging the apps AFTER they die is a domain largely unfamiliar to people who got their start in VC++.

Overall, I find that it presisely the level of control you need to exersize over your project to make it work that makes UNIX programming tools manageable. When you write every single line of code yourself, write a makefile to make it all come together, figure out the header and library dependencies, run tools like ptrace a few times - when you are done with all that, you *really* know your code. So, then debugging in gdb is not hard, because after you've been around the block a few times, you learn to divide your codebase in manageable modules, to make intuitive (to you) choices that can be recreated at latter time, and to have a really good idea on what is dependant on what.

Notice, this is not an attempt to knock windows programming. ALL good programmers, regardless of language or platform, acquire these skills with time. The difference between unix and windows programming, IMO, is that the former is up and in your face from the get go. VC++ does too much stuff for you by default - managing the project/workspace, figuring out the dependencies, even writing sizable chunk of codes when you use some framework like mfc or atl. It's good at it - but I am a believer in only using the tools when you know WHAT they do.
 

DaisyMan

Ars Tribunus Militum
2,135
Psycho:
why don't you just try it?
'Cuz I'm lazy :p

Is the argument that the debugger is good because it can be used remotely? There are remote debugging options for most of the UI-based debuggers that I know of. Certainly Delphi and MS have it. If I had to remotely debug an app I was writing in any UI-based dev environment, with a UI-based debugger, I don't think I would be unduly inconvenienced.

BTF: Actually, I would argue that a CLI app inherently has no UI since it doesn't take into consideration how the data is presented to the user, nor how it receives data from the user (i.e., how it interfaces). All the programmer is concerned about is *something* talking to STDIN and STDOUT, and posting data to STDOUT. It has no more "user interface" than a COM object has. It has interfaces, certainly, but not a user one.

I think this is further evidenced by the fact that the input to the program could be a file, piped output from another program, or any number of other sources in addition to the user.

The definition of UI in my mind is that it presents a customized interface for users in particular, and is concerned as to the presentation and layout of the input and output aspects of the interface and their "physical" arrangement - ideas manifestly foreign to a CLI.

w_mute: it sounds like I suspected. I could easily see half of my screen getting filled with watch variables on each step, since I can't just drag them out of the way for a second as I can with a UI, and many debugging tasks (for instances, iterating a list of objects, where I want to inspect the object properties at each iteration) can involve a good number of watched variable. Bleh. Thanks for the explanation, though :)
 
aaahh!

I didn't think my thread would get so much replies lol

I downloaded some manuals for gdb, no GUI isn't attractive but if it works that's all that matters. Thanks Psycho for the info :)

I'll check into cygwin too, because I am not confident into writing my code with Vc++ and then go to g++.

I had also heard about Emacs, but didn't know it did stuff with gdb like that.

Thanks all :)
 
One very cool thing about gdb is i's ability to revive programs from the dead by loading core dump files. VC++ does not have a post-mortem debugger, and only recently did PeterB clued me in on the existance of a free post-mortem debugger for windows. Debugging the apps AFTER they die is a domain largely unfamiliar to people who got their start in VC++.
We-ell.

The debugging mode I use most often is a JIT post-mortem debugger (VC++). That is, wait until the program crashes, then attach the debugger immediately to the just-stopped program.
 
Status
Not open for further replies.