]> granicus.if.org Git - strace/blob - proc.c
Trivial fixes
[strace] / proc.c
1 /*
2  * Copyright (c) 1993, 1994, 1995 Rick Sladkey <jrs@world.std.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *      $Id$
28  */
29
30 #include "defs.h"
31
32 #ifdef SVR4
33 #ifndef HAVE_MP_PROCFS
34
35 static const struct xlat proc_status_flags[] = {
36         { PR_STOPPED,   "PR_STOPPED"    },
37         { PR_ISTOP,     "PR_ISTOP"      },
38         { PR_DSTOP,     "PR_DSTOP"      },
39         { PR_ASLEEP,    "PR_ASLEEP"     },
40         { PR_FORK,      "PR_FORK"       },
41         { PR_RLC,       "PR_RLC"        },
42         { PR_PTRACE,    "PR_PTRACE"     },
43         { PR_PCINVAL,   "PR_PCINVAL"    },
44         { PR_ISSYS,     "PR_ISSYS"      },
45 #ifdef PR_STEP
46         { PR_STEP,      "PR_STEP"       },
47 #endif
48 #ifdef PR_KLC
49         { PR_KLC,       "PR_KLC"        },
50 #endif
51 #ifdef PR_ASYNC
52         { PR_ASYNC,     "PR_ASYNC"      },
53 #endif
54 #ifdef PR_PCOMPAT
55         { PR_PCOMPAT,   "PR_PCOMPAT"    },
56 #endif
57         { 0,            NULL            },
58 };
59
60 static const struct xlat proc_status_why[] = {
61         { PR_REQUESTED, "PR_REQUESTED"  },
62         { PR_SIGNALLED, "PR_SIGNALLED"  },
63         { PR_SYSENTRY,  "PR_SYSENTRY"   },
64         { PR_SYSEXIT,   "PR_SYSEXIT"    },
65         { PR_JOBCONTROL,"PR_JOBCONTROL" },
66         { PR_FAULTED,   "PR_FAULTED"    },
67 #ifdef PR_SUSPENDED
68         { PR_SUSPENDED, "PR_SUSPENDED"  },
69 #endif
70 #ifdef PR_CHECKPOINT
71         { PR_CHECKPOINT,"PR_CHECKPOINT" },
72 #endif
73         { 0,            NULL            },
74 };
75
76 static const struct xlat proc_run_flags[] = {
77         { PRCSIG,       "PRCSIG"        },
78         { PRCFAULT,     "PRCFAULT"      },
79         { PRSTRACE,     "PRSTRACE"      },
80         { PRSHOLD,      "PRSHOLD"       },
81         { PRSFAULT,     "PRSFAULT"      },
82         { PRSVADDR,     "PRSVADDR"      },
83         { PRSTEP,       "PRSTEP"        },
84         { PRSABORT,     "PRSABORT"      },
85         { PRSTOP,       "PRSTOP"        },
86         { 0,            NULL            },
87 };
88
89 int
90 proc_ioctl(struct tcb *tcp, int code, int arg)
91 {
92         int val;
93         prstatus_t status;
94         prrun_t run;
95
96         if (entering(tcp))
97                 return 0;
98
99         switch (code) {
100         case PIOCSTATUS:
101         case PIOCSTOP:
102         case PIOCWSTOP:
103                 if (arg == 0)
104                         tprintf(", NULL");
105                 else if (syserror(tcp))
106                         tprintf(", %#x", arg);
107                 else if (umove(tcp, arg, &status) < 0)
108                         tprintf(", {...}");
109                 else {
110                         tprintf(", {pr_flags=");
111                         printflags(proc_status_flags, status.pr_flags, "PR_???");
112                         if (status.pr_why) {
113                                 tprintf(", pr_why=");
114                                 printxval(proc_status_why, status.pr_why,
115                                           "PR_???");
116                         }
117                         switch (status.pr_why) {
118                         case PR_SIGNALLED:
119                         case PR_JOBCONTROL:
120                                 tprintf(", pr_what=");
121                                 printsignal(status.pr_what);
122                                 break;
123                         case PR_FAULTED:
124                                 tprintf(", pr_what=%d", status.pr_what);
125                                 break;
126                         case PR_SYSENTRY:
127                         case PR_SYSEXIT:
128                                 tprintf(", pr_what=SYS_%s",
129                                         sysent[status.pr_what].sys_name);
130                                 break;
131                         }
132                         tprintf(", ...}");
133                 }
134                 return 1;
135         case PIOCRUN:
136                 if (arg == 0)
137                         tprintf(", NULL");
138                 else if (umove(tcp, arg, &run) < 0)
139                         tprintf(", {...}");
140                 else {
141                         tprintf(", {pr_flags=");
142                         printflags(proc_run_flags, run.pr_flags, "PR???");
143                         tprintf(", ...}");
144                 }
145                 return 1;
146 #ifdef PIOCSET
147         case PIOCSET:
148         case PIOCRESET:
149                 if (umove(tcp, arg, &val) < 0)
150                         tprintf(", [?]");
151                 else {
152                         tprintf(", [");
153                         printflags(proc_status_flags, val, "PR_???");
154                         tprintf("]");
155                 }
156                 return 1;
157 #endif /* PIOCSET */
158         case PIOCKILL:
159         case PIOCUNKILL:
160                 /* takes a pointer to a signal */
161                 if (umove(tcp, arg, &val) < 0)
162                         tprintf(", [?]");
163                 else {
164                         tprintf(", [");
165                         printsignal(val);
166                         tprintf("]");
167                 }
168                 return 1;
169         case PIOCSFORK:
170         case PIOCRFORK:
171         case PIOCSRLC:
172         case PIOCRRLC:
173                 /* doesn't take an arg */
174                 return 1;
175         default:
176                 /* ad naseum */
177                 return 0;
178         }
179 }
180
181 #endif /* HAVE_MP_PROCFS */
182 #endif /* SVR4 */
183
184 #ifdef FREEBSD
185 #include <sys/pioctl.h>
186
187 static const struct xlat proc_status_why[] = {
188         { S_EXEC,       "S_EXEC"        },
189         { S_SIG,        "S_SIG"         },
190         { S_SCE,        "S_SCE"         },
191         { S_SCX,        "S_SCX"         },
192         { S_CORE,       "S_CORE"        },
193         { S_EXIT,       "S_EXIT"        },
194         { 0,            NULL            }
195 };
196
197 static const struct xlat proc_status_flags[] = {
198         { PF_LINGER,    "PF_LINGER"     },
199         { PF_ISUGID,    "PF_ISUGID"     },
200         { 0,            NULL            }
201 };
202
203 int
204 proc_ioctl(struct tcb *tcp, int code, int arg)
205 {
206         int val;
207         struct procfs_status status;
208
209         if (entering(tcp))
210                 return 0;
211
212         switch (code) {
213         case PIOCSTATUS:
214         case PIOCWAIT:
215                 if (arg == 0)
216                         tprintf(", NULL");
217                 else if (syserror(tcp))
218                         tprintf(", %x", arg);
219                 else if (umove(tcp, arg, &status) < 0)
220                         tprintf(", {...}");
221                 else {
222                         tprintf(", {state=%d, flags=", status.state);
223                         printflags(proc_status_flags, status.flags, "PF_???");
224                         tprintf(", events=");
225                         printflags(proc_status_why, status.events, "S_???");
226                         tprintf(", why=");
227                         printxval(proc_status_why, status.why, "S_???");
228                         tprintf(", val=%lu}", status.val);
229                 }
230                 return 1;
231         case PIOCBIS:
232                 tprintf(", ");
233                 printflags(proc_status_why, arg, "S_???");
234                 return 1;
235                 return 1;
236         case PIOCSFL:
237                 tprintf(", ");
238                 printflags(proc_status_flags, arg, "PF_???");
239                 return 1;
240         case PIOCGFL:
241                 if (syserror(tcp))
242                         tprintf(", %#x", arg);
243                 else if (umove(tcp, arg, &val) < 0)
244                         tprintf(", {...}");
245                 else {
246                         tprintf(", [");
247                         printflags(proc_status_flags, val, "PF_???");
248                         tprintf("]");
249                 }
250                 return 1;
251         default:
252                 /* ad naseum */
253                 return 0;
254         }
255 }
256 #endif