Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/jvmti/hprof/hprof_frame.c
38829 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/* This file contains support for handling frames, or (method,location) pairs. */4142#include "hprof.h"4344/*45* Frames map 1-to-1 to (methodID,location) pairs.46* When no line number is known, -1 should be used.47*48* Frames are mostly used in traces (see hprof_trace.c) and will be marked49* with their status flag as they are written out to the hprof output file.50*51*/5253enum LinenoState {54LINENUM_UNINITIALIZED = 0,55LINENUM_AVAILABLE = 1,56LINENUM_UNAVAILABLE = 257};5859typedef struct FrameKey {60jmethodID method;61jlocation location;62} FrameKey;6364typedef struct FrameInfo {65unsigned short lineno;66unsigned char lineno_state; /* LinenoState */67unsigned char status;68SerialNumber serial_num;69} FrameInfo;7071static FrameKey*72get_pkey(FrameIndex index)73{74void *key_ptr;75int key_len;7677table_get_key(gdata->frame_table, index, &key_ptr, &key_len);78HPROF_ASSERT(key_len==sizeof(FrameKey));79HPROF_ASSERT(key_ptr!=NULL);80return (FrameKey*)key_ptr;81}8283static FrameInfo *84get_info(FrameIndex index)85{86FrameInfo *info;8788info = (FrameInfo*)table_get_info(gdata->frame_table, index);89return info;90}9192static void93list_item(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg)94{95FrameKey key;96FrameInfo *info;9798HPROF_ASSERT(key_ptr!=NULL);99HPROF_ASSERT(key_len==sizeof(FrameKey));100HPROF_ASSERT(info_ptr!=NULL);101102key = *((FrameKey*)key_ptr);103info = (FrameInfo*)info_ptr;104debug_message(105"Frame 0x%08x: method=%p, location=%d, lineno=%d(%d), status=%d \n",106i, (void*)key.method, (jint)key.location,107info->lineno, info->lineno_state, info->status);108}109110void111frame_init(void)112{113gdata->frame_table = table_initialize("Frame",1141024, 1024, 1023, (int)sizeof(FrameInfo));115}116117FrameIndex118frame_find_or_create(jmethodID method, jlocation location)119{120FrameIndex index;121static FrameKey empty_key;122FrameKey key;123jboolean new_one;124125key = empty_key;126key.method = method;127key.location = location;128new_one = JNI_FALSE;129index = table_find_or_create_entry(gdata->frame_table,130&key, (int)sizeof(key), &new_one, NULL);131if ( new_one ) {132FrameInfo *info;133134info = get_info(index);135info->lineno_state = LINENUM_UNINITIALIZED;136if ( location < 0 ) {137info->lineno_state = LINENUM_UNAVAILABLE;138}139info->serial_num = gdata->frame_serial_number_counter++;140}141return index;142}143144void145frame_list(void)146{147debug_message(148"--------------------- Frame Table ------------------------\n");149table_walk_items(gdata->frame_table, &list_item, NULL);150debug_message(151"----------------------------------------------------------\n");152}153154void155frame_cleanup(void)156{157table_cleanup(gdata->frame_table, NULL, NULL);158gdata->frame_table = NULL;159}160161void162frame_set_status(FrameIndex index, jint status)163{164FrameInfo *info;165166info = get_info(index);167info->status = (unsigned char)status;168}169170void171frame_get_location(FrameIndex index, SerialNumber *pserial_num,172jmethodID *pmethod, jlocation *plocation, jint *plineno)173{174FrameKey *pkey;175FrameInfo *info;176jint lineno;177178pkey = get_pkey(index);179*pmethod = pkey->method;180*plocation = pkey->location;181info = get_info(index);182lineno = (jint)info->lineno;183if ( info->lineno_state == LINENUM_UNINITIALIZED ) {184info->lineno_state = LINENUM_UNAVAILABLE;185if ( gdata->lineno_in_traces ) {186if ( pkey->location >= 0 && !isMethodNative(pkey->method) ) {187lineno = getLineNumber(pkey->method, pkey->location);188if ( lineno >= 0 ) {189info->lineno = (unsigned short)lineno; /* save it */190info->lineno_state = LINENUM_AVAILABLE;191}192}193}194}195if ( info->lineno_state == LINENUM_UNAVAILABLE ) {196lineno = -1;197}198*plineno = lineno;199*pserial_num = info->serial_num;200}201202jint203frame_get_status(FrameIndex index)204{205FrameInfo *info;206207info = get_info(index);208return (jint)info->status;209}210211212