2

Is there any way to print to a text file the code that it's being executed for debugging purposes?

for example:

if (i == 1)
{
 a = true;
}
else
{
 a = false
}

So when i = 1 we print to a text file:

if (i == 1)
{
 a = true;
}
else

and when i != 1 we print to the text file

if (i == 1)
else
{
 a = false
}

I am not saying that this is a good practice. I know that gdb and other tools are much better to debug code so please don't get mad if you think that it's an awful idea. I was just wondering if it can be done. It would be like adding a printf after every line so we only print the lines that get executed. No thread save or anything like that.

Katu
  • 1,296
  • 1
  • 24
  • 38
  • what debugger/compiler are you using? If you have something like visual studio, you can compile w/ debug information to get functionality similar to what you want. – Red Alert Dec 11 '13 at 09:38
  • You may be able to do it with a script running gdb. From the compiler it is not possible. – Devolus Dec 11 '13 at 09:38
  • Not realistically possible; for any "real" program the result would be Giga- if not Terabytes. – MSalters Dec 11 '13 at 12:55
  • I feel safe in saying: There is no problem to which this is the correct solution. – Speed8ump Dec 11 '13 at 13:13
  • Hmmm may be you're really asking for a XY problem here. Elaborate on your use case please. As I mentioned in my answer, analyzing the executed code pathes is possible using the coverage analysis code injection. Running a particular test case (input selection) will give you a detailed coverage report, which codepaths' were executed how often. – πάντα ῥεῖ Dec 12 '13 at 01:56
  • thanks for the comments. I won't elaborate the question because is not specific to a problem and I don't want to encourage people to give alternative solutions to a problem. I just want to know if it can be done – Katu Dec 12 '13 at 07:49
  • Why would say this is a bad practice ? It totally depends on what your motivation is. If you have to figure out the bugs quickly, why not ! If code size/load is not a concern, the solution above is VERY USEFUL to pinpoint bugs in the code. For ex. If you knew just by looking at the logs what the value of i is, it'll help you pinpoint the bug faster. IBM has a whole division (formerly TELELOGIC) that develops tools along these lines. Check out what SDL is. I've used it in the past and its mighty powerful. It prints every line of code executed though its used in a different way. – Utkarsh Kumar Mar 21 '14 at 23:27
  • possible duplicate of [How to print the next N executed lines automatically in GDB?](http://stackoverflow.com/questions/764382/how-to-print-the-next-n-executed-lines-automatically-in-gdb) – Ciro Santilli OurBigBook.com Jul 02 '15 at 19:34

2 Answers2

0

I think what you want hasn't anything to do with debugging in the first place, but with unit testing and test coverage:

You'll need to create unit tests (e.g. using googletest) for your code and compile it with code coverage options switched on (e.g. --coverage for GCC). Then you can use a tool to create a coverage report (e.g. lcov/genhtml for the mentioned toolchain).

The unit tests will control the input for your cases (i = 1/0).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
-1

For debugging purposes I would say it is not practical. Yes, you can do a printf before/after each line of execution, but that would just clog up your program. Also, if you're talking about debugging the execution of loops, you will end up printing a bunch of junk over and over again and would have to look forever to find potential bugs. In short, use breakpoints.

However, from a theoretical standpoint, it is possible to create a program that outputs itself. This is a little different from what you want because you only need parts of your program, but my best guess is that with a little modification it can be done.

Lawrence H
  • 467
  • 4
  • 10