Debian Codegraph @

testsuite/systemtap.examples/process/psig.stp

raw
1 #! /bin/sh
2
3 //bin/true && exec stap -DMAXACTION=10000 -g $0 ${1+"$@"}
4
5 # psig
6 # Copyright (C) 2008-2012 Red Hat, Inc., Eugene Teo <[email protected]>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 as
10 # published by the Free Software Foundation.
11 #
12 # psig reports information about signals by the process id.
13 #
14 # psig is not a port of Solaris' psig tool. It was written based on the example
15 # output at http://developers.sun.com/solaris/articles/solaris_linux_app.html.
16 # This script requires the signal.stp-psig.patch for tapset/signal.stp (not in
17 # the upstream version of SystemTap at the time of writing).
18 #
19 # $ psig $$ | head -10
20 # 1852: bash
21 # HUP caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
22 # INT caught 0x0808d280 0
23 # QUIT ignored
24 # ILL caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
25 # TRAP caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
26 # ABRT caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
27 # BUS caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
28 # FPE caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,FPE,USR1,SEGV,USR2,PIPE,ALRM,TERM,XCPU,XFSZ,VTALRM,SYS
29 # KILL default
30 # $ stap -V # systemtap-20071229.tar.bz2
31 # SystemTap translator/driver (version 0.6/0.131 built 2008-01-12)
32 # $ cat /etc/redhat-release
33 # Fedora release 8 (Werewolf)
34 # $ uname -a
35 # Linux kerndev.xxx.redhat.com 2.6.23.9-85.fc8 #1 SMP Fri Dec 7 15:49:59 EST 2007 i686 i686 i386 GNU/Linux
36 #
37 # NOTES:
38 # HUP caught 0x0808d040 0 HUP,INT,ILL,TRAP,ABRT,BUS,...
39 # [.......][.......][..........][.][............................
40 # |<-- type of signal (1..64)
41 # |<-- signal disposition. it can be SIG_{DFL,IGN,ERR} or a pointer to a function
42 # |<-- address of the signal-catching function
43 # |<-- sa_flags. see the code snippet below
44 # |<-- set of signals to be blocked when executing the handler
45 #
46 # linux-2.6/include/asm-x86/signal.h:
47 # [...]
48 # /*
49 # * SA_FLAGS values:
50 # *
51 # * SA_ONSTACK indicates that a registered stack_t will be used.
52 # * SA_RESTART flag to get restarting signals (which were the default long ago)
53 # * SA_NOCLDSTOP flag to turn off SIGCHLD when children stop.
54 # * SA_RESETHAND clears the handler when the signal is delivered.
55 # * SA_NOCLDWAIT flag on SIGCHLD to inhibit zombies.
56 # * SA_NODEFER prevents the current signal from being masked in the handler.
57 # *
58 # * SA_ONESHOT and SA_NOMASK are the historical Linux names for the Single
59 # * Unix names RESETHAND and NODEFER respectively.
60 # */
61 # #define SA_NOCLDSTOP 0x00000001u
62 # #define SA_NOCLDWAIT 0x00000002u
63 # #define SA_SIGINFO 0x00000004u
64 # #define SA_ONSTACK 0x08000000u
65 # #define SA_RESTART 0x10000000u
66 # #define SA_NODEFER 0x40000000u
67 # #define SA_RESETHAND 0x80000000u
68 #
69 # #define SA_NOMASK SA_NODEFER
70 # #define SA_ONESHOT SA_RESETHAND
71 #
72 # #define SA_RESTORER 0x04000000
73 #
74
75 global _NSIG = 64
76
77 function get_k_sigaction:long (task:long, sig:long) %{
78 struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task);
79
80 STAP_RETVALUE = (long)&p->sighand->action[(int)STAP_ARG_sig];
81 %}
82
83 function get_task_info:string (task:long) %{
84 char pid[10]; /* just to realign the header properly */
85 struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task);
86
87 if (!p)
88 strlcpy(STAP_RETVALUE, "NULL", MAXSTRINGLEN);
89 else {
90 sprintf(pid, "%d:", p->pid);
91 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%-8s %s", pid, p->comm);
92 }
93 %}
94
95 function translate_mask:string (mask:string) {
96 str = signal_str(strtol(tokenize(mask, ","), 10))
97 while (1) {
98 sig = signal_str(strtol(tokenize("", ","), 10))
99 if (strlen(sig) == 0)
100 break;
101 str = str . "," . sig
102 }
103 return str;
104 }
105
106 /*
107 * if sa_flags is 0, then return 0. If not, return the interpreted sa_flags.
108 */
109 function sa_flags_str2:string (sa_flags:string) %{
110 if (strlen(STAP_ARG_sa_flags) == 0)
111 strcpy(STAP_ARG_sa_flags, "0");
112 strlcpy (STAP_RETVALUE, STAP_ARG_sa_flags, MAXSTRINGLEN);
113 %}
114
115 probe begin {
116 %( $# < 1
117 %? pid = target()
118 %: pid = $1
119 %)
120 # if (pid == 0) error ("Please provide valid target process-id as $1 or -x PID");
121 task = pid2task(pid)
122 assert(task, "pid2task: process not found. exiting.\n")
123
124 task_info = get_task_info(task)
125 assert(!isinstr(task_info, "NULL"), "get_task_info: invalid task_struct. exiting.\n")
126 printf("%s\n", task_info)
127
128 for (i = 0; i < _NSIG; ++i) {
129 handler_status = ""
130 act = get_k_sigaction(task, i)
131 assert(act, "get_k_sigaction: invalid k_sigaction pointer. exiting.\n")
132 sig = signal_str(i+1)
133
134 handler = sa_handler_str(get_sa_handler(act))
135 # XXX: convert hex pointer via usymdata() to useful function
136 if (! (isinstr(handler, "default") || isinstr(handler, "ignored"))) {
137 blocked = is_sig_blocked(task, i+1)
138 if (blocked)
139 handler_status = "blocked,"
140 handler_status = handler_status . "caught"
141 } else
142 handler_status = handler
143
144 flags = sa_flags_str2(sa_flags_str(get_sa_flags(act)))
145 mask = sigset_mask_str(get_sigaction_mask(act))
146
147 printf("%-8s %-8s ", sig, handler_status);
148 if (isinstr(handler_status, "caught"))
149 printf("%s %s %s\n", handler, flags, translate_mask(mask))
150 else
151 printf("\n")
152 }
153
154 exit()
155 }