1 <?xml version="1.0" encoding='ISO-8859-1'?> 2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"> 3 4 <book id="oprofile-devel-guide"> 5 <bookinfo> 6 <title>OProfile JIT agent developer guide</title> 7 8 <authorgroup> 9 <author> 10 <firstname>Maynard</firstname> 11 <surname>Johnson</surname> 12 <affiliation> 13 <address><email>maynardj (a] us.ibm.com</email></address> 14 </affiliation> 15 </author> 16 </authorgroup> 17 18 <copyright> 19 <year>2007</year> 20 <holder>IBM Corporation</holder> 21 </copyright> 22 </bookinfo> 23 24 <toc></toc> 25 26 <chapter id="developing"> 27 <title>Developing a new JIT agent</title> 28 <para> 29 OProfile includes a header file and library that are intended to be used by 30 developers who wish to extend OProfile's JIT support to other non-supported 31 virtual machines. This developer guide describes these development files and how 32 to use them. 33 </para> 34 <sect1 id="jit-devel-overview"> 35 <title>Overview</title> 36 <para> 37 OProfile already includes some implementations that use the JIT support, 38 e.g., the Java Virtual Machine Toolkit Interface (JVMTI) library, 39 libjvmti_oprofile.so. In developing a new implementation, you will 40 likely follow a similar (if not identical) procedure as was used in 41 developing the JVMTI library. Following are the high level steps to follow: 42 <orderedlist> 43 <listitem>Ensure your virtual machine provides an API that, at minimum, 44 can provide the following information about dynamically compiled code: 45 <itemizedlist> 46 <listitem>Notification when compilation occurs</listitem> 47 <listitem>Name of the symbol (i.e., function or class/method, etc.)</listitem> 48 <listitem>Address in anonymous memory where the compiled code was loaded</listitem> 49 <listitem>Length of the compiled code segment</listitem> 50 </itemizedlist> 51 </listitem> 52 <listitem>Write an agent library that communicates with your VM to obtain 53 compiled code notifications. Invoke the required functions from opagent.h 54 (<xref linkend="jit-interface" />) and link your library with libopagent.so 55 (installed at <filename><oprofile_install_dir>/lib/oprofile</filename>). 56 </listitem> 57 </orderedlist> 58 59 </para> 60 </sect1> 61 <sect1 id="jit-interface"> 62 <title>Implementing JIT support for a new virtual machine</title> 63 <para> 64 The JIT support API for OProfile is defined 65 in <filename><oprofile-install-dir>/include/opagent.h</filename>. 66 Some parts of the API are mandatory for an agent library to use; other 67 parts are optional. The mandatory functions are shown below. 68 </para> 69 <screen> 70 op_agent_t op_open_agent(void); 71 72 void op_close_agent(op_agent_t hdl); 73 74 int op_write_native_code(op_agent_t hdl, char const * symbol_name, 75 uint64_t vma, const void * code, 76 const unsigned int code_size); 77 </screen> 78 79 <para> 80 To implement this part of your library, you must perform the 81 following steps: 82 <orderedlist> 83 <listitem>Implement a function to set up initial communication with the VM. 84 Once communication to the VM is established, your agent library should call 85 <function>op_op_agent()</function> and cache the returned <code>op_agent_t</code> handle for use in 86 future calls.</listitem> 87 <listitem>Perform any necessary steps to register with the VM to be notified of 88 compiled code load events. Registration must include a callback function you 89 will implement in the library to handle the compiled code load events.</listitem> 90 <listitem>The callback function mentioned above must obtain all required 91 information from the VM to pass to libopagent via <function>op_write_native_code()</function>.</listitem> 92 <listitem>When disconnecting from the VM, your library should call 93 <function>op_agent_close()</function>.</listitem> 94 </orderedlist> 95 </para> 96 97 <para>Use of the functions below are optional, depending on the kinds of information your VM 98 can provide to your agent library. See the JVMTI agent library for an example of how to use 99 these functions. 100 <screen> 101 int op_unload_native_code(op_agent_t hdl, uint64_t vma); 102 103 int op_write_debug_line_info(op_agent_t hdl, void const * code, 104 size_t nr_entry, 105 struct debug_line_info const * compile_map); 106 </screen> 107 </para> 108 <note>While the libopagent functions are thread-safe, you should not use them in 109 signal handlers. 110 </note> 111 </sect1> 112 </chapter> 113 114 115 <chapter id="jit-api"> 116 <title>The JIT support API</title> 117 <para> 118 This chapter describes the JIT support API. See opagent.h for more details. 119 </para> 120 121 <sect1 id="op_open_agent"> 122 <title>op_open_agent</title> 123 124 <funcsynopsis>Initializes the agent library. 125 <funcsynopsisinfo>#include <opagent.h></funcsynopsisinfo> 126 <funcprototype> 127 <funcdef>op_agent_t <function>op_open_agent</function></funcdef> 128 <paramdef>void</paramdef> 129 </funcprototype> 130 </funcsynopsis> 131 <note> 132 <title>Description</title> 133 This function must be called by agents before any other function. 134 Creates and opens a JIT dump file in <filename>/var/lib/oprofile/jitdump</filename> 135 using the naming convention <filename><process_id>.dump</filename>. 136 </note> 137 <note> 138 <title>Parameters</title> 139 None 140 </note> 141 <note> 142 <title>Return value</title> 143 <para>Returns a valid <code>op_agent_t</code> handle or NULL. 144 If NULL is returned, <code>errno</code> is set to indicate the nature of the error. For a list 145 of possible <code>errno</code> values, see the man pages for:</para> 146 <code> 147 stat, creat, gettimeofday, fdopen, fwrite 148 </code> 149 </note> 150 </sect1> 151 152 <sect1 id="op_close_agent"> 153 <title>op_close_agent</title> 154 <funcsynopsis>Uninitialize the agent library. 155 <funcsynopsisinfo>#include <opagent.h></funcsynopsisinfo> 156 <funcprototype> 157 <funcdef>int <function>op_close_agent</function></funcdef> 158 <paramdef>op_agent_t <parameter>hdl</parameter></paramdef> 159 </funcprototype> 160 </funcsynopsis> 161 <note> 162 <title>Description</title> 163 Frees all resources and closes open file handles. 164 </note> 165 <note> 166 <title>Parameters</title> 167 <parameter>hdl : </parameter>Handle returned from an earlier call to 168 <function>op_open_agent()</function> 169 </note> 170 <note> 171 <title>Return value</title> 172 <para>Returns 0 on success; -1 otherwise. If -1 is returned, <code>errno</code> is set 173 to indicate the nature of the error. 174 <code>errno</code> is set to EINVAL if an invalid <code>op_agent_t</code> 175 handle is passed. For a list of other possible <code>errno</code> values, see the man pages for:</para> 176 <code>gettimeofday, fwrite</code> 177 </note> 178 </sect1> 179 180 <sect1 id="op_write_native_code"> 181 <title>op_write_native_code</title> 182 <funcsynopsis>Write information about compiled code to a JIT dump file. 183 <funcsynopsisinfo>#include <opagent.h></funcsynopsisinfo> 184 <funcprototype> 185 <funcdef>int <function>op_write_native_code</function></funcdef> 186 <paramdef>op_agent_t<parameter>hdl</parameter></paramdef> 187 <paramdef>char const *<parameter>symbol_name</parameter></paramdef> 188 <paramdef>uint64_t<parameter>vma</parameter></paramdef> 189 <paramdef>void const *<parameter>code</parameter></paramdef> 190 <paramdef>const unsigned int<parameter>code_size</parameter></paramdef> 191 </funcprototype> 192 </funcsynopsis> 193 <note> 194 <title>Description</title> 195 Signal the dynamic generation of native code from a virtual machine. 196 Writes a JIT dump record to the open JIT dump file using the passed information. 197 </note> 198 <note> 199 <title>Parameters</title> 200 <para> 201 <parameter>hdl : </parameter>Handle returned from an earlier call to 202 <function>op_open_agent()</function> 203 </para> 204 <para> 205 <parameter>symbol_name : </parameter>The name of the symbol being dynamically compiled. 206 This name can (and should) contain all necessary information to disambiguate it from 207 symbols of the same name; e.g., class, method signature. 208 </para> 209 <para> 210 <parameter>vma : </parameter>Virtual memory address of the executable code 211 </para> 212 <para> 213 <parameter>code : </parameter>Pointer to the location of the compiled code. 214 Theoretically, this may be a different location from 215 that given by the vma argument. For some JIT compilers, 216 obtaining the code may be impractical. For this (or any other) 217 reason, the agent can choose to pass NULL for this paraemter. 218 If NULL is passed, no code will be copied into the JIT dump 219 file. 220 </para> 221 <para> 222 <parameter>code_size : </parameter>Size of the compiled code 223 </para> 224 </note> 225 <note> 226 <title>Return value</title> 227 <para>Returns 0 on success; -1 otherwise. If -1 is returned, <code>errno</code> is set 228 to indicate the nature of the error. 229 <code>errno</code> is set to EINVAL if an invalid <code>op_agent_t</code> 230 handle is passed. For a list of other possible <code>errno</code> values, see the man pages for:</para> 231 <code>gettimeofday, fwrite</code> 232 </note> 233 </sect1> 234 235 236 <sect1 id="op_write_debug_line_info"> 237 <title>op_write_debug_line_info</title> 238 <funcsynopsis>Write debug information about compiled code to a JIT dump file. 239 <funcsynopsisinfo>#include <opagent.h></funcsynopsisinfo> 240 <funcprototype> 241 <funcdef>int <function>op_write_debug_line_info</function></funcdef> 242 <paramdef>op_agent_t<parameter>hdl</parameter></paramdef> 243 <paramdef>void const *<parameter>code</parameter></paramdef> 244 <paramdef>size_t<parameter>nr_entry</parameter></paramdef> 245 <paramdef>struct debug_line_info const *<parameter>compile_map</parameter></paramdef> 246 </funcprototype> 247 </funcsynopsis> 248 <note> 249 <title>Description</title> 250 Add debug line information to a piece of code. An <function>op_write_native_code()</function> 251 with the same code pointer should have occurred before this call. It's not 252 necessary to provide one lineno information entry per machine instruction; 253 the array can contain hole. 254 </note> 255 <note> 256 <title>Parameters</title> 257 <para> 258 <parameter>hdl : </parameter>Handle returned from an earlier call to 259 <function>op_open_agent()</function> 260 </para> 261 <para> 262 <parameter>code : </parameter>Pointer to the location of the code with debug info 263 </para> 264 <para> 265 <parameter>nr_entry : </parameter>Number of entries in compile_map 266 </para> 267 <para> 268 <parameter>compile_map : </parameter>Array of struct debug_line_info. See the JVMTI agent 269 library implementation for an example of what information should be retrieved 270 from a VM to fill out this data structure. 271 </para> 272 </note> 273 <note> 274 <title>Return value</title> 275 <para>Returns 0 on success; -1 otherwise. If -1 is returned, <code>errno</code> is set 276 to indicate the nature of the error. 277 <code>errno</code> is set to EINVAL if an invalid <code>op_agent_t</code> 278 handle is passed. For a list of other possible <code>errno</code> values, see the man pages for:</para> 279 <code>gettimeofday, ftell, fwrite</code> 280 </note> 281 </sect1> 282 283 <sect1 id="op_unload_native_code"> 284 <title>op_unload_native_code</title> 285 <funcsynopsis>Write information to the JIT dump file about invalidated compiled code. 286 <funcsynopsisinfo>#include <opagent.h></funcsynopsisinfo> 287 <funcprototype> 288 <funcdef>int <function>op_unload_native_code</function></funcdef> 289 <paramdef>op_agent_t<parameter>hdl</parameter></paramdef> 290 <paramdef>uint64_t<parameter>vma</parameter></paramdef> 291 </funcprototype> 292 </funcsynopsis> 293 <note> 294 <title>Description</title> 295 Signal the invalidation of native code from a virtual machine.</note> 296 <note> 297 <title>Parameters</title> 298 <para> 299 <parameter>hdl : </parameter>Handle returned from an earlier call to 300 <function>op_open_agent()</function> 301 </para> 302 <para> 303 <parameter>vma : </parameter>Virtual memory address of the compiled code being unloaded. 304 An <function>op_write_native_code()</function> with the same vma should have occurred before this call. 305 </para> 306 </note> 307 <note> 308 <title>Return value</title> 309 <para>Returns 0 on success; -1 otherwise. If -1 is returned, <code>errno</code> is set 310 to indicate the nature of the error. 311 <code>errno</code> is set to EINVAL if an invalid <code>op_agent_t</code> 312 handle is passed. For a list of other possible <code>errno</code> values, see the man pages for:</para> 313 <code>gettimeofday, fwrite</code> 314 </note> 315 </sect1> 316 317 </chapter> 318 319 </book> 320