jcov_util.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 647 行 · 第 1/2 页
C
647 行
/* * @(#)jcov_util.c 1.20 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */#include "javavm/include/porting/ansi/stdlib.h"#include "javavm/include/porting/ansi/string.h"#include "jvmpi.h"#include "jcov.h"#include "jcov_types.h"#include "jcov_jvm.h"#include "jcov_error.h"#include "jcov_htables.h"#include "jcov_hash.h"#include "jcov_util.h"#include "jcov_setup.h"const char *DUMMY_SRC_PREF;const char *DUMMY_SRC_SUFF;int opc_lengths[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 99, 99, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 5, 1, 3, 2, 3, 1, 1, 3, 3, 1, 1, 1, 4, 3, 3, 5, 5, 1};void *jcov_calloc(SIZE_T size) { void *p = (void *)calloc(1, size); if (p == NULL) { jcov_error("***out of memory***"); CALL(ProfilerExit)((jint)1); } memory_allocated += size; return p;}void jcov_dummy_free(void *p) { printf("free: %p\n", p);}UINT8 read1bytes(UINT8 **buf, jint *len, int *err_code) { *len = *len - 1; if ((*err_code = (*len < 0))) return 0; (*buf)++; return (*buf-1)[0];}UINT16 read2bytes(UINT8 **buf, jint *len, int *err_code) { UINT16 res; UINT8 *buf1 = *buf; *len = *len - 2; if ((*err_code = (*len < 0))) return 0; res = (UINT16)((buf1[0] << 8) | buf1[1]); *buf += 2; return res;}INT32 read4bytes(UINT8 **buf, jint *len, int *err_code) { INT32 res; UINT8 *buf1 = *buf; *len = *len - 4; if ((*err_code = (*len < 0))) return 0; res = (buf1[0] << 24) | (buf1[1] << 16) | (buf1[2] << 8) | buf1[3]; *buf += 4; return res;}#define CHK_UTF(cond) if (cond) { \ if (verbose_mode > 0) jcov_error("bad UTF string"); \ *err_code = 1; \ return NULL; \}char *readUTF8(UINT8 **buf, jint *len, int utflen, int *err_code) { UINT8 *str = (UINT8*)jcov_calloc(utflen + 1); int count = 0; int strlen = 0; while (count < utflen) { UINT8 char2, char3; UINT8 c = read1bytes(buf, len, err_code); if (*err_code) return NULL; switch (c >> 4) { default: /* 0xxx xxxx */ count++; str[strlen++] = c; break; case 0xC: case 0xD: /* 110x xxxx 10xx xxxx */ count += 2; CHK_UTF(count > utflen); char2 = read1bytes(buf, len, err_code); CHK_UTF((char2 & 0xC0) != 0x80); str[strlen++] = '?'; break; case 0xE: /* 1110 xxxx 10xx xxxx 10xx xxxx */ count += 3; CHK_UTF(count > utflen); char2 = read1bytes(buf, len, err_code); char3 = read1bytes(buf, len, err_code); CHK_UTF(((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)); str[strlen++] = '?'; break; case 0x8: case 0x9: case 0xA: case 0xB: case 0xF: /* 10xx xxxx, 1111 xxxx */ CHK_UTF(1); } if (*err_code) return NULL; } str[strlen] = '\0'; return (char*)str;}#ifdef DEBUG#define def_name(e,v) case e: v=#e ; breakstatic char *get_cp_entry_type_name(int tag) { char* s = "UNKNOWN_TYPE"; switch (tag) { def_name(JVM_CONSTANT_Utf8, s); def_name(JVM_CONSTANT_Class, s); def_name(JVM_CONSTANT_Methodref, s); def_name(JVM_CONSTANT_InterfaceMethodref, s); def_name(JVM_CONSTANT_NameAndType, s); def_name(JVM_CONSTANT_Integer, s); def_name(JVM_CONSTANT_Float, s); def_name(JVM_CONSTANT_Long, s); def_name(JVM_CONSTANT_Double, s); def_name(JVM_CONSTANT_String, s); def_name(JVM_CONSTANT_Fieldref, s); } return s;}void print_cp_entry(cp_entry_t *cp_entry) { if (cp_entry == NULL) { printf("NULL\n"); return; } printf("%s ", get_cp_entry_type_name(cp_entry->tag)); switch (cp_entry->tag) { case JVM_CONSTANT_Utf8: printf("\"%s\"\n", cp_entry->u.Utf8.bytes); break; case JVM_CONSTANT_Class: printf("#%d\n", cp_entry->u.Class.name_index); break; case JVM_CONSTANT_Methodref: printf("class: #%d, name_and_type: #%d\n", cp_entry->u.Methodref.class_index, cp_entry->u.Methodref.name_and_type_index); break; case JVM_CONSTANT_InterfaceMethodref: printf("class: #%d, name_and_type: #%d\n", cp_entry->u.InterfaceMethodref.class_index, cp_entry->u.InterfaceMethodref.name_and_type_index); break; case JVM_CONSTANT_NameAndType: printf("name: #%d, type: #%d\n", cp_entry->u.NameAndType.name_index, cp_entry->u.NameAndType.type_index); break; case JVM_CONSTANT_Integer: case JVM_CONSTANT_Float: /* we're not interested in the rest of tags */ printf("0x%x\n", (unsigned)cp_entry->u.Integer.val); break; case JVM_CONSTANT_Long: case JVM_CONSTANT_Double: printf("hi: 0x%x, lo: 0x%x\n", (unsigned)cp_entry->u.Long.hi, (unsigned)cp_entry->u.Long.lo); break; case JVM_CONSTANT_String: printf("#%d\n", cp_entry->u.String.cp_index); break; case JVM_CONSTANT_Fieldref: printf("class: #%d, name_and_type: #%d\n", cp_entry->u.Field.class_index, cp_entry->u.Field.name_and_type_index); break; }}void print_cp(cp_entry_t **cp, int size) { int i; for (i = 0; i < size; i++) { printf("#%d ", i+1); print_cp_entry(cp[i]); }}void debug_print_filter(jcov_filter_t *filter) { int i; printf("FILTER\n"); if (filter == NULL) return; printf("INCL:\n"); for (i = 0; i < filter->incl_size; i++) printf("\t%s\n", filter->incl[i]); printf("EXCL:\n"); for (i = 0; i < filter->excl_size; i++) printf("\t%s\n", filter->excl[i]);}void debug_print_cov_table(cov_item_t *table, int size) { int i; char *types[] = { "METHOD", "FICT_METHOD", "BLOCK", "FICT_RET", "CASE", "SWITCH_WO_DEF", "BRANCH_TRUE", "BRANCH_FALSE" }; for (i = 0; i < size; i++) { cov_item_t it = table[i]; printf("type: %s\tpc: %d, line: %ld pos: %ld\n", types[it.type - 1], it.pc, it.where_line, it.where_pos); }}void debug_print_crt_entry(crt_entry_t *e) { char tmp[128]; char *s; int i; int CRT_TYPE_ARR_LEN = 9; int CRT_TYPE_ARR[] = { CRT_STATEMENT, CRT_BLOCK, CRT_ASSIGNMENT, CRT_FLOW_CONTROLLER, CRT_FLOW_TARGET, CRT_INVOKE, CRT_CREATE, CRT_BRANCH_TRUE, CRT_BRANCH_FALSE }; char *CRT_TYPE_STR_ARR[] = { "STMT", "BLOCK", "ASSGN", "F_CTRL", "F_TARG", "INVOKE", "CREATE", "B_TRUE", "B_FALSE" }; if (e == NULL) { printf("NULL"); return; } for (i = 0, s = tmp; i < CRT_TYPE_ARR_LEN; i++) { if (e->flags & CRT_TYPE_ARR[i] & 0xFFFF) { sprintf(s, "%s ", CRT_TYPE_STR_ARR[i]); s += strlen(CRT_TYPE_STR_ARR[i]) + 1; } } printf("ps_s=%d pc_e=%d rng_s=%ld rng_e=%ld [%s]", e->pc_start, e->pc_end, e->rng_start, e->rng_end, tmp);}#endif /* DEBUG */void jcov_free_cp_entry(cp_entry_t *cp_entry);cp_entry_t *read_next_cp_entry(UINT8 **buf, jint *len, int *err_code) { int utflen; cp_entry_t *cp_buf = (cp_entry_t *)jcov_calloc(sizeof(cp_entry_t)); cp_buf->tag = read1bytes(buf, len, err_code); if (!*err_code) { switch (cp_buf->tag) { case JVM_CONSTANT_Utf8: utflen = read2bytes(buf, len, err_code); if (*err_code) break; cp_buf->u.Utf8.bytes = readUTF8(buf, len, utflen, err_code); break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?