diff --git a/cr-show.c b/cr-show.c index 6bc470c8e..1ec068ca1 100644 --- a/cr-show.c +++ b/cr-show.c @@ -32,6 +32,7 @@ #include "protobuf/remap-file-path.pb-c.h" #include "protobuf/fown.pb-c.h" #include "protobuf/fs.pb-c.h" +#include "protobuf/pstree.pb-c.h" #define DEF_PAGES_PER_LINE 6 @@ -433,29 +434,30 @@ out: static int show_collect_pstree(int fd_pstree, struct list_head *collect) { - struct pstree_entry e; + PstreeEntry *e; pr_img_head(CR_FD_PSTREE); while (1) { - u32 pid; int ret; struct pstree_item *item = NULL; - ret = read_img_eof(fd_pstree, &e); + e = NULL; + ret = pb_read_eof(fd_pstree, &e, pstree_entry); if (ret <= 0) goto out; - pr_msg("pid: %8d ppid %8d pgid: %8d sid %8d nr_threads: %8d\n", - e.pid, e.ppid, e.pgid, e.sid, e.nr_threads); + pr_msg("pid: %8d ppid %8d pgid: %8d sid %8d n_threads: %8d\n", + (int)e->pid, (int)e->ppid, (int)e->pgid, + (int)e->sid, (int)e->n_threads); if (collect) { item = xzalloc(sizeof(struct pstree_item)); if (!item) return -1; - item->pid.virt = e.pid; - item->nr_threads = e.nr_threads; - item->threads = xzalloc(sizeof(u32) * e.nr_threads); + item->pid.virt = e->pid; + item->nr_threads = e->n_threads; + item->threads = xzalloc(sizeof(u32) * e->n_threads); if (!item->threads) { xfree(item); return -1; @@ -464,23 +466,23 @@ static int show_collect_pstree(int fd_pstree, struct list_head *collect) list_add_tail(&item->list, collect); } - if (e.nr_threads) { + if (e->n_threads) { pr_msg(" \\\n"); pr_msg(" --- threads: "); - while (e.nr_threads--) { - ret = read_img(fd_pstree, &pid); - if (ret < 0) - goto out; - pr_msg(" %6d", pid); + while (e->n_threads--) { + pr_msg(" %6d", (int)e->threads[e->n_threads]); if (item) - item->threads[e.nr_threads].virt = pid; + item->threads[e->n_threads].virt = e->threads[e->n_threads]; } pr_msg("\n"); } + pstree_entry__free_unpacked(e, NULL); } out: + if (e) + pstree_entry__free_unpacked(e, NULL); pr_img_tail(CR_FD_PSTREE); return 0; } diff --git a/include/image.h b/include/image.h index b4d35a126..b74ff9a59 100644 --- a/include/image.h +++ b/include/image.h @@ -75,14 +75,6 @@ typedef struct { */ #define REMAP_GHOST (1 << 31) -struct pstree_entry { - u32 pid; - u32 ppid; - u32 pgid; - u32 sid; - u32 nr_threads; -} __packed; - struct pipe_entry { u32 id; u32 pipe_id; diff --git a/protobuf/Makefile b/protobuf/Makefile index 501212f64..96662ffd7 100644 --- a/protobuf/Makefile +++ b/protobuf/Makefile @@ -30,6 +30,7 @@ PROTO_FILES += eventpoll.proto PROTO_FILES += fh.proto PROTO_FILES += inotify.proto PROTO_FILES += fs.proto +PROTO_FILES += pstree.proto HDRS := $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES)) SRCS := $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES)) diff --git a/protobuf/pstree.proto b/protobuf/pstree.proto new file mode 100644 index 000000000..6cbcfd3e3 --- /dev/null +++ b/protobuf/pstree.proto @@ -0,0 +1,7 @@ +message pstree_entry { + required uint32 pid = 1; + required uint32 ppid = 2; + required uint32 pgid = 3; + required uint32 sid = 4; + repeated uint32 threads = 5; +} diff --git a/pstree.c b/pstree.c index 6db300ef9..95560fa80 100644 --- a/pstree.c +++ b/pstree.c @@ -6,6 +6,9 @@ #include "restorer.h" #include "util.h" +#include "protobuf.h" +#include "protobuf/pstree.pb-c.h" + struct pstree_item *root_item; void free_pstree(struct pstree_item *root_item) @@ -71,7 +74,7 @@ struct pstree_item *pstree_item_next(struct pstree_item *item) int dump_pstree(struct pstree_item *root_item) { struct pstree_item *item = root_item; - struct pstree_entry e; + PstreeEntry e = PSTREE_ENTRY__INIT; int ret = -1, i; int pstree_fd; @@ -90,16 +93,20 @@ int dump_pstree(struct pstree_item *root_item) e.ppid = item->parent ? item->parent->pid.virt : 0; e.pgid = item->pgid; e.sid = item->sid; - e.nr_threads = item->nr_threads; + e.n_threads = item->nr_threads; - if (write_img(pstree_fd, &e)) + e.threads = xmalloc(sizeof(e.threads[0]) * e.n_threads); + if (!e.threads) goto err; - for (i = 0; i < item->nr_threads; i++) { - if (write_img_buf(pstree_fd, - &item->threads[i].virt, sizeof(u32))) - goto err; - } + for (i = 0; i < item->nr_threads; i++) + e.threads[i] = item->threads[i].virt; + + ret = pb_write(pstree_fd, &e, pstree_entry); + xfree(e.threads); + + if (ret) + goto err; } ret = 0; @@ -122,9 +129,9 @@ int prepare_pstree(void) return ps_fd; while (1) { - struct pstree_entry e; + PstreeEntry *e; - ret = read_img_eof(ps_fd, &e); + ret = pb_read_eof(ps_fd, &e, pstree_entry); if (ret <= 0) break; @@ -133,19 +140,19 @@ int prepare_pstree(void) if (pi == NULL) break; - pi->pid.virt = e.pid; - if (e.pid > max_pid) - max_pid = e.pid; + pi->pid.virt = e->pid; + if (e->pid > max_pid) + max_pid = e->pid; - pi->pgid = e.pgid; - if (e.pgid > max_pid) - max_pid = e.pgid; + pi->pgid = e->pgid; + if (e->pgid > max_pid) + max_pid = e->pgid; - pi->sid = e.sid; - if (e.sid > max_pid) - max_pid = e.sid; + pi->sid = e->sid; + if (e->sid > max_pid) + max_pid = e->sid; - if (e.ppid == 0) { + if (e->ppid == 0) { BUG_ON(root_item); root_item = pi; pi->parent = NULL; @@ -157,14 +164,14 @@ int prepare_pstree(void) * and sit among the last item's ancestors. */ while (parent) { - if (parent->pid.virt == e.ppid) + if (parent->pid.virt == e->ppid) break; parent = parent->parent; } if (parent == NULL) for_each_pstree_item(parent) - if (parent->pid.virt == e.ppid) + if (parent->pid.virt == e->ppid) break; if (parent == NULL) { @@ -179,22 +186,19 @@ int prepare_pstree(void) parent = pi; - pi->nr_threads = e.nr_threads; - pi->threads = xmalloc(e.nr_threads * sizeof(struct pid)); + pi->nr_threads = e->n_threads; + pi->threads = xmalloc(e->n_threads * sizeof(struct pid)); if (!pi->threads) break; ret = 0; - for (i = 0; i < e.nr_threads; i++) { - ret = read_img_buf(ps_fd, &pi->threads[i].virt, sizeof(u32)); - if (ret < 0) - break; - } - if (ret < 0) - break; + for (i = 0; i < e->n_threads; i++) + pi->threads[i].virt = e->threads[i]; - task_entries->nr += e.nr_threads; + task_entries->nr += e->n_threads; task_entries->nr_tasks++; + + pstree_entry__free_unpacked(e, NULL); } close(ps_fd);