dtpstree.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // DT PS Tree
  2. //
  3. // Douglas Thrift
  4. //
  5. // $Id$
  6. #include <climits>
  7. #include <cstdio>
  8. #include <iostream>
  9. #include <map>
  10. #include <set>
  11. #include <sstream>
  12. #include <string>
  13. #include <err.h>
  14. #include <fcntl.h>
  15. #include <kvm.h>
  16. #include <libgen.h>
  17. #include <paths.h>
  18. #include <popt.h>
  19. #include <pwd.h>
  20. #include <sys/param.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/user.h>
  23. #include "foreach.hpp"
  24. #define DTPSTREE_PROGRAM "dtpstree"
  25. #define DTPSTREE_VERSION "1.0.1"
  26. class Proc;
  27. typedef std::map<pid_t, Proc *> PidMap;
  28. typedef std::multimap<std::string, Proc *> NameMap;
  29. enum Flags
  30. {
  31. Arguments = 0x0001,
  32. Ascii = 0x0002,
  33. Compact = 0x0004,
  34. Highlight = 0x0008,
  35. Vt100 = 0x0010,
  36. ShowKernel = 0x0020,
  37. Long = 0x0040,
  38. NumericSort = 0x0080,
  39. ShowPids = 0x0104,
  40. ShowTitles = 0x0200,
  41. UidChanges = 0x0400,
  42. Unicode = 0x0800,
  43. Version = 0x1000,
  44. Pid = 0x2000,
  45. User = 0x4000
  46. };
  47. class Proc
  48. {
  49. const int &flags_;
  50. kvm_t *kd_;
  51. kinfo_proc *proc_;
  52. Proc *parent_;
  53. PidMap childrenByPid_;
  54. NameMap childrenByName_;
  55. bool highlight_;
  56. public:
  57. Proc(const int &flags, kvm_t *kd, kinfo_proc *proc) : flags_(flags), kd_(kd), proc_(proc) {}
  58. inline std::string name() const { return proc_->ki_comm; }
  59. inline pid_t parent() const { return proc_->ki_ppid; }
  60. inline pid_t pid() const { return proc_->ki_pid; }
  61. void child(Proc *proc)
  62. {
  63. proc->parent_ = this;
  64. childrenByPid_[proc->pid()] = proc;
  65. childrenByName_.insert(NameMap::value_type(proc->name(), proc));
  66. }
  67. void highlight()
  68. {
  69. highlight_ = true;
  70. if (parent_)
  71. parent_->highlight();
  72. }
  73. inline bool root() const { return !parent_; }
  74. void printByPid(const std::string &indent = "") const
  75. {
  76. std::cout << indent << print(indent.size()) << std::endl;
  77. _foreach (const PidMap, child, childrenByPid_)
  78. child->second->printByPid(indent + " ");
  79. }
  80. void printByName(const std::string &indent = "") const
  81. {
  82. std::cout << indent << print(indent.size()) << std::endl;
  83. _foreach (const NameMap, child, childrenByName_)
  84. child->second->printByName(indent + " ");
  85. }
  86. private:
  87. std::string print(std::string::size_type indent) const
  88. {
  89. std::ostringstream print;
  90. if (highlight_)
  91. print << "\033[1m";
  92. print << name();
  93. bool _pid(flags_ & ShowPids), _args(flags_ & Arguments);
  94. bool change(flags_ & UidChanges && parent_ && uid() != parent_->uid());
  95. bool parens((_pid || change) && !_args);
  96. if (parens)
  97. print << '(';
  98. if (_pid)
  99. {
  100. if (!parens)
  101. print << ',';
  102. print << pid();
  103. }
  104. if (change)
  105. {
  106. if (!parens || _pid)
  107. print << ',';
  108. print << user();
  109. }
  110. if (parens)
  111. print << ')';
  112. if (highlight_)
  113. print << "\033[22m";
  114. if (_args)
  115. print << args(indent + print.str().size());
  116. return print.str();
  117. }
  118. inline bool children() const { return childrenByPid_.size(); }
  119. inline uid_t uid() const { return proc_->ki_ruid; }
  120. std::string user() const
  121. {
  122. passwd *user(getpwuid(uid()));
  123. return user->pw_name;
  124. }
  125. std::string args(std::string::size_type indent) const
  126. {
  127. char **argv(kvm_getargv(kd_, proc_, 0));
  128. std::ostringstream args;
  129. if (argv && *argv)
  130. for (++argv; *argv; ++argv)
  131. {
  132. if (!(flags_ & Long) && (indent += 1 + std::strlen(*argv)) > 75)
  133. {
  134. args << " ...";
  135. break;
  136. }
  137. args << ' ' << *argv;
  138. }
  139. return args.str();
  140. }
  141. };
  142. int main(int argc, char *argv[])
  143. {
  144. int flags(0);
  145. pid_t hpid, pid;
  146. char *user(NULL);
  147. {
  148. poptOption options[] = {
  149. { "arguments", 'a', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Arguments, "show command line arguments", NULL },
  150. { "ascii", 'A', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Ascii, "use ASCII line drawing characters", NULL },
  151. { "compact", 'c', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Compact, "don't compact identical subtrees", NULL },
  152. { "highlight-all", 'h', POPT_ARG_NONE, NULL, 'h', "highlight current process and its ancestors", NULL },
  153. { "highlight-pid", 'H', POPT_ARG_INT, &hpid, 'H', "highlight this process and its ancestors", "PID" },
  154. { "vt100", 'G', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Vt100, "use VT100 line drawing characters", NULL },
  155. { "show-kernel", 'k', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, ShowKernel, "show kernel processes", NULL },
  156. { "long", 'l', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Long, "don't truncate long lines", NULL },
  157. { "numeric-sort", 'n', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, NumericSort, "sort output by PID", NULL },
  158. { "show-pids", 'p', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, ShowPids, "show PIDs; implies -c", NULL },
  159. { "show-titles", 't', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, ShowTitles, "show process titles", NULL },
  160. { "uid-changes", 'u', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, UidChanges, "show uid transitions", NULL },
  161. { "unicode", 'U', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Unicode, "use Unicode line drawing characters", NULL },
  162. { "version", 'V', POPT_ARG_VAL | POPT_ARGFLAG_OR, &flags, Version, "display version information", NULL },
  163. { "pid", '\0', POPT_ARG_INT, &pid, 'p', "start at this PID", "PID" },
  164. { "user", '\0', POPT_ARG_STRING, &user, 'u', "show only trees rooted at processes of this user", "USER" },
  165. POPT_AUTOHELP
  166. POPT_TABLEEND
  167. };
  168. poptContext context(poptGetContext(NULL, argc, const_cast<const char **>(argv), options, 0));
  169. int versionArgc;
  170. const char **versionArgv;
  171. poptParseArgvString("-V", &versionArgc, &versionArgv);
  172. poptAlias versionAlias = { NULL, 'v', versionArgc, versionArgv };
  173. poptAddAlias(context, versionAlias, 0);
  174. poptSetOtherOptionHelp(context, "[OPTION...] [PID|USER]");
  175. int option;
  176. while ((option = poptGetNextOpt(context)) >= 0)
  177. switch (option)
  178. {
  179. case 'h':
  180. hpid = getpid();
  181. case 'H':
  182. flags |= Highlight;
  183. break;
  184. case 'p':
  185. flags |= Pid;
  186. flags &= ~User;
  187. break;
  188. case 'u':
  189. flags |= User;
  190. flags &= ~Pid;
  191. }
  192. if (option != -1)
  193. errx(1, "%s: %s", poptStrerror(option), poptBadOption(context, 0));
  194. for (const char **arg(poptGetArgs(context)); arg && *arg; ++arg)
  195. {
  196. char *end;
  197. long value(std::strtol(*arg, &end, 0));
  198. if (*arg == end || *end != '\0')
  199. {
  200. std::free(user);
  201. user = strdup(*arg);
  202. flags |= User;
  203. flags &= ~Pid;
  204. }
  205. else if (value > INT_MAX || value < INT_MIN)
  206. errx(1, "%s: %s", poptStrerror(POPT_ERROR_OVERFLOW), *arg);
  207. else
  208. {
  209. pid = value;
  210. flags |= Pid;
  211. flags &= ~User;
  212. }
  213. }
  214. poptFreeContext(context);
  215. }
  216. if (flags & Version)
  217. {
  218. std::cout << DTPSTREE_PROGRAM " " DTPSTREE_VERSION << std::endl;
  219. return 0;
  220. }
  221. char error[_POSIX2_LINE_MAX];
  222. kvm_t *kd(kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, error));
  223. if (!kd)
  224. errx(1, "%s", error);
  225. typedef kinfo_proc *InfoProc;
  226. int count;
  227. InfoProc procs(kvm_getprocs(kd, KERN_PROC_PROC, 0, &count));
  228. if (!procs)
  229. errx(1, "%s", kvm_geterr(kd));
  230. PidMap pids;
  231. _forall (InfoProc, proc, procs, procs + count)
  232. if (flags & ShowKernel || proc->ki_ppid != 0 || proc->ki_pid == 1)
  233. pids[proc->ki_pid] = new Proc(flags, kd, proc);
  234. enum { PidSort, NameSort } sort(flags & NumericSort ? PidSort : NameSort);
  235. _foreach (PidMap, pid, pids)
  236. {
  237. Proc *proc(pid->second);
  238. PidMap::iterator parent(pids.find(proc->parent()));
  239. if (parent != pids.end())
  240. parent->second->child(proc);
  241. }
  242. if (flags & Highlight)
  243. {
  244. PidMap::iterator pid(pids.find(hpid));
  245. if (pid != pids.end())
  246. pid->second->highlight();
  247. }
  248. NameMap names;
  249. _foreach (PidMap, pid, pids)
  250. {
  251. Proc *proc(pid->second);
  252. if (proc->root())
  253. switch (sort)
  254. {
  255. case PidSort:
  256. proc->printByPid();
  257. break;
  258. case NameSort:
  259. names.insert(NameMap::value_type(proc->name(), proc));
  260. }
  261. }
  262. switch (sort)
  263. {
  264. case NameSort:
  265. _foreach (NameMap, name, names)
  266. name->second->printByName();
  267. default:
  268. break;
  269. }
  270. std::setlocale(LC_ALL, "");
  271. char line[MB_LEN_MAX];
  272. int size(std::wctomb(line, L'└'));
  273. if (size != -1)
  274. std::cout << std::string(line, size) << std::endl;
  275. return 0;
  276. }
  277. // display a tree of processes