]> sourceware.org Git - systemtap.git/blob - tapset/linux/task.stp
update copyrights
[systemtap.git] / tapset / linux / task.stp
1 // task information tapset
2 // Copyright (C) 2006 Intel Corporation.
3 // Copyright (C) 2010-2014 Red Hat Inc.
4 //
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
8 // later version.
9
10 %{
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>
15 #endif
16 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
17 #include <linux/sched/rt.h>
18 #endif
19 #ifndef STAPCONF_TASK_UID
20 #include <linux/cred.h>
21 #endif
22 %}
23
24 /**
25 * sfunction task_current - The current task_struct of the current task
26 *
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.
30 */
31 function task_current:long () %{ /* pure */
32 STAP_RETVALUE = (long)current;
33 %}
34
35 function _task_rlimit_cur:long (task:long, nd_limit:long)
36 {
37 if (nd_limit < 0 || nd_limit >= %{ RLIM_NLIMITS %}) {
38 return -1;
39 }
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;
43 }
44
45 /* sfunction task_rlimit - The current resource limit of the task
46 *
47 * @task: task_struct pointer
48 * @lim_str: String representing limit.
49 *
50 * Description: Little bit slower way how ger resource limits of
51 * process.
52 * There is need translate string into number for each call.
53 */
54 function task_rlimit:long (task:long, lim_str:string)
55 {
56 lim = rlimit_from_str(lim_str);
57 if (lim == -1) { return -1; }
58 return _task_rlimit_cur(task, lim);
59 }
60
61 /* Fast and "safe" way how to do it. */
62
63 function task_rlimit_cpu:long (task:long)
64 {
65 return _task_rlimit_cur(task, %{ RLIMIT_CPU %} );
66 }
67
68 function task_rlimit_fsize:long (task:long)
69 {
70 return _task_rlimit_cur(task, %{ RLIMIT_FSIZE %});
71 }
72
73 function task_rlimit_data:long (task:long)
74 {
75 return _task_rlimit_cur(task, %{ RLIMIT_DATA %});
76 }
77
78 function task_rlimit_stack:long (task:long)
79 {
80 return _task_rlimit_cur(task, %{ RLIMIT_STACK %});
81 }
82
83 function task_rlimit_core:long (task:long)
84 {
85 return _task_rlimit_cur(task, %{ RLIMIT_CORE %});
86 }
87
88 function task_rlimit_rss:long (task:long)
89 {
90 return _task_rlimit_cur(task, %{ RLIMIT_RSS %});
91 }
92
93 function task_rlimit_nproc:long (task:long)
94 {
95 return _task_rlimit_cur(task, %{ RLIMIT_NPROC %});
96 }
97
98 function task_rlimit_nofile:long(task:long)
99 {
100 return _task_rlimit_cur(task, %{ RLIMIT_NOFILE %});
101 }
102
103 function task_rlimit_memlock:long(task:long)
104 {
105 return _task_rlimit_cur(task, %{ RLIMIT_MEMLOCK %});
106 }
107
108 function task_rlimit_as:long(task:long)
109 {
110 return _task_rlimit_cur(task, %{ RLIMIT_AS %});
111 }
112
113 function task_rlimit_locks:long(task:long)
114 {
115 return _task_rlimit_cur(task, %{ RLIMIT_LOCKS %});
116 }
117
118 function task_rlimit_sigpending:long(task:long)
119 {
120 %( kernel_v >= "2.6.8" %?
121 return _task_rlimit_cur(task, %{ RLIMIT_SIGPENDING %});
122 %:
123 return -1
124 %)
125 }
126
127 function task_rlimit_msgqueue:long(task:long)
128 {
129 %( kernel_v >= "2.6.8" %?
130 return _task_rlimit_cur(task, %{ RLIMIT_MSGQUEUE %});
131 %:
132 return -1
133 %)
134 }
135
136 function task_rlimit_nice:long(task:long)
137 {
138 %( kernel_v >= "2.6.12" %?
139 return _task_rlimit_cur(task, %{ RLIMIT_NICE %});
140 %:
141 return -1
142 %)
143 }
144
145 function task_rlimit_rtprio:long(task:long)
146 {
147 %( kernel_v >= "2.6.12" %?
148 return _task_rlimit_cur(task, %{ RLIMIT_RTPRIO %});
149 %:
150 return -1
151 %)
152 }
153
154 function task_rlimit_rttime:long(task:long)
155 {
156 %( kernel_v >= "2.6.25" %?
157 return _task_rlimit_cur(task, %{ RLIMIT_RTTIME %});
158 %:
159 return -1
160 %)
161 }
162
163 /**
164 * sfunction task_parent - The task_struct of the parent task
165 *
166 * @task: task_struct pointer
167 *
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.
171 */
172 function task_parent:long(task:long)
173 {
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)
177 }
178
179 /**
180 * sfunction task_state - The state of the task
181 *
182 * @task: task_struct pointer
183 *
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).
187 */
188 function task_state:long (task:long)
189 {
190 return @cast(task, "task_struct", "kernel<linux/sched.h>")->state
191 }
192
193 /**
194 * sfunction task_execname - The name of the task
195 *
196 * @task: task_struct pointer
197 *
198 * Description: Return the name of the given task.
199 */
200 function task_execname:string (task:long)
201 {
202 return kernel_string(@cast(task, "task_struct", "kernel<linux/sched.h>")->comm)
203 }
204
205 /**
206 * sfunction task_pid - The process identifier of the task
207 *
208 * @task: task_struct pointer
209 *
210 * Description: This fucntion returns the process id of the given task.
211 */
212 function task_pid:long (task:long)
213 {
214 return @cast(task, "task_struct", "kernel<linux/sched.h>")->tgid
215 }
216
217 /**
218 * sfunction pid2task - The task_struct of the given process identifier
219 *
220 * @pid: process identifier
221 *
222 * Description: Return the task struct of the given process id.
223 */
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);
229 rcu_read_lock();
230 t = pid_task(p_pid, PIDTYPE_PID);
231 put_pid(p_pid);
232 #else
233 rcu_read_lock();
234 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
235 t = find_task_by_vpid (t_pid);
236 #else
237 t = find_task_by_pid (t_pid);
238 #endif /* 2.6.24 */
239 #endif /* 2.6.31 */
240 rcu_read_unlock();
241 STAP_RETVALUE = (long)t;
242 %}
243
244 /**
245 * sfunction pid2execname - The name of the given process identifier
246 *
247 * @pid: process identifier
248 *
249 * Description: Return the name of the given process id.
250 */
251 function pid2execname:string (pid:long) {
252 tsk = pid2task(pid)
253 if (tsk)
254 return task_execname(tsk)
255 return ""
256 }
257
258 /**
259 * sfunction task_tid - The thread identifier of the task
260 *
261 * @task: task_struct pointer
262 *
263 * Description: This function returns the thread id of the given task.
264 */
265 function task_tid:long (task:long)
266 {
267 return @cast(task, "task_struct", "kernel<linux/sched.h>")->pid
268 }
269
270
271 /**
272 * sfunction task_gid - The group identifier of the task
273 *
274 * @task: task_struct pointer
275 *
276 * Description: This function returns the group id of the given task.
277 */
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));
282 CATCH_DEREF_FAULT();
283 #else
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))
287 #endif
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));
291 #else
292 STAP_RETVALUE = task_gid (t);
293 #endif
294 #endif
295 %}
296
297
298 /**
299 * sfunction task_egid - The effective group identifier of the task
300 *
301 * @task: task_struct pointer
302 *
303 * Description: This function returns the effective group id of the given task.
304 */
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));
309 CATCH_DEREF_FAULT();
310 #else
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))
314 #endif
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));
318 #else
319 STAP_RETVALUE = task_egid (t);
320 #endif
321 #endif
322 %}
323
324 /**
325 * sfunction task_uid - The user identifier of the task
326 *
327 * @task: task_struct pointer
328 *
329 * Description: This function returns the user id of the given task.
330 */
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));
335 CATCH_DEREF_FAULT();
336 #else
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));
340 #else
341 STAP_RETVALUE = task_uid (t);
342 #endif
343 #endif
344 %}
345
346 /**
347 * sfunction task_euid - The effective user identifier of the task
348 *
349 * @task: task_struct pointer
350 *
351 * Description: This function returns the effective user id of the given task.
352 */
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));
357 CATCH_DEREF_FAULT();
358 #else
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));
362 #else
363 STAP_RETVALUE = task_euid (t);
364 #endif
365 #endif
366 %}
367
368
369 /**
370 * sfunction task_prio - The priority value of the task
371 *
372 * @task: task_struct pointer
373 *
374 * Description: This function returns the priority value of the given task.
375 */
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;
379 CATCH_DEREF_FAULT();
380 %}
381
382
383 /**
384 * sfunction task_nice - The nice value of the task
385 *
386 * @task: task_struct pointer
387 *
388 * Description: This function returns the nice value of the given task.
389 */
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;
393 CATCH_DEREF_FAULT();
394 %}
395
396 /**
397 * sfunction task_cpu - The scheduled cpu of the task
398 *
399 * @task: task_struct pointer
400 *
401 * Description: This function returns the scheduled cpu for the given task.
402 */
403 function task_cpu:long (task:long)
404 {
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
408 }
409
410 /**
411 * sfunction task_open_file_handles - The number of open files of the task
412 *
413 * @task: task_struct pointer
414 *
415 * Description: This function returns the number of open file handlers for the given task.
416 */
417 function task_open_file_handles:long (task:long)
418 %{ /* pure */
419 int locked = 0;
420 unsigned int count=0, fd, max;
421 struct task_struct *t;
422 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
423 /* Older kernels */
424 struct files_struct *f;
425 #else
426 struct files_struct *fs;
427 struct fdtable *f;
428 #endif
429 t = (struct task_struct *)(long)STAP_ARG_task;
430 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
431 /* Older kernels */
432 f = kread(&(t->files));
433 #else
434 fs = kread(&(t->files));
435 f = kread(&(fs->fdt));
436 #endif
437 rcu_read_lock();
438 locked = 1;
439 max = kread(&(f->max_fds));
440 for (fd = 0; fd < max; fd++) {
441 if ( kread(&(f->fd[fd])) != NULL)
442 count ++;
443 }
444 STAP_RETVALUE = count;
445 CATCH_DEREF_FAULT();
446 if (locked)
447 rcu_read_unlock();
448 %}
449
450 /**
451 * sfunction task_max_file_handles - The max number of open files for the task
452 *
453 * @task: task_struct pointer
454 *
455 * Description: This function returns the maximum number of file handlers for the given task.
456 */
457 function task_max_file_handles:long (task:long)
458 %{ /* pure */
459 int locked = 0;
460 struct task_struct *t;
461 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
462 struct files_struct *f;
463 #else
464 struct files_struct *fs;
465 struct fdtable *f;
466 #endif
467 t = (struct task_struct *)(long)STAP_ARG_task;
468 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
469 f = kread(&(t->files));
470 #else
471 fs = kread (&(t->files));
472 f = kread(&(fs->fdt));
473 #endif
474 rcu_read_lock();
475 locked = 1;
476 STAP_RETVALUE = kread(&(f->max_fds));
477 CATCH_DEREF_FAULT();
478 if (locked)
479 rcu_read_unlock();
480 %}
This page took 0.072039 seconds and 5 git commands to generate.