hprof_check.c

来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 1,150 行 · 第 1/3 页

C
1,150
字号
/* * @(#)hprof_check.c	1.6 05/11/17 *  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: *  * -Redistribution of source code must retain the above copyright notice, this *  list of conditions and the following disclaimer. *  * -Redistribution in binary form must reproduce the above copyright notice,  *  this list of conditions and the following disclaimer in the documentation *  and/or other materials provided with the distribution. *  * Neither the name of Sun Microsystems, Inc. or the names of contributors may  * be used to endorse or promote products derived from this software without  * specific prior written permission. *  * This software is provided "AS IS," without a warranty of any kind. ALL  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. *  * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* Functionality for checking hprof format=b output. *//* ONLY used with logflags=4. *//* Verifies and write a verbose textual version of a format=b file. *   Textual output file is gdata->checkfilename, fd is gdata->check_fd. *   Buffer is in gdata too, see gdata->check* variables. *   Could probably be isolated to a separate library or utility. */#include "hprof.h"typedef TableIndex HprofId;#include "hprof_b_spec.h"static int type_size[ /*HprofType*/ ] =  HPROF_TYPE_SIZES;/* For map from HPROF_UTF8 to a string */typedef struct UmapInfo {    char *str;} UmapInfo;/* Field information */typedef struct Finfo {    HprofId   id;    HprofType ty;} Finfo;/* Class information map from class ID (ClassIndex) to class information */typedef struct CmapInfo {    int      max_finfo;    int      n_finfo;    Finfo   *finfo;    int      inst_size;    HprofId  sup;} CmapInfo;/* Read raw bytes from the file image, update the pointer */static void read_raw(unsigned char **pp, unsigned char *buf, int len) {    while ( len > 0 ) {        *buf = **pp;        buf++;        (*pp)++;        len--;    }}/* Read various sized elements, properly converted from big to right endian. *    File will contain big endian format. */static unsigned read_u1(unsigned char **pp) {    unsigned char b;        read_raw(pp, &b, 1);    return b;}static unsigned read_u2(unsigned char **pp) {    unsigned short s;    read_raw(pp, (void*)&s, 2);    return md_htons(s);}static unsigned read_u4(unsigned char **pp) {    unsigned int u;    read_raw(pp, (void*)&u, 4);    return md_htonl(u);}static jlong read_u8(unsigned char **pp) {    unsigned int high;    unsigned int low;    jlong        x;    high = read_u4(pp);    low  = read_u4(pp);    x = high;    x = (x << 32) | low;    return x;}static HprofId read_id(unsigned char **pp) {    return (HprofId)read_u4(pp);}/* System error routine */static voidsystem_error(const char *system_call, int rc, int errnum){    char buf[256];    char details[256];    details[0] = 0;    if ( errnum != 0 ) {        md_system_error(details, (int)sizeof(details));    } else if ( rc >= 0 ) {        (void)strcpy(details,"Only part of buffer processed");    }    if ( details[0] == 0 ) {        (void)strcpy(details,"Unknown system error condition");    }    (void)md_snprintf(buf, sizeof(buf), "System %s failed: %s\n",                            system_call, details);    HPROF_ERROR(JNI_TRUE, buf);}/* Write to a fd */static void system_write(int fd, void *buf, int len){    int res;       HPROF_ASSERT(fd>=0);    res = md_write(fd, buf, len);    if (res < 0 || res!=len) {        system_error("write", res, errno);    }}/* Flush check buffer */static voidcheck_flush(void){    if ( gdata->check_fd < 0 ) {        return;    }    if (gdata->check_buffer_index) {        system_write(gdata->check_fd, gdata->check_buffer, gdata->check_buffer_index);        gdata->check_buffer_index = 0;    }}/* Read out a given typed element */static jvalueread_val(unsigned char **pp, HprofType ty){    jvalue        val;    static jvalue empty_val;       val = empty_val;    switch ( ty ) {        case 0:        case HPROF_ARRAY_OBJECT:         case HPROF_NORMAL_OBJECT:             val.i = read_id(pp);            break;        case HPROF_BYTE:         case HPROF_BOOLEAN:            val.b = read_u1(pp);            break;        case HPROF_CHAR:         case HPROF_SHORT:            val.s = read_u2(pp);            break;        case HPROF_FLOAT:         case HPROF_INT:            val.i = read_u4(pp);            break;        case HPROF_DOUBLE:         case HPROF_LONG:            val.j = read_u8(pp);            break;        default:            HPROF_ERROR(JNI_TRUE, "bad type number");            break;    }    return val;}/* Move arbitrary byte stream into gdata->check_fd */static void check_raw(void *buf, int len){    if ( gdata->check_fd < 0 ) {        return;    }    if ( len <= 0 ) {        return;    }    if (gdata->check_buffer_index + len > gdata->check_buffer_size) {        check_flush();        if (len > gdata->check_buffer_size) {            system_write(gdata->check_fd, buf, len);            return;        }    }    (void)memcpy(gdata->check_buffer + gdata->check_buffer_index, buf, len);    gdata->check_buffer_index += len;}/* Printf for gdata->check_fd */static void check_printf(char *fmt, ...){    char    buf[1024];    va_list args;       if ( gdata->check_fd < 0 ) {        return;    }    va_start(args, fmt);    (void)md_vsnprintf(buf, sizeof(buf), fmt, args);    buf[sizeof(buf)-1] = 0;    check_raw(buf, (int)strlen(buf));    va_end(args);}/* Printf of an element for gdata->check_fd */static voidcheck_printf_val(HprofType ty, jvalue val, int long_form){    jint low;    jint high;    switch ( ty ) {        case HPROF_ARRAY_OBJECT:            check_printf("0x%08x", val.i);            break;        case HPROF_NORMAL_OBJECT:            check_printf("0x%08x", val.i);            break;        case HPROF_BOOLEAN:            check_printf("0x%02x", val.b);            break;        case HPROF_CHAR:            if ( long_form ) {                if ( val.s < 0 || val.s > 0x7f || !isprint(val.s) ) {                    check_printf("0x%04x", val.s);                } else {                    check_printf("0x%04x(%c)", val.s, val.s);                }            } else {                if ( val.s < 0 || val.s > 0x7f || !isprint(val.s) ) {                    check_printf("\\u%04x", val.s);                } else {                    check_printf("%c", val.s);                }            }            break;        case HPROF_FLOAT:            low  = jlong_low(val.j);            check_printf("0x%08x(%f)", low, (double)val.f);            break;        case HPROF_DOUBLE:            high = jlong_high(val.j);            low  = jlong_low(val.j);            check_printf("0x%08x%08x(%f)", high, low, val.d);            break;        case HPROF_BYTE:            check_printf("0x%02x", val.b);            break;        case HPROF_SHORT:            check_printf("0x%04x", val.s);            break;        case HPROF_INT:            check_printf("0x%08x", val.i);            break;        case HPROF_LONG:            high = jlong_high(val.j);            low  = jlong_low(val.j);            check_printf("0x%08x%08x", high, low);            break;    }}/* Printf of a string for gdata->check_fd */static voidcheck_printf_str(char *str){    int len;    int i;    if ( str == NULL ) {        check_printf("<null>");    }    check_printf("\"");    len = (int)strlen(str);    for (i = 0; i < len; i++) {        unsigned char c;        c = str[i];        if ( isprint(c) ) {            check_printf("%c", c);        } else {            check_printf("\\x%02x", c);        }    }    check_printf("\"");}/* Printf of a utf8 id for gdata->check_fd */static voidcheck_print_utf8(struct LookupTable *utab, char *prefix, HprofId id){    TableIndex uindex;    if ( id == 0 ) {        check_printf("%s0x%x", prefix, id);    } else {        uindex = table_find_entry(utab, &id, sizeof(id));        if ( uindex == 0 ) {            check_printf("%s0x%x", prefix, id);        } else {            UmapInfo *umap;            umap = (UmapInfo*)table_get_info(utab, uindex);            HPROF_ASSERT(umap!=NULL);            HPROF_ASSERT(umap->str!=NULL);            check_printf("%s0x%x->", prefix, id);            check_printf_str(umap->str);        }    }}/* Add a instance field information to this cmap. */static voidadd_inst_field_to_cmap(CmapInfo *cmap, HprofId id, HprofType ty){   int i;     HPROF_ASSERT(cmap!=NULL);   i = cmap->n_finfo++;   if ( i+1 >= cmap->max_finfo ) {       int    osize;       Finfo *new_finfo;              osize            = cmap->max_finfo;       cmap->max_finfo += 12;       new_finfo = (Finfo*)HPROF_MALLOC(cmap->max_finfo*(int)sizeof(Finfo));       (void)memset(new_finfo,0,cmap->max_finfo*(int)sizeof(Finfo));       if ( i == 0 ) {           cmap->finfo = new_finfo;       } else {           (void)memcpy(new_finfo,cmap->finfo,osize*(int)sizeof(Finfo));           HPROF_FREE(cmap->finfo);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?