2

I have a C file in which I would like to add a print at the beginning and end of each function that includes file name, function name and line number. I am trying to automate this for an entire folder of C files using python.

Example: For function add in math.c file

int add(int a, int b){
   sum = a +b;
   return sum;
}

I would want the result file to look something like this:

int add(int a, int b){
   printf("Math.c, add, 12");
   sum = a +b;
   return sum;     
}

I am looking at the regex as suggested in this link Using Python Regex to Find C function in File

Any other suggestions? Ctags is the other option I am exploring, but I prefer using python libraries if any for this purpose.

Edit:

  • Running the code on a microcontroller
  • Not using Linux
  • Debugger with limited capabilities (can get printf only)
Shrikanth N
  • 652
  • 3
  • 17
  • Why are you not going with the regex option? It should suit your purpose (although if you really want to catch every possible notation, you should go for something bigger like a parser, but I think this is out of this context) – hellow Jul 03 '18 at 06:46
  • 1
    Would using a library to build a complete AST be too much? https://github.com/eliben/pycparser seems to be able to do this in pure python. – Lanting Jul 03 '18 at 06:46
  • this example from the above library lists all the functions: https://github.com/eliben/pycparser/blob/master/examples/func_defs.py – Lanting Jul 03 '18 at 06:48
  • @hellow - the regex option would help me identify the function name but would need additional code to update something in the function. I was looking at maybe finding the opening and closing braces of a function and add the printf. – Shrikanth N Jul 03 '18 at 06:49
  • @Lanting - thanks, the link you shared looks interesting. Let me check it out. – Shrikanth N Jul 03 '18 at 06:51
  • 1
    Why are you not using your C compiler's option to instrument functions? – Ignacio Vazquez-Abrams Jul 03 '18 at 06:51
  • 1
    You appear to be adding debugging information to your executable file. If so, you might be better off using the tools already available. Try compiling your code with the `-g` flag to include information about [symbols and line numbers](https://stackoverflow.com/a/89619/1679849), then run your program inside a debugger like GDB. – r3mainer Jul 03 '18 at 06:53
  • 1
    @IgnacioVazquez-Abrams - I am aware of some code instrumentation tools but not sure about the C compiler option. Could you please, elaborate or maybe share some links? – Shrikanth N Jul 03 '18 at 06:53
  • 1
    @squeamishossifrage - sorry for not adding more details. I am not using Linux and I am running this on a microcontroller. Only output available for me is the printf on debugger with limited capabilities. – Shrikanth N Jul 03 '18 at 06:56
  • 1
    1)search for open curly bracket 2)search back for '(' 3)skip spaces backward 4) for (i; isalnum(buf[i]) || buf[i] == '_'; i--); 5)i++; and buf[i] now is first letter of the func. name 6)Insert after next '\n'. – purec Jul 03 '18 at 07:22
  • 1
    @IgnacioVazquez-Abrams following on : gcc -finstrument-functions (since you are on embedded, you are probably on gcc). https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html – Evan Benn Jul 03 '18 at 07:30
  • 1
    Possible duplicate of [Print the file name, line number and function name of a calling function - C Prog](https://stackoverflow.com/questions/8884335/print-the-file-name-line-number-and-function-name-of-a-calling-function-c-pro) – Mathieu Jul 03 '18 at 07:32

1 Answers1

2

You can use predefined macros such as __file__, __func__ and __line__. This would result in a common printf statement:

printf("%s, %s, %d\n", __FILE__, __func__, __LINE__)

which you need to add to each function.

This question has already be similarly answered here: Print the file name, line number and function name of a calling function - C Prog

Sandy
  • 895
  • 6
  • 17