|
| 1 | +#include "cache.h" |
| 2 | + |
| 3 | +#include "strbuf.h" |
| 4 | +#include "strvec.h" |
| 5 | +#include "trace2.h" |
| 6 | + |
| 7 | +static void get_ancestry_names(struct strvec *names) |
| 8 | +{ |
| 9 | + /* |
| 10 | + * NEEDSWORK: We could gather the entire pstree into an array to match |
| 11 | + * functionality with compat/win32/trace2_win32_process_info.c. |
| 12 | + * To do so, we may want to examine /proc/<pid>/stat. For now, just |
| 13 | + * gather the immediate parent name which is readily accessible from |
| 14 | + * /proc/$(getppid())/comm. |
| 15 | + */ |
| 16 | + struct strbuf procfs_path = STRBUF_INIT; |
| 17 | + struct strbuf name = STRBUF_INIT; |
| 18 | + |
| 19 | + /* try to use procfs if it's present. */ |
| 20 | + strbuf_addf(&procfs_path, "/proc/%d/comm", getppid()); |
| 21 | + if (strbuf_read_file(&name, procfs_path.buf, 0)) { |
| 22 | + strbuf_release(&procfs_path); |
| 23 | + strbuf_trim_trailing_newline(&name); |
| 24 | + strvec_push(names, strbuf_detach(&name, NULL)); |
| 25 | + } |
| 26 | + |
| 27 | + return; |
| 28 | + /* NEEDSWORK: add non-procfs-linux implementations here */ |
| 29 | +} |
| 30 | + |
| 31 | +void trace2_collect_process_info(enum trace2_process_info_reason reason) |
| 32 | +{ |
| 33 | + if (!trace2_is_enabled()) |
| 34 | + return; |
| 35 | + |
| 36 | + /* someday we may want to write something extra here, but not today */ |
| 37 | + if (reason == TRACE2_PROCESS_INFO_EXIT) |
| 38 | + return; |
| 39 | + |
| 40 | + if (reason == TRACE2_PROCESS_INFO_STARTUP) { |
| 41 | + /* |
| 42 | + * NEEDSWORK: we could do the entire ptree in an array instead, |
| 43 | + * see compat/win32/trace2_win32_process_info.c. |
| 44 | + */ |
| 45 | + struct strvec names = STRVEC_INIT; |
| 46 | + |
| 47 | + get_ancestry_names(&names); |
| 48 | + |
| 49 | + if (names.nr) |
| 50 | + trace2_cmd_ancestry(names.v); |
| 51 | + strvec_clear(&names); |
| 52 | + } |
| 53 | + |
| 54 | + return; |
| 55 | +} |
0 commit comments