Skip to content
Advertisement

How to find encapsulating function name from line number

I am looking at a log file which just tells me the filename and the line number inside that file that has the Error. What I am interested is knowing the encapsulating function. For example, here are the contents of the log file

JavaScript

and here are the contents of the file foo.file

JavaScript

Based on the above log file, I want to get function names def and ghi returned. I have tried the partial solution provided by @larsks and added [[::blank::]]

JavaScript

It is failing on abc_1234 (...) and def_442 (...) as there is a space before (. I can’t get the above to work

Advertisement

Answer

In order to map a line number to a function definition, you’ll will need to iterate through your source file looking for function definitions, and then print out the current one when you encounter a target line number. For example, something like this:

JavaScript

If you put this in a file called findline.awk and call it like this:

JavaScript

Then it will print the name of the function that contains line 26. This script as written isn’t terribly robust, but it hopefully gives you some ideas on how to proceed.

See the awk documentation for details about the gensub function.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement