array.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * linux/fs/proc/array.c
  3. *
  4. * Copyright (C) 1992 by Linus Torvalds
  5. * based on ideas by Darren Senn
  6. *
  7. * Fixes:
  8. * Michael. K. Johnson: stat,statm extensions.
  9. * <johnsonm@stolaf.edu>
  10. *
  11. * Pauline Middelink : Made cmdline,envline only break at '\0's, to
  12. * make sure SET_PROCTITLE works. Also removed
  13. * bad '!' which forced address recalculation for
  14. * EVERY character on the current page.
  15. * <middelin@polyware.iaf.nl>
  16. *
  17. * Danny ter Haar : added cpuinfo
  18. * <dth@cistron.nl>
  19. *
  20. * Alessandro Rubini : profile extension.
  21. * <rubini@ipvvis.unipv.it>
  22. *
  23. * Jeff Tranter : added BogoMips field to cpuinfo
  24. * <Jeff_Tranter@Mitel.COM>
  25. *
  26. * Bruno Haible : remove 4K limit for the maps file
  27. * <haible@ma2s2.mathematik.uni-karlsruhe.de>
  28. *
  29. * Yves Arrouye : remove removal of trailing spaces in get_array.
  30. * <Yves.Arrouye@marin.fdn.fr>
  31. *
  32. * Jerome Forissier : added per-CPU time information to /proc/stat
  33. * and /proc/<pid>/cpu extension
  34. * <forissier@isia.cma.fr>
  35. * - Incorporation and non-SMP safe operation
  36. * of forissier patch in 2.1.78 by
  37. * Hans Marcus <crowbar@concepts.nl>
  38. *
  39. * aeb@cwi.nl : /proc/partitions
  40. *
  41. *
  42. * Alan Cox : security fixes.
  43. * <Alan.Cox@linux.org>
  44. *
  45. * Al Viro : safe handling of mm_struct
  46. *
  47. * Gerhard Wichert : added BIGMEM support
  48. * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
  49. *
  50. * Al Viro & Jeff Garzik : moved most of the thing into base.c and
  51. * : proc_misc.c. The rest may eventually go into
  52. * : base.c too.
  53. */
  54. #include <linux/types.h>
  55. #include <linux/errno.h>
  56. #include <linux/time.h>
  57. #include <linux/kernel.h>
  58. #include <linux/kernel_stat.h>
  59. #include <linux/tty.h>
  60. #include <linux/string.h>
  61. #include <linux/mman.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/ioport.h>
  64. #include <linux/mm.h>
  65. #include <linux/hugetlb.h>
  66. #include <linux/pagemap.h>
  67. #include <linux/swap.h>
  68. #include <linux/slab.h>
  69. #include <linux/smp.h>
  70. #include <linux/signal.h>
  71. #include <linux/highmem.h>
  72. #include <linux/file.h>
  73. #include <linux/times.h>
  74. #include <linux/cpuset.h>
  75. #include <linux/rcupdate.h>
  76. #include <linux/delayacct.h>
  77. #include <asm/uaccess.h>
  78. #include <asm/pgtable.h>
  79. #include <asm/io.h>
  80. #include <asm/processor.h>
  81. #include "internal.h"
  82. /* Gcc optimizes away "strlen(x)" for constant x */
  83. #define ADDBUF(buffer, string) \
  84. do { memcpy(buffer, string, strlen(string)); \
  85. buffer += strlen(string); } while (0)
  86. static inline char * task_name(struct task_struct *p, char * buf)
  87. {
  88. int i;
  89. char * name;
  90. char tcomm[sizeof(p->comm)];
  91. get_task_comm(tcomm, p);
  92. ADDBUF(buf, "Name:\t");
  93. name = tcomm;
  94. i = sizeof(tcomm);
  95. do {
  96. unsigned char c = *name;
  97. name++;
  98. i--;
  99. *buf = c;
  100. if (!c)
  101. break;
  102. if (c == '\\') {
  103. buf[1] = c;
  104. buf += 2;
  105. continue;
  106. }
  107. if (c == '\n') {
  108. buf[0] = '\\';
  109. buf[1] = 'n';
  110. buf += 2;
  111. continue;
  112. }
  113. buf++;
  114. } while (i);
  115. *buf = '\n';
  116. return buf+1;
  117. }
  118. /*
  119. * The task state array is a strange "bitmap" of
  120. * reasons to sleep. Thus "running" is zero, and
  121. * you can test for combinations of others with
  122. * simple bit tests.
  123. */
  124. static const char *task_state_array[] = {
  125. "R (running)", /* 0 */
  126. "S (sleeping)", /* 1 */
  127. "D (disk sleep)", /* 2 */
  128. "T (stopped)", /* 4 */
  129. "T (tracing stop)", /* 8 */
  130. "Z (zombie)", /* 16 */
  131. "X (dead)" /* 32 */
  132. };
  133. static inline const char * get_task_state(struct task_struct *tsk)
  134. {
  135. unsigned int state = (tsk->state & (TASK_RUNNING |
  136. TASK_INTERRUPTIBLE |
  137. TASK_UNINTERRUPTIBLE |
  138. TASK_STOPPED |
  139. TASK_TRACED)) |
  140. (tsk->exit_state & (EXIT_ZOMBIE |
  141. EXIT_DEAD));
  142. const char **p = &task_state_array[0];
  143. while (state) {
  144. p++;
  145. state >>= 1;
  146. }
  147. return *p;
  148. }
  149. static inline char * task_state(struct task_struct *p, char *buffer)
  150. {
  151. struct group_info *group_info;
  152. int g;
  153. struct fdtable *fdt = NULL;
  154. rcu_read_lock();
  155. buffer += sprintf(buffer,
  156. "State:\t%s\n"
  157. "Tgid:\t%d\n"
  158. "Pid:\t%d\n"
  159. "PPid:\t%d\n"
  160. "TracerPid:\t%d\n"
  161. "Uid:\t%d\t%d\t%d\t%d\n"
  162. "Gid:\t%d\t%d\t%d\t%d\n",
  163. get_task_state(p),
  164. p->tgid, p->pid,
  165. pid_alive(p) ? rcu_dereference(p->real_parent)->tgid : 0,
  166. pid_alive(p) && p->ptrace ? rcu_dereference(p->parent)->pid : 0,
  167. p->uid, p->euid, p->suid, p->fsuid,
  168. p->gid, p->egid, p->sgid, p->fsgid);
  169. task_lock(p);
  170. if (p->files)
  171. fdt = files_fdtable(p->files);
  172. buffer += sprintf(buffer,
  173. "FDSize:\t%d\n"
  174. "Groups:\t",
  175. fdt ? fdt->max_fds : 0);
  176. rcu_read_unlock();
  177. group_info = p->group_info;
  178. get_group_info(group_info);
  179. task_unlock(p);
  180. for (g = 0; g < min(group_info->ngroups,NGROUPS_SMALL); g++)
  181. buffer += sprintf(buffer, "%d ", GROUP_AT(group_info,g));
  182. put_group_info(group_info);
  183. buffer += sprintf(buffer, "\n");
  184. return buffer;
  185. }
  186. static char * render_sigset_t(const char *header, sigset_t *set, char *buffer)
  187. {
  188. int i, len;
  189. len = strlen(header);
  190. memcpy(buffer, header, len);
  191. buffer += len;
  192. i = _NSIG;
  193. do {
  194. int x = 0;
  195. i -= 4;
  196. if (sigismember(set, i+1)) x |= 1;
  197. if (sigismember(set, i+2)) x |= 2;
  198. if (sigismember(set, i+3)) x |= 4;
  199. if (sigismember(set, i+4)) x |= 8;
  200. *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
  201. } while (i >= 4);
  202. *buffer++ = '\n';
  203. *buffer = 0;
  204. return buffer;
  205. }
  206. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  207. sigset_t *catch)
  208. {
  209. struct k_sigaction *k;
  210. int i;
  211. k = p->sighand->action;
  212. for (i = 1; i <= _NSIG; ++i, ++k) {
  213. if (k->sa.sa_handler == SIG_IGN)
  214. sigaddset(ign, i);
  215. else if (k->sa.sa_handler != SIG_DFL)
  216. sigaddset(catch, i);
  217. }
  218. }
  219. static inline char * task_sig(struct task_struct *p, char *buffer)
  220. {
  221. unsigned long flags;
  222. sigset_t pending, shpending, blocked, ignored, caught;
  223. int num_threads = 0;
  224. unsigned long qsize = 0;
  225. unsigned long qlim = 0;
  226. sigemptyset(&pending);
  227. sigemptyset(&shpending);
  228. sigemptyset(&blocked);
  229. sigemptyset(&ignored);
  230. sigemptyset(&caught);
  231. rcu_read_lock();
  232. if (lock_task_sighand(p, &flags)) {
  233. pending = p->pending.signal;
  234. shpending = p->signal->shared_pending.signal;
  235. blocked = p->blocked;
  236. collect_sigign_sigcatch(p, &ignored, &caught);
  237. num_threads = atomic_read(&p->signal->count);
  238. qsize = atomic_read(&p->user->sigpending);
  239. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  240. unlock_task_sighand(p, &flags);
  241. }
  242. rcu_read_unlock();
  243. buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
  244. buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
  245. /* render them all */
  246. buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
  247. buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
  248. buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
  249. buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
  250. buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
  251. return buffer;
  252. }
  253. static inline char *task_cap(struct task_struct *p, char *buffer)
  254. {
  255. return buffer + sprintf(buffer, "CapInh:\t%016x\n"
  256. "CapPrm:\t%016x\n"
  257. "CapEff:\t%016x\n",
  258. cap_t(p->cap_inheritable),
  259. cap_t(p->cap_permitted),
  260. cap_t(p->cap_effective));
  261. }
  262. static inline char *task_context_switch_counts(struct task_struct *p,
  263. char *buffer)
  264. {
  265. return buffer + sprintf(buffer, "voluntary_ctxt_switches:\t%lu\n"
  266. "nonvoluntary_ctxt_switches:\t%lu\n",
  267. p->nvcsw,
  268. p->nivcsw);
  269. }
  270. int proc_pid_status(struct task_struct *task, char * buffer)
  271. {
  272. char * orig = buffer;
  273. struct mm_struct *mm = get_task_mm(task);
  274. buffer = task_name(task, buffer);
  275. buffer = task_state(task, buffer);
  276. if (mm) {
  277. buffer = task_mem(mm, buffer);
  278. mmput(mm);
  279. }
  280. buffer = task_sig(task, buffer);
  281. buffer = task_cap(task, buffer);
  282. buffer = cpuset_task_status_allowed(task, buffer);
  283. #if defined(CONFIG_S390)
  284. buffer = task_show_regs(task, buffer);
  285. #endif
  286. buffer = task_context_switch_counts(task, buffer);
  287. return buffer - orig;
  288. }
  289. static clock_t task_utime(struct task_struct *p)
  290. {
  291. clock_t utime = cputime_to_clock_t(p->utime),
  292. total = utime + cputime_to_clock_t(p->stime);
  293. u64 temp;
  294. /*
  295. * Use CFS's precise accounting:
  296. */
  297. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  298. if (total) {
  299. temp *= utime;
  300. do_div(temp, total);
  301. }
  302. utime = (clock_t)temp;
  303. return utime;
  304. }
  305. static clock_t task_stime(struct task_struct *p)
  306. {
  307. clock_t stime = cputime_to_clock_t(p->stime);
  308. /*
  309. * Use CFS's precise accounting. (we subtract utime from
  310. * the total, to make sure the total observed by userspace
  311. * grows monotonically - apps rely on that):
  312. */
  313. stime = nsec_to_clock_t(p->se.sum_exec_runtime) - task_utime(p);
  314. return stime;
  315. }
  316. static int do_task_stat(struct task_struct *task, char * buffer, int whole)
  317. {
  318. unsigned long vsize, eip, esp, wchan = ~0UL;
  319. long priority, nice;
  320. int tty_pgrp = -1, tty_nr = 0;
  321. sigset_t sigign, sigcatch;
  322. char state;
  323. int res;
  324. pid_t ppid = 0, pgid = -1, sid = -1;
  325. int num_threads = 0;
  326. struct mm_struct *mm;
  327. unsigned long long start_time;
  328. unsigned long cmin_flt = 0, cmaj_flt = 0;
  329. unsigned long min_flt = 0, maj_flt = 0;
  330. cputime_t cutime, cstime;
  331. clock_t utime, stime;
  332. unsigned long rsslim = 0;
  333. char tcomm[sizeof(task->comm)];
  334. unsigned long flags;
  335. state = *get_task_state(task);
  336. vsize = eip = esp = 0;
  337. mm = get_task_mm(task);
  338. if (mm) {
  339. vsize = task_vsize(mm);
  340. eip = KSTK_EIP(task);
  341. esp = KSTK_ESP(task);
  342. }
  343. get_task_comm(tcomm, task);
  344. sigemptyset(&sigign);
  345. sigemptyset(&sigcatch);
  346. cutime = cstime = cputime_zero;
  347. utime = stime = 0;
  348. rcu_read_lock();
  349. if (lock_task_sighand(task, &flags)) {
  350. struct signal_struct *sig = task->signal;
  351. if (sig->tty) {
  352. tty_pgrp = pid_nr(sig->tty->pgrp);
  353. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  354. }
  355. num_threads = atomic_read(&sig->count);
  356. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  357. cmin_flt = sig->cmin_flt;
  358. cmaj_flt = sig->cmaj_flt;
  359. cutime = sig->cutime;
  360. cstime = sig->cstime;
  361. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  362. /* add up live thread stats at the group level */
  363. if (whole) {
  364. struct task_struct *t = task;
  365. do {
  366. min_flt += t->min_flt;
  367. maj_flt += t->maj_flt;
  368. utime += task_utime(t);
  369. stime += task_stime(t);
  370. t = next_thread(t);
  371. } while (t != task);
  372. min_flt += sig->min_flt;
  373. maj_flt += sig->maj_flt;
  374. utime += cputime_to_clock_t(sig->utime);
  375. stime += cputime_to_clock_t(sig->stime);
  376. }
  377. sid = signal_session(sig);
  378. pgid = process_group(task);
  379. ppid = rcu_dereference(task->real_parent)->tgid;
  380. unlock_task_sighand(task, &flags);
  381. }
  382. rcu_read_unlock();
  383. if (!whole || num_threads<2)
  384. wchan = get_wchan(task);
  385. if (!whole) {
  386. min_flt = task->min_flt;
  387. maj_flt = task->maj_flt;
  388. utime = task_utime(task);
  389. stime = task_stime(task);
  390. }
  391. /* scale priority and nice values from timeslices to -20..20 */
  392. /* to make it look like a "normal" Unix priority/nice value */
  393. priority = task_prio(task);
  394. nice = task_nice(task);
  395. /* Temporary variable needed for gcc-2.96 */
  396. /* convert timespec -> nsec*/
  397. start_time =
  398. (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
  399. + task->real_start_time.tv_nsec;
  400. /* convert nsec -> ticks */
  401. start_time = nsec_to_clock_t(start_time);
  402. res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %u %lu \
  403. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  404. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu\n",
  405. task->pid,
  406. tcomm,
  407. state,
  408. ppid,
  409. pgid,
  410. sid,
  411. tty_nr,
  412. tty_pgrp,
  413. task->flags,
  414. min_flt,
  415. cmin_flt,
  416. maj_flt,
  417. cmaj_flt,
  418. utime,
  419. stime,
  420. cputime_to_clock_t(cutime),
  421. cputime_to_clock_t(cstime),
  422. priority,
  423. nice,
  424. num_threads,
  425. start_time,
  426. vsize,
  427. mm ? get_mm_rss(mm) : 0,
  428. rsslim,
  429. mm ? mm->start_code : 0,
  430. mm ? mm->end_code : 0,
  431. mm ? mm->start_stack : 0,
  432. esp,
  433. eip,
  434. /* The signal information here is obsolete.
  435. * It must be decimal for Linux 2.0 compatibility.
  436. * Use /proc/#/status for real-time signals.
  437. */
  438. task->pending.signal.sig[0] & 0x7fffffffUL,
  439. task->blocked.sig[0] & 0x7fffffffUL,
  440. sigign .sig[0] & 0x7fffffffUL,
  441. sigcatch .sig[0] & 0x7fffffffUL,
  442. wchan,
  443. 0UL,
  444. 0UL,
  445. task->exit_signal,
  446. task_cpu(task),
  447. task->rt_priority,
  448. task->policy,
  449. (unsigned long long)delayacct_blkio_ticks(task));
  450. if(mm)
  451. mmput(mm);
  452. return res;
  453. }
  454. int proc_tid_stat(struct task_struct *task, char * buffer)
  455. {
  456. return do_task_stat(task, buffer, 0);
  457. }
  458. int proc_tgid_stat(struct task_struct *task, char * buffer)
  459. {
  460. return do_task_stat(task, buffer, 1);
  461. }
  462. int proc_pid_statm(struct task_struct *task, char *buffer)
  463. {
  464. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  465. struct mm_struct *mm = get_task_mm(task);
  466. if (mm) {
  467. size = task_statm(mm, &shared, &text, &data, &resident);
  468. mmput(mm);
  469. }
  470. return sprintf(buffer,"%d %d %d %d %d %d %d\n",
  471. size, resident, shared, text, lib, data, 0);
  472. }