blob: 62a0007faf8988108cbc48644c905d0842aa9fd4 [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>
10#include <string.h>
11#include <assert.h>
12#include <ctype.h>
13
14static const char gHexDigit[] = "0123456789abcdef";
15
16/*
17 * Print a hex dump. Just hands control off to the fancy version.
18 */
19void printHexDump(const void* vaddr, size_t length)
20{
21 printHexDumpEx(stdout, vaddr, length, kHexDumpLocal, "");
22}
23void printHexDump2(const void* vaddr, size_t length, const char* prefix)
24{
25 printHexDumpEx(stdout, vaddr, length, kHexDumpLocal, prefix);
26}
27
28/*
29 * Print a hex dump in this format:
30 *
3101234567: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef\n
32 */
33void printHexDumpEx(FILE* fp, const void* vaddr, size_t length,
34 HexDumpMode mode, const char* prefix)
35{
36 const unsigned char* addr = vaddr;
37 char out[77]; /* exact fit */
38 unsigned int offset; /* offset to show while printing */
39 char* hex;
40 char* asc;
41 int gap;
42
43 if (mode == kHexDumpLocal)
44 offset = 0;
45 else
46 offset = (int) addr;
47
48 memset(out, ' ', sizeof(out)-1);
49 out[8] = ':';
50 out[sizeof(out)-2] = '\n';
51 out[sizeof(out)-1] = '\0';
52
53 gap = (int) offset & 0x0f;
54 while (length) {
55 unsigned int lineOffset = offset & ~0x0f;
56 int i, count;
57
58 hex = out;
59 asc = out + 59;
60
61 for (i = 0; i < 8; i++) {
62 *hex++ = gHexDigit[lineOffset >> 28];
63 lineOffset <<= 4;
64 }
65 hex++;
66 hex++;
67
68 count = ((int)length > 16-gap) ? 16-gap : (int) length; /* cap length */
69 assert(count != 0);
70 assert(count+gap <= 16);
71
72 if (gap) {
73 /* only on first line */
74 hex += gap * 3;
75 asc += gap;
76 }
77
78 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{
121 int connectPort, listenPort;
122 int cc;
123
124 if (argc < 2 || argc > 3) {
125 usage("jdwpspy");
126 return 2;
127 }
128
129 setvbuf(stdout, NULL, _IONBF, 0);
130
131 /* may want this to be host:port */
132 connectPort = atoi(argv[1]);
133
134 if (argc > 2)
135 listenPort = atoi(argv[2]);
136 else
137 listenPort = connectPort + 1;
138
139 cc = run("localhost", connectPort, listenPort);
140
141 return (cc != 0);
142}
143