Vladimir Prus


vladimirprus.com

Thursday, April 06, 2006

Printf debugging 2006

One of the oldest methods of debugging is "printf debugging" -- putting various print statements in the code and then staring at the output. That's useful not only if you can't use a debugger. If the program does not crash, but produces wrong results after long computation, it's hard to figure where exactly the problem lies. In that case, printing intermediate data can be a very efficient method.

The only problem is that after adding print statements the program must be recompiled, and after debugging print statements must be removed. But it's possible to make gdb into printing machine using so called "breakpoint commands". Each breakpoint can have a list of commands that will be executed when breakpoint is hit. The commands can include printing and "continue". Here's a simplified example of gdb script I've used recently:

break main.cpp:1353
commands
   print ('lvk::nm_model::NM'*)this
   printf "Entering 'run', proc %d\n", $->processor_number
   continue
end
run

After putting this to a file "script", gdb can be run as:

gdb -batch -x script name_of_program > log
producing logs of variable values as certain points of the program.

Starting with version 3.3.0, similar functionality is available in KDevelop. Just click on the "Tracing" column in breakpoints window, select variables to print and click OK.

More screenshots here and here. This is a beginning, future KDevelop version will allow to specify custom commands for breakpoints.

19 comments:

Anonymous said...

Wow, never know about this useful trick.

Anonymous said...

Nice, but why do you use pixelated (non anti-aliased) fonts? It makes the writing look like it's in the stone age :)

Vladimir Prus said...

Heh, I can't control which fonts are antialised on your machine ;-)

Anonymous said...

Nice, but why do you use pixelated (non anti-aliased) fonts? It makes the writing look like it's in the stone age :)
I thought all developers preferred to avoid headache inducing blurry fonts when doing serious coding.

superstoned said...

> I thought all developers
> preferred to avoid headache
> inducing blurry fonts when doing
> serious coding.

depends on the way you anti-alias your fonts... there are ways to keep em clear and actually make em more readable.

Anonymous said...

Anonymous said...

> Nice, but why do you use
>pixelated (non anti-aliased) fonts?
>It makes the writing look like it's
>in the stone age :)

I always wonder why people look at things totally out of the topic of blog post while commenting. Its really hard for developers to blog(most of them aren't professional bloggers) keeping all these extra stuff in mind :) Thank god, atleast you weren't rude :)

Anyway this is really nice useful trick. Thanks for this tip :)
--
gopala

Nathan William said...

Engineers who wish to migrate to Australia must submit a Competency Demonstration Report and the Engineers Australia must approve it. Thus, engineering students look forward to getting
Cdr Australia which assists them to get a flawless CDR. This reduces the chances of rejection by the EA.
Most students and experienced civil engineers are today looking forward to migrating to Australia as the job scope is better and bigger with a high pay scale. Therefore, getting CDR help is the only scope to gain entry into the country.
The CDR for Engineers Australia must be impressive and abide by the ANZSCO code and guidelines. It should also showcase the skills of the applicants as a suitable civil engineer to work according to the Australian rules and regulations.
The service providers who assist the students with CDR have experienced CDR report writing. These writers know the requirements of a CDR and do it accordingly.

rickypauls said...

Great post with informative details of Assignment Help. For effective help to complete your project related to any subject, visit greatassignmenthelp or opt for live chat option.
Assignment Help Online
Online Assignment Help
Assignment Help Online Services
Assignment Helper
Assignment Assistance
Assignment Help Experts
Online Assignment Help Services
Assignment Writing Help

Michael Jones said...

It is not easy for many students to do assignments on their own as it requires a lot of research and time for making assignments. That is why; they take help from Online Assignment help.Java Assignment help

myassignmenthelpau said...

I suggest everyone to choose Assignment Help Australia for getting the best guidance in their academia. The proficient experts assist as per the requirements and learning styles desired by the university.

Olivia Crew said...

Livewebtutors aims at providing the students with a one stop solution for all the academic related issues. The company has Livewebtutors is an online Assignment Help and assignment writing service offering expert assignment help. Hire Australian assignment writing experts.
visit now:- Assignment Help

Olivia Crew said...

Livewebtutors aims at providing the students with a one stop solution for all the academic related issues. The company has Livewebtutors is an online Assignment Help and assignment writing service offering expert assignment help. Hire Australian assignment writing experts.
visit now:- Nursing Assignment Help

idealassignmenthelp said...

A very high level post with a knowledgeable information .thanks you for giving me such a nice information. If you need any college level
My Assignment Help at reliable quality with better work. Kindly visit Ideal assignment help.

Bella said...

Your Blog is appriciatable.
Paperssolution
topacademictutors
liveassignmenthelp

darylharper said...

Make your Travel smarter Cash Back offers and make your Online Credit Card Payment using your account with any other bank.

printersupportnumber said...

Nice Post..
Is your HP printer getting offline all of sudden while printing? Because of this issue, are you unable to print any document or file? Want an instant support from top-most technicians to bring it online from offline? If really so, then place a single call at HP Printer Support Phone Number where you will receive the exact possible answer of your offline printer related query. Our technical engineers are talented and knowledgeable in technical field. So, you will be proffered handy remedy within a minute.

Olivia Crew said...

A very high level post with a knowledgeable information .thanks you for giving me such a nice information. If you need any college level Assignment Help at reliable quality with better work. Kindly visit livewebtutors website we providing best assignment help services in
Australia, USA, UK, UAE, and Canada etc.
Read more now :- Assignment Help Australia

Robert Duvall said...

Having an HP printer driver on operating system is very important for starting a print job. Your printer won’t be worthy until you download and install the correct driver for your HP printer. Therefore, in case you don’t know the steps of printer driver installation, you can simply open your web browser and enter the URL 123.hp.com onto the search bar. The entire downloading procedure will be come up in front of you and once you go through those steps, printer driver will surely be installed on PC. But if facing some complications while following the given points, you can give an instant ring at XYZ. Your problem will be sorted out within a pinch by our technical team.

john amber said...

I suggest all members choose assignment help Australia for the best guideline in your academia. The perfect and expert assistant your requirement and learning assignment project by the university.