0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A/* This file contains support for handling frames, or (method,location) pairs. */
0N/A
0N/A#include "hprof.h"
0N/A
0N/A/*
0N/A * Frames map 1-to-1 to (methodID,location) pairs.
0N/A * When no line number is known, -1 should be used.
0N/A *
0N/A * Frames are mostly used in traces (see hprof_trace.c) and will be marked
0N/A * with their status flag as they are written out to the hprof output file.
0N/A *
0N/A */
0N/A
0N/Aenum LinenoState {
0N/A LINENUM_UNINITIALIZED = 0,
0N/A LINENUM_AVAILABLE = 1,
0N/A LINENUM_UNAVAILABLE = 2
0N/A};
0N/A
0N/Atypedef struct FrameKey {
0N/A jmethodID method;
0N/A jlocation location;
0N/A} FrameKey;
0N/A
0N/Atypedef struct FrameInfo {
0N/A unsigned short lineno;
0N/A unsigned char lineno_state; /* LinenoState */
0N/A unsigned char status;
0N/A SerialNumber serial_num;
0N/A} FrameInfo;
0N/A
0N/Astatic FrameKey*
0N/Aget_pkey(FrameIndex index)
0N/A{
0N/A void *key_ptr;
0N/A int key_len;
0N/A
0N/A table_get_key(gdata->frame_table, index, &key_ptr, &key_len);
0N/A HPROF_ASSERT(key_len==sizeof(FrameKey));
0N/A HPROF_ASSERT(key_ptr!=NULL);
0N/A return (FrameKey*)key_ptr;
0N/A}
0N/A
0N/Astatic FrameInfo *
0N/Aget_info(FrameIndex index)
0N/A{
0N/A FrameInfo *info;
0N/A
0N/A info = (FrameInfo*)table_get_info(gdata->frame_table, index);
0N/A return info;
0N/A}
0N/A
0N/Astatic void
0N/Alist_item(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg)
0N/A{
0N/A FrameKey key;
0N/A FrameInfo *info;
0N/A
0N/A HPROF_ASSERT(key_ptr!=NULL);
0N/A HPROF_ASSERT(key_len==sizeof(FrameKey));
0N/A HPROF_ASSERT(info_ptr!=NULL);
0N/A
0N/A key = *((FrameKey*)key_ptr);
0N/A info = (FrameInfo*)info_ptr;
0N/A debug_message(
0N/A "Frame 0x%08x: method=%p, location=%d, lineno=%d(%d), status=%d \n",
0N/A i, (void*)key.method, (jint)key.location,
0N/A info->lineno, info->lineno_state, info->status);
0N/A}
0N/A
0N/Avoid
0N/Aframe_init(void)
0N/A{
0N/A gdata->frame_table = table_initialize("Frame",
0N/A 1024, 1024, 1023, (int)sizeof(FrameInfo));
0N/A}
0N/A
0N/AFrameIndex
0N/Aframe_find_or_create(jmethodID method, jlocation location)
0N/A{
0N/A FrameIndex index;
0N/A static FrameKey empty_key;
0N/A FrameKey key;
0N/A jboolean new_one;
0N/A
0N/A key = empty_key;
0N/A key.method = method;
0N/A key.location = location;
0N/A new_one = JNI_FALSE;
0N/A index = table_find_or_create_entry(gdata->frame_table,
0N/A &key, (int)sizeof(key), &new_one, NULL);
0N/A if ( new_one ) {
0N/A FrameInfo *info;
0N/A
0N/A info = get_info(index);
0N/A info->lineno_state = LINENUM_UNINITIALIZED;
0N/A if ( location < 0 ) {
0N/A info->lineno_state = LINENUM_UNAVAILABLE;
0N/A }
0N/A info->serial_num = gdata->frame_serial_number_counter++;
0N/A }
0N/A return index;
0N/A}
0N/A
0N/Avoid
0N/Aframe_list(void)
0N/A{
0N/A debug_message(
0N/A "--------------------- Frame Table ------------------------\n");
0N/A table_walk_items(gdata->frame_table, &list_item, NULL);
0N/A debug_message(
0N/A "----------------------------------------------------------\n");
0N/A}
0N/A
0N/Avoid
0N/Aframe_cleanup(void)
0N/A{
0N/A table_cleanup(gdata->frame_table, NULL, NULL);
0N/A gdata->frame_table = NULL;
0N/A}
0N/A
0N/Avoid
0N/Aframe_set_status(FrameIndex index, jint status)
0N/A{
0N/A FrameInfo *info;
0N/A
0N/A info = get_info(index);
0N/A info->status = (unsigned char)status;
0N/A}
0N/A
0N/Avoid
0N/Aframe_get_location(FrameIndex index, SerialNumber *pserial_num,
0N/A jmethodID *pmethod, jlocation *plocation, jint *plineno)
0N/A{
0N/A FrameKey *pkey;
0N/A FrameInfo *info;
0N/A jint lineno;
0N/A
0N/A pkey = get_pkey(index);
0N/A *pmethod = pkey->method;
0N/A *plocation = pkey->location;
0N/A info = get_info(index);
0N/A lineno = (jint)info->lineno;
0N/A if ( info->lineno_state == LINENUM_UNINITIALIZED ) {
0N/A info->lineno_state = LINENUM_UNAVAILABLE;
0N/A if ( gdata->lineno_in_traces ) {
0N/A if ( pkey->location >= 0 && !isMethodNative(pkey->method) ) {
0N/A lineno = getLineNumber(pkey->method, pkey->location);
0N/A if ( lineno >= 0 ) {
0N/A info->lineno = (unsigned short)lineno; /* save it */
0N/A info->lineno_state = LINENUM_AVAILABLE;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if ( info->lineno_state == LINENUM_UNAVAILABLE ) {
0N/A lineno = -1;
0N/A }
0N/A *plineno = lineno;
0N/A *pserial_num = info->serial_num;
0N/A}
0N/A
0N/Ajint
0N/Aframe_get_status(FrameIndex index)
0N/A{
0N/A FrameInfo *info;
0N/A
0N/A info = get_info(index);
0N/A return (jint)info->status;
0N/A}