# OProfile JIT agent developer guide

### Maynard Johnson

`<maynardj@us.ibm.com>`

Copyright © 2007 IBM Corporation

# Chapter 1. Developing a new JIT agent

OProfile includes a header file and library that are intended to be used by developers who wish to extend OProfile's JIT support to other non-supported virtual machines. This developer guide describes these development files and how to use them.

## 1. Overview

OProfile already includes some implementations that use the JIT support, e.g., the Java Virtual Machine Toolkit Interface (JVMTI) library, libjvmti_oprofile.so. In developing a new implementation, you will likely follow a similar (if not identical) procedure as was used in developing the JVMTI library. Following are the high level steps to follow:

1. Ensure your virtual machine provides an API that, at minimum, can provide the following information about dynamically compiled code:
   - Notification when compilation occurs
   - Name of the symbol (i.e., function or class/method, etc.)
   - Address in anonymous memory where the compiled code was loaded
   - Length of the compiled code segment
2. Write an agent library that communicates with your VM to obtain compiled code notifications. Invoke the required functions from opagent.h (Section 2, “Implementing JIT support for a new virtual machine”) and link your library with libopagent.so (installed at `<oprofile_install_dir>/lib/oprofile`).

## 2. Implementing JIT support for a new virtual machine

The JIT support API for OProfile is defined in `<oprofile-install-dir>/include/opagent.h`. Some parts of the API are mandatory for an agent library to use; other parts are optional. The mandatory functions are shown below.

    op_agent_t op_open_agent(void);

    void op_close_agent(op_agent_t hdl);

    int op_write_native_code(op_agent_t hdl, char const * symbol_name,
                             uint64_t vma, const void * code,
                             const unsigned int code_size);

To implement this part of your library, you must perform the following steps:

1. Implement a function to set up initial communication with the VM. Once communication to the VM is established, your agent library should call `op_op_agent()` and cache the returned `op_agent_t` handle for use in future calls.
2. Perform any necessary steps to register with the VM to be notified of compiled code load events. Registration must include a callback function you will implement in the library to handle the compiled code load events.
3. The callback function mentioned above must obtain all required information from the VM to pass to libopagent via `op_write_native_code()`.
4. When disconnecting from the VM, your library should call `op_agent_close()`.

Use of the functions below are optional, depending on the kinds of information your VM can provide to your agent library. See the JVMTI agent library for an example of how to use these functions.

    int op_unload_native_code(op_agent_t hdl, uint64_t vma);

    int op_write_debug_line_info(op_agent_t hdl, void const * code,
                                 size_t nr_entry,
                                 struct debug_line_info const * compile_map);

### Note

While the libopagent functions are thread-safe, you should not use them in signal handlers.

# Chapter 2. The JIT support API

This chapter describes the JIT support API. See opagent.h for more details.

## 1. op_open_agent

Initializes the agent library.

    #include <opagent.h>

`op_agent_t op_open_agent(` | `)`; |

`void`;

### Description

This function must be called by agents before any other function. Creates and opens a JIT dump file in `/var/lib/oprofile/jitdump` using the naming convention `<process_id>.dump`.

### Parameters

None

### Return value

Returns a valid `op_agent_t` handle or NULL. If NULL is returned, `errno` is set to indicate the nature of the error. For a list of possible `errno` values, see the man pages for:

`stat, creat, gettimeofday, fdopen, fwrite`

## 2. op_close_agent

Uninitialize the agent library.

    #include <opagent.h>

`int op_close_agent(` | hdl`)`; |

`op_agent_t hdl`;

### Description

Frees all resources and closes open file handles.

### Parameters

`hdl :` Handle returned from an earlier call to `op_open_agent()`

### Return value

Returns 0 on success; -1 otherwise. If -1 is returned, `errno` is set to indicate the nature of the error. `errno` is set to EINVAL if an invalid `op_agent_t` handle is passed. For a list of other possible `errno` values, see the man pages for:

`gettimeofday, fwrite`

## 3. op_write_native_code

Write information about compiled code to a JIT dump file.

    #include <opagent.h>

`int op_write_native_code(` | hdl, |
| symbol_name, |
| vma, |
| code, |
| code_size`)`; |

`op_agent_thdl`;
`char const *symbol_name`;
`uint64_tvma`;
`void const *code`;
`const unsigned intcode_size`;

### Description

Signal the dynamic generation of native code from a virtual machine. Writes a JIT dump record to the open JIT dump file using the passed information.

### Parameters

`hdl :` Handle returned from an earlier call to `op_open_agent()`

`symbol_name :` The name of the symbol being dynamically compiled. This name can (and should) contain all necessary information to disambiguate it from symbols of the same name; e.g., class, method signature.

`vma :` Virtual memory address of the executable code

`code :` Pointer to the location of the compiled code. Theoretically, this may be a different location from that given by the vma argument. For some JIT compilers, obtaining the code may be impractical. For this (or any other) reason, the agent can choose to pass NULL for this paraemter. If NULL is passed, no code will be copied into the JIT dump file.

`code_size :` Size of the compiled code

### Return value

Returns 0 on success; -1 otherwise. If -1 is returned, `errno` is set to indicate the nature of the error. `errno` is set to EINVAL if an invalid `op_agent_t` handle is passed. For a list of other possible `errno` values, see the man pages for:

`gettimeofday, fwrite`

## 4. op_write_debug_line_info

Write debug information about compiled code to a JIT dump file.

    #include <opagent.h>

`int op_write_debug_line_info(` | hdl, |
| code, |
| nr_entry, |
| compile_map`)`; |

`op_agent_thdl`;
`void const *code`;
`size_tnr_entry`;
`struct debug_line_info const *compile_map`;

### Description

Add debug line information to a piece of code. An `op_write_native_code()` with the same code pointer should have occurred before this call. It's not necessary to provide one lineno information entry per machine instruction; the array can contain hole.

### Parameters

`hdl :` Handle returned from an earlier call to `op_open_agent()`

`code :` Pointer to the location of the code with debug info

`nr_entry :` Number of entries in compile_map

`compile_map :` Array of struct debug_line_info. See the JVMTI agent library implementation for an example of what information should be retrieved from a VM to fill out this data structure.

### Return value

Returns 0 on success; -1 otherwise. If -1 is returned, `errno` is set to indicate the nature of the error. `errno` is set to EINVAL if an invalid `op_agent_t` handle is passed. For a list of other possible `errno` values, see the man pages for:

`gettimeofday, ftell, fwrite`

## 5. op_unload_native_code

Write information to the JIT dump file about invalidated compiled code.

    #include <opagent.h>

`int op_unload_native_code(` | hdl, |
| vma`)`; |

`op_agent_thdl`;
`uint64_tvma`;

### Description

Signal the invalidation of native code from a virtual machine.

### Parameters

`hdl :` Handle returned from an earlier call to `op_open_agent()`

`vma :` Virtual memory address of the compiled code being unloaded. An `op_write_native_code()` with the same vma should have occurred before this call.

### Return value

Returns 0 on success; -1 otherwise. If -1 is returned, `errno` is set to indicate the nature of the error. `errno` is set to EINVAL if an invalid `op_agent_t` handle is passed. For a list of other possible `errno` values, see the man pages for:

`gettimeofday, fwrite`