January 6th, 2025

Printf Debugging Is OK

Alex Dixon highlights the programming community's debate on IDEs versus text editors, noting junior developers' lack of debugging experience. He advocates for various debugging tools and methods to improve skills.

Read original articleLink Icon
AgreementFrustrationCuriosity
Printf Debugging Is OK

Alex Dixon discusses the ongoing debate in the programming community regarding the use of Integrated Development Environments (IDEs) and debuggers versus simpler text editors like Notepad for coding. He notes that while he regularly uses debuggers such as Visual Studio and Xcode, many junior developers lack experience with debugging tools, which are often not taught in academic settings. Dixon emphasizes that debugging is essential, especially in complex projects where bugs may arise from legacy code or contributions from others. He argues that tools like Address Sanitizer and Undefined Behavior Sanitizer can significantly aid in identifying issues that would otherwise be difficult to track down. While he primarily relies on debuggers, he acknowledges that there are scenarios where 'printf' debugging is necessary, particularly in release builds or when dealing with hardware events. He also advocates for the use of custom debugging tools that complement existing ones, enhancing the debugging process. Ultimately, Dixon concludes that the goal is to effectively find and fix bugs, regardless of the tools used, and encourages developers to explore various debugging methods to improve their skills.

- The debate over using IDEs versus simple text editors for coding continues in the programming community.

- Many junior developers lack debugging experience, which is often not covered in educational programs.

- Tools like Address Sanitizer can help identify bugs that are hard to track down in complex codebases.

- 'Printf' debugging can be useful in specific scenarios, such as release builds or hardware interactions.

- Custom debugging tools can enhance the debugging process and should complement existing tools.

AI: What people are saying
The discussion around debugging methods reveals a variety of perspectives on the use of printf debugging versus traditional debuggers.
  • Many commenters agree that printf debugging is a valid and often effective method, especially in situations where setting up a debugger is cumbersome.
  • Some emphasize the importance of understanding both debugging techniques, suggesting that each has its place depending on the context and complexity of the issue.
  • There are concerns about the potential pitfalls of relying too heavily on printf debugging, such as the risk of leaving debug statements in production code.
  • Several participants highlight the need for efficient debugging tools and practices, advocating for a balance between using print statements and more sophisticated debugging methods.
  • Overall, the conversation reflects a broader debate about the best practices in programming and debugging, with a call for flexibility and adaptability in tool usage.
Link Icon 53 comments
By @kentonv - 4 months
If you have a reproducible test case that runs reasonably quickly, then I think printf debugging is usually just as good as a "real" debugger, and a lot easier. I typically have my test output log open in one editor frame. I make a change to the code in another frame, save it, my build system immediately re-runs the test, and the test log immediately refreshes. So when the test isn't working, I just keep adding printfs to get more info out of the log until I figure it out.

This sounds so dumb but it works out to be equivalent to some very powerful debugger features. You don't need a magical debugger that lets you modify code on-the-fly while continuing the same debug session... just change the code and re-run the test. You don't need a magical record-replay debugger that lets you go "back in time"... just add a printf earlier in the control flow and re-run the test. You don't need a magical debugger that can break when a property is modified... just temporarily modify the property to have a setter function and printf in the setter.

Most importantly, though, this sort of debugging is performed using the same language and user interface I use all day to write code in the first place, so I don't have to spend time trying to remember how to do stuff in a debugger... it's just code.

BUT... this is all contingent on having fast-running automated tests that can reproduce your bugs. But you should have that anyway.

By @victorNicollet - 4 months
One of the hardest bugs I've investigated required the extreme version of debugging with printf: sprinkling the code with dump statements to produce about 500GiB of compressed binary trace, and writing a dedicated program to sift through it.

The main symptom was a non-deterministic crash in the middle of a 15-minute multi-threaded execution that should have been 100% deterministic. The debugger revealed that the contents of an array had been modified incorrectly, but stepping through the code prevented the crash, and it was not always the same array or the same position within that array. I suspected that the array writes were somehow dependent on a race, but placing a data breakpoint prevented the crash. So, I started dumping trace information. It was a rather silly game of adding more traces, running the 15-minute process 10 times to see if the overhead of producing the traces made the race disappear, and trying again.

The root cause was a "read, decompress and return a copy of data X from disk" method which was called with the 2023 assumption that a fresh copy would be returned every time, but was written with the 2018 optimization that if two threads asked for the same data "at the same time", the same copy could be returned to both to save on decompression time...

By @onre - 4 months
I've gotten an OS to run on a new platform with a debugging tool portfolio consisting of a handful of LEDs and a pushbutton. After getting to the point where I could printf() to the console felt like more than anyone could ever ask for.

Anecdote aside, it certainly doesn't hurt to be able to debug things without a debugger if it comes to that.

By @pryelluw - 4 months
Whatever works so I can fix it and be home on time. I will even print (in paper) the code and step through it with a pen. Again, whatever works.

Also, will we ever move forward from these sort of discussions? Back when I was a mechanic no one argued about basics troubleshooting strategies. We just aimed to learn them and apply them all (as necessary).

By @malkia - 4 months
25 years ago I worked on a port of PC -> Playstation 1 game. We did not had proper devkits, but the yaroze model ("amateur", allowing for "printf"-debugging of sorts)

Long story short, our game worked as long as the printfs we had were kept, we had macro to remove them (in "Release/Ship") but the game crashed.

The crash was due to side-effect of printf clearing some math errors.... So here you go!

By @recursivedoubts - 4 months
agree entirely that both techniques are useful and should be learned by programmers

one thing I think the "just do print debugging" folks miss is what a good teaching tool a visual debugger is: you can learn about what a call stack really is, step through conditionals, iterations, closures, etc and get a feel for how they really work

at some level being a good programmer means you can emulate the code in your head, and a good visual developer can really help new programmers develop that skill, especially if they aren't naturals

i emphasize the debugger in all the classes i teach for this reason

By @saagarjha - 4 months
I find that a lot of this discussion just melts away if you are aware of the options that are available and then take your pick. Why, sometimes I've switched between debuggers and printfs as many as six times before breakfast.

If you don't know what a debugger does though that's something you should really get on ASAP. Likewise if you can't figure out how to get log messages out of your thing. Really all there is to it, figure out what you want to do after than and spend your time actually doing something productive instead of getting in a stupid holy war on the internet about it.

By @gghoop - 4 months
If using the debugger is less efficient than using printf then it's a symptom of a wider problem. To be clear though, "printf debugging" is not the same thing as adding structured debug logs to your service and enabling it on demand via some config. Most production services should never be logging unstructured output to stdout. Printf debugging is just throwing out some text to stdout, and it generally means running unit tests locally and iterating through an (add printf, run test, add more printf) loop until the bug is found. Unfortunately it's the default way to debug locally reproducible bugs for so many engineers. So while I don't see the point in not using printf for purely ideological reasons, I avoid building software where this is the simplest option and use the service's structured logger if not easily reproducible. I also generally think it's bad to default to printf debugging in the way I have defined it here and find that competent use of debugger is more often a faster way to debug.
By @omgJustTest - 4 months
An interesting note: printf'ing a variable can sometimes alter how a variable is cached. This is a particularly useful fact in memory locations that can change as a result of hardware operations (like a change of a status bit by a hardware element).

printf'ing effectively enforces a similar condition to `volatile` on the underlying memory segment when it is read.

One can encounter tersely written code that works perfectly with printf statements, but status bits never get "updated" (CPU cache purged) without the printf and hangs the program.

By @bluenose69 - 4 months
Balance is the key, but if I only had a debugger, and not the printf option, I'd be working more slowly and swearing a bit more.

I work in a natural science, and use computing for numerical simulations and data analyses. For coding problems, a debugger is pretty handy. But for finding errors in the underlying mathematics, I tend to rely on a printf -> grep -> analysis chain. This might make files of several hundred Mb in size. The last part is generally done in R, because that lets me apply graphical and statistical methods to discover problematic conditions. Often these conditions do not crop up for quite a long time, and my task will be to find out just what I did wrong, with something like a problematic formulation of boundary condition that created an anomalous signal near an edge of a domain, but only once a signal had propagated from a forcing zone into that region of state space.

By @GuB-42 - 4 months
I have changed my mind a couple of times on that subject. My opinion now is that printf debugging is not ok, but that it is not your fault.

printf debugging is a symptom of poor tooling. It is like saying that driving in nails with a rock is fine. It works, but the truth is that if you are using a rock, that's probably because you don't have a good hammer. And if on every job site, there are seasoned workers banging rocks like cavemen, maybe hammer manufacturers should take notice.

By @forrestthewoods - 4 months
It’s shocking how many people never use a debugger. Like sure printf debugging is good enough sometimes. But never? That’s wild.

Honestly I think attaching a debugger should be the first debugging tool you reach for. Sometimes printf debugging is required. Sometimes printf debugging is even better. But a debugger should always be the tool of first choice.

And if your setup makes it difficult to attach a debugger then you should re-evaluate your setup. I definitely jump through hoops to make sure that all the components can be run locally so I can debug. Sure you’ll have some “only in production” type bugs. But the more bugs you can trivially debug locally the better.

Of course I also primarily write C++ code. If you’re stuck with JavaScript you maybe the calculus is different. I wouldn’t know.

By @bradley13 - 4 months
If it's a one-off situation, as opposed to something that should be part of your long-term test cases? Then yes, using print-statements is fine. Everyone does it, this isn't (or shouldn't be) controversial.

Sometimes a series of print-statements are better for helping you understand exactly when and how a bug occurs. This is particularly true in situations where the debugger is difficult or impossible to use (e.g., multi-threading). Of course, in that situation, logging may be better, but that's just glorified printf.

By @overfl0w - 4 months
And then there are those rare cases where inserting a print or a new condition to use for conditional breakpoint forces the compiler to output slightly different code which does not produce the bug. Essentially this is similar to the Observer effect in quantum mechanics where the system is disturbed simply by observing it. Also the bug cannot be reproduced with optimizations disabled.

How are those cases debugged then? By enabling the debug symbols AND the optimizations and using the debugger, looking at the code and the disassembly side by side and trying to keep your sanity as the steps hop back and forth through the code. Telling yourself that the bug is real and it just cannot be reproduced easily because it depends on multiple factors + hardware states. Ah! I sometimes miss those kinds of bugs which make you question your reality.

By @mark_undoio - 4 months
I found this interesting:

> I know for some people this is often a terrible UX because of the performance of debug builds, so a prerequisite here is fast debug builds.

The reasons debug builds perform badly are kind of mixed, in my experience looking at other people's set ups:

Building without optimisations

It's fairly common to believe that debug builds have to be built with -O0 (no optimisations) but this isn't true (at least, not on the most common platforms out there). There's no need to build something that's too slow to be useful.

You can always add debug info by using -g (on gcc / clang). Use -g3 to get the maximum level. This is independent of optimisation.

You can build with any level of optimisation you want and the debugger will do its best to provide you a sensible interpretation - at higher optimisation levels this can give some unintuitive behaviours but, fundamentally, the debugger will still work.

Gcc provides the "-Og" optimisation level, which attempts to balance reasonably intuitive behaviour under the debugger with decent performance (clang also supports this but, last I checked, it's just an alias to -O1.

Doing a ton of self-checks

People often add a load of self-checking, stress testing behaviours and other things "I might want when looking for a bug" to their code and gate it on the NDEBUG macro.

The logic here is reasonable - you have a build that people use for debugging, so over time that build accumulates loads of behaviours that might help find a bug.

The trouble is, this can give you a build that's too slow / weird in its behaviours to actually be representative. And then it's no use for finding some of your bugs anymore!

I think it would be better here to have a separate "dimension" for self-checking (e.g. have a separate macro you define to activate it), rather than forcing "debug build" to mean so many things.

By @thrdbndndn - 4 months
I have a very specific technical (UX) reason for using `print()` to debug sometimes.

In VS Code, if you want to run debugger with arguments (especially for CLI programs), you have to put these arguments in launch.json and then run the debugger.

This is often tedious to do, because I usually have typed these arguments and tested in terminal before, and now I have to convert them into json format, which is annoying.

To make it worse, VS Code uses a separate window for debug console than your main terminal, so they don't share history/output.

So if I know what to look at already and don't really need a full debugger, I often just use print() temporally.

By @ok123456 - 4 months
A trace log of single-character print statements or equivalent is sometimes the simplest and most effective way to debug flow. This is particularly true for recursive functions, where anything more becomes too much, too fast.
By @mark_undoio - 4 months
> You could use a conditional breakpoint within the debugger, but they can be slow and for me historically unreliable, but this little snippet gives you your own conditional breakpoint that works without fail.

In theory, you can make conditional breakpoints very fast using an in-process agent. For GDB (for instance) this gives the ability to evaluate conditional breakpoints within the process itself, rather than switching back to GDB: https://sourceware.org/gdb/current/onlinedocs/gdb.html/In_00...

I've always found the GDB documentation to be a bit vague about how you set up the in-process agent but I found this: (see 20.3.4 "Tracepoints support in gdbserver") https://sourceware.org/gdb/current/onlinedocs/gdb.html/Serve...

When we implemented in-process evaluation of conditional breakpoints in UDB (https://undo.io/products/udb/) it made the software run about 3000x faster than bare GDB with a frequently-hit conditional breakpoint in place. In principle, with a setup that just uses GDB and in-process evaluation you should do even better.

By @mvdtnz - 4 months
We literally just saw a case[0] where an application (iterm2) mistakenly left verbose logging on in a component of a production build and logged sensitive console contents to connected hosts.

Are you diligent enough to remove your sensitive logging/printf statements EVERY time, for the rest of your career? Or should you make a habit of doing things properly from day one?

0 https://news.ycombinator.com/item?id=42579472

By @frou_dh - 4 months
You shouldn't have to go back and modify a program's source-code just to find out what some part does when run. That's just plainly obvious to me as a principle.

If we have no other option then sometimes we have to use non-ideal approaches, but I don't get the impulse to start saying that tooling/observability poverty is actually good.

By @seba_dos1 - 4 months
The main advantage of printf is that it's usually just there, one line of code away. Setting up a useful debugger session can be much more involved (it varies widely depending on what you're working on), so my ADHD mind will absolutely try to get away with printf first if it's possible. Sometimes it ends up being counter-productive, and you just learn to recognize when to bother with experience.

That said, there are some contexts where this is reversed - printing something useful without affecting the debugged code may actually be more involved than, say, attaching a JTAG probe and stepping through with a debugger. Though sometimes both of those are a luxury you can't afford, so you better be able to manage without them anyway (and this may happen to you regardless of whether you're working on low-level hardware bring-up or some shiny new JavaScript framework).

By @binary_slinger - 4 months
Languages like Python and Java could benefit greatly from adopting the kind of powerful console logging you see in browsers. The ability to inspect objects by simply logging their pointers is incredibly useful for debugging and understanding program state.
By @readthenotes1 - 4 months
TDD is ok, but LDD is better. If you can figure out what went wrong with your code from the logs, there's a non-negative chance that whoever gets the joy of maintaining said code might be able to as well
By @oreally - 4 months
The most ideal and default go-to for quick inspections of program flow and variables should always be quick access to a good debugger when possible (ie. one single keystroke).

Everytime you have to do a printf it's a slowdown, and you can't out-argue the fact that you have to type up to 20ish keystrokes and excite a number of brain neuron cells trying to remember what that printf syntax or variable name was. In comparison to a debugger that automatically prints out your call stack and local variables even without you having to prompt them.

By @shadowgovt - 4 months
As with so many categorical guidelines, there are circumstances to use it and circumstances to not.

The key insight is that printf() is a heavyweight operation ("What, you want to build a string? A human readable string? Okay, one second, lemme just pull in the locale library..."). If you're debugging something at the business-logic layer, it's probably fine.

If you're debugging a memory leak, calling a function that's going to make a deep-dive on the stack and move a lot of memory around is likely to obscure your bug.

By @ruicraveiro - 3 months
Imagine some people arguing that cars should be driven with the steering wheel while others argued it should be driven by using the accelerator and the brake pedals.
By @bArray - 4 months
Honestly, it's just about doing what is easier at the time. Re-compiling an application in debug can be a pain sometimes, or getting back to a specific state within the application to inspect what is going on. Some have mentioned about hardware (which marches on with or without software running), but similarly part of a system where you only really control one sub-system/interface.

My print statements normally come when "I have no idea why this is breaking", and I start sanity checking the basics to make sure that things I previously thought were true, remain so.

Just recently I was doing something in C after a long time, and had something like this (simplified):

    #include <stdio.h>
    int main(){
      int a = 0; // Input from elsewhere
      switch(a){
        case 0 :
          printf("0\n");
          break;
        defult :
          printf("?\n");
          break;
      }
      return 0;
    }
It was late at night, it compiled, so I knew it wasn't a grammar issue. But after testing with printf()'s I realised that the default case was never being hit and performing the core action. It turns out 'defult' highlights in my editor and compiles fine in GCC. Turns out that any word in that location compiles fine in GCC. Nasty!
By @pjmlp - 4 months
Printf debugging is definitely OK, when that is the only tool available.

Other than that, people should spend time learning the ins and outs of their debugging tools, like they do for the compiler switches and language details.

Additionally, when having the option to pick the programming language, it isn't only the syntax sugar of the grammar, or its semantics that matter, it is the whole ecosystem, including debugging tools.

Personally I rather have a great debugging experience than less characters per line of code.

By @giancarlostoro - 4 months
In my eyes Kibana / Elastic logging is even better, basic logging is useful for local-only dev work, but once its getting deployed, a more serious logging setup is infinitely more useful. You can log all relevant data down to a specific event or request and really dig into things as needed. If you use log levels correctly, you can get drastically more detailed by getting all the "debug" logs. This was the bread and butter at a former employer.
By @hot_gril - 4 months
I joined a research project a while back that was mostly about squeezing performance out of something and testing various configurations. The previous researcher had set up a fancy metric collector that was cumbersome, besides also affecting the performance a little. I replaced it with printf + a Python script that parses logs.

That said, I was pleasantly surprised I was able to attach a debugger to that system. Some bugs really needed it.

By @anal_reactor - 4 months
Printf debugging is best debugging because it's the only technique available in all environments. You've just learned to use Visual C++ debugger? Great, here is some Python code to debug. Or maybe the bug is in a bash script that only works within a container on a cloud. Or is it the ansible deployment script that is wrong? IDK, have fun, use your debugging skills.
By @pondidum - 4 months
I also think that printf debugging is fine, but there are better tools [1] - however they might be more involved to setup, or not be available in your codebase.

[1]: https://andydote.co.uk/2024/11/24/print-debugging-tracing/

By @5- - 4 months
note that you can also add breakpoints directly to your code, in the same way as printf, no ide needed.

sadly there is no standard way to do this (c++ is reportedly getting one in '26: https://en.cppreference.com/w/cpp/utility/breakpoint), so you just need to use what your platform provides. here's a partial list: https://stackoverflow.com/a/49079078

e.g. __debugbreak in msvc, asm("int3") on x86[_64], raise(SIGTRAP) in posix.

By @chmod775 - 4 months
Why even obsess over how others ought to get stuff done? Judge the results.
By @sixthDot - 4 months
That post does not mention how contracts can help as preliminaries clues on the nature of the bugs.

Sure you can printf or run gdb, or whathever, but first if something like a contract has failed it will be easyer.

By @gringow - 4 months
I also use something like printf() from the beginning, just with a shorter name, and its printing is conditional, so I usually don't remove it from the code. :)
By @atq2119 - 4 months
Regarding the first paragraph, I have the impression that gamedev and graphics folks have largely moved to Mastodon. There's a gamedev Mastodon instance.
By @konschubert - 4 months
Print debugging gives you more of a Birds Eye view on the bug search.

Stepping through code is more like having your nose on the ground.

Both have their merits.

By @petabyt - 4 months
As somebody who hacks cameras and does reverse engineering and firmware development, log debugging is actually a luxury :D
By @coolgoose - 4 months
I usually call this ddd, debug driven development, or in case of php die() driven development :)
By @0xDEAFBEAD - 4 months
>The main arguments are “if you need to use a debugger you’re an idiot and you don’t understand the code you are writing” (that’s not an actual quote but there was a similar take along those lines). Then there is “If you can’t use a debugger you’re an idiot”. The hating on the ‘printf’ crew is omnipresent.

My holier-than-thou take on this topic is: Whenever possible, debug by adding assert statements and test cases.

By @nejsjsjsbsb - 4 months
Do people think it is not?

The only time it is not OK is when breakpoint debugging is overall faster but you are avoiding the hassle of setting up the debugger.

Also OK: adding a console.log or print in your node modules or python package cache.

And btw splunk, datadog etc. is just printf at scale.

By @from-nibly - 4 months
Print debugging us for when you have a quick itteration cycle. I don't know what debuggers are for because if you don't have a quick itteration cycle you should be killing yourself to have a quick iteration cycle.
By @jfoster - 4 months
Of course it's okay, and at the same time no one will use it as soon as something else is more convenient & effective.

Been waiting for "something else" ~30 years & counting.

By @semiinfinitely - 4 months
working at google forced me to learn how to code without using a debugger
By @imron - 4 months
> I usually name my variable ‘a’.

Me too!

By @LordGrignard - 4 months
all the rest of the stuff is okay but seriously? 2 finger typing? as a programmer? you're gonna be there for AAAGES and your keyboard probably runs a sidebusiness with how long it takes you to type

I know I'm stirring up shit here but there really are benefits to touch typing (I mean just think about it, using 10 fingers instead of 2 is gonna be so much faster assuming you have 10 dingers)

By @mherkender - 4 months
I can't imagine what it's like to be so passionate about something that works, and it's not some philosophical/moral issue, just -- real programmers use X?

I use vim, IDEs, debuggers, printf debugging, whatever works. A tool is a tool. I guess my holier-than-thou position is against the idea that there's one right way to do anything.

By @invalidname - 4 months
Counterpoint: nope.

Logging is great for long term issue resolution. There's tracepoints/logpoints which let you refine the debugging experience in runtime without accidentally committing prints to the repo.

There are specific types of projects that are very hard to debug (I'm working on one right now), that's a valid exception but it also indicates something that should be fixed in our stack. Print debugging is a hack, do we use it? Sure. Is it OK to hack? Maybe. Should a hack be normalized? Nope.

Print debugging is a crutch that covers up bad logging or problematic debugging infrastructure. If you reach for it too much it probably means you have a problem.