stacktrace – Grey Panthers Savannah https://grey-panther.net Just another WordPress site Thu, 03 Feb 2011 16:07:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 206299117 Augmenting Log4J stack traces with class versions https://grey-panther.net/2011/02/augmenting-log4j-stack-traces-with-class-versions.html https://grey-panther.net/2011/02/augmenting-log4j-stack-traces-with-class-versions.html#comments Thu, 03 Feb 2011 16:07:00 +0000 https://grey-panther.net/?p=84 If you have multiple versions of your code in production, it is extremely useful for the log to include the version of the classes when producing a stacktrace, otherwise it is very hard to match the lines in the stacktrace with the lines of the source code (sidenote: there is an optimization in the Sun JVM where – if an exception is thrown “too much” – the JVM stops providing stacktrace – see this article about it and to learn how to disable this feature).

If you are using Log4J as your logging framework, with a little magic you can turn the following stacktrace:

17:47:40,208 ERROR [main] [TestLog4jExtendedStacktrace] An exception has occurred!
java.lang.IllegalArgumentException: Test exception
 at ...Callee.called(TestLog4jExtendedStacktrace.java:144)
 at ...Caller.call(TestLog4jExtendedStacktrace.java:136)
 ...

Into this (note the class versions at the bottom):

java.lang.IllegalArgumentException: Test exception
 at ...Callee.called(TestLog4jExtendedStacktrace.java:144)
 at ...Caller.call(TestLog4jExtendedStacktrace.java:136)
 ...

Callee: $Revision: 56 $
Caller: $Revision: 56 $

The main mechanism is as follows:

  • Each class contains a static String field called “VCS_VERSION”. This field is set (trough the magic keyword substitution – see the documentation for SVN, CVS) to the last version when the file was committed to your VCS.
  • When a stacktrace is sent to the logger, it will look at the classes and if they have the “VCS_VERSION” field, it will output it at the end (it doesn’t annotate the stacktrace itself, because some tools – like IDEs – depend on the stacktrace having a certain format for them to be able to process it – like adding one-click “go-to-line” shortcuts)

As usual, the source can be found in the repository.

Some more implementation details: the current way of modifying the stacktrace is quite hackish (adding a filter and trough reflection modifying the passed in LoggingEvent object). However it has the advantage of being usable without modifying your config files (ie. if you have many config files, but can modify it in only one place in the code, this is a good solution. More “proper” alternatives would be to implement a “wrapper” appender (like AsyncAppender) or a wrapper around Layout, however both of these require you to modify your configuration files.

]]>
https://grey-panther.net/2011/02/augmenting-log4j-stack-traces-with-class-versions.html/feed 1 84
How to generate a stackdump with GDB https://grey-panther.net/2009/10/how-to-generate-a-stackdump-with-gdb.html https://grey-panther.net/2009/10/how-to-generate-a-stackdump-with-gdb.html#respond Fri, 30 Oct 2009 15:29:00 +0000 https://grey-panther.net/?p=179 4054760074_609af75332_o I’m not a big GDB guy, but Google always helps:

  • Create a textfile with the following content:
    set height 0
    thread apply all bt
    detach
    quit
  • Run the following command:
    gdb $EXE -pid $PID -command $TEXTFILE > $OUTPUTFILE

    where:

    • $EXE is the path to the executable
    • $PID is the PID it is running under
    • $TEXTFILE is the file where your’ve saved the previous commands
    • $OUTPUTFILE is the file where you would like your stackdump to be saved.

The cool little crawling logo was taken from HiR, head over there for an explanation.

]]>
https://grey-panther.net/2009/10/how-to-generate-a-stackdump-with-gdb.html/feed 0 179