forked from mchalupa/wldbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.c
281 lines (237 loc) · 6.68 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2014 Marek Chalupa
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "wldbg.h"
#include "wldbg-pass.h"
#include "wldbg-parse-message.h"
enum options {
SEPARATE = 1 ,
DECIMAL = 1 << 1,
DECODE = 1 << 2,
TOFILE = 1 << 3,
RAW = 1 << 4,
CLIENTONLY = 1 << 5,
SERVERONLY = 1 << 6,
STATS = 1 << 7,
NOOUT = 1 << 8,
HUMAN = 1 << 9,
};
struct dump {
uint64_t options;
const char *file;
int file_fd;
struct {
uint64_t in_msg;
uint64_t out_msg;
uint64_t in_bytes;
uint64_t out_bytes;
} stats;
};
static void
dump_to_file(struct wldbg_message *message, struct dump *dump)
{
if (write(dump->file_fd, message->data, message->size)
!= (ssize_t) message->size) {
perror("Dumping to file failed");
}
}
static void
dump_message(struct wldbg_message *message, struct dump *dump)
{
uint32_t i;
uint32_t *data = message->data;
uint32_t options = dump->options;
size_t size = 0;
if (options & TOFILE) {
dump_to_file(message, dump);
/* if user want only dump to file, do not print anything */
if (!(options & (~TOFILE)))
return;
}
if (options & NOOUT)
return;
if (options & DECODE)
options |= SEPARATE;
if (options & HUMAN)
wldbg_message_print(message);
if (!(options & RAW))
return;
printf("%s: ", message->from == CLIENT ? "CLIENT" : "SERVER");
for (i = 0; i < message->size / sizeof(uint32_t) ; ++i) {
if (options & SEPARATE) {
if (size == 0 && message->size > i + 1) {
size = data[i + 1] >> 16;
if (options & DECODE) {
printf("\n id: %u opcode: %u size: %lu:\n\t",
data[i], data[i + 1] & 0xffff,
size);
} else {
printf("\n | %2lu | ", size);
}
}
size -= sizeof(uint32_t);
}
if (options & DECIMAL)
printf("%d ", data[i]);
else
printf("%08x ", data[i]);
}
putchar('\n');
}
static int
dump_in(void *user_data, struct wldbg_message *message)
{
struct dump *dump = user_data;
if (dump->options & STATS) {
++dump->stats.in_msg;
dump->stats.in_bytes += message->size;
}
if (dump->options & CLIENTONLY && !(dump->options & SERVERONLY))
return PASS_NEXT;
dump_message(message, dump);
return PASS_NEXT;
}
static int
dump_out(void *user_data, struct wldbg_message *message)
{
struct dump *dump = user_data;
if (dump->options & STATS) {
++dump->stats.out_msg;
dump->stats.out_bytes += message->size;
}
if (dump->options & SERVERONLY && !(dump->options & CLIENTONLY))
return PASS_NEXT;
dump_message(message, dump);
return PASS_NEXT;
}
static void
print_help(void *user_data)
{
(void) user_data;
printf(" --- Dump data going via wire --- \n"
"\n"
"Usage: wldbg dump arg1 arg2 ...\n"
"\n"
"Available arguments:\n"
/* XXX what should raw mean anyway? */
" raw -- not implemented\n"
" human -- print human readable output\n"
" decode -- decode the header of message\n"
" decimal -- print numbers in decimal format\n"
" client -- dump only messages from client\n"
" server -- dump only messages from server\n"
" stats --\n"
" statistics -- gather and print statistics on exit\n"
" no-output -- do not print anything (except stats at exit)\n"
" help -- print this help\n"
" to-file -- dump raw data into file\n");
}
static int
dump_init(struct wldbg *wldbg, struct wldbg_pass *pass, int argc, const char *argv[])
{
int i;
uint64_t flags = 0;
struct dump *dump = malloc(sizeof *dump);
if (!dump)
return -1;
(void) wldbg;
for (i = 1; i < argc; ++i) {
if (strcmp(argv[i], "raw") == 0)
flags |= RAW;
else if (strcmp(argv[i], "decode") == 0)
flags |= DECODE;
else if (strcmp(argv[i], "human") == 0)
flags |= HUMAN;
else if (strcmp(argv[i], "separate") == 0)
flags |= SEPARATE;
else if (strcmp(argv[i], "decimal") == 0)
flags |= DECIMAL;
else if (strcmp(argv[i], "client") == 0)
flags |= CLIENTONLY;
else if (strcmp(argv[i], "server") == 0)
flags |= SERVERONLY;
else if (strcmp(argv[i], "stats") == 0)
flags |= STATS;
else if (strcmp(argv[i], "statistics") == 0)
flags |= STATS;
else if (strcmp(argv[i], "no-output") == 0)
flags |= NOOUT;
else if (strcmp(argv[i], "help") == 0) {
print_help(NULL);
/* let wldbg exit after loading the pass */
wldbg_exit(wldbg);
} else if (strcmp(argv[i], "to-file") == 0) {
flags |= TOFILE;
dump->file = argv[i + 1];
}
}
/* if user did not explicitly requests
* humand readable output AND raw output, assume
* that she/he wants only raw or only human output */
if (!(flags & HUMAN))
flags |= RAW;
if (flags & TOFILE) {
dump->file_fd = open(dump->file, O_WRONLY | O_CREAT | O_EXCL, 0755);
if (dump->file_fd == -1) {
perror("Opening file for dumping");
free(dump);
return -1;
}
}
dump->options = flags;
pass->user_data = dump;
return 0;
}
static void
dump_destroy(void *data)
{
struct dump *dump = data;
fflush(stdout);
if (dump->options & STATS) {
printf("----------------------\n"
"Messages from server: %lu (%lu bytes)\n"
"Messages from client: %lu (%lu bytes)\n"
"----------------------\n",
dump->stats.in_msg, dump->stats.in_bytes,
dump->stats.out_msg, dump->stats.out_bytes);
}
if (dump->options & TOFILE) {
close(dump->file_fd);
}
free(dump);
}
struct wldbg_pass wldbg_pass = {
.init = dump_init,
.destroy = dump_destroy,
.server_pass = dump_in,
.client_pass = dump_out,
.help = print_help,
.description = "Dump data going through the wire",
0
};