blob: b53f99b8cd232b3c097b8d86167a0f1dcd786fe9 [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * JDWP spy.
5 */
6#define _JDWP_MISC_INLINE
7#include "Common.h"
8#include <stdlib.h>
9#include <stdio.h>
SangWook Hanac8ed5e2012-07-15 22:51:02 +090010#include <stdint.h>
The Android Open Source Project52d4c302009-03-03 19:29:09 -080011#include <string.h>
12#include <assert.h>
13#include <ctype.h>
14
15static const char gHexDigit[] = "0123456789abcdef";
16
17/*
18 * Print a hex dump. Just hands control off to the fancy version.
19 */
20void printHexDump(const void* vaddr, size_t length)
21{
22 printHexDumpEx(stdout, vaddr, length, kHexDumpLocal, "");
23}
24void printHexDump2(const void* vaddr, size_t length, const char* prefix)
25{
26 printHexDumpEx(stdout, vaddr, length, kHexDumpLocal, prefix);
27}
28
29/*
30 * Print a hex dump in this format:
31 *
3201234567: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef\n
33 */
34void printHexDumpEx(FILE* fp, const void* vaddr, size_t length,
35 HexDumpMode mode, const char* prefix)
36{
Brian Carlstromac8836d2011-05-05 00:19:46 -070037 const unsigned char* addr = reinterpret_cast<const unsigned char*>(vaddr);
The Android Open Source Project52d4c302009-03-03 19:29:09 -080038 char out[77]; /* exact fit */
SangWook Hanac8ed5e2012-07-15 22:51:02 +090039 uintptr_t offset; /* offset to show while printing */
The Android Open Source Project52d4c302009-03-03 19:29:09 -080040 char* hex;
41 char* asc;
42 int gap;
43
44 if (mode == kHexDumpLocal)
45 offset = 0;
46 else
SangWook Hanac8ed5e2012-07-15 22:51:02 +090047 offset = (uintptr_t) addr;
The Android Open Source Project52d4c302009-03-03 19:29:09 -080048
49 memset(out, ' ', sizeof(out)-1);
50 out[8] = ':';
51 out[sizeof(out)-2] = '\n';
52 out[sizeof(out)-1] = '\0';
53
54 gap = (int) offset & 0x0f;
55 while (length) {
56 unsigned int lineOffset = offset & ~0x0f;
Brian Carlstromac8836d2011-05-05 00:19:46 -070057 char* hex = out;
58 char* asc = out + 59;
The Android Open Source Project52d4c302009-03-03 19:29:09 -080059
Brian Carlstromac8836d2011-05-05 00:19:46 -070060 for (int i = 0; i < 8; i++) {
The Android Open Source Project52d4c302009-03-03 19:29:09 -080061 *hex++ = gHexDigit[lineOffset >> 28];
62 lineOffset <<= 4;
63 }
64 hex++;
65 hex++;
66
Brian Carlstromac8836d2011-05-05 00:19:46 -070067 int count = ((int)length > 16-gap) ? 16-gap : (int) length; /* cap length */
The Android Open Source Project52d4c302009-03-03 19:29:09 -080068 assert(count != 0);
69 assert(count+gap <= 16);
70
71 if (gap) {
72 /* only on first line */
73 hex += gap * 3;
74 asc += gap;
75 }
76
Brian Carlstromac8836d2011-05-05 00:19:46 -070077 int i;
The Android Open Source Project52d4c302009-03-03 19:29:09 -080078 for (i = gap ; i < count+gap; i++) {
79 *hex++ = gHexDigit[*addr >> 4];
80 *hex++ = gHexDigit[*addr & 0x0f];
81 hex++;
82 if (isprint(*addr))
83 *asc++ = *addr;
84 else
85 *asc++ = '.';
86 addr++;
87 }
88 for ( ; i < 16; i++) {
89 /* erase extra stuff; only happens on last line */
90 *hex++ = ' ';
91 *hex++ = ' ';
92 hex++;
93 *asc++ = ' ';
94 }
95
96 fprintf(fp, "%s%s", prefix, out);
97
98 gap = 0;
99 length -= count;
100 offset += count;
101 }
102}
103
104
105/*
106 * Explain it.
107 */
108static void usage(const char* progName)
109{
110 fprintf(stderr, "Usage: %s VM-port [debugger-listen-port]\n\n", progName);
111 fprintf(stderr,
112"When a debugger connects to the debugger-listen-port, jdwpspy will connect\n");
113 fprintf(stderr, "to the VM on the VM-port.\n");
114}
115
116/*
117 * Parse args.
118 */
119int main(int argc, char* argv[])
120{
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800121 if (argc < 2 || argc > 3) {
122 usage("jdwpspy");
123 return 2;
124 }
125
126 setvbuf(stdout, NULL, _IONBF, 0);
127
128 /* may want this to be host:port */
Brian Carlstromac8836d2011-05-05 00:19:46 -0700129 int connectPort = atoi(argv[1]);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800130
Brian Carlstromac8836d2011-05-05 00:19:46 -0700131 int listenPort;
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800132 if (argc > 2)
133 listenPort = atoi(argv[2]);
134 else
135 listenPort = connectPort + 1;
136
Brian Carlstromac8836d2011-05-05 00:19:46 -0700137 int cc = run("localhost", connectPort, listenPort);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800138
139 return (cc != 0);
140}