Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:11+00:00 2026-05-26T18:17:11+00:00

I have a very simple agent, basically just the required Agent_OnLoad method signature. If

  • 0

I have a very simple agent, basically just the required Agent_OnLoad method signature.

If I compile it with g++.

 g++ -g -fno-strict-aliasing -fPIC -fno-omit-frame-pointer -W -Wall  -Wno-unused -Wno-parentheses  -I. -I../agent_util -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include/linux  -c -o ../src/testagent.o -DMAX_THREADS=1000 -DJVMTI_TYPE=1 ../src/testagent.c

and create a shared lib and run a test on the agent

LD_LIBRARY_PATH=`pwd` /home/mnc/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//bin/java -agentlib:testagent -version

I get an error

Error occurred during initialization of VM
Could not find agent library on the library path or in the local directory: testagent
make: *** [test] Error 1

If I compile as using the following command i.e. compile as C, it works ok.

gcc -Wl,-soname=calltracer.so -g -fno-strict-aliasing -fPIC -fno-omit-frame-pointer -W -Wall  -Wno-unused -Wno-parentheses  -I. -I../agent_util -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include/linux  -c -o ../src/testagent.o -DMAX_THREADS=1000 -DJVMTI_TYPE=1 ../src/testagent.c

and then create a shred lib and test it

LD_LIBRARY_PATH=`pwd` /home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//bin/java -agentlib:testagent -version
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)

it works ok.

The problem is the code I have, is cpp code for the actual method and not c. Can an agent be created using c++ code? I suspect it is but I don’t know what I’m doing wrong.

Here’s the source for my test agent. Cant get much more simple then this.

/*testagent.c*/
#include "jni.h"
#include "jvmti.h"


JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
{
return 0;
}

/* Agent_OnUnload() is called last */
JNIEXPORT void JNICALL
Agent_OnUnload(JavaVM *vm)
{
}

This works ok when compiled as a c file

here's the output from the nm command
0000046c T Agent_OnLoad
00000476 T Agent_OnUnload
00001f18 a _DYNAMIC
00001ff4 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
00001f08 d __CTOR_END__
00001f04 d __CTOR_LIST__
00001f10 d __DTOR_END__
00001f0c d __DTOR_LIST__
000004d4 r __FRAME_END__
00001f14 d __JCR_END__
00001f14 d __JCR_LIST__
0000200c A __bss_start
         w __cxa_finalize@@GLIBC_2.1.3
00000480 t __do_global_ctors_aux
000003b0 t __do_global_dtors_aux
00002008 d __dso_handle
         w __gmon_start__
00000467 t __i686.get_pc_thunk.bx
0000200c A _edata
00002014 A _end
000004b8 T _fini
00000348 T _init
0000200c b completed.7021
00002010 b dtor_idx.7023
00000430 t frame_dummy

Here’s the another version, I added your suggestion of the extern "C" but I had the same result as before, the library could not be found.

/*testagent.c*/
#include "jni.h"
#include "jvmti.h"

extern "C" {
    JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
    {
        return 0;
    }
}

/* Agent_OnUnload() is called last */
JNIEXPORT void JNICALL
Agent_OnUnload(JavaVM *vm)
{
}

Here’s the output from the nm command

000004bc T Agent_OnLoad
000004c6 T Agent_OnUnload
0000200c d DW.ref.__gxx_personality_v0
00001f18 a _DYNAMIC
00001ff4 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
00001f08 d __CTOR_END__
00001f04 d __CTOR_LIST__
00001f10 d __DTOR_END__
00001f0c d __DTOR_LIST__
00000594 r __FRAME_END__
00001f14 d __JCR_END__
00001f14 d __JCR_LIST__
00002010 A __bss_start
         w __cxa_finalize@@GLIBC_2.1.3
000004d0 t __do_global_ctors_aux
00000400 t __do_global_dtors_aux
00002008 d __dso_handle
         w __gmon_start__
         U __gxx_personality_v0
000004b7 t __i686.get_pc_thunk.bx
00002010 A _edata
00002018 A _end
00000508 T _fini
0000039c T _init
00002010 b completed.7021
00002014 b dtor_idx.7023
00000480 t frame_dummy

The trace from the nm commands are slightly different but they both include the Agent_OnLoad.

This is the command line used to create the shared lib in both cases.

cc -g -fno-strict-aliasing -fPIC -fno-omit-frame-pointer -W -Wall  -Wno-unused -Wno-parentheses  -I. -I../agent_util -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include -I/home/user/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//include/linux  -Wl,-soname=libtestagent.so -static-libgcc -mimpure-text  -shared -o libtestagent.so ../src/testagent.o -lc

out put from ldd, NOT working case (g++)

ldd libtestagent.so 
linux-gate.so.1 =>  (0x00d96000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x0019a000)
/lib/ld-linux.so.2 (0x005ee000)

output from ldd, working case (gcc)

ldd libtestagent.so 
linux-gate.so.1 =>  (0x00544000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00908000)
/lib/ld-linux.so.2 (0x003a2000)

Been using linux for about 15 years never knew you could do LD_DEBUG=all, very useful.
Here’s the interesting output

2689:   symbol=__gxx_personality_v0;  lookup in file=/home/mnc/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013//bin/java [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/tls/i686/cmov/libpthread.so.0 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/home/mnc/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013/bin/../jre/lib/i386/jli/libjli.so [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/tls/i686/cmov/libdl.so.2 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/tls/i686/cmov/libc.so.6 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/ld-linux.so.2 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/home/mnc/apps/Genuitec/Common/binary/com.sun.java.jdk.linux.x86_1.6.0.013/jre/lib/i386/server/libjvm.so [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/tls/i686/cmov/libm.so.6 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/home/mnc/apps/javacalltracer/Calltracer/jvmti/libtestagent.so [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/tls/i686/cmov/libc.so.6 [0]
      2689: symbol=__gxx_personality_v0;  lookup in file=/lib/ld-linux.so.2 [0]
      2689: /home/mnc/apps/javacalltracer/Calltracer/jvmti/libtestagent.so: error: symbol lookup error: undefined symbol: __gxx_personality_v0 (fatal)
      2689: 
      2689: file=/home/mnc/apps/javacalltracer/Calltracer/jvmti/libtestagent.so [0];  destroying link map
Error occurred during initialization of VM

I did a search on stackoverflow regarding this, a post suggested adding a global for this symbol
so I’ve added __gxx_personality_v0 as a
void *__gxx_personality_v0;

and now the JVM found the library, when compiling with g++.

java,#gcc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:17:12+00:00Added an answer on May 26, 2026 at 6:17 pm

    You didn’t show us contents of testagent.c.

    My guess is that you are being bitten by C++ name mangling, either because you didn’t use JNIEXPORT when defining Agent_OnLoad(), or because JNIEXPORT doesn’t include extern "C" on your platform.

    Surrounding your definition of Agent_OnLoad() with extern "C" { and } should be all you need.

    You can verify whether name mangling is in fact your problem by executing

    nm libtestagent.so | grep Agent_OnLoad
    

    and comparing the result for working (gcc) and broken (g++) versions.

    Update:

    Ok, so C++ name mangling wasn’t it. You next step should be finding out why dynamic linker is failing to dlopen("libtestagent.so"). You can do that by prefixing your command with LD_DEBUG=all, collecting the working and non-working output (output will be huge), and looking for the differences.

    Update 2:

    I’ve added __gxx_personality_v0 as a void *__gxx_personality_v0;

    That’s not quite the correct way to fix the problem, and will likely cause you problems later when you start using actual C++ in the agent.

    The correct way to fix the problem is to link the library with g++, and not gcc. That will add a dependency on libstdc++.so.6, which defines __gxx_personality_v0 and a bunch of other stuff your C++ code will need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have very simple window where I have 2 buttons - one for cancel,
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding
I have a very simple bit of script that changes the status of an
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have a very simple Java RMI Server that looks like the following: import
I have a very simple ASP.Net page that acts as a front end for
I have a very simple question. I want to test whether a particular port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.