This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
dbg.c
318 lines (282 loc) · 7.48 KB
/
dbg.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#define EVADE_STDIO_BAN 1
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/file.h>
#include <ctype.h>
#include "dbg.h"
#include "util.h"
#include "ringbuf.h"
#include "proto.h"
#include "channel.h"
#include "constants.h"
#include "fs.h"
#ifndef NDEBUG
FILE* dbgout = NULL;
static int dbglock_fd = -1;
static int dbglock_level = 0;
void
dbg_init(void)
{
const char* dv = getenv("FB_ADB_DEBUG");
if (dv == NULL) {
/* Noop */
} else if (strcmp(dv, "1") == 0) {
dbgout = fdopen(fileno(stderr), "w");
} else if (dv[0] == '>' && dv[1] == '>') {
dbgout = fopen(dv+2, "a");
if (dbgout != NULL)
dbglock_fd = fileno(dbgout);
} else if (dv[0] == '>') {
dbgout = fopen(dv+1, "w");
if (dbgout != NULL)
dbglock_fd = fileno(dbgout);
}
}
static void
dbg_internal(const char* fmt, va_list args)
{
int saved_errno = errno;
SCOPED_RESLIST(rl_dbg);
dbglock();
fprintf(dbgout, "%s(%4d): ", prgname, getpid());
vfprintf(dbgout, fmt, args);
putc('\n', dbgout);
fflush(dbgout);
errno = saved_errno;
}
void
dbg_1v(const char* fmt, va_list args)
{
if (dbg_enabled_p())
dbg_internal(fmt, args);
}
void
dbg_1(const char* fmt, ...)
{
if (dbg_enabled_p()) {
va_list args;
va_start(args, fmt);
dbg_internal(fmt, args);
va_end(args);
}
}
static void
cleanup_dbginit(void* arg)
{
unlink((const char*) arg);
}
void
dbglock_init(void)
{
if (dbglock_fd != -1)
return;
if (!dbg_enabled_p())
return;
const char envvar[] = "FB_ADB_DBGLOCK_NAME";
/* No, we can't just inherit the file descriptor. Without a
* separate file open, taking the lock won't block. */
const char* fn = getenv(envvar);
if (fn == NULL) {
const char* pfx = DEFAULT_TEMP_DIR;
char* tmpfname = xaprintf("%s/fb-adb-dbg-XXXXXX", pfx);
struct cleanup* cl = cleanup_allocate();
int tmpfd = mkostemp(tmpfname, O_CLOEXEC);
if (tmpfd != -1) {
setenv(envvar, tmpfname, 1);
cleanup_commit(cl, cleanup_dbginit, tmpfname);
dbglock_fd = tmpfd;
}
return;
}
dbglock_fd = open(fn, O_CLOEXEC | O_RDWR);
}
static void
cleanup_dbglock(void* ignored)
{
if (--dbglock_level == 0)
flock(dbglock_fd, LOCK_UN);
}
void
dbglock(void)
{
int saved_errno = errno;
if (!dbg_enabled_p())
return;
if (dbglock_fd == -1)
return;
if (dbglock_level++ == 0) {
WITH_IO_SIGNALS_ALLOWED();
flock(dbglock_fd, LOCK_EX);
}
cleanup_commit(cleanup_allocate(), cleanup_dbglock, 0);
errno = saved_errno;
}
void
iovec_dbg(const struct iovec* iov, unsigned nio)
{
for (unsigned i = 0; i < nio; ++i) {
char* b = (char*)iov[i].iov_base;
size_t blen = iov[i].iov_len;
for (unsigned j = 0; j < blen; ++j) {
dbg(" iov[%u][%u] c 0x%02x %c",
i, j, b[j], isprint(b[j]) ? b[j] : '.');
}
}
}
void
ringbuf_dbg(const struct ringbuf* rb)
{
struct iovec iov[2];
ringbuf_readable_iov(rb, iov, ringbuf_size(rb));
iovec_dbg(iov, ARRAYSIZE(iov));
}
static const char*
msgtoname(const struct msg* msg)
{
#define M(_name) \
if (msg->type == _name) { \
return #_name; \
}
ENUM_MSG_TYPES(M);
#undef M
return "MSG_???";
}
void
dbgmsg(const struct msg* msg, const char* tag)
{
switch (msg->type) {
case MSG_CHANNEL_DATA: {
struct msg_channel_data* m = (void*) msg;
dbg("%s %s ch=%s sz=%u payloadsz=%zu",
tag, msgtoname(msg),
chname(m->channel), m->msg.size, m->msg.size - sizeof (*m));
break;
}
case MSG_CHANNEL_DATA_LZ4: {
struct msg_channel_data_lz4* m = (void*) msg;
dbg(("%s %s ch=%s sz=%u "
"payloadsz=%zu uncomp=%u"),
tag, msgtoname(msg),
chname(m->channel), m->msg.size, m->msg.size - sizeof (*m),
(unsigned) m->uncompressed_size);
break;
}
case MSG_CHANNEL_WINDOW: {
struct msg_channel_window* m = (void*) msg;
dbg("%s %s ch=%s d=%u",
tag, msgtoname(msg),
chname(m->channel), m->window_delta);
break;
}
case MSG_CHANNEL_CLOSE: {
dbg("%s %s ch=%s",
tag, msgtoname(msg),
chname(((struct msg_channel_close*)msg)->channel));
break;
}
case MSG_WINDOW_SIZE: {
struct msg_window_size* ws = (void*) msg;
dbg("%s %s row=%u col=%u xpixel=%u ypixel=%u",
tag, msgtoname(msg),
ws->ws.row, ws->ws.col, ws->ws.xpixel, ws->ws.ypixel);
break;
}
case MSG_CHILD_EXIT: {
struct msg_child_exit* m = (void*) msg;
dbg("%s %s status=%u", tag, msgtoname(msg), m->exit_status);
break;
}
case MSG_CHDIR: {
struct msg_chdir* m = (void*) msg;
dbg("%s %s dir=%.*s",
tag, msgtoname(msg),
(int) (m->msg.size - sizeof (*m)),
m->dir);
break;
}
default: {
dbg("%s %s type=%d sz=%d",
tag,
msgtoname(msg),
msg->type, msg->size);
break;
}
}
}
const char*
chname(int chno)
{
static const char* chnames[] = {
"FROM_PEER",
"TO_PEER",
"CHILD_STDIN",
"CHILD_STDOUT",
"CHILD_STDERR"
};
if (chno < ARRAYSIZE(chnames))
return chnames[chno];
return "?!?!?";
}
void
dbgch(const char* label, struct channel** ch, unsigned nrch)
{
SCOPED_RESLIST(rl_dbgch);
unsigned chno;
dbglock();
dbg("DBGCH[%s]", label);
for (chno = 0; chno < nrch; ++chno) {
struct channel* c = ch[chno];
struct pollfd p = channel_request_poll(ch[chno]);
const char* pev;
switch (p.events) {
case POLLIN | POLLOUT:
pev = "POLLIN,POLLOUT";
break;
case POLLIN:
pev = "POLLIN";
break;
case POLLOUT:
pev = "POLLOUT";
break;
case 0:
pev = "NONE";
break;
default:
pev = xaprintf("%xd", p.events);
break;
}
assert(p.fd == -1 || p.fd == c->fdh->fd);
dbg(" %-18s size:%-4zu room:%-4zu window:%-4d %s%-2s %p %s%s",
xaprintf("ch[%d=%s]", chno, chname(chno)),
ringbuf_size(c->rb),
ringbuf_room(c->rb),
c->window,
(c->dir == CHANNEL_FROM_FD ? "<" : ">"),
((c->fdh != NULL)
? xaprintf("%d", c->fdh->fd)
: (c->sent_eof ? "!!" : "!?")),
c,
pev,
c->adb_hack_state ? " *" : "");
}
}
#endif