Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the source file of a compiled method?

Tags:

c#

reflection

I'm really stumped by this one!

The StackFrame object (MSDN Link) has a GetFileName method that returns the original path of the source file that compiled the executing method (provided symbols were generated and included with the executing assemblies). It looks like this information is used to generate full exception text.

I'm trying to find a way to get at this information if the method isn't currently executing. I've been poking around the reflection API and haven't seen a way to get at this information. I assume it must be in there somewhere.

Does anyone else know of a reflection based method (or indeed any other method) that can get me the code file name?

Any ideas, comments or abuse gratefully accepted.

Many thanks!

like image 294
Chip Phat Avatar asked Apr 09 '10 13:04

Chip Phat


People also ask

Can I retrieve the source code of my Python objects?

At the same time, it can also retrieve the source code of your Python objects. Please note dill is not a standard library, so you must install it separately. Its API is quite similar to inspect 's.

Is there a command to retrieve the source code of rtvclsrc?

I go into this in detail in RTVCLSRC makes a comeback. If it is RPG or RPGLE then there is no IBM command to retrieve the source code. I need to be a bit creative in my thinking.

How to retrieve the source code of an RPG/RPGLE object?

If it is RPG or RPGLE then there is no IBM command to retrieve the source code. I need to be a bit creative in my thinking. When I compile a RPG/RPGLE object I have choices in the debug views and these are included in the compiled object. I wrote about the debug views and the advantages of them for RPGLE in Debug views finding your favorite.

How to check if the source is included in the program?

If the program was compiled with the "Source listing options" parameter, OPTION, of *SRCDBG or *LSTDBG then the source was included in the program object.


1 Answers

Reflection can only provide type information from the assembly metadata. Getting the address requires the .pdb debugging file and the address of the function in memory, as compiled by the JIT compiler. You can't get the address without the StackFrame.GetNativeOffset() method or the debugger interfaces, assuming the method is even compiled. The latter approach can't work in-process, a program cannot debug itself.

The CLR doesn't have any trouble because it can retrieve the method address from the stack frames when it processes the exception. That's still an imperfect art, it cannot see addresses of methods that were inlined. Having those stack frames is the required first step.

like image 141
Hans Passant Avatar answered Nov 11 '22 17:11

Hans Passant