CACAO
native.hpp
Go to the documentation of this file.
1 /* src/native/native.hpp - native library support
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5  Copyright (C) 2008 Theobroma Systems Ltd.
6 
7  This file is part of CACAO.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2, or (at
12  your option) any later version.
13 
14  This program is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  02110-1301, USA.
23 
24 */
25 
26 
27 #ifndef NATIVE_HPP_
28 #define NATIVE_HPP_ 1
29 
30 #include <map>
31 #include <set>
32 #include <vector>
33 #include "native/jni.hpp"
34 #include "threads/mutex.hpp"
35 #include "vm/class.hpp"
36 #include "vm/global.hpp"
37 #include "vm/loader.hpp"
38 #include "vm/method.hpp"
39 #include "vm/os.hpp"
40 #include "vm/utf8.hpp"
41 
42 
43 /* defines ********************************************************************/
44 
45 #define NATIVE_METHODS_COUNT sizeof(methods) / sizeof(JNINativeMethod)
46 
47 
48 #define NATIVE_LIBRARY_PREFIX "lib"
49 
50 #if defined(__DARWIN__)
51 # define NATIVE_LIBRARY_SUFFIX ".dylib"
52 #else
53 # define NATIVE_LIBRARY_SUFFIX ".so"
54 #endif
55 
56 #if defined(ENABLE_DL)
57 /**
58  * Represents a native library.
59  */
61 private:
62  Utf8String _filename; ///< Name of the native library.
63  classloader_t *_classloader; ///< Defining classloader.
64  void *_handle; ///< Filesystem handle.
65 
66 public:
67  NativeLibrary(Utf8String filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
68  NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
69 
70  inline classloader_t* get_classloader() const { return _classloader; }
71  inline Utf8String get_filename () const { return _filename; }
72  inline void* get_handle () const { return _handle; }
73 
74  void* open();
75  void close();
76  bool load(JNIEnv* env);
77  bool is_loaded();
78  void* resolve_symbol(Utf8String symbolname) const;
79 };
80 
81 
82 /**
83  * Table containing all loaded native libraries.
84  */
86 private:
87  Mutex _mutex; ///< Mutex to make the container thread-safe.
88  typedef std::multimap<classloader_t*, NativeLibrary> MAP;
90 
91 private:
92  // Comparator class.
93  class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, Utf8String, bool> {
94  public:
95  bool operator() (std::pair<classloader_t*, NativeLibrary> args, Utf8String filename) const
96  {
97  return (args.second.get_filename() == filename);
98  }
99  };
100 
101 public:
102  void add(NativeLibrary& library);
103  bool is_loaded(NativeLibrary& library);
104  void* resolve_symbol(Utf8String symbolname, classloader_t* classloader);
105 };
106 #endif /* defined(ENABLE_DL) */
107 
108 
109 /**
110  * Represents a native method.
111  */
113 private:
114  Utf8String _classname; ///< Class name.
115  Utf8String _name; ///< Method name.
116  Utf8String _descriptor; ///< Method signature.
117  void* _function; ///< Pointer to the native function.
118 
119  friend bool operator< (const NativeMethod& first, const NativeMethod& second);
120 
121 public:
122  NativeMethod(Utf8String classname, Utf8String name, Utf8String signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
123  NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
124 
125  inline void* get_function() const { return _function; }
126 };
127 
128 
129 /**
130  * Table containing all native methods registered with the VM.
131  */
133 private:
135  std::set<NativeMethod> _methods;
136 
137 private:
138  // Comparator class.
139  class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, Utf8String, bool> {
140  public:
141  bool operator() (std::pair<classloader_t*, NativeLibrary> args, Utf8String filename) const
142  {
143  return (args.second.get_filename() == filename);
144  }
145  };
146 
147 public:
148  void register_methods(Utf8String classname, const JNINativeMethod* methods, size_t count);
149  void* resolve_method(methodinfo* m);
151 };
152 
153 
154 #if defined(ENABLE_JVMTI)
155 /**
156  * Represents a registered native agent.
157  */
158 class NativeAgent {
159 private:
160  char* _library;
161  char* _options;
162 
163 public:
164  NativeAgent(char* library, char* options) : _library(library), _options(options) {}
165 
166  char* get_library() const { return _library; }
167  char* get_options() const { return _options; }
168 };
169 
170 
171 /**
172  * Table containing all native agent libraries.
173  */
174 class NativeAgents {
175 private:
176  std::vector<NativeAgent> _agents;
177 
178 public:
179  void register_agent_library(char* library, char* options);
180  void register_agent_path(char* path, char* options);
181  bool load_agents();
182  bool unload_agents();
183 };
184 #endif /* defined(ENABLE_JVMTI) */
185 
186 /* function prototypes ********************************************************/
187 
190 
191 #endif // NATIVE_HPP_
192 
193 
194 /*
195  * These are local overrides for various environment variables in Emacs.
196  * Please do not remove this and leave it at the end of the file, where
197  * Emacs will automagically detect them.
198  * ---------------------------------------------------------------------
199  * Local variables:
200  * mode: c++
201  * indent-tabs-mode: t
202  * c-basic-offset: 4
203  * tab-width: 4
204  * End:
205  * vim:noexpandtab:sw=4:ts=4:
206  */
Utf8String _classname
Class name.
Definition: native.hpp:114
Utf8String get_filename() const
Definition: native.hpp:71
void * open()
Open this native library.
Definition: native.cpp:398
Table containing all native methods registered with the VM.
Definition: native.hpp:132
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
classloader_t * get_classloader() const
Definition: native.hpp:70
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
void * get_handle() const
Definition: native.hpp:72
void close()
Close this native library.
Definition: native.cpp:434
void * resolve_symbol(Utf8String symbolname, classloader_t *classloader)
Try to find a symbol with the given name in all loaded native libraries defined by classloader...
Definition: native.cpp:606
bool operator()(std::pair< classloader_t *, NativeLibrary > args, Utf8String filename) const
Definition: native.hpp:141
bool operator()(std::pair< classloader_t *, NativeLibrary > args, Utf8String filename) const
Definition: native.hpp:95
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
Utf8String _name
Method name.
Definition: native.hpp:115
friend bool operator<(const NativeMethod &first, const NativeMethod &second)
Definition: native.cpp:212
bool load(JNIEnv *env)
Load this native library and initialize it, if possible.
Definition: native.cpp:469
std::set< NativeMethod > _methods
Definition: native.hpp:135
bool is_loaded()
Checks if this native library is loaded.
Definition: native.cpp:530
std::multimap< classloader_t *, NativeLibrary > MAP
Definition: native.hpp:88
static JNINativeMethod methods[]
NativeMethod(methodinfo *m)
Definition: native.hpp:123
classloader_t * _classloader
Defining classloader.
Definition: native.hpp:63
bool is_loaded(NativeLibrary &library)
Checks if the given native library is loaded.
Definition: native.cpp:578
void * _handle
Filesystem handle.
Definition: native.hpp:64
Mutex _mutex
Definition: native.hpp:134
Represents a native library.
Definition: native.hpp:60
void add(NativeLibrary &library)
Add the given native library to the native libraries table.
Definition: native.cpp:557
NativeLibrary(Utf8String filename, classloader_t *classloader=0, void *handle=0)
Definition: native.hpp:67
Utf8String _descriptor
Method signature.
Definition: native.hpp:116
void * find_registered_method(methodinfo *m)
Try to find the given method in the native methods registered with the VM.
Definition: native.cpp:380
void * resolve_method(methodinfo *m)
Resolves a native method, maybe from a dynamic library.
Definition: native.cpp:271
void * _function
Pointer to the native function.
Definition: native.hpp:117
java_handle_t * native_new_and_init(classinfo *c)
Registers a new native agent by specified by it&#39;s library name and with an optional options string...
Definition: native.cpp:729
Utf8String _filename
Name of the native library.
Definition: native.hpp:62
Mutex _mutex
Mutex to make the container thread-safe.
Definition: native.hpp:87
NativeLibrary(void *handle)
Definition: native.hpp:68
void * get_function() const
Definition: native.hpp:125
NativeMethod(Utf8String classname, Utf8String name, Utf8String signature, void *function)
Definition: native.hpp:122
Table containing all loaded native libraries.
Definition: native.hpp:85
java_handle_t * native_new_and_init_string(classinfo *c, java_handle_t *s)
Definition: native.cpp:762
void * resolve_symbol(Utf8String symbolname) const
Resolve the given symbol in this native library.
Definition: native.cpp:545
Represents a native method.
Definition: native.hpp:112