Re: Debugging JNI shared libraries

From: Alf P. Steinbach (alfps_at_start.no)
Date: 10/07/03


Date: Tue, 07 Oct 2003 10:01:02 GMT

On 07 Oct 2003 09:49:44 GMT, Dave Neary <real.addressinthesig@thisoneis.invalid> wrote:

>Hi all,
>
>I have been trying to get some JNI code working on Win32 (the code is
>libxmlj from the classpathx project). Compiles fine on Linux, and
>compiles OK on Win32 when I have teh requisite stuff installed and I'm
>using mingw, and I make a few changes (I really should learn how to use
>Visual Studio).
>
>I load my DLL/so via System.loadLibrary, and on Linux things go off
>without a hitch. On Win32, I get an access violation, attempt to
>reference object at address 0x00000... - looks like a null pointer
>dereference to me. So I compiled the Linux .so with efence (hoping to
>provoke a core dump in an environment that I know pretty well, and
>diagnose the problem), but I have not been able to replicate the problem
>at all.
>
>Which brings me to my question. How can I debug this kind of thing on
>Win32? Is there a way to load a DLL in a debugger, and (say) when
>DllMain gets called, attach to the dll?

You attach to the process/thread, not the DLL (which is just part of the
code as far as the debugger is concerned). Attach to the Java process
from the debugger, set a breakpoint in the DLL, let it run till the
breakpoint hits. However,

> How can I go about localising
>the problem? The best option available to me at the moment seems to be a
>bunch of printfs, which will perhaps help localise things.

I found trace output to be the cleanest solution when doing JNI stuff, but
that was years ago (1997?). Things to be aware of include, definitely, proper
exception handling (never let an exception escape up to the Java VM, it will
then go into panic mode), proper string conversion, and so on. I found it
best to build a small library of JNI wrappers and support classes. Also be
aware of threading. For an explicitly loaded DLL it's not safe to use the
Visual C++ support for thread local storage (if you use that compiler).

> It would be nice to get a core dump/stack trace though.

That's the Unix way; in Windows, use interactive debugging. MS Visual Studio,
which IMO is a really horrible piece of software (it insists on changing things,
like uppercase/lowercase, HTML entities, pictures added as resources (it actually
_mangles_ them!), and so on), has a very good, incredibly good, debugger.

>So - any Win32 people with good debugging tips for debugging shared
>objects?

They're not very different from non-shared.



Relevant Pages

  • Re: How to connect to children process
    ... Main problem is amount of manual operations required per debug cycle. ... connection to DLL with debugger takes sensible amount of time and manual ... Is the main problem that the debugger keeps symbols file loaded ... AFAIK that's how VS2003 debugger works, but WinDbg should usually ...
    (microsoft.public.vsnet.debugging)
  • Re: How to connect to children process
    ... AFAIK that's how VS2003 debugger works, ... unload symbols completely after the dll ... Also, when setting breakpoint on the needed function (in WinDbg), ... I'm creating code with int 3 in place that I'm willing debug. ...
    (microsoft.public.vsnet.debugging)
  • Re: AD+ crash logs and .dmps Part II
    ... This a .dll provided by an online payment verification ... The Debugger docs have some basic info but overall, understanding ... details the formation and usage of the stack. ...
    (microsoft.public.inetserver.iis.security)
  • Memory error with funcs that return char*
    ... DllImport statements to link back into a Win32 C DLL that has several ... Here is a sample function I am using that just returns a hard coded string: ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Thread crashing on return instruction...
    ... On exiting the DLL during exe termination the application ... Some breakpoints were not even seen by the debugger, ... PB) If the primary thread is exitted, the kernel kills all other still ...
    (microsoft.public.windowsce.embedded.vc)