Poor Man's Debugging?

Status
Not open for further replies.

earl

Well-known member
3,066
You don't get left helpless when writing stuff involving multiple processes, multiple threads, embedded, inside a kernel, with a complex / message passing GUI, remotely, or anywhere else where debuggers tend to choke.

Yes I do know that with enough time, software, and hardware, you can probably configure a debugger to run in most of those environments. Not easily, and most managers won't drop a bunch of money just so you can debug something. We do some embedded work and finally got a decent oscilloscope.

-earl-
 

asdfjkl;

Well-known member
1,351
Originally posted by muppetman559:
I was reading a post about developing skills necessary for real life programming, and someone was saying that learning how to debug things without using a debugger through use print statements and dumping output to a file. What are the advantages of these techniques compared to using a debugger in your IDE?

having print statements everywhere is a pain in the ass ?

there's nothing to be learned from not using a good debugger
 

DrPizza

Well-known member
93,870
Originally posted by ThoughtCriminal:
You can see how your program is doing in places a debugger doesn't work to well. Callback functions I think my fit in that catagory.
Maybe in your debugger. Certainly, they work perfectly well in mine.

Driver development too.
So this stuff WinNT outputs to its serial port is just for fun? No, it's not.

You can monitor your program without the debugger stopping it and making you step thru.
Yes, I can. Can I see what's in memory at the time? No, not easily (Yes, I have tools for dumping a process's memory space to a file non-intrusively, but no, they're not very convenient to use). Is printing out to a file helpful? Yes. But it relies on you printing out the right thing. Sometimes, I don't know where the problem lies, or I'm mistaken in my assumption. A debugger doesn't know or care -- it can show me anything. It also gives me a stack trace, which I find invaluable.

Release build. Release builds in VC++ have no debugging info.
Of course they can. You think that few hundred Mbytes of crap I have in \winnt\symbols is just there for shits and giggles?
 

EgalitarianBovine

Ars Tribunus Angusticlavius
8,086
Debuggers are nice for certain tasks, but other are harder. Developing embedded software, for example, you don't have access to a debugger that can run in an embedded device and let you track stuff.

Also, debuggers are not mature enough in concurrent situations (multiple threads, continuations, etc.) to give you the flexibility you need (sometimes).

printf debugging is something that can be used _anywhere_ you have access to an independent byte stream to which you can print stuff.

On the other hand, for most common situations, debuggers make it easier and quicker to analyze data structures and quickly step through programs.

Bottom line is, it's possible to debug without special tools, and you should be able to do that, just in case you ever run across a situation where debuggers are not available.

-EB
 

_klamath_

Ars Scholae Palatinae
770
According to Harlan Mills: "An interactive debugger is an outstanding example of what is not needed -- it encourages trial-and-error hacking rather than systematic design, and also hides marginal people barely qualified for precision programming."

I'm not sure if I agree with him, but it's an interesting opinion. A lot of the other ACM pundits (e.g. Dijkstra) don't like debuggers either, IIRC.

Personally, I've never found the need for one...
 

Victim12

Ars Tribunus Militum
1,978
Here's one real world example of "print statement debugging":

Every once in a while I come into work and people tell me my program handed back an error and didn't work right. So I go check the log file it made while it was running last night, which is pretty much a bunch of print statements with different levels of importance (like debug statements, warnings, critical errors, informative stuff). And so I just look at the logs my program made while running last night and figure out what happened.

It's nice to have that instead of trying to reproduce the error while using a debugger, lots of those errors are those sneaky things that are hard to repeat, and sometimes if I used a test or development computer it would work just fine instead of the production machine where my program messed up.
 
Originally posted by _klamath_:
According to Harlan Mills: "An interactive debugger is an outstanding example of what is not needed -- it encourages trial-and-error hacking rather than systematic design, and also hides marginal people barely qualified for precision programming."

I'm not sure if I agree with him, but it's an interesting opinion. A lot of the other ACM pundits (e.g. Dijkstra) don't like debuggers either, IIRC.

Personally, I've never found the need for one...
Harlan Mills comes from a mainframe background, and wrote that during the 1980s. IMO it's ignoring the uses to which people put such tools. His background is that of a mainframe programmer, where the programmer needn't necessarily even compile that which they've written -- where the idea of using a compiler to catch simple typos would be unheard of. IMO, that's a waste of my time. Reading my code to check I haven't transposed a character here or there isn't a valuable use of my time -- the compiler can do it much more quickly and much more accurately. Similarly, sprinkling <span style="font-familiy: 'Lucida Console', monospace;">printf()</span>s all over the place will never tell me as much as an interactive debugger. If I have some reasonably complex structure, I would have to spend a not inconsiderable amount of time simply writing statements to display its state. Fuck alone knows what I'd have to do to get a nice stack trace for a crash.

A debugger lets me step through line-by-line to ensure that the program is doing what I expect it to do. The is by no means mutually exclusive with good design. Asking the computer to see if there are any errors in the code is no less valid than asking one's peers. Good design does not guarantee accurate implementation.
 
Originally posted by Victim12:
Every once in a while I come into work and people tell me my program handed back an error and didn't work right. So I go check the log file it made while it was running last night, which is pretty much a bunch of print statements with different levels of importance (like debug statements, warnings, critical errors, informative stuff). And so I just look at the logs my program made while running last night and figure out what happened.

It's nice to have that instead of trying to reproduce the error while using a debugger, lots of those errors are those sneaky things that are hard to repeat, and sometimes if I used a test or development computer it would work just fine instead of the production machine where my program messed up.
If you're in an environment where you can afford to have your programs spitting out details of their internals 24/7, I would imagine it's more useful than if you can't. For most of what I do, it wouldn't be appropriate to do such things. There might be an option to have varying levels of output produced, but if someone turns the highest level on after the fact, it doesn't do much to help.
 

Victim12

Ars Tribunus Militum
1,978
If you're in an environment where you can afford to have your programs spitting out details of their internals 24/7, I would imagine it's more useful than if you can't. For most of what I do, it wouldn't be appropriate to do such things. There might be an option to have varying levels of output produced, but if someone turns the highest level on after the fact, it doesn't do much to help.

You're exactly right. For many (most?) programs, you don't want a megabyte worth of logs being written out every day. I wouldn't do something like that for every program, but for this one it is a BIG help, because there are so many things that could be different if you tried to reproduce the error the next day. It's just an example, I don't want to tell people to ditch their debugger because that is also extremely useful.
 
Originally posted by ThoughtCriminal:
Kick ass, I can have debugging symbols in VC++ for release builds? Can you tell me how?

One place I worked all the programmers told me release builds couldn't have debuggin info. There were experienced programmers too :eek:
Tell the compiler to produce a program database with /Zi.
Tell the linker to produce a debug file with /DEBUG.
Optionally, tell the linker to strip private symbols with /PDBSTRIPPED.
 

Gorth

Ars Praefectus
4,336
Subscriptor
Originally posted by Victim12:
It's nice to have that instead of trying to reproduce the error while using a debugger, lots of those errors are those sneaky things that are hard to repeat, and sometimes if I used a test or development computer it would work just fine instead of the production machine where my program messed up.

If you've got a strong debugger like gdb handy, you don't have to reproduce it... have your prg dump core and then you can load that core file and work from there. VERY nice with 'randomly' disappearing services.
 
PeterB, most of that apparent crap I spewed out up there was from programmers I've worked with. I am not a professional programmer myself.

One guy I knew working in printer drivers said the only way to debug them was directing debug messages to a file.
--------------------------------------------
Thanks for explaining how to get debug code in VC release. I have been using VC IDE for assembly programming with MASM. Now that I'm moving more to C++, I have a lot more toys to find/learn in VC.
 
Originally posted by PsychoOne:
Peter, can you elaborate on Post-mortem debugging with VC++? I've used gdb on core files before, but I have no idea on how to go about loading a memory dump into VC++ debugger. A hint would be most appreciated by myself, and I am sure others. Thanks!

PO
VC++ doesn't do post-mortem debugging (only interactive and JIT debugging).

However, windbg, MS's standalone debugger, does (it's also both a kernel-mode and user-mode debugger). Somewhere on MS's site is a debug tools download, and it should contain the current version of windbg. It'll load a crash dump, plus appropriate .pdbs, to give reasonably detailed information. It's clunkier than VC++ (it's command-line driven, more or less), but it works.

As for generating the dumps, Dr Watson can do it, and the userdump tool from MS can also do it. userdump can do it on-demand, and, if you want, without terminating the application.
 
According to Harlan Mills: "An interactive debugger is an outstanding example of what is not needed -- it encourages trial-and-error hacking rather than systematic design, and also hides marginal people barely qualified for precision programming."
As great a tool as an interactive debugger (especially a good one) is, that statement is unfortunately very true. A lot of people don't quite know how to code... so they try something, it doesn't compile. They try again, it compiles. They run it, it doesn't quite work. They trace it in the debugger, find a location to patch it, insert an if statement, and away the code goes. Sad, but true.
 

Victim12

Ars Tribunus Militum
1,978
If you've got a strong debugger like gdb handy, you don't have to reproduce it... have your prg dump core and then you can load that core file and work from there. VERY nice with 'randomly' disappearing services.

I'm no expert with gdb, I just know the basics of stepping through code and examining stuff... I have a few questions about this, it will be pretty cool if I can do this.

1) Can I call a function or something that will make my program dump core at a moment where an error was discovered, but continue to run? And then load that core into gdb later on to examine?

2) Can gdb trace execution of a program backwards from where it dumped core, and let me examine previous values for a variable?
 
Hey Victim12,

1) Can I call a function or something that will make my program dump core at a moment where an error was discovered, but continue to run? And then load that core into gdb later on to examine?

Use abort(3)

2) Can gdb trace execution of a program backwards from where it dumped core, and let me examine previous values for a variable?

In a sense. You get the record of all your stack frames up to the point, so you can do things like stepping back from your function to the caller, and seeing if something (e.g. a global) has changed. As far as I know, to do anything more complicated, you'd have toactually start your program from within GDB, and set a watch on your variable of interest. However, I am a relative newbie when it comes to GDB, perhaps someone else is more qualified to answer.

PO
 

Gorth

Ars Praefectus
4,336
Subscriptor
Originally posted by PsychoOne:
In a sense. You get the record of all your stack frames up to the point, so you can do things like stepping back from your function to the caller, and seeing if something (e.g. a global) has changed. As far as I know, to do anything more complicated, you'd have toactually start your program from within GDB, and set a watch on your variable of interest. However, I am a relative newbie when it comes to GDB, perhaps someone else is more qualified to answer.

PO

Right on. The core dump is a snapshot of the registers and variables along with your calling stack at that point. You can't (at least AFAIK) execute anything from it. But it does tell you exactly where your crash occured, you can find which variable was wrong and then look back in your code from there. It would be nice to continue, but critical errors like a seg fault just can't (gracefully) be cotinued after...
 

bugbum1

Ars Scholae Palatinae
893
We do some embedded work and finally got a decent oscilloscope. -earl-
yeah, it's hard to run a debugger on a pic processor :D . in some types of embedded work, ICE (in circuit emulation) is the only way to find problems (and printf debugging means driving a line high and setting the oscilloscope trigger). if you really need a debugger, you can try CPU simulation... but only if you have the big bucks.
 

johnfoobar

Seniorius Lurkius
11
look at log4j. it's a logging api for java, but the principles apply to software in general. (there is log4py and log4cpp also).

highlights:
- you can put logging statements into your code and leave them there forever (no more commenting out printfs or printlns).
- you can assign your logging statements different levels of priority and filter them based on the priority you're interested in (INFO, DEBUG, WARN, FATAL, etc.)
- you configure log reading software to get only the logging output you want (there is log reading software for terminals, syslog etc. with log4j).
- it's optimised so the cost of logging is trivial. (this is quantified on the website). i have encountered a bit of FUD when introducing proper logging to projects i'm working on so back yourself up with some evidence/ammo to zap that before you try to sell it.

in the 'practise of programming' (great book), kernighan and pike mention how debuggers aren't all they're cracked up to be.

the other point worth remembering is that in distributed systems (which is basically everything n-tier where n > 1) you generally don't have debugging tools because they're a PITA to set up.

as you can tell, i'm sold on logging. 'nuff said.
 
Pros/Cons of printline debugging.

Pros:
1) Forces you to think about what you're looking for. (i.e. where is the problem occuring and what nugget of information would best help me understand whats going on).
2) Validates your assertions of what you expect.
3) Sorta a nice log.


Cons:
1) Too many debug/printf statements causes problems of seperating the wheat from the chaff (what the hell is all this crap!)
2) Turn-around time.
3) efficiency, I/O is expensive!
 

johnfoobar

Seniorius Lurkius
11
> Pros:
> 1) Forces you to think about what you're looking for.
> (i.e. where is the problem occuring and what nugget of
> information would best help me understand whats going on).
> 2) Validates your assertions of what you expect.
> 3) Sorta a nice log.

agreed.

> Cons:
> 1) Too many debug/printf statements causes problems of
> seperating the wheat from the chaff (what the hell is all
> this crap!)

aka scrolling blindness.. this is why assigning your logging statements categories (usually corresponding to priority levels) is a Good Thing.

first of all, the act of assigning a category makes you think about what you're printing and why you're printing it. second, you can filter the output you see based on the category or categories you want to see, hence you don't have any chaff, just wheat.

each logging statement takes one line of code; no more than a println.

> 2) Turn-around time.

with logging, you do it once and do it right (hopefully anyway). the same applies to testing, and pays similar dividends.

> 3) efficiency, I/O is expensive!

logging libraries like log4j are optimised so that when you don't want logging output, the I/O is eliminated altogether. (the code checks this, so that the penalty in production code is no more than the cost of evaluating an if branch, which translates (more or less) into a branch-not-equal in machine code).
 
Status
Not open for further replies.