Skip to content

Commit 3330c45

Browse files
committed
cmdline add option father to dump parent cmdline
Signed-off-by: Penguin. <chenguanyou9338@gmail.com>
1 parent 066440c commit 3330c45

2 files changed

Lines changed: 58 additions & 32 deletions

File tree

cmdline/cmd.c

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,15 @@
66
#include <getopt.h>
77
#include <string.h>
88

9-
void parser_cmdline_main(void) {
10-
struct task_context *tc = NULL;
9+
void parser_cmdline_form_context(struct task_context* tc) {
1110
struct task_mem_usage task_mem_usage, *tm;
1211
ulong arg_start_addr = 0x0;
1312
ulong arg_end_addr = 0x0;
1413
char anon[ANON_BUFSIZE];
1514
int current_offset = 0;
16-
int pid = CURRENT_PID();
1715
unsigned char* page_buf;
1816
bool parse_zram = true;
1917

20-
int opt;
21-
int option_index = 0;
22-
optind = 0; // reset
23-
static struct option long_options[] = {
24-
{"task", required_argument, 0,'t'},
25-
{"pid", required_argument, 0,'p'},
26-
{0, 0, 0, 0 },
27-
};
28-
29-
while ((opt = getopt_long(argcnt - 1, &args[1], "t:p:",
30-
long_options, &option_index)) != -1) {
31-
switch (opt) {
32-
case 'p':
33-
pid = atoi(optarg);
34-
break;
35-
case 't':
36-
tc = task_to_context(htol(optarg, FAULT_ON_ERROR, NULL));
37-
break;
38-
}
39-
}
40-
41-
if (!tc) {
42-
tc = pid_to_context(pid);
43-
if (!tc) {
44-
fprintf(fp, "No such pid: %d\n", pid);
45-
return;
46-
}
47-
}
48-
4918
set_context(tc->task, NO_PID, FALSE);
5019

5120
tm = &task_mem_usage;
@@ -103,17 +72,72 @@ void parser_cmdline_main(void) {
10372
anon[ANON_BUFSIZE - 1] = '\0';
10473
free(page_buf);
10574

75+
fprintf(fp, "PID: %-8ld ", tc->pid);
10676
int length = arg_end_addr - arg_start_addr;
10777
do {
10878
fprintf(fp, "%s ", &anon[current_offset]);
10979
current_offset += strlen(&anon[current_offset]) + 1;
11080
} while (current_offset < length);
11181
fprintf(fp, "\n");
82+
83+
}
84+
85+
void parser_cmdline_main(void) {
86+
struct task_context *tc = NULL;
87+
int pid = CURRENT_PID();
88+
bool dump_all = false;
89+
ulong parent;
90+
91+
int opt;
92+
int option_index = 0;
93+
optind = 0; // reset
94+
static struct option long_options[] = {
95+
{"task", required_argument, 0,'t'},
96+
{"pid", required_argument, 0,'p'},
97+
{"father", no_argument, 0,'f'},
98+
{0, 0, 0, 0 },
99+
};
100+
101+
while ((opt = getopt_long(argcnt - 1, &args[1], "t:p:f",
102+
long_options, &option_index)) != -1) {
103+
switch (opt) {
104+
case 'p':
105+
pid = atoi(optarg);
106+
break;
107+
case 't':
108+
tc = task_to_context(htol(optarg, FAULT_ON_ERROR, NULL));
109+
break;
110+
case 'f':
111+
dump_all = true;
112+
break;
113+
}
114+
}
115+
116+
if (!tc) {
117+
tc = pid_to_context(pid);
118+
if (!tc) {
119+
fprintf(fp, "No such pid: %d\n", pid);
120+
return;
121+
}
122+
}
123+
124+
if (!dump_all)
125+
parser_cmdline_form_context(tc);
126+
else {
127+
do {
128+
parser_cmdline_form_context(tc);
129+
readmem(tc->task + OFFSET(task_struct_parent), KVADDR,
130+
&parent, sizeof(void *), "task_struct parent", FAULT_ON_ERROR);
131+
tc = task_to_context(parent);
132+
} while (tc->pid != 0);
133+
}
112134
}
113135

114136
void parser_cmdline_usage(void) {
115137
fprintf(fp, "Usage: lp cmdline [OPTION] ...\n");
116138
fprintf(fp, "Option:\n");
117139
fprintf(fp, " -t, --task print task_struct cmdline\n");
118140
fprintf(fp, " -p, --pid print pid cmdline\n");
141+
fprintf(fp, " -f, --father foreach print pid cmdline\n");
142+
119143
}

cmdline/cmd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
void parser_cmdline_main(void);
1010
void parser_cmdline_usage(void);
1111

12+
void parser_cmdline_form_context(struct task_context* tc);
13+
1214
#endif // CMDLINE_CMD_H_

0 commit comments

Comments
 (0)