Wednesday Nov 25, 2009

Adplus configuration

While working on a Java Plugin(on Windows) issue, I found one useful feature of
Adplus.

I was having a problem with LoadLibraryEx() and I was trying to load a dll into
another process' address space but it was not getting loaded. Adplus report was
showing DLL_Load and DLL_Unload First Chance Exceptions for the attempts of
loading and then unloading the dll. I wanted to find why the dll was getting
unloaded immediately after it was attempted to get loaded. And for this, I
wanted to obtain the stack trace or crash dump for this First Chance unloading
Exception. The default action for this event is to just log the details.
Adplus report:
  DLL_Load [ld]       return: GN GN
      1st chance: Log
      2nd chance: Log
  DLL_UnLoad [ud]       return: GN GN
      1st chance: Log
      2nd chance: Log
  Process_Shut_Down [epr]       return: VOID VOID
      1st chance: Log;Time;EventLog;Stacks;FullDump
      2nd chance: 


Then I found a way on how to change the actions for these exceptions. Adplus
can accept a configuration file where in we can specify the actions for
Exceptions. I created a conf file 'conf.txt' with following contents:
<ADPlus>
  <Settings>
    <ProcessID>4856</ProcessID>
    <RunMode>CRASH</RunMode>
    <OutputDir>d:\\poonam</OutputDir>
  </Settings>
  <Exceptions>
    <Config>
      <Code>AllExceptions</Code>
      <Actions1>MiniDump;Stack;Log;EventLog</Actions1>
    </Config>
  </Exceptions>
</ADPlus>


and ran adplus:

# adplus -c conf.txt

With this I could get the stack trace and crash dump of DLL_Unload event. And
adplus log contained:
  DLL_Load [ld]       return: GN GN
      1st chance: MiniDump;Stack;Log;EventLog
      2nd chance: Log
  DLL_UnLoad [ud]       return: GN GN
      1st chance: MiniDump;Stack;Log;EventLog
      2nd chance: Log
  Process_Shut_Down [epr]       return: VOID VOID
      1st chance: MiniDump;Stack;Log;EventLog
      2nd chance:


Details on Adplus configuration here:
http://msdn.microsoft.com/en-us/library/cc390932.aspx

Also, it is possible to define the actions for a specific Exception provided
you know the ExceptionCode. I could not find the code for DLL_UnLoad exception
so I used 'AllExceptions'.
<Code> { ExceptionCode | AllExceptions } </Code>

Sunday Oct 19, 2008

Windows crash dumps for Java Processes

Windows Crash Dump is memory dump of a process running on a Windows system. These dumps can be very useful for debugging Java process crashes. In this entry I discuss how to collect sane Crash Dumps for Java process crashes on Windows machines that can later be analyzed using Windbg or other 'Debugging Tools For Windows'.

I have a simple java class test which uses native library test.dll using JNI. test.dll implements a 'native' method where it accesses a null pointer and causes a crash. Complete source of this crashing program is here


Windows NT, 2000 and XP

Let's first run this program on a Windows NT/2000/XP machine:

D:\\demo>java test
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10011f68, pid=2020, tid=640
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode, sharing windows-x86)
# Problematic frame:
# C [test.dll+0x11f68]
#
# An error report file with more information is saved as:
# D:\\demo\\hs_err_pid2020.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Running this program created an hs_err log file in the same folder. Error log file contains the following stack trace:

C [test.dll+0x1032]
j test.f()V+0
j test.main([Ljava/lang/String;)V+19
v ~StubRoutines::call_stub
V [jvm.dll+0x873ed]
V [jvm.dll+0xdfb96]
V [jvm.dll+0x872be]
V [jvm.dll+0x8e501]
C [java.exe+0x14c5]
C [java.exe+0x69cd]
C [kernel32.dll+0x16fd7]

Now, let's run it with -XX:+ShowMessageBoxOnError JVM option. I get the following message box:

Attaching Visual Studio or Windbg to this process or collecting a crash dump with Dr Watson and opening that crash dump with Windbg shows the following call trace:

ntdll!DbgBreakPoint
jvm!VMError::show_message_box+0x7f
jvm!VMError::report_and_die+0xe7
jvm!report_error+0x2d
jvm!topLevelExceptionFilter+0x54d
jvm!os::os_exception_wrapper+0x7d
msvcr71!except_handler3+0x61
jvm!JavaCalls::call+0x23
jvm!jni_invoke_static+0xb1
jvm!jni_CallStaticVoidMethod+0x86
java+0x209e
java+0x898f
kernel32!GetModuleFileNameA+0x1b4

This shows JVM's error handling frames and does not show that the crash happened in function test.f().

This is because, by default, the First Chance Exceptions are not sent to the System Debugger. And Debuggers receive only the Second Chance Exceptions. The exception in test.f() was a first Chance Exception that was hanlded by JVM. Please see details on First and Second chance exceptions: http://support.microsoft.com/kb/286350

So how can we collect crash dumps that contain the correct stack trace of the crash. Let's try 'adplus' to collect the crash dumps.

Start adplus in crash mode:
D:\\windbg>adplus -crash -pn java.exe

and then start your java process with -XX:+ShowMessageBoxOnError

By default, adplus creates mini dumps on First Chance Exceptions and full memory dumps on Second Chance Exceptions. It can be changed; details here: http://support.microsoft.com/kb/286350

'adplus' would create dump files in a folder like Crash_Mode__Date_08-12-2008__Time_15-12-56PM. Using Windbg, open the dump created at First Chance Exception and it shows the crashing frame as:

test!Java_test_f+0x22

Ah! that's what I was looking for.



Windows 2008 and Windows Vista

Dr. Watson is not available on Windows 2008 and Windows Vista

Starting with Windows Server 2008 and Windows Vista Service Pack1 (SP1), Windows has new error reporting mechanism called 'Windows Error Reporting' or WER. WER can be configured so that full user-mode dumps are collected and stored locally after a user-mode application crashes. This feature is not enabled by default. Enabling this feature requires administrator privileges. To enable and configure the feature, we need to use the following registry values under the
HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps key.

Details here:

http://msdn.microsoft.com/en-us/library/bb787181(VS.85).aspx

And yes, here also we can use 'adplus' to collect Crash Dumps for First Chance and Second Chance Exceptions.



Enjoy Debugging !

About

poonam

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today