Cheers!
I am making a function in C that allows obtaining a more detailed record of what happens in the program, something similar to a debug , but trying to summarize things for the programmer.
I found the documentation of the CPP Preprocessor (GNU/Linux) and there they indicated the different macros and constants that can be used in our program so that functions can also be used and these macros can be used there .
Thanks to this, carry out the following program:
#include <stdio.h>
#include <stdlib.h>
/* Dump para variables */
#define dump(variable) { \
printf("La variable '%s' ocupa %d bytes\n", #variable, sizeof(variable)); \
}
/* Dump para funciones */
#define dumpf(){ \
printf("Se llama a funcion %s()\n", __FUNCTION__); \
}
void funcion(int argumento){
dumpf();
printf("%d\n", argumento + argumento);
}
void sumar(int a, int b){
dumpf();
printf("%d + %d = %d\n", a, b, (a+b));
}
int main(int argc, char *argv[]){
/* Variables */
int variableINT = 45;
char variableCHAR = 'A';
char *variablePTR = "Hola Mundo";
/* Debug de Variables*/
dump(variableINT);
dump(variableCHAR);
dump(variablePTR);
/* Debug de Funcion*/
funcion(10);
sumar(5, 5);
return EXIT_SUCCESS;
}
This program allows us to know the weight in bytes of a variable thanks to the dump() function , it also allows us to know which function we are calling through the dumpf() function .
Up to this point, the output of the console program is:
The variable 'variableINT' occupies 4 bytes
The variable 'variableCHAR' occupies 1 bytes
The variable 'variablePTR' occupies 4 bytes
function is called function()
twenty
Sum() function is called
5 + 5 = 10
After that, to make our "debug" a little more readable and easier to understand , I wanted to add something that would indicate the line where the function call was being made, so I made the following modification in the function dumpf( ) :
/* Dump para funciones */
#define dumpf(){ \
printf("Se llama a funcion %s() en la linea %d\n", __FUNCTION__, __LINE__); \
}
I added the LINE macro , which in theory indicates the line where the instruction is being executed.
But the result that the program throws is:
function function() is called on line 15
twenty
function add() is called on line 20
In other words, it is indicating the line where the function was originally called within each of the function() and add() functions , it does not indicate the line where the function was actually called, which are actually lines 37 and 38.
How would it be possible to obtain the line in which the function call was made? Are there any drawbacks to the LINE macro when it is included in a function? Since the Preprocessor is supposed to replace these values before doing the compilation.
I thank you in advance for your help.
What you are looking for is a macro that tells you that the function has been called from line X and that will not work if you call the macro from the function itself... Think that the message is being generated at compile time ... the compiler at that point doesn't know where you're going to call the function from.
To achieve what you want you should call the macro before calling the function (or adapt the macro so that it is able to call the function)... in any case it is not very practical:
And the result would be:
If you are interested in having the execution traces, perhaps you should consider learning to use the code debugger... in programs as small as the one you show, it does not make much sense to include a trace system.
No way. The information you're looking for is the stack trace which is determined at runtime while macros like
__LINE__
and__FUNCTION__
are tokens that the pre-processor translates into literals just before compiling.That is, the macro
__LINE__
written on line 100 will be translated by the literal 100 before compiling. The only way would be passing each function a parameter indicating the call point:The call to
funcion(1, __LINE__)
will be translated by the pre-processor tofuncion(1, 11)
while itsumar(3, 2, __LINE__)
will be translated tosumar(3, 2, 12)
.Maybe you are thinking that it is enough for you and you can use default parameters... but it would not work:
The previous code, when passing through the pre-processor, would look like:
So the output of the program would be:
Which you already know is false, since the functions were called on lines 11 and 12.
Conclusion.
Check your compiler's documentation to see if you can access the stack trace during program execution.