1 // task information tapset
2 // Copyright (C) 2006 Intel Corporation.
3 // Copyright (C) 2010-2014 Red Hat Inc.
5 // This file is part of systemtap, and is free software. You can
6 // redistribute it and/or modify it under the terms of the GNU General
7 // Public License (GPL); either version 2, or (at your option) any
11 #include <linux/version.h>
12 #include <linux/file.h>
13 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,25)
14 #include <linux/fdtable.h>
16 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
17 #include <linux/sched/rt.h>
19 #ifndef STAPCONF_TASK_UID
20 #include <linux/cred.h>
25 * sfunction task_current - The current task_struct of the current task
27 * Description: This function returns the task_struct representing the current process.
28 * This address can be passed to the various task_*() functions to extract
29 * more task-specific data.
31 function task_current:long () %{ /* pure */
32 STAP_RETVALUE = (long)current;
35 function _task_rlimit_cur:long (task:long, nd_limit:long)
37 if (nd_limit < 0 || nd_limit >= %{ RLIM_NLIMITS %}) {
40 sig = @cast(task, "task_struct", "kernel<linux/sched.h>")->signal;
41 rlimit = @cast(sig, "signal_struct", "kernel<linux/sched.h>")->rlim;
42 return @cast(rlimit, "rlimit", "kernel<linux/sched.h>")[nd_limit]->rlim_cur;
45 /* sfunction task_rlimit - The current resource limit of the task
47 * @task: task_struct pointer
48 * @lim_str: String representing limit.
50 * Description: Little bit slower way how ger resource limits of
52 * There is need translate string into number for each call.
54 function task_rlimit:long (task:long, lim_str:string)
56 lim = rlimit_from_str(lim_str);
57 if (lim == -1) { return -1; }
58 return _task_rlimit_cur(task, lim);
61 /* Fast and "safe" way how to do it. */
63 function task_rlimit_cpu:long (task:long)
65 return _task_rlimit_cur(task, %{ RLIMIT_CPU %} );
68 function task_rlimit_fsize:long (task:long)
70 return _task_rlimit_cur(task, %{ RLIMIT_FSIZE %});
73 function task_rlimit_data:long (task:long)
75 return _task_rlimit_cur(task, %{ RLIMIT_DATA %});
78 function task_rlimit_stack:long (task:long)
80 return _task_rlimit_cur(task, %{ RLIMIT_STACK %});
83 function task_rlimit_core:long (task:long)
85 return _task_rlimit_cur(task, %{ RLIMIT_CORE %});
88 function task_rlimit_rss:long (task:long)
90 return _task_rlimit_cur(task, %{ RLIMIT_RSS %});
93 function task_rlimit_nproc:long (task:long)
95 return _task_rlimit_cur(task, %{ RLIMIT_NPROC %});
98 function task_rlimit_nofile:long(task:long)
100 return _task_rlimit_cur(task, %{ RLIMIT_NOFILE %});
103 function task_rlimit_memlock:long(task:long)
105 return _task_rlimit_cur(task, %{ RLIMIT_MEMLOCK %});
108 function task_rlimit_as:long(task:long)
110 return _task_rlimit_cur(task, %{ RLIMIT_AS %});
113 function task_rlimit_locks:long(task:long)
115 return _task_rlimit_cur(task, %{ RLIMIT_LOCKS %});
118 function task_rlimit_sigpending:long(task:long)
120 %( kernel_v >= "2.6.8" %?
121 return _task_rlimit_cur(task, %{ RLIMIT_SIGPENDING %});
127 function task_rlimit_msgqueue:long(task:long)
129 %( kernel_v >= "2.6.8" %?
130 return _task_rlimit_cur(task, %{ RLIMIT_MSGQUEUE %});
136 function task_rlimit_nice:long(task:long)
138 %( kernel_v >= "2.6.12" %?
139 return _task_rlimit_cur(task, %{ RLIMIT_NICE %});
145 function task_rlimit_rtprio:long(task:long)
147 %( kernel_v >= "2.6.12" %?
148 return _task_rlimit_cur(task, %{ RLIMIT_RTPRIO %});
154 function task_rlimit_rttime:long(task:long)
156 %( kernel_v >= "2.6.25" %?
157 return _task_rlimit_cur(task, %{ RLIMIT_RTTIME %});
164 * sfunction task_parent - The task_struct of the parent task
166 * @task: task_struct pointer
168 * Description: This function returns the parent task_struct of
169 * the given task. This address can be passed to the various
170 * task_*() functions to extract more task-specific data.
172 function task_parent:long(task:long)
174 return @choose_defined(
175 @cast(task, "task_struct", "kernel<linux/sched.h>")->real_parent,
176 @cast(task, "task_struct", "kernel<linux/sched.h>")->parent)
180 * sfunction task_state - The state of the task
182 * @task: task_struct pointer
184 * Description: Return the state of the given task, one of:
185 * TASK_RUNNING (0), TASK_INTERRUPTIBLE (1), TASK_UNINTERRUPTIBLE (2),
186 * TASK_STOPPED (4), TASK_TRACED (8), EXIT_ZOMBIE (16), or EXIT_DEAD (32).
188 function task_state:long (task:long)
190 return @cast(task, "task_struct", "kernel<linux/sched.h>")->state
194 * sfunction task_execname - The name of the task
196 * @task: task_struct pointer
198 * Description: Return the name of the given task.
200 function task_execname:string (task:long)
202 return kernel_string(@cast(task, "task_struct", "kernel<linux/sched.h>")->comm)
206 * sfunction task_pid - The process identifier of the task
208 * @task: task_struct pointer
210 * Description: This fucntion returns the process id of the given task.
212 function task_pid:long (task:long)
214 return @cast(task, "task_struct", "kernel<linux/sched.h>")->tgid
218 * sfunction pid2task - The task_struct of the given process identifier
220 * @pid: process identifier
222 * Description: Return the task struct of the given process id.
224 function pid2task:long (pid:long) %{ /* pure */
225 struct task_struct *t = NULL;
226 pid_t t_pid = (pid_t)(long)STAP_ARG_pid;
227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
228 struct pid *p_pid = find_get_pid(t_pid);
230 t = pid_task(p_pid, PIDTYPE_PID);
234 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
235 t = find_task_by_vpid (t_pid);
237 t = find_task_by_pid (t_pid);
241 STAP_RETVALUE = (long)t;
245 * sfunction pid2execname - The name of the given process identifier
247 * @pid: process identifier
249 * Description: Return the name of the given process id.
251 function pid2execname:string (pid:long) {
254 return task_execname(tsk)
259 * sfunction task_tid - The thread identifier of the task
261 * @task: task_struct pointer
263 * Description: This function returns the thread id of the given task.
265 function task_tid:long (task:long)
267 return @cast(task, "task_struct", "kernel<linux/sched.h>")->pid
272 * sfunction task_gid - The group identifier of the task
274 * @task: task_struct pointer
276 * Description: This function returns the group id of the given task.
278 function task_gid:long (task:long) %{ /* pure */
279 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
280 #ifdef STAPCONF_TASK_UID
281 STAP_RETVALUE = kread(&(t->gid));
284 /* If task_gid() isn't defined, make our own. */
285 #if !defined(task_gid) && defined(task_cred_xxx)
286 #define task_gid(task) (task_cred_xxx((task), gid))
288 /* XXX: We can't easily kread this rcu-protected field. */
289 #ifdef CONFIG_USER_NS
290 STAP_RETVALUE = from_kgid_munged(task_cred_xxx(t, user_ns), task_gid(t));
292 STAP_RETVALUE = task_gid (t);
299 * sfunction task_egid - The effective group identifier of the task
301 * @task: task_struct pointer
303 * Description: This function returns the effective group id of the given task.
305 function task_egid:long (task:long) %{ /* pure */
306 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
307 #ifdef STAPCONF_TASK_UID
308 STAP_RETVALUE = kread(&(t->egid));
311 /* If task_egid() isn't defined, make our own. */
312 #if !defined(task_egid) && defined(task_cred_xxx)
313 #define task_egid(task) (task_cred_xxx((task), egid))
315 /* XXX: We can't easily kread this rcu-protected field. */
316 #ifdef CONFIG_USER_NS
317 STAP_RETVALUE = from_kgid_munged(task_cred_xxx(t, user_ns), task_egid(t));
319 STAP_RETVALUE = task_egid (t);
325 * sfunction task_uid - The user identifier of the task
327 * @task: task_struct pointer
329 * Description: This function returns the user id of the given task.
331 function task_uid:long (task:long) %{ /* pure */
332 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
333 #ifdef STAPCONF_TASK_UID
334 STAP_RETVALUE = kread(&(t->uid));
337 /* XXX: We can't easily kread this rcu-protected field. */
338 #ifdef CONFIG_USER_NS
339 STAP_RETVALUE = from_kuid_munged(task_cred_xxx(t, user_ns), task_uid(t));
341 STAP_RETVALUE = task_uid (t);
347 * sfunction task_euid - The effective user identifier of the task
349 * @task: task_struct pointer
351 * Description: This function returns the effective user id of the given task.
353 function task_euid:long (task:long) %{ /* pure */
354 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
355 #ifdef STAPCONF_TASK_UID
356 STAP_RETVALUE = kread(&(t->euid));
359 /* XXX: We can't easily kread this rcu-protected field. */
360 #ifdef CONFIG_USER_NS
361 STAP_RETVALUE = from_kuid_munged(task_cred_xxx(t, user_ns), task_euid(t));
363 STAP_RETVALUE = task_euid (t);
370 * sfunction task_prio - The priority value of the task
372 * @task: task_struct pointer
374 * Description: This function returns the priority value of the given task.
376 function task_prio:long (task:long) %{ /* pure */
377 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
378 STAP_RETVALUE = kread(&(t->prio)) - MAX_RT_PRIO;
384 * sfunction task_nice - The nice value of the task
386 * @task: task_struct pointer
388 * Description: This function returns the nice value of the given task.
390 function task_nice:long (task:long) %{ /* pure */
391 struct task_struct *t = (struct task_struct *)(long)STAP_ARG_task;
392 STAP_RETVALUE = kread(&(t->static_prio)) - MAX_RT_PRIO - 20;
397 * sfunction task_cpu - The scheduled cpu of the task
399 * @task: task_struct pointer
401 * Description: This function returns the scheduled cpu for the given task.
403 function task_cpu:long (task:long)
405 ti = @choose_defined(@cast(task, "task_struct", "kernel<linux/sched.h>")->stack,
406 @cast(task, "task_struct", "kernel<linux/sched.h>")->thread_info);
407 return @cast(ti, "thread_info", "kernel<linux/sched.h>")->cpu
411 * sfunction task_open_file_handles - The number of open files of the task
413 * @task: task_struct pointer
415 * Description: This function returns the number of open file handlers for the given task.
417 function task_open_file_handles:long (task:long)
420 unsigned int count=0, fd, max;
421 struct task_struct *t;
422 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
424 struct files_struct *f;
426 struct files_struct *fs;
429 t = (struct task_struct *)(long)STAP_ARG_task;
430 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
432 f = kread(&(t->files));
434 fs = kread(&(t->files));
435 f = kread(&(fs->fdt));
439 max = kread(&(f->max_fds));
440 for (fd = 0; fd < max; fd++) {
441 if ( kread(&(f->fd[fd])) != NULL)
444 STAP_RETVALUE = count;
451 * sfunction task_max_file_handles - The max number of open files for the task
453 * @task: task_struct pointer
455 * Description: This function returns the maximum number of file handlers for the given task.
457 function task_max_file_handles:long (task:long)
460 struct task_struct *t;
461 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
462 struct files_struct *f;
464 struct files_struct *fs;
467 t = (struct task_struct *)(long)STAP_ARG_task;
468 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
469 f = kread(&(t->files));
471 fs = kread (&(t->files));
472 f = kread(&(fs->fdt));
476 STAP_RETVALUE = kread(&(f->max_fds));