annotate src/share/tools/hsdis/hsdis-demo.c @ 9050:97d605522fcb

8027434: "-XX:OnOutOfMemoryError" uses fork instead of vfork Summary: On Linux, use vfork in case of an OOM. Reviewed-by: dholmes, iklam
author phh
date Mon, 25 Feb 2019 21:38:45 +0000
parents 9a18c71dbd25
children
rev   line source
jrose@100 1 /*
minqi@3658 2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
jrose@100 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jrose@100 4 *
dbuck@8956 5 * The Universal Permissive License (UPL), Version 1.0
jrose@100 6 *
dbuck@8956 7 * Subject to the condition set forth below, permission is hereby granted to
dbuck@8956 8 * any person obtaining a copy of this software, associated documentation
dbuck@8956 9 * and/or data (collectively the "Software"), free of charge and under any
dbuck@8956 10 * and all copyright rights in the Software, and any and all patent rights
dbuck@8956 11 * owned or freely licensable by each licensor hereunder covering either (i)
dbuck@8956 12 * the unmodified Software as contributed to or provided by such licensor,
dbuck@8956 13 * or (ii) the Larger Works (as defined below), to deal in both
jrose@100 14 *
dbuck@8956 15 * (a) the Software, and
dbuck@8956 16 *
dbuck@8956 17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
dbuck@8959 18 * if one is included with the Software (each a "Larger Work" to which the
dbuck@8956 19 * Software is contributed by such licensors),
dbuck@8956 20 *
dbuck@8956 21 * without restriction, including without limitation the rights to copy,
dbuck@8956 22 * create derivative works of, display, perform, and distribute the Software
dbuck@8956 23 * and make, use, sell, offer for sale, import, export, have made, and have
dbuck@8956 24 * sold the Software and the Larger Work(s), and to sublicense the foregoing
dbuck@8956 25 * rights on either these or other terms.
dbuck@8956 26 *
dbuck@8956 27 * This license is subject to the following condition:
dbuck@8956 28 *
dbuck@8956 29 * The above copyright notice and either this complete permission notice or
dbuck@8956 30 * at a minimum a reference to the UPL must be included in all copies or
dbuck@8956 31 * substantial portions of the Software.
dbuck@8956 32 *
dbuck@8956 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
dbuck@8956 34 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
dbuck@8956 35 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
dbuck@8956 36 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
dbuck@8956 37 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
dbuck@8956 38 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
dbuck@8956 39 * USE OR OTHER DEALINGS IN THE SOFTWARE.
jrose@100 40 *
trims@1472 41 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 42 * or visit www.oracle.com if you need additional information or have any
trims@1472 43 * questions.
jrose@100 44 *
jrose@100 45 */
jrose@100 46
jrose@100 47 /* hsdis-demo.c -- dump a range of addresses as native instructions
jrose@100 48 This demonstrates the protocol required by the HotSpot PrintAssembly option.
jrose@100 49 */
jrose@100 50
minqi@3658 51 #include <stdio.h>
minqi@3658 52 #include <stdlib.h>
minqi@3658 53 #include <string.h>
minqi@3658 54 #include <inttypes.h>
minqi@3658 55
jrose@100 56 #include "hsdis.h"
jrose@100 57
jrose@100 58
jrose@100 59 void greet(const char*);
minqi@3658 60 void disassemble(uintptr_t, uintptr_t);
jrose@100 61 void end_of_file();
jrose@100 62
jrose@100 63 const char* options = NULL;
jrose@100 64 int raw = 0;
jrose@100 65 int xml = 0;
jrose@100 66
jrose@100 67 int main(int ac, char** av) {
jrose@100 68 int greeted = 0;
jrose@100 69 int i;
jrose@100 70 for (i = 1; i < ac; i++) {
jrose@100 71 const char* arg = av[i];
jrose@100 72 if (arg[0] == '-') {
jrose@100 73 if (!strcmp(arg, "-xml"))
jrose@100 74 xml ^= 1;
jrose@100 75 else if (!strcmp(arg, "-raw"))
jrose@100 76 raw ^= 1;
jrose@100 77 else if (!strncmp(arg, "-options=", 9))
jrose@100 78 options = arg+9;
jrose@100 79 else
never@720 80 { printf("Usage: %s [-xml] [name...]\n", av[0]); exit(2); }
jrose@100 81 continue;
jrose@100 82 }
jrose@100 83 greet(arg);
jrose@100 84 greeted = 1;
jrose@100 85 }
jrose@100 86 if (!greeted)
jrose@100 87 greet("world");
jrose@100 88 printf("...And now for something completely different:\n");
minqi@3658 89 void *start = (void*) &main;
minqi@3658 90 void *end = (void*) &end_of_file;
sgehwolf@8976 91 #if defined(__ia64) || (defined(__powerpc__) && !defined(ABI_ELFv2))
minqi@3658 92 /* On IA64 and PPC function pointers are pointers to function descriptors */
minqi@3658 93 start = *((void**)start);
minqi@3658 94 end = *((void**)end);
minqi@3658 95 #endif
minqi@3658 96 disassemble(start, (end > start) ? end : start + 64);
jrose@100 97 printf("Cheers!\n");
jrose@100 98 }
jrose@100 99
jrose@100 100 void greet(const char* whom) {
jrose@100 101 printf("Hello, %s!\n", whom);
jrose@100 102 }
jrose@100 103
jrose@100 104 void end_of_file() { }
jrose@100 105
jrose@100 106 /* don't disassemble after this point... */
jrose@100 107
jrose@100 108 #include "dlfcn.h"
jrose@100 109
minqi@3809 110 #define DECODE_INSTRUCTIONS_VIRTUAL_NAME "decode_instructions_virtual"
minqi@3809 111 #define DECODE_INSTRUCTIONS_NAME "decode_instructions"
jrose@100 112 #define HSDIS_NAME "hsdis"
jrose@100 113 static void* decode_instructions_pv = 0;
minqi@3809 114 static void* decode_instructions_sv = 0;
jrose@100 115 static const char* hsdis_path[] = {
never@720 116 HSDIS_NAME"-"LIBARCH LIB_EXT,
never@720 117 "./" HSDIS_NAME"-"LIBARCH LIB_EXT,
never@720 118 #ifdef TARGET_DIR
never@720 119 TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT,
jrose@100 120 #endif
jrose@100 121 NULL
jrose@100 122 };
jrose@100 123
jrose@100 124 static const char* load_decode_instructions() {
jrose@100 125 void* dllib = NULL;
jrose@100 126 const char* *next_in_path = hsdis_path;
jrose@100 127 while (1) {
minqi@3809 128 decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_VIRTUAL_NAME);
minqi@3809 129 decode_instructions_sv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME);
minqi@3809 130 if (decode_instructions_pv != NULL || decode_instructions_sv != NULL)
jrose@100 131 return NULL;
jrose@100 132 if (dllib != NULL)
minqi@3809 133 return "plugin does not defined "DECODE_INSTRUCTIONS_VIRTUAL_NAME" and "DECODE_INSTRUCTIONS_NAME;
jrose@100 134 for (dllib = NULL; dllib == NULL; ) {
jrose@100 135 const char* next_lib = (*next_in_path++);
jrose@100 136 if (next_lib == NULL)
never@720 137 return "cannot find plugin "HSDIS_NAME LIB_EXT;
jrose@100 138 dllib = dlopen(next_lib, RTLD_LAZY);
jrose@100 139 }
jrose@100 140 }
jrose@100 141 }
jrose@100 142
jrose@100 143
jrose@100 144 static const char* lookup(void* addr) {
minqi@3658 145 #if defined(__ia64) || defined(__powerpc__)
minqi@3658 146 /* On IA64 and PPC function pointers are pointers to function descriptors */
minqi@3658 147 #define CHECK_NAME(fn) \
minqi@3658 148 if (addr == *((void**) &fn)) return #fn;
minqi@3658 149 #else
jrose@100 150 #define CHECK_NAME(fn) \
jrose@100 151 if (addr == (void*) &fn) return #fn;
minqi@3658 152 #endif
jrose@100 153
jrose@100 154 CHECK_NAME(main);
jrose@100 155 CHECK_NAME(greet);
jrose@100 156 return NULL;
jrose@100 157 }
jrose@100 158
jrose@100 159 /* does the event match the tag, followed by a null, space, or slash? */
jrose@100 160 #define MATCH(event, tag) \
jrose@100 161 (!strncmp(event, tag, sizeof(tag)-1) && \
jrose@100 162 (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1])))
jrose@100 163
jrose@100 164
jrose@100 165 static const char event_cookie[] = "event_cookie"; /* demo placeholder */
minqi@3658 166 static void* simple_handle_event(void* cookie, const char* event, void* arg) {
minqi@3658 167 if (MATCH(event, "/insn")) {
minqi@3658 168 // follow each complete insn by a nice newline
minqi@3658 169 printf("\n");
minqi@3658 170 }
minqi@3658 171 return NULL;
minqi@3658 172 }
minqi@3658 173
jrose@100 174 static void* handle_event(void* cookie, const char* event, void* arg) {
jrose@100 175 #define NS_DEMO "demo:"
jrose@100 176 if (cookie != event_cookie)
jrose@100 177 printf("*** bad event cookie %p != %p\n", cookie, event_cookie);
jrose@100 178
jrose@100 179 if (xml) {
jrose@100 180 /* We could almost do a printf(event, arg),
jrose@100 181 but for the sake of a better demo,
jrose@100 182 we dress the result up as valid XML.
jrose@100 183 */
jrose@100 184 const char* fmt = strchr(event, ' ');
jrose@100 185 int evlen = (fmt ? fmt - event : strlen(event));
jrose@100 186 if (!fmt) {
jrose@100 187 if (event[0] != '/') {
jrose@100 188 printf("<"NS_DEMO"%.*s>", evlen, event);
jrose@100 189 } else {
jrose@100 190 printf("</"NS_DEMO"%.*s>", evlen-1, event+1);
jrose@100 191 }
jrose@100 192 } else {
jrose@100 193 if (event[0] != '/') {
jrose@100 194 printf("<"NS_DEMO"%.*s", evlen, event);
jrose@100 195 printf(fmt, arg);
jrose@100 196 printf(">");
jrose@100 197 } else {
jrose@100 198 printf("<"NS_DEMO"%.*s_done", evlen-1, event+1);
jrose@100 199 printf(fmt, arg);
jrose@100 200 printf("/></"NS_DEMO"%.*s>", evlen-1, event+1);
jrose@100 201 }
jrose@100 202 }
jrose@100 203 }
jrose@100 204
jrose@100 205 if (MATCH(event, "insn")) {
jrose@100 206 const char* name = lookup(arg);
jrose@100 207 if (name) printf("%s:\n", name);
jrose@100 208
jrose@100 209 /* basic action for <insn>: */
jrose@100 210 printf(" %p\t", arg);
jrose@100 211
jrose@100 212 } else if (MATCH(event, "/insn")) {
minqi@3658 213 // follow each complete insn by a nice newline
minqi@3658 214 printf("\n");
jrose@100 215 } else if (MATCH(event, "mach")) {
jrose@100 216 printf("Decoding for CPU '%s'\n", (char*) arg);
jrose@100 217
jrose@100 218 } else if (MATCH(event, "addr")) {
jrose@100 219 /* basic action for <addr/>: */
jrose@100 220 const char* name = lookup(arg);
jrose@100 221 if (name) {
jrose@100 222 printf("&%s (%p)", name, arg);
jrose@100 223 /* return non-null to notify hsdis not to print the addr */
jrose@100 224 return arg;
jrose@100 225 }
jrose@100 226 }
jrose@100 227
jrose@100 228 /* null return is always safe; can mean "I ignored it" */
jrose@100 229 return NULL;
jrose@100 230 }
jrose@100 231
jrose@100 232 #define fprintf_callback \
jrose@100 233 (decode_instructions_printf_callback_ftype)&fprintf
jrose@100 234
minqi@3658 235 void disassemble(uintptr_t from, uintptr_t to) {
jrose@100 236 const char* err = load_decode_instructions();
jrose@100 237 if (err != NULL) {
jrose@100 238 printf("%s: %s\n", err, dlerror());
jrose@100 239 exit(1);
jrose@100 240 }
minqi@3809 241 decode_func_vtype decode_instructions_v
minqi@3809 242 = (decode_func_vtype) decode_instructions_pv;
minqi@3809 243 decode_func_stype decode_instructions_s
minqi@3809 244 = (decode_func_stype) decode_instructions_sv;
jrose@100 245 void* res;
minqi@3809 246 if (decode_instructions_pv != NULL) {
minqi@3809 247 printf("\nDecoding from %p to %p...with %s\n", from, to, DECODE_INSTRUCTIONS_VIRTUAL_NAME);
minqi@3809 248 if (raw) {
minqi@3809 249 res = (*decode_instructions_v)(from, to,
minqi@3809 250 (unsigned char*)from, to - from,
minqi@3809 251 simple_handle_event, stdout,
minqi@3809 252 NULL, stdout,
minqi@3809 253 options, 0);
minqi@3809 254 } else {
minqi@3809 255 res = (*decode_instructions_v)(from, to,
minqi@3809 256 (unsigned char*)from, to - from,
minqi@3809 257 handle_event, (void*) event_cookie,
minqi@3809 258 fprintf_callback, stdout,
minqi@3809 259 options, 0);
minqi@3809 260 }
minqi@3809 261 if (res != (void*)to)
minqi@3809 262 printf("*** Result was %p!\n", res);
jrose@100 263 }
minqi@3809 264 void* sres;
minqi@3809 265 if (decode_instructions_sv != NULL) {
minqi@3809 266 printf("\nDecoding from %p to %p...with old decode_instructions\n", from, to, DECODE_INSTRUCTIONS_NAME);
minqi@3809 267 if (raw) {
minqi@3809 268 sres = (*decode_instructions_s)(from, to,
minqi@3809 269 simple_handle_event, stdout,
minqi@3809 270 NULL, stdout,
minqi@3809 271 options);
minqi@3809 272 } else {
minqi@3809 273 sres = (*decode_instructions_s)(from, to,
minqi@3809 274 handle_event, (void*) event_cookie,
minqi@3809 275 fprintf_callback, stdout,
minqi@3809 276 options);
minqi@3809 277 }
minqi@3809 278 if (sres != (void *)to)
minqi@3809 279 printf("*** Result of decode_instructions %p!\n", sres);
minqi@3809 280 }
jrose@100 281 }