hotspot/src/share/tools/hsdis/hsdis-demo.c
author minqi
Mon, 24 Sep 2012 12:44:00 -0700
changeset 13873 7b72e3873785
parent 8921 14bfe81f2a9d
child 14384 801df8025142
permissions -rw-r--r--
6879063: SA should use hsdis for disassembly Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler. Reviewed-by: twisti, jrose, sla Contributed-by: yumin.qi@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     1
/*
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
     2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     4
 *
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     8
 *
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    13
 * accompanied this code).
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    14
 *
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    21
 * questions.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    22
 *
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    23
 */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    24
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    25
/* hsdis-demo.c -- dump a range of addresses as native instructions
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    26
   This demonstrates the protocol required by the HotSpot PrintAssembly option.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    27
*/
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    28
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    29
#include <stdio.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    30
#include <stdlib.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    31
#include <string.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    32
#include <inttypes.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    33
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    34
#include "hsdis.h"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    35
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    36
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    37
void greet(const char*);
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    38
void disassemble(uintptr_t, uintptr_t);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    39
void end_of_file();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    40
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    41
const char* options = NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    42
int         raw     = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    43
int         xml     = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    44
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    45
int main(int ac, char** av) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    46
  int greeted = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    47
  int i;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    48
  for (i = 1; i < ac; i++) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    49
    const char* arg = av[i];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    50
    if (arg[0] == '-') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    51
      if (!strcmp(arg, "-xml"))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    52
        xml ^= 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    53
      else if (!strcmp(arg, "-raw"))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    54
        raw ^= 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    55
      else if (!strncmp(arg, "-options=", 9))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    56
        options = arg+9;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    57
      else
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    58
        { printf("Usage: %s [-xml] [name...]\n", av[0]); exit(2); }
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    59
      continue;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    60
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    61
    greet(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    62
    greeted = 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    63
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    64
  if (!greeted)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    65
    greet("world");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    66
  printf("...And now for something completely different:\n");
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    67
  void *start = (void*) &main;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    68
  void *end = (void*) &end_of_file;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    69
#if defined(__ia64) || defined(__powerpc__)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    70
  /* On IA64 and PPC function pointers are pointers to function descriptors */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    71
  start = *((void**)start);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    72
  end = *((void**)end);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    73
#endif
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    74
  disassemble(start, (end > start) ? end : start + 64);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    75
  printf("Cheers!\n");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    76
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    77
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    78
void greet(const char* whom) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    79
  printf("Hello, %s!\n", whom);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    80
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    81
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    82
void end_of_file() { }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    83
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    84
/* don't disassemble after this point... */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    85
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    86
#include "dlfcn.h"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    87
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    88
#define DECODE_INSTRUCTIONS_NAME "decode_instructions_virtual"
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    89
#define HSDIS_NAME               "hsdis"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    90
static void* decode_instructions_pv = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    91
static const char* hsdis_path[] = {
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    92
  HSDIS_NAME"-"LIBARCH LIB_EXT,
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    93
  "./" HSDIS_NAME"-"LIBARCH LIB_EXT,
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    94
#ifdef TARGET_DIR
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    95
  TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT,
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    96
#endif
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    97
  NULL
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    98
};
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    99
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   100
static const char* load_decode_instructions() {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   101
  void* dllib = NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   102
  const char* *next_in_path = hsdis_path;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   103
  while (1) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   104
    decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   105
    if (decode_instructions_pv != NULL)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   106
      return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   107
    if (dllib != NULL)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   108
      return "plugin does not defined "DECODE_INSTRUCTIONS_NAME;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   109
    for (dllib = NULL; dllib == NULL; ) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   110
      const char* next_lib = (*next_in_path++);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   111
      if (next_lib == NULL)
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   112
        return "cannot find plugin "HSDIS_NAME LIB_EXT;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   113
      dllib = dlopen(next_lib, RTLD_LAZY);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   114
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   115
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   116
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   117
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   118
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   119
static const char* lookup(void* addr) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   120
#if defined(__ia64) || defined(__powerpc__)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   121
  /* On IA64 and PPC function pointers are pointers to function descriptors */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   122
#define CHECK_NAME(fn) \
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   123
  if (addr == *((void**) &fn))  return #fn;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   124
#else
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   125
#define CHECK_NAME(fn) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   126
  if (addr == (void*) &fn)  return #fn;
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   127
#endif
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   128
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   129
  CHECK_NAME(main);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   130
  CHECK_NAME(greet);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   131
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   132
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   133
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   134
/* does the event match the tag, followed by a null, space, or slash? */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   135
#define MATCH(event, tag) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   136
  (!strncmp(event, tag, sizeof(tag)-1) && \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   137
   (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1])))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   138
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   139
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   140
static const char event_cookie[] = "event_cookie"; /* demo placeholder */
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   141
static void* simple_handle_event(void* cookie, const char* event, void* arg) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   142
  if (MATCH(event, "/insn")) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   143
    // follow each complete insn by a nice newline
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   144
    printf("\n");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   145
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   146
  return NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   147
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   148
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   149
static void* handle_event(void* cookie, const char* event, void* arg) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   150
#define NS_DEMO "demo:"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   151
  if (cookie != event_cookie)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   152
    printf("*** bad event cookie %p != %p\n", cookie, event_cookie);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   153
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   154
  if (xml) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   155
    /* We could almost do a printf(event, arg),
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   156
       but for the sake of a better demo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   157
       we dress the result up as valid XML.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   158
    */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   159
    const char* fmt = strchr(event, ' ');
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   160
    int evlen = (fmt ? fmt - event : strlen(event));
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   161
    if (!fmt) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   162
      if (event[0] != '/') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   163
        printf("<"NS_DEMO"%.*s>", evlen, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   164
      } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   165
        printf("</"NS_DEMO"%.*s>", evlen-1, event+1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   166
      }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   167
    } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   168
      if (event[0] != '/') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   169
        printf("<"NS_DEMO"%.*s", evlen, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   170
        printf(fmt, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   171
        printf(">");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   172
      } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   173
        printf("<"NS_DEMO"%.*s_done", evlen-1, event+1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   174
        printf(fmt, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   175
        printf("/></"NS_DEMO"%.*s>", evlen-1, event+1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   176
      }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   177
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   178
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   179
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   180
  if (MATCH(event, "insn")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   181
    const char* name = lookup(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   182
    if (name)  printf("%s:\n", name);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   183
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   184
    /* basic action for <insn>: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   185
    printf(" %p\t", arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   186
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   187
  } else if (MATCH(event, "/insn")) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   188
    // follow each complete insn by a nice newline
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   189
    printf("\n");
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   190
  } else if (MATCH(event, "mach")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   191
    printf("Decoding for CPU '%s'\n", (char*) arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   192
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   193
  } else if (MATCH(event, "addr")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   194
    /* basic action for <addr/>: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   195
    const char* name = lookup(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   196
    if (name) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   197
      printf("&%s (%p)", name, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   198
      /* return non-null to notify hsdis not to print the addr */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   199
      return arg;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   200
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   201
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   202
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   203
  /* null return is always safe; can mean "I ignored it" */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   204
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   205
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   206
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   207
#define fprintf_callback \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   208
  (decode_instructions_printf_callback_ftype)&fprintf
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   209
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   210
void disassemble(uintptr_t from, uintptr_t to) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   211
  const char* err = load_decode_instructions();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   212
  if (err != NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   213
    printf("%s: %s\n", err, dlerror());
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   214
    exit(1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   215
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   216
  printf("Decoding from %p to %p...\n", from, to);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   217
  decode_instructions_ftype decode_instructions
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   218
    = (decode_instructions_ftype) decode_instructions_pv;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   219
  void* res;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   220
  if (raw && xml) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   221
    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   222
  } else if (raw) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   223
    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   224
  } else {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   225
    res = (*decode_instructions)(from, to, (unsigned char*)from, to - from,
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   226
                                 handle_event, (void*) event_cookie,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   227
                                 fprintf_callback, stdout,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   228
                                 options);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   229
  }
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   230
  if (res != (void*)to)
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   231
    printf("*** Result was %p!\n", res);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   232
}