Print this page
11238 librtld_db demos should work with gcc 7
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/librtld_db/demo/common/main.c
+++ new/usr/src/cmd/sgs/librtld_db/demo/common/main.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 #include <stdio.h>
27 27 #include <stdlib.h>
28 28 #include <unistd.h>
29 29 #include <sys/uio.h>
30 30 #include <fcntl.h>
31 31 #include <string.h>
32 32 #include <errno.h>
33 33 #include <sys/types.h>
34 34 #include <sys/signal.h>
35 35 #include <sys/fault.h>
36 36 #include <sys/syscall.h>
37 37 #include <procfs.h>
38 38 #include <sys/auxv.h>
39 39 #include <libelf.h>
40 40 #include <sys/param.h>
41 41 #include <sys/machelf.h>
42 42 #include <stdarg.h>
43 43
44 44 #include <proc_service.h>
45 45
46 46 #include "rdb.h"
47 47 #include "disasm.h"
48 48 #include "gram.h"
49 49
50 50 #define PROCSIZE 20
51 51
52 52 static void
53 53 init_proc()
54 54 {
55 55 int pfd;
56 56 char procname[PROCSIZE];
57 57 sigset_t sigset;
58 58 fltset_t fltset;
59 59 sysset_t sysset;
60 60 long oper, pflags;
61 61 struct iovec piov[2];
62 62
63 63 /*
64 64 * open our own /proc file and set tracing flags
65 65 */
66 66 (void) snprintf(procname, PROCSIZE, "/proc/%d/ctl", EC_SWORD(getpid()));
67 67 if ((pfd = open(procname, O_WRONLY)) < 0) {
68 68 (void) fprintf(stderr, "can't open %s\n", procname);
69 69 exit(1);
70 70 }
71 71
72 72 /*
73 73 * inherit on fork, and kill-on-last-close
74 74 */
75 75 oper = PCSET;
76 76 piov[0].iov_base = (caddr_t)(&oper);
77 77 piov[0].iov_len = sizeof (oper);
78 78 pflags = PR_FORK;
79 79 piov[1].iov_base = (caddr_t)&pflags;
80 80 piov[1].iov_len = sizeof (pflags);
81 81
82 82 if (writev(pfd, piov, 2) == -1)
83 83 perr("init_proc: PCSET");
84 84
85 85 /*
86 86 * no signal tracing
87 87 */
88 88 oper = PCSTRACE;
89 89 premptyset(&sigset);
90 90 piov[1].iov_base = (caddr_t)&sigset;
91 91 piov[1].iov_len = sizeof (sigset);
92 92 if (writev(pfd, piov, 2) == -1)
93 93 perr("PCSTRACE");
94 94
95 95 /*
96 96 * no fault tracing
97 97 */
98 98 oper = PCSFAULT;
99 99 premptyset(&fltset);
100 100 piov[1].iov_base = (caddr_t)&fltset;
101 101 piov[1].iov_len = sizeof (fltset);
102 102 if (writev(pfd, piov, 2) == -1)
103 103 perr("PCSFAULT");
104 104
105 105 /*
106 106 * no syscall tracing
107 107 */
108 108 oper = PCSENTRY;
109 109 premptyset(&sysset);
110 110 piov[1].iov_base = (caddr_t)&sysset;
111 111 piov[1].iov_len = sizeof (sysset);
112 112 if (writev(pfd, piov, 2) == -1)
113 113 perr("PSENTRY");
114 114
115 115 /*
116 116 * except exit from exec() or execve()
117 117 */
118 118 oper = PCSEXIT;
119 119 premptyset(&sysset);
120 120 praddset(&sysset, SYS_execve);
121 121 if (writev(pfd, piov, 2) == -1)
122 122 perr("PCSEXIT");
123 123
124 124 (void) close(pfd);
125 125 }
126 126
127 127 int
128 128 main(int argc, char *argv[])
129 129 {
130 130 int pctlfd;
131 131 int pstatusfd;
132 132 char procname[PROCSIZE];
133 133 char *command;
134 134 char *rdb_commands = NULL;
135 135 pid_t cpid;
136 136 pstatus_t pstatus;
137 137 sysset_t sysset;
138 138 int c;
139 139 int error = 0;
140 140 long oper;
141 141 struct iovec piov[2];
142 142 extern FILE *yyin;
143 143
144 144 command = argv[0];
145 145
146 146 while ((c = getopt(argc, argv, "f:")) != EOF)
147 147 switch (c) {
148 148 case 'f':
149 149 rdb_commands = optarg;
150 150 break;
151 151 case '?':
152 152 break;
153 153 }
154 154
155 155 if (error || (optind == argc)) {
156 156 (void) printf("usage: %s [-f file] executable "
157 157 "[executable arguments ...]\n", command);
158 158 (void) printf("\t-f command file\n");
159 159 exit(1);
160 160 }
161 161
↓ open down ↓ |
161 lines elided |
↑ open up ↑ |
162 162 /*
163 163 * set up for tracing the child.
164 164 */
165 165 init_proc();
166 166
167 167 /*
168 168 * create a child to fork and exec from.
169 169 */
170 170 if ((cpid = fork()) == 0) {
171 171 (void) execv(argv[optind], &argv[optind]);
172 - perr(argv[1]);
172 + perr(argv[optind]);
173 173 }
174 174
175 175 if (cpid == -1) /* fork() failure */
176 176 perr(command);
177 177
178 178 /*
179 179 * initialize libelf
180 180 */
181 181 if (elf_version(EV_CURRENT) == EV_NONE) {
182 182 (void) fprintf(stderr, "elf_version() failed: %s\n",
183 183 elf_errmsg(0));
184 184 exit(1);
185 185 }
186 186
187 187 /*
188 188 * initialize librtld_db
189 189 */
190 190 if (rd_init(RD_VERSION) != RD_OK) {
191 191 (void) fprintf(stderr, "librtld_db::rd_init() failed: version "
192 192 "submitted: %d\n", RD_VERSION);
193 193 exit(1);
194 194 }
195 195
196 196 /* rd_log(1); */
197 197
198 198 /*
199 199 * Child should now be waiting after the successful
200 200 * exec.
201 201 */
202 202 (void) snprintf(procname, PROCSIZE, "/proc/%d/ctl", EC_SWORD(cpid));
203 203 (void) printf("parent: %d child: %d child procname: %s\n",
204 204 EC_SWORD(getpid()), EC_SWORD(cpid), procname);
205 205 if ((pctlfd = open(procname, O_WRONLY)) < 0) {
206 206 perror(procname);
207 207 (void) fprintf(stderr, "%s: can't open child %s\n",
208 208 command, procname);
209 209 exit(1);
210 210 }
211 211
212 212 /*
213 213 * wait for child process.
214 214 */
215 215 oper = PCWSTOP;
216 216 piov[0].iov_base = (caddr_t)&oper;
217 217 piov[0].iov_len = sizeof (oper);
218 218 if (writev(pctlfd, piov, 1) == -1)
219 219 perr("PCWSTOP");
220 220
221 221 /*
222 222 * open /proc/<cpid>/status
223 223 */
224 224 (void) snprintf(procname, PROCSIZE, "/proc/%d/status", EC_SWORD(cpid));
225 225 if ((pstatusfd = open(procname, O_RDONLY)) == -1)
226 226 perr(procname);
227 227
228 228 if (read(pstatusfd, &pstatus, sizeof (pstatus)) == -1)
229 229 perr("status read failed");
230 230
231 231 /*
232 232 * Make sure that it stopped where we expected.
233 233 */
234 234 while ((pstatus.pr_lwp.pr_why == PR_SYSEXIT) &&
235 235 (pstatus.pr_lwp.pr_what == SYS_execve)) {
236 236 long pflags = 0;
237 237 if (!(pstatus.pr_lwp.pr_reg[R_PS] & ERRBIT)) {
238 238 /* successfull exec(2) */
239 239 break;
240 240 }
241 241
242 242 oper = PCRUN;
243 243 piov[1].iov_base = (caddr_t)&pflags;
244 244 piov[1].iov_len = sizeof (pflags);
245 245 if (writev(pctlfd, piov, 2) == -1)
246 246 perr("PCRUN1");
247 247
248 248 oper = PCWSTOP;
249 249 if (writev(pctlfd, piov, 1) == -1)
250 250 perr("PCWSTOP");
251 251
252 252 if (read(pstatusfd, &pstatus, sizeof (pstatus)) == -1)
253 253 perr("status read failed");
254 254 }
255 255
256 256 premptyset(&sysset);
257 257 oper = PCSEXIT;
258 258 piov[1].iov_base = (caddr_t)&sysset;
259 259 piov[1].iov_len = sizeof (sysset);
260 260 if (writev(pctlfd, piov, 2) == -1)
261 261 perr("PIOCSEXIT");
262 262
263 263 /*
264 264 * Did we stop where we expected ?
265 265 */
266 266 if ((pstatus.pr_lwp.pr_why != PR_SYSEXIT) ||
267 267 (pstatus.pr_lwp.pr_what != SYS_execve)) {
268 268 long pflags = 0;
269 269
270 270 (void) fprintf(stderr, "Didn't catch the exec, why: %d "
271 271 "what: %d\n", pstatus.pr_lwp.pr_why,
272 272 pstatus.pr_lwp.pr_what);
273 273
274 274 oper = PCRUN;
275 275 piov[1].iov_base = (caddr_t)&pflags;
276 276 piov[1].iov_len = sizeof (pflags);
277 277 if (writev(pctlfd, piov, 2) == -1)
278 278 perr("PCRUN2");
279 279 exit(1);
280 280 }
281 281
282 282 (void) ps_init(pctlfd, pstatusfd, cpid, &proch);
283 283
284 284 if (rdb_commands) {
285 285 if ((yyin = fopen(rdb_commands, "r")) == NULL) {
286 286 (void) printf("unable to open %s for input\n",
287 287 rdb_commands);
288 288 perr("fopen");
289 289 }
290 290 } else {
291 291 proch.pp_flags |= FLG_PP_PROMPT;
292 292 rdb_prompt();
293 293 }
294 294 (void) yyparse();
295 295
296 296 if (proch.pp_flags & FLG_PP_PACT) {
297 297 long pflags = PRCFAULT;
298 298
299 299 (void) printf("\ncontinuing the hung process...\n");
300 300
301 301 pctlfd = proch.pp_ctlfd;
302 302 (void) ps_close(&proch);
303 303
304 304 oper = PCRUN;
305 305 piov[1].iov_base = (caddr_t)&pflags;
306 306 piov[1].iov_len = sizeof (pflags);
307 307 if (writev(pctlfd, piov, 2) == -1)
308 308 perr("PCRUN2");
309 309 (void) close(pctlfd);
310 310 }
311 311
312 312 return (0);
313 313 }
↓ open down ↓ |
131 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX