Get values of parameters in stack trace

Unfortunately, this is not possible: at the time when you catch the exception in the handler, all the stack frames with the method parameters are gone. Once the control leaves your function, you can no longer access its parameter values.

Since you know the specific function where the crash happens, you could set up an exception handler there to collect all the parameters of interest, and re-throw a wrapped exception. Once the diagnostics is complete, you could revert the code back to normal:

void SuspiciousFunction(string name, long count) {
    try {
        // The code of your function goes here
    } catch (Exception e) {
        var args = new Dictionary<string,object> {
            { "name" , name  }
        ,   { "count", count }
        };
        throw new MySpecialException(e, args);
    }
}

I would capture the exception in the method it's thrown, gather your parameters and any other needed info, then rethrow the error with a new ApplicationException or other custom Exception that contains your additional info.


From the documentation Environment.StackTrace i would say it is possible. They say

The stack trace information for each method call is formatted as follows:

"at FullClassName. MethodName (MethodParams) in FileName :line LineNumber "


Comments

  1. Leandro

    • 2019/7/26

    Get values of parameters in stack trace · There's no way to do it from inside the CLR, however some tools like AVIcode can do it by hooking into 

  2. Kian

    • 2019/11/27

    I can get information about a parameter by StackTrace using something like this: catch (Exception ex) { var st = new StackTrace(ex); System.Reflection.ParameterInfo pi = st.GetFrame(0).GetMethod().GetParameters().First(); } I want know how i get the value of parameter. Example: If my method in stack trace was like: void MyMethod(object value)

  3. Reyes

    • 2017/8/13

    Even better would be to print the stacktrace with the actual parameters values for each method (when logging exception for instance).

  4. Zyaire

    • 2015/7/16

    1. You can't modify the stack trace, but you can include the value of the variable in your error message: Protected Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim x As String Try x = "testvar" Inner_A (x) Catch ex As Exception strErrorMesssage &= "X = " & x & Environment.NewLine & DirectCast (ex, System.ApplicationException).StackTrace.ToString () End Try End Sub.

  5. Kareem

    • 2016/8/27

    When an error is not caught, Julia shows a stack trace with the method signatures. This is informative, but sometimes the actual arguments to the functions 

  6. Jameson

    • 2016/7/3

    I can get information about a parameter by StackTrace using something like this: catch (Exception ex) { var st = new StackTrace(ex); System.Reflection.ParameterInfo pi = st.GetFrame(0).GetMethod().GetParameters().First(); } I want know how i get the value of parameter. Example: If my method in stack trace was like: void MyMethod(object value)

  7. Hector

    • 2017/3/6

    args, array, If inside a function, this lists the functions arguments. One line of code to print simplest and shortest human readable backtrace:) <?php

  8. Dennis

    • 2018/11/16

    1. Call stack - no problem. 2. Parameters passed to the methods in the call stack – this is the issue. Here I do not have the values passed (only the one passed to the method where the exception was caught) Trees, I’m not sure I followed you completely…. Object graph is similar but not the same.

  9. Gray

    • 2015/3/4

    For functions without debugging information hexadecimal numbers are displayed for the arguments. These numbers are not necessarily meaningful. When a function 

  10. Lucian

    • 2018/5/1

    you what the return value is (not the type, but the value)? It would be nice if there was a GetParameterValues method on the. StackFrame instance, which would take a ParameterInfo instance and then. return the value in that parameter. If you want to get this information, you have to pass the values to your.

  11. Reyes

    • 2017/8/8

    Either using stack trace or Reflextion I was able to get all the details of classes and function. Did any one know about accessing function 

  12. Valente

    • 2020/5/16

    Listing 12 shows how the value in the stack trace matches up with all four parameter values that were passed in. The value of true is an 8 bit value that is represented with the value of 1 and the value of false is represented with the value of 0. The value of 25 in binary is 11001 which converts to 19 in hexadecimal.

  13. Keith

    • 2017/9/22

    This document outlines V8's JavaScript stack trace API. The optional constructorOpt parameter allows you to pass in a function value.

  14. Johan

    • 2020/2/25

    Get the ID hash of the CAB file that is associated with the error for which you want to retrieve the stack trace. To get this value, use the get details for an error in your desktop application method to retrieve details for a specific error in your app, and use the cabIdHash value in the response body of that method.

  15. Armani

    • 2018/9/1

    Similar, but print only the outermost n frames. backtrace full: Print the values of the local variables also. bt full. The names where and 

  16. Malachi

    • 2016/1/21

    The label Thread dumping my own stack was inserted by the application program using the label parameter. The thread start routine in this example is threadfunc 

  17. Zander

    • 2017/7/6

    Parameters any, data Optional. Zero or more objects to be output to console along with the trace. These are assembled and formatted 

Comments are closed.

Recent Posts