src/utils/hsdis/hsdis-demo.c
author tonyp
Fri, 19 Jul 2019 12:39:31 -0400
changeset 55751 014decdb5086
parent 51271 b6e0bfe4a6ec
permissions -rw-r--r--
8227179: Test for new gc+metaspace=info output format Reviewed-by: tschatzl, kbarrett
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
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     5
 * The Universal Permissive License (UPL), Version 1.0
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     6
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     7
 * Subject to the condition set forth below, permission is hereby granted to
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     8
 * any person obtaining a copy of this software, associated documentation
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
     9
 * and/or data (collectively the "Software"), free of charge and under any
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    10
 * and all copyright rights in the Software, and any and all patent rights
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    11
 * owned or freely licensable by each licensor hereunder covering either (i)
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    12
 * the unmodified Software as contributed to or provided by such licensor,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    13
 * or (ii) the Larger Works (as defined below), to deal in both
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    14
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    15
 * (a) the Software, and
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    16
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    17
 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
51271
b6e0bfe4a6ec 8208541: non-ASCII characters in hsdis UPL text
dbuck
parents: 51202
diff changeset
    18
 * if one is included with the Software (each a "Larger Work" to which the
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    19
 * Software is contributed by such licensors),
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    20
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    21
 * without restriction, including without limitation the rights to copy,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    22
 * create derivative works of, display, perform, and distribute the Software
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    23
 * and make, use, sell, offer for sale, import, export, have made, and have
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    24
 * sold the Software and the Larger Work(s), and to sublicense the foregoing
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    25
 * rights on either these or other terms.
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    26
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    27
 * This license is subject to the following condition:
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    28
 *
51202
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    29
 * The above copyright notice and either this complete permission notice or
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    30
 * at a minimum a reference to the UPL must be included in all copies or
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    31
 * substantial portions of the Software.
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    32
 *
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    33
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    34
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    35
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    36
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    37
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    38
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
59b0d8afc831 8208183: update HSDIS plugin license to UPL
dbuck
parents: 47216
diff changeset
    39
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    40
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    41
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    42
 * 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
    43
 * questions.
347
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
 */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    46
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    47
/* 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
    48
   This demonstrates the protocol required by the HotSpot PrintAssembly option.
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    49
*/
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    50
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    51
#include <stdio.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    52
#include <stdlib.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    53
#include <string.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    54
#include <inttypes.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    55
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    56
#include "hsdis.h"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    57
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    58
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    59
void greet(const char*);
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    60
void disassemble(uintptr_t, uintptr_t);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    61
void end_of_file();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    62
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    63
const char* options = NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    64
int         raw     = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    65
int         xml     = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    66
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    67
int main(int ac, char** av) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    68
  int greeted = 0;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    69
  int i;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    70
  for (i = 1; i < ac; i++) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    71
    const char* arg = av[i];
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    72
    if (arg[0] == '-') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    73
      if (!strcmp(arg, "-xml"))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    74
        xml ^= 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    75
      else if (!strcmp(arg, "-raw"))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    76
        raw ^= 1;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    77
      else if (!strncmp(arg, "-options=", 9))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    78
        options = arg+9;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    79
      else
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
    80
        { 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
    81
      continue;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    82
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    83
    greet(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    84
    greeted = 1;
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
  if (!greeted)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    87
    greet("world");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    88
  printf("...And now for something completely different:\n");
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    89
  void *start = (void*) &main;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    90
  void *end = (void*) &end_of_file;
35075
ca79cbf3f106 8073139: PPC64: User-visible arch directory and os.arch value on ppc64le cause issues with Java tooling
asmundak
parents: 14384
diff changeset
    91
#if defined(__ia64) || (defined(__powerpc__) && !defined(ABI_ELFv2))
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    92
  /* On IA64 and PPC function pointers are pointers to function descriptors */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    93
  start = *((void**)start);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    94
  end = *((void**)end);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    95
#endif
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
    96
  disassemble(start, (end > start) ? end : start + 64);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
    97
  printf("Cheers!\n");
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
void greet(const char* whom) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   101
  printf("Hello, %s!\n", whom);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   102
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   103
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   104
void end_of_file() { }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   105
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   106
/* don't disassemble after this point... */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   107
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   108
#include "dlfcn.h"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   109
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   110
#define DECODE_INSTRUCTIONS_VIRTUAL_NAME "decode_instructions_virtual"
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   111
#define DECODE_INSTRUCTIONS_NAME "decode_instructions"
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   112
#define HSDIS_NAME               "hsdis"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   113
static void* decode_instructions_pv = 0;
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   114
static void* decode_instructions_sv = 0;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   115
static const char* hsdis_path[] = {
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   116
  HSDIS_NAME"-"LIBARCH LIB_EXT,
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   117
  "./" HSDIS_NAME"-"LIBARCH LIB_EXT,
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   118
#ifdef TARGET_DIR
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   119
  TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT,
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   120
#endif
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   121
  NULL
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   122
};
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   123
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   124
static const char* load_decode_instructions() {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   125
  void* dllib = NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   126
  const char* *next_in_path = hsdis_path;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   127
  while (1) {
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   128
    decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_VIRTUAL_NAME);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   129
    decode_instructions_sv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   130
    if (decode_instructions_pv != NULL || decode_instructions_sv != NULL)
347
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
    if (dllib != NULL)
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   133
      return "plugin does not defined "DECODE_INSTRUCTIONS_VIRTUAL_NAME" and "DECODE_INSTRUCTIONS_NAME;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   134
    for (dllib = NULL; dllib == NULL; ) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   135
      const char* next_lib = (*next_in_path++);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   136
      if (next_lib == NULL)
2564
85c3ae53ca1c 6684007: PrintAssembly plugin not available for linux or windows
never
parents: 347
diff changeset
   137
        return "cannot find plugin "HSDIS_NAME LIB_EXT;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   138
      dllib = dlopen(next_lib, RTLD_LAZY);
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
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   141
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   142
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   143
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   144
static const char* lookup(void* addr) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   145
#if defined(__ia64) || defined(__powerpc__)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   146
  /* On IA64 and PPC function pointers are pointers to function descriptors */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   147
#define CHECK_NAME(fn) \
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   148
  if (addr == *((void**) &fn))  return #fn;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   149
#else
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   150
#define CHECK_NAME(fn) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   151
  if (addr == (void*) &fn)  return #fn;
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   152
#endif
347
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
  CHECK_NAME(main);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   155
  CHECK_NAME(greet);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   156
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   157
}
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
/* 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
   160
#define MATCH(event, tag) \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   161
  (!strncmp(event, tag, sizeof(tag)-1) && \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   162
   (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1])))
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   163
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   164
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   165
static const char event_cookie[] = "event_cookie"; /* demo placeholder */
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   166
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
   167
  if (MATCH(event, "/insn")) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   168
    // follow each complete insn by a nice newline
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   169
    printf("\n");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   170
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   171
  return NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   172
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   173
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   174
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
   175
#define NS_DEMO "demo:"
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   176
  if (cookie != event_cookie)
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   177
    printf("*** bad event cookie %p != %p\n", cookie, event_cookie);
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
  if (xml) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   180
    /* We could almost do a printf(event, arg),
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   181
       but for the sake of a better demo,
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   182
       we dress the result up as valid XML.
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
    const char* fmt = strchr(event, ' ');
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   185
    int evlen = (fmt ? fmt - event : strlen(event));
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   186
    if (!fmt) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   187
      if (event[0] != '/') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   188
        printf("<"NS_DEMO"%.*s>", evlen, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   189
      } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   190
        printf("</"NS_DEMO"%.*s>", evlen-1, event+1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   191
      }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   192
    } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   193
      if (event[0] != '/') {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   194
        printf("<"NS_DEMO"%.*s", evlen, event);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   195
        printf(fmt, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   196
        printf(">");
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   197
      } else {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   198
        printf("<"NS_DEMO"%.*s_done", evlen-1, event+1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   199
        printf(fmt, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   200
        printf("/></"NS_DEMO"%.*s>", evlen-1, event+1);
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
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   204
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   205
  if (MATCH(event, "insn")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   206
    const char* name = lookup(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   207
    if (name)  printf("%s:\n", name);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   208
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   209
    /* basic action for <insn>: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   210
    printf(" %p\t", arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   211
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   212
  } else if (MATCH(event, "/insn")) {
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   213
    // follow each complete insn by a nice newline
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   214
    printf("\n");
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   215
  } else if (MATCH(event, "mach")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   216
    printf("Decoding for CPU '%s'\n", (char*) arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   217
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   218
  } else if (MATCH(event, "addr")) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   219
    /* basic action for <addr/>: */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   220
    const char* name = lookup(arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   221
    if (name) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   222
      printf("&%s (%p)", name, arg);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   223
      /* 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
   224
      return arg;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   225
    }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   226
  }
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   227
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   228
  /* null return is always safe; can mean "I ignored it" */
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   229
  return NULL;
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   230
}
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   231
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   232
#define fprintf_callback \
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   233
  (decode_instructions_printf_callback_ftype)&fprintf
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   234
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 8921
diff changeset
   235
void disassemble(uintptr_t from, uintptr_t to) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   236
  const char* err = load_decode_instructions();
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   237
  if (err != NULL) {
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   238
    printf("%s: %s\n", err, dlerror());
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   239
    exit(1);
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   240
  }
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   241
  decode_func_vtype decode_instructions_v
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   242
    = (decode_func_vtype) decode_instructions_pv;
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   243
  decode_func_stype decode_instructions_s
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   244
    = (decode_func_stype) decode_instructions_sv;
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   245
  void* res;
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   246
  if (decode_instructions_pv != NULL) {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   247
    printf("\nDecoding from %p to %p...with %s\n", from, to, DECODE_INSTRUCTIONS_VIRTUAL_NAME);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   248
    if (raw) {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   249
      res = (*decode_instructions_v)(from, to,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   250
                                     (unsigned char*)from, to - from,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   251
                                     simple_handle_event, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   252
                                     NULL, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   253
                                     options, 0);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   254
    } else {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   255
      res = (*decode_instructions_v)(from, to,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   256
                                    (unsigned char*)from, to - from,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   257
                                     handle_event, (void*) event_cookie,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   258
                                     fprintf_callback, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   259
                                     options, 0);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   260
    }
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   261
    if (res != (void*)to)
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   262
      printf("*** Result was %p!\n", res);
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   263
  }
14384
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   264
  void* sres;
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   265
  if (decode_instructions_sv != NULL) {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   266
    printf("\nDecoding from %p to %p...with old decode_instructions\n", from, to, DECODE_INSTRUCTIONS_NAME);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   267
    if (raw) {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   268
      sres = (*decode_instructions_s)(from, to,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   269
                                      simple_handle_event, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   270
                                      NULL, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   271
                                      options);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   272
    } else {
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   273
      sres = (*decode_instructions_s)(from, to,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   274
                                      handle_event, (void*) event_cookie,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   275
                                      fprintf_callback, stdout,
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   276
                                      options);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   277
    }
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   278
    if (sres != (void *)to)
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   279
      printf("*** Result of decode_instructions %p!\n", sres);
801df8025142 8000489: older builds of hsdis don't work anymore after 6879063
minqi
parents: 13873
diff changeset
   280
  }
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents:
diff changeset
   281
}