forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr-show.c
574 lines (468 loc) · 12.2 KB
/
cr-show.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "asm/types.h"
#include "list.h"
#include "imgset.h"
#include "namespaces.h"
#include "compiler.h"
#include "cr_options.h"
#include "util.h"
#include "sockets.h"
#include "image.h"
#include "uts_ns.h"
#include "ipc_ns.h"
#include "pstree.h"
#include "cr-show.h"
#include "crtools.h"
#include "protobuf.h"
#include "protobuf/pstree.pb-c.h"
#include "protobuf/pipe-data.pb-c.h"
#include "protobuf/siginfo.pb-c.h"
#define DEF_PAGES_PER_LINE 6
static LIST_HEAD(pstree_list);
static void pipe_data_handler(struct cr_img *img, void *obj)
{
PipeDataEntry *e = obj;
print_image_data(img, e->bytes, opts.show_pages_content);
}
static int nice_width_for(unsigned long addr)
{
int ret = 3;
while (addr) {
addr >>= 4;
ret++;
}
return ret;
}
static inline void pr_xdigi(unsigned char *data, size_t len, int pos)
{
if (pos < len)
pr_msg("%02x ", data[pos]);
else
pr_msg(" ");
}
static inline void pr_xsym(unsigned char *data, size_t len, int pos)
{
char sym;
if (pos < len)
sym = data[pos];
else
sym = ' ';
pr_msg("%c", isprint(sym) ? sym : '.');
}
void print_data(unsigned long addr, unsigned char *data, size_t size)
{
int i, j, addr_len;
unsigned zero_line = 0;
addr_len = nice_width_for(addr + size);
for (i = 0; i < size; i += 16) {
if (*(u64 *)(data + i) == 0 && *(u64 *)(data + i + 8) == 0) {
if (zero_line == 0)
zero_line = 1;
else {
if (zero_line == 1) {
pr_msg("*\n");
zero_line = 2;
}
continue;
}
} else
zero_line = 0;
pr_msg("%#0*lx: ", addr_len, addr + i);
for (j = 0; j < 8; j++)
pr_xdigi(data, size, i + j);
pr_msg(" ");
for (j = 8; j < 16; j++)
pr_xdigi(data, size, i + j);
pr_msg(" |");
for (j = 0; j < 8; j++)
pr_xsym(data, size, i + j);
pr_msg(" ");
for (j = 8; j < 16; j++)
pr_xsym(data, size, i + j);
pr_msg("|\n");
}
}
void print_image_data(struct cr_img *img, unsigned int length, int show)
{
void *data;
int ret;
if (!show) {
lseek(img_raw_fd(img), length, SEEK_CUR);
return;
}
pr_msg("\n");
data = xmalloc(length);
if (!data)
return;
ret = read_img_buf(img, (unsigned char *)data, length);
if (ret < 0) {
xfree(data);
return;
}
print_data(0, (unsigned char *)data, length);
xfree(data);
}
static void show_pagemaps(struct cr_img *img, void *obj)
{
pb_show_plain_pretty(img, PB_PAGEMAP, "nr_pages:%u");
}
void show_siginfo(struct cr_img *img)
{
int ret;
pr_img_head(CR_FD_SIGNAL);
while (1) {
SiginfoEntry *sie;
siginfo_t *info;
ret = pb_read_one_eof(img, &sie, PB_SIGINFO);
if (ret <= 0)
break;
info = (siginfo_t *) sie->siginfo.data;
pr_msg("signal: si_signo=%d si_code=%x\n",
info->si_signo, info->si_code);
siginfo_entry__free_unpacked(sie, NULL);
}
pr_img_tail(CR_FD_SIGNAL);
}
static int pstree_item_from_pb(PstreeEntry *e, struct pstree_item *item)
{
int i;
item->pid.virt = e->pid;
item->nr_threads = e->n_threads;
item->threads = xzalloc(sizeof(struct pid) * e->n_threads);
if (!item->threads)
return -1;
for (i = 0; i < item->nr_threads; i++)
item->threads[i].virt = e->threads[i];
return 0;
}
static void pstree_handler(struct cr_img *img, void *obj)
{
PstreeEntry *e = obj;
struct pstree_item *item = NULL;
item = xzalloc(sizeof(struct pstree_item));
if (!item)
return;
if (pstree_item_from_pb(e, item)) {
xfree(item);
return;
}
list_add_tail(&item->sibling, &pstree_list);
}
static void show_collect_pstree(struct cr_img *img, int collect)
{
pb_show_plain_payload_pretty(img, PB_PSTREE,
collect ? pstree_handler : NULL, "*:%d");
}
static inline char *task_state_str(int state)
{
switch (state) {
case TASK_ALIVE:
return "running/sleeping";
case TASK_DEAD:
return "zombie";
default:
return "UNKNOWN";
}
}
static void show_core_regs(UserX86RegsEntry *regs)
{
#define pr_regs4(s, n1, n2, n3, n4) \
pr_msg("\t%8s: 0x%-16"PRIx64" " \
"%8s: 0x%-16"PRIx64" " \
"%8s: 0x%-16"PRIx64" " \
"%8s: 0x%-16"PRIx64"\n", \
#n1, s->n1, \
#n2, s->n2, \
#n3, s->n3, \
#n4, s->n4)
#define pr_regs3(s, n1, n2, n3) \
pr_msg("\t%8s: 0x%-16"PRIx64" " \
"%8s: 0x%-16"PRIx64" " \
"%8s: 0x%-16"PRIx64"\n", \
#n1, s->n1, \
#n2, s->n2, \
#n3, s->n3)
pr_msg("\t---[ GP registers set ]---\n");
pr_regs4(regs, cs, ip, ds, es);
pr_regs4(regs, ss, sp, fs, gs);
pr_regs4(regs, di, si, dx, cx);
pr_regs4(regs, ax, r8, r9, r10);
pr_regs4(regs, r11, r12, r13, r14);
pr_regs3(regs, r15, bp, bx);
pr_regs4(regs, orig_ax, flags, fs_base, gs_base);
pr_msg("\n");
}
void show_thread_info(ThreadInfoX86 *thread_info)
{
if (!thread_info)
return;
pr_msg("\t---[ Thread info ]---\n");
pr_msg("\tclear_tid_addr: 0x%"PRIx64"\n", thread_info->clear_tid_addr);
pr_msg("\n");
show_core_regs(thread_info->gpregs);
}
static struct {
u32 magic;
u32 mask;
char *hint;
} magic_hints[] = {
{ .magic = 0x45311224, .mask = 0xffffffff, .hint = "ip route dump", },
{ .magic = 0x47361222, .mask = 0xffffffff, .hint = "ip ifaddr dump", },
{ .magic = 0x00008b1f, .mask = 0x0000ffff, .hint = "gzip file", },
{ },
};
static void try_hint_magic(u32 magic)
{
int i;
for (i = 0; magic_hints[i].hint != 0; i++)
if ((magic & magic_hints[i].mask) == magic_hints[i].magic)
pr_msg("This can be %s\n", magic_hints[i].hint);
}
#define SHOW_PLAIN(name) { name##_MAGIC, PB_##name, false, NULL, NULL, }
/* nothing special behind this -S, just to avoid heavy patching */
#define SHOW_PLAINS(name) { name##S_MAGIC, PB_##name, false, NULL, NULL, }
#define SHOW_VERT(name) { name##_MAGIC, PB_##name, true, NULL, NULL, }
static struct show_image_info show_infos[] = {
SHOW_VERT(INVENTORY),
SHOW_VERT(CORE),
SHOW_VERT(IDS),
SHOW_VERT(CREDS),
SHOW_VERT(UTSNS),
SHOW_VERT(IPC_VAR),
SHOW_VERT(FS),
SHOW_VERT(GHOST_FILE),
SHOW_VERT(MM),
SHOW_VERT(CGROUP),
SHOW_PLAINS(REG_FILE),
SHOW_PLAINS(NS_FILE),
SHOW_PLAIN(EVENTFD_FILE),
SHOW_PLAIN(EVENTPOLL_FILE),
SHOW_PLAIN(EVENTPOLL_TFD),
SHOW_PLAIN(SIGNALFD),
SHOW_PLAIN(TIMERFD),
SHOW_PLAIN(INOTIFY_FILE),
SHOW_PLAIN(INOTIFY_WD),
SHOW_PLAIN(FANOTIFY_FILE),
SHOW_PLAIN(FANOTIFY_MARK),
SHOW_PLAINS(VMA),
SHOW_PLAINS(PIPE),
SHOW_PLAIN(FIFO),
SHOW_PLAIN(SIGACT),
SHOW_PLAIN(NETLINK_SK),
SHOW_PLAIN(REMAP_FPATH),
SHOW_PLAINS(MNT),
SHOW_PLAINS(TTY_FILE),
SHOW_PLAIN(TTY_INFO),
SHOW_PLAIN(RLIMIT),
SHOW_PLAIN(TUNFILE),
SHOW_PLAINS(EXT_FILE),
SHOW_PLAIN(IRMAP_CACHE),
SHOW_PLAIN(CPUINFO),
SHOW_PLAIN(USERNS),
SHOW_PLAIN(NETNS),
{ FILE_LOCKS_MAGIC, PB_FILE_LOCK, false, NULL, "3:%u", },
{ TCP_STREAM_MAGIC, PB_TCP_STREAM, true, show_tcp_stream, "1:%u 2:%u 3:%u 4:%u 12:%u", },
{ STATS_MAGIC, PB_STATS, true, NULL, "1.1:%u 1.2:%u 1.3:%u 1.4:%u 1.5:%Lu 1.6:%Lu 1.7:%Lu 1.8:%u", },
{ FDINFO_MAGIC, PB_FDINFO, false, NULL, "flags:%#o fd:%d", },
{ UNIXSK_MAGIC, PB_UNIX_SK, false, NULL, "1:%#x 2:%#x 3:%d 4:%d 5:%d 6:%d 7:%d 8:%#x 11:S", },
{ INETSK_MAGIC, PB_INET_SK, false, NULL, "1:%#x 2:%#x 3:%d 4:%d 5:%d 6:%d 7:%d 8:%d 9:%2x 11:A 12:A", },
{ PACKETSK_MAGIC, PB_PACKET_SOCK, false, NULL, "5:%d", },
{ ITIMERS_MAGIC, PB_ITIMER, false, NULL, "*:%Lu", },
{ POSIX_TIMERS_MAGIC, PB_POSIX_TIMER, false, NULL, "*:%d 5:%Lu 7:%Lu 8:%lu 9:%Lu 10:%Lu", },
{ NETDEV_MAGIC, PB_NETDEV, false, NULL, "2:%d", },
{ PAGEMAP_MAGIC, PB_PAGEMAP_HEAD, true, show_pagemaps, NULL, },
{ PIPES_DATA_MAGIC, PB_PIPE_DATA, false, pipe_data_handler, NULL, },
{ FIFO_DATA_MAGIC, PB_PIPE_DATA, false, pipe_data_handler, NULL, },
{ SK_QUEUES_MAGIC, PB_SK_QUEUES, false, sk_queue_data_handler, NULL, },
{ IPCNS_SHM_MAGIC, PB_IPC_SHM, false, ipc_shm_handler, NULL, },
{ IPCNS_SEM_MAGIC, PB_IPC_SEM, false, ipc_sem_handler, NULL, },
{ IPCNS_MSG_MAGIC, PB_IPCNS_MSG_ENT, false, ipc_msg_handler, NULL, },
{ }
};
static int cr_parse_file(void)
{
u32 magic;
int ret = -1, fd;
struct cr_img *img = NULL;
fd = open(opts.show_dump_file, O_RDONLY);
if (fd < 0) {
pr_perror("Can't open %s", opts.show_dump_file);
goto out;
}
img = img_from_fd(fd);
if (!img)
goto out;
if (read_img(img, &magic) < 0)
goto out;
ret = cr_parse_fd(img, magic);
out:
if (img)
close_image(img);
else
close_safe(&fd);
return ret;
}
int cr_parse_fd(struct cr_img *img, u32 magic)
{
int ret = 0, i;
if (magic == IMG_COMMON_MAGIC || magic == IMG_SERVICE_MAGIC) {
if (read_img(img, &magic) < 0)
goto out;
}
if (magic == PSTREE_MAGIC) {
show_collect_pstree(img, 0);
goto out;
}
if (magic == SIGNAL_MAGIC || magic == PSIGNAL_MAGIC) {
show_siginfo(img);
goto out;
}
for (i = 0; show_infos[i].magic; i++) {
struct show_image_info *si;
si = &show_infos[i];
if (si->magic != magic)
continue;
do_pb_show_plain(img, si->pb_type, si->single,
si->payload, si->fmt);
goto out;
}
ret = -1;
pr_err("Unknown magic %#x in %s\n",
magic, opts.show_dump_file);
try_hint_magic(magic);
out:
return ret;
}
static int cr_show_pstree_item(struct pstree_item *item)
{
int ret = -1, i;
struct cr_img *img;
struct cr_imgset *cr_imgset = NULL;
TaskKobjIdsEntry *ids;
cr_imgset = cr_task_imgset_open(item->pid.virt, O_SHOW);
if (!cr_imgset)
goto out;
pr_msg("Task %d:\n", item->pid.virt);
pr_msg("----------------------------------------\n");
cr_parse_fd(img_from_set(cr_imgset, CR_FD_CORE), CORE_MAGIC);
if (item->nr_threads > 1) {
for (i = 0; i < item->nr_threads; i++) {
if (item->threads[i].virt == item->pid.virt)
continue;
img = open_image(CR_FD_CORE, O_SHOW, item->threads[i].virt);
if (!img)
goto outc;
pr_msg("Thread %d.%d:\n", item->pid.virt, item->threads[i].virt);
pr_msg("----------------------------------------\n");
cr_parse_fd(img, CORE_MAGIC);
close_image(img);
}
}
pr_msg("Resources for %d:\n", item->pid.virt);
pr_msg("----------------------------------------\n");
for (i = _CR_FD_TASK_FROM + 1; i < _CR_FD_TASK_TO; i++)
if ((i != CR_FD_CORE) && (i != CR_FD_IDS)) {
pr_msg("* ");
pr_msg(imgset_template[i].fmt, item->pid.virt);
pr_msg(":\n");
cr_parse_fd(img_from_set(cr_imgset, i), imgset_template[i].magic);
}
img = open_image(CR_FD_RLIMIT, O_SHOW, item->pid.virt);
if (img) {
pr_msg("* ");
pr_msg(imgset_template[CR_FD_RLIMIT].fmt, item->pid.virt);
pr_msg(":\n");
cr_parse_fd(img, RLIMIT_MAGIC);
close_image(img);
}
if (pb_read_one(img_from_set(cr_imgset, CR_FD_IDS), &ids, PB_IDS) > 0) {
img = open_image(CR_FD_FDINFO, O_SHOW, ids->files_id);
if (img) {
pr_msg("* ");
pr_msg(imgset_template[CR_FD_FDINFO].fmt, ids->files_id);
pr_msg(":\n");
cr_parse_fd(img, FDINFO_MAGIC);
close_image(img);
}
task_kobj_ids_entry__free_unpacked(ids, NULL);
}
pr_msg("---[ end of task %d ]---\n", item->pid.virt);
ret = 0;
outc:
close_cr_imgset(&cr_imgset);
out:
return ret;
}
static int cr_show_pid(int pid)
{
int ret;
struct cr_img *img;
struct pstree_item item;
img = open_image(CR_FD_PSTREE, O_SHOW);
if (!img)
return -1;
while (1) {
PstreeEntry *pe;
ret = pb_read_one_eof(img, &pe, PB_PSTREE);
if (ret <= 0) {
close_image(img);
return ret;
}
if (pe->pid == pid) {
pstree_item_from_pb(pe, &item);
pstree_entry__free_unpacked(pe, NULL);
break;
}
pstree_entry__free_unpacked(pe, NULL);
}
close_image(img);
return cr_show_pstree_item(&item);
}
static int cr_show_all(void)
{
struct pstree_item *item = NULL, *tmp;
int ret = -1, pid;
struct cr_img *img;
img = open_image(CR_FD_PSTREE, O_SHOW);
if (!img)
goto out;
show_collect_pstree(img, 1);
close_image(img);
pid = list_first_entry(&pstree_list, struct pstree_item, sibling)->pid.virt;
ret = try_show_namespaces(pid);
if (ret)
goto out;
list_for_each_entry(item, &pstree_list, sibling)
if (cr_show_pstree_item(item))
break;
out:
list_for_each_entry_safe(item, tmp, &pstree_list, sibling) {
list_del(&item->sibling);
xfree(item->threads);
xfree(item);
}
return ret;
}
int cr_show(int pid)
{
if (isatty(STDOUT_FILENO)) {
pr_msg("The \"show\" action is deprecated by the CRIT utility.\n");
pr_msg("To view an image use the \"crit decode -i $name --pretty\" command.\n");
return -1;
}
if (opts.show_dump_file)
return cr_parse_file();
if (pid)
return cr_show_pid(pid);
return cr_show_all();
}