src/share/tools/hsdis/hsdis-demo.c

changeset 4093
5a98bf7d847b
parent 2708
1d1603768966
child 4244
3d701c802d01
     1.1 --- a/src/share/tools/hsdis/hsdis-demo.c	Thu Sep 20 03:49:15 2012 -0700
     1.2 +++ b/src/share/tools/hsdis/hsdis-demo.c	Mon Sep 24 12:44:00 2012 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -26,14 +26,16 @@
    1.11     This demonstrates the protocol required by the HotSpot PrintAssembly option.
    1.12  */
    1.13  
    1.14 +#include <stdio.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <string.h>
    1.17 +#include <inttypes.h>
    1.18 +
    1.19  #include "hsdis.h"
    1.20  
    1.21 -#include "stdio.h"
    1.22 -#include "stdlib.h"
    1.23 -#include "string.h"
    1.24  
    1.25  void greet(const char*);
    1.26 -void disassemble(void*, void*);
    1.27 +void disassemble(uintptr_t, uintptr_t);
    1.28  void end_of_file();
    1.29  
    1.30  const char* options = NULL;
    1.31 @@ -62,7 +64,14 @@
    1.32    if (!greeted)
    1.33      greet("world");
    1.34    printf("...And now for something completely different:\n");
    1.35 -  disassemble((void*) &main, (void*) &end_of_file);
    1.36 +  void *start = (void*) &main;
    1.37 +  void *end = (void*) &end_of_file;
    1.38 +#if defined(__ia64) || defined(__powerpc__)
    1.39 +  /* On IA64 and PPC function pointers are pointers to function descriptors */
    1.40 +  start = *((void**)start);
    1.41 +  end = *((void**)end);
    1.42 +#endif
    1.43 +  disassemble(start, (end > start) ? end : start + 64);
    1.44    printf("Cheers!\n");
    1.45  }
    1.46  
    1.47 @@ -76,7 +85,7 @@
    1.48  
    1.49  #include "dlfcn.h"
    1.50  
    1.51 -#define DECODE_INSTRUCTIONS_NAME "decode_instructions"
    1.52 +#define DECODE_INSTRUCTIONS_NAME "decode_instructions_virtual"
    1.53  #define HSDIS_NAME               "hsdis"
    1.54  static void* decode_instructions_pv = 0;
    1.55  static const char* hsdis_path[] = {
    1.56 @@ -108,8 +117,14 @@
    1.57  
    1.58  
    1.59  static const char* lookup(void* addr) {
    1.60 +#if defined(__ia64) || defined(__powerpc__)
    1.61 +  /* On IA64 and PPC function pointers are pointers to function descriptors */
    1.62 +#define CHECK_NAME(fn) \
    1.63 +  if (addr == *((void**) &fn))  return #fn;
    1.64 +#else
    1.65  #define CHECK_NAME(fn) \
    1.66    if (addr == (void*) &fn)  return #fn;
    1.67 +#endif
    1.68  
    1.69    CHECK_NAME(main);
    1.70    CHECK_NAME(greet);
    1.71 @@ -123,6 +138,14 @@
    1.72  
    1.73  
    1.74  static const char event_cookie[] = "event_cookie"; /* demo placeholder */
    1.75 +static void* simple_handle_event(void* cookie, const char* event, void* arg) {
    1.76 +  if (MATCH(event, "/insn")) {
    1.77 +    // follow each complete insn by a nice newline
    1.78 +    printf("\n");
    1.79 +  }
    1.80 +  return NULL;
    1.81 +}
    1.82 +
    1.83  static void* handle_event(void* cookie, const char* event, void* arg) {
    1.84  #define NS_DEMO "demo:"
    1.85    if (cookie != event_cookie)
    1.86 @@ -162,10 +185,8 @@
    1.87      printf(" %p\t", arg);
    1.88  
    1.89    } else if (MATCH(event, "/insn")) {
    1.90 -    /* basic action for </insn>:
    1.91 -       (none, plugin puts the newline for us
    1.92 -    */
    1.93 -
    1.94 +    // follow each complete insn by a nice newline
    1.95 +    printf("\n");
    1.96    } else if (MATCH(event, "mach")) {
    1.97      printf("Decoding for CPU '%s'\n", (char*) arg);
    1.98  
    1.99 @@ -186,7 +207,7 @@
   1.100  #define fprintf_callback \
   1.101    (decode_instructions_printf_callback_ftype)&fprintf
   1.102  
   1.103 -void disassemble(void* from, void* to) {
   1.104 +void disassemble(uintptr_t from, uintptr_t to) {
   1.105    const char* err = load_decode_instructions();
   1.106    if (err != NULL) {
   1.107      printf("%s: %s\n", err, dlerror());
   1.108 @@ -197,15 +218,15 @@
   1.109      = (decode_instructions_ftype) decode_instructions_pv;
   1.110    void* res;
   1.111    if (raw && xml) {
   1.112 -    res = (*decode_instructions)(from, to, NULL, stdout, NULL, stdout, options);
   1.113 +    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
   1.114    } else if (raw) {
   1.115 -    res = (*decode_instructions)(from, to, NULL, NULL, NULL, stdout, options);
   1.116 +    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
   1.117    } else {
   1.118 -    res = (*decode_instructions)(from, to,
   1.119 +    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from,
   1.120                                   handle_event, (void*) event_cookie,
   1.121                                   fprintf_callback, stdout,
   1.122                                   options);
   1.123    }
   1.124 -  if (res != to)
   1.125 +  if (res != (void*)to)
   1.126      printf("*** Result was %p!\n", res);
   1.127  }

mercurial