Debian Codegraph @

testsuite/systemtap.examples/io/ttyspy.stp

raw
1 #! /usr/bin/env stap
2 # May also need --skip-badvars
3
4 global activity_time, activity_log
5
6 /* Concatenate head and tail, to a max of @num chars, preferring to keep the tail
7 (as if it were a recent history buffer). */
8 function strcattail:string(head:string,tail:string,num:long) %{ /* pure */
9 unsigned taillen = strlen(STAP_ARG_tail);
10 unsigned headlen = strlen(STAP_ARG_head);
11 unsigned maxlen = STAP_ARG_num < MAXSTRINGLEN ? STAP_ARG_num : MAXSTRINGLEN;
12 unsigned headkeep = min(maxlen-taillen,headlen);
13 unsigned headdrop = headlen-headkeep;
14
15 if (headkeep)
16 strlcpy (STAP_RETVALUE, &STAP_ARG_head[headdrop], headkeep+1); /* includes \0 */
17 strlcat (STAP_RETVALUE, STAP_ARG_tail, maxlen);
18 %}
19
20 probe kernel.function("tty_audit_add_data") {
21 major=$tty->driver->major;
22 minor=$tty->driver->minor_start + $tty->index;
23 try { pgrp=@choose_defined($tty->ctrl->pgrp->numbers[0]->nr,
24 $tty->pgrp %( kernel_v >= "2.6.24" %? ->numbers[0]->nr %: %) )}
25 catch { }
26 data=kernel_string_n($data,$size);
27 uid=uid()
28
29 activity_time[major,minor,pgrp,uid] = gettimeofday_s();
30 activity_log[major,minor,pgrp,uid]
31 = strcattail(activity_log[major,minor,pgrp,uid],data,40);
32 }
33
34 probe timer.s(3) {
35 ansi_clear_screen ()
36 printf("(%3s,%2s,%5s,%5s)\n", "maj","min","pgrp","uid");
37 foreach ([x,y,z,u] in activity_time-) {
38 printf("(%3d,%3d,%5d,%5d) %s\n", x,y,z,u,
39 text_str(activity_log[x,y,z,u]))
40 }
41
42 /* delete last record, if older than 60 seconds */
43 if (activity_time[x,y,z,u]+60 < gettimeofday_s()) {
44 delete activity_time[x,y,z,u]
45 delete activity_log[x,y,z,u]
46 }
47 }
48