Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/intel/dtrace/sdt.c
+++ new/usr/src/uts/intel/dtrace/sdt.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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 28 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
29 29 */
30 30
31 31 #include <sys/modctl.h>
32 32 #include <sys/sunddi.h>
33 33 #include <sys/dtrace.h>
34 34 #include <sys/kobj.h>
35 35 #include <sys/stat.h>
36 36 #include <sys/conf.h>
37 37 #include <vm/seg_kmem.h>
38 38 #include <sys/stack.h>
39 39 #include <sys/frame.h>
40 40 #include <sys/dtrace_impl.h>
41 41 #include <sys/cmn_err.h>
42 42 #include <sys/sysmacros.h>
43 43 #include <sys/privregs.h>
44 44 #include <sys/sdt_impl.h>
45 45
46 46 #define SDT_PATCHVAL 0xf0
47 47 #define SDT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & sdt_probetab_mask)
48 48 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
49 49
50 50 static dev_info_t *sdt_devi;
51 51 static int sdt_verbose = 0;
52 52 static sdt_probe_t **sdt_probetab;
53 53 static int sdt_probetab_size;
54 54 static int sdt_probetab_mask;
55 55
56 56 /*ARGSUSED*/
57 57 static int
58 58 sdt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
59 59 {
60 60 uintptr_t stack0, stack1, stack2, stack3, stack4;
61 61 int i = 0;
62 62 sdt_probe_t *sdt = sdt_probetab[SDT_ADDR2NDX(addr)];
63 63
64 64 #ifdef __amd64
65 65 /*
66 66 * On amd64, stack[0] contains the dereferenced stack pointer,
67 67 * stack[1] contains savfp, stack[2] contains savpc. We want
68 68 * to step over these entries.
69 69 */
70 70 i += 3;
71 71 #endif
72 72
73 73 for (; sdt != NULL; sdt = sdt->sdp_hashnext) {
74 74 if ((uintptr_t)sdt->sdp_patchpoint == addr) {
75 75 /*
76 76 * When accessing the arguments on the stack, we must
77 77 * protect against accessing beyond the stack. We can
78 78 * safely set NOFAULT here -- we know that interrupts
79 79 * are already disabled.
80 80 */
81 81 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
82 82 stack0 = stack[i++];
83 83 stack1 = stack[i++];
84 84 stack2 = stack[i++];
85 85 stack3 = stack[i++];
86 86 stack4 = stack[i++];
87 87 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
88 88 CPU_DTRACE_BADADDR);
89 89
90 90 dtrace_probe(sdt->sdp_id, stack0, stack1,
91 91 stack2, stack3, stack4);
92 92
93 93 return (DTRACE_INVOP_NOP);
94 94 }
95 95 }
96 96
97 97 return (0);
98 98 }
99 99
100 100 /*ARGSUSED*/
101 101 static void
102 102 sdt_provide_module(void *arg, struct modctl *ctl)
103 103 {
104 104 struct module *mp = ctl->mod_mp;
105 105 char *modname = ctl->mod_modname;
106 106 sdt_probedesc_t *sdpd;
107 107 sdt_probe_t *sdp, *old;
108 108 sdt_provider_t *prov;
109 109 int len;
110 110
111 111 /*
112 112 * One for all, and all for one: if we haven't yet registered all of
113 113 * our providers, we'll refuse to provide anything.
114 114 */
115 115 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
116 116 if (prov->sdtp_id == DTRACE_PROVNONE)
117 117 return;
118 118 }
119 119
120 120 if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
121 121 return;
122 122
123 123 for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
124 124 char *name = sdpd->sdpd_name, *func, *nname;
125 125 int i, j;
126 126 sdt_provider_t *prov;
127 127 ulong_t offs;
128 128 dtrace_id_t id;
129 129
130 130 for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
131 131 char *prefix = prov->sdtp_prefix;
132 132
133 133 if (strncmp(name, prefix, strlen(prefix)) == 0) {
134 134 name += strlen(prefix);
135 135 break;
136 136 }
137 137 }
138 138
139 139 nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
140 140
141 141 for (i = 0, j = 0; name[j] != '\0'; i++) {
142 142 if (name[j] == '_' && name[j + 1] == '_') {
143 143 nname[i] = '-';
144 144 j += 2;
145 145 } else {
146 146 nname[i] = name[j++];
147 147 }
148 148 }
149 149
150 150 nname[i] = '\0';
151 151
152 152 sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
153 153 sdp->sdp_loadcnt = ctl->mod_loadcnt;
154 154 sdp->sdp_ctl = ctl;
155 155 sdp->sdp_name = nname;
156 156 sdp->sdp_namelen = len;
157 157 sdp->sdp_provider = prov;
158 158
159 159 func = kobj_searchsym(mp, sdpd->sdpd_offset, &offs);
160 160
161 161 if (func == NULL)
162 162 func = "<unknown>";
163 163
164 164 /*
165 165 * We have our provider. Now create the probe.
166 166 */
167 167 if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
168 168 func, nname)) != DTRACE_IDNONE) {
169 169 old = dtrace_probe_arg(prov->sdtp_id, id);
170 170 ASSERT(old != NULL);
171 171
172 172 sdp->sdp_next = old->sdp_next;
173 173 sdp->sdp_id = id;
174 174 old->sdp_next = sdp;
175 175 } else {
176 176 sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
177 177 modname, func, nname, 3, sdp);
178 178
179 179 mp->sdt_nprobes++;
180 180 }
181 181
182 182 sdp->sdp_hashnext =
183 183 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
184 184 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
185 185
186 186 sdp->sdp_patchval = SDT_PATCHVAL;
187 187 sdp->sdp_patchpoint = (uint8_t *)sdpd->sdpd_offset;
188 188 sdp->sdp_savedval = *sdp->sdp_patchpoint;
189 189 }
190 190 }
191 191
192 192 /*ARGSUSED*/
193 193 static void
194 194 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
195 195 {
196 196 sdt_probe_t *sdp = parg, *old, *last, *hash;
197 197 struct modctl *ctl = sdp->sdp_ctl;
198 198 int ndx;
199 199
200 200 if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
201 201 if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
202 202 ctl->mod_loaded)) {
203 203 ((struct module *)(ctl->mod_mp))->sdt_nprobes--;
204 204 }
205 205 }
206 206
207 207 while (sdp != NULL) {
208 208 old = sdp;
209 209
210 210 /*
211 211 * Now we need to remove this probe from the sdt_probetab.
212 212 */
213 213 ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
214 214 last = NULL;
215 215 hash = sdt_probetab[ndx];
216 216
217 217 while (hash != sdp) {
218 218 ASSERT(hash != NULL);
219 219 last = hash;
220 220 hash = hash->sdp_hashnext;
221 221 }
222 222
223 223 if (last != NULL) {
224 224 last->sdp_hashnext = sdp->sdp_hashnext;
225 225 } else {
226 226 sdt_probetab[ndx] = sdp->sdp_hashnext;
227 227 }
228 228
229 229 kmem_free(sdp->sdp_name, sdp->sdp_namelen);
230 230 sdp = sdp->sdp_next;
231 231 kmem_free(old, sizeof (sdt_probe_t));
232 232 }
233 233 }
234 234
235 235 /*ARGSUSED*/
236 236 static int
237 237 sdt_enable(void *arg, dtrace_id_t id, void *parg)
238 238 {
239 239 sdt_probe_t *sdp = parg;
240 240 struct modctl *ctl = sdp->sdp_ctl;
241 241
242 242 ctl->mod_nenabled++;
243 243
244 244 /*
245 245 * If this module has disappeared since we discovered its probes,
246 246 * refuse to enable it.
247 247 */
248 248 if (!ctl->mod_loaded) {
249 249 if (sdt_verbose) {
250 250 cmn_err(CE_NOTE, "sdt is failing for probe %s "
251 251 "(module %s unloaded)",
252 252 sdp->sdp_name, ctl->mod_modname);
253 253 }
254 254 goto err;
255 255 }
256 256
257 257 /*
258 258 * Now check that our modctl has the expected load count. If it
259 259 * doesn't, this module must have been unloaded and reloaded -- and
260 260 * we're not going to touch it.
261 261 */
262 262 if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
263 263 if (sdt_verbose) {
264 264 cmn_err(CE_NOTE, "sdt is failing for probe %s "
265 265 "(module %s reloaded)",
266 266 sdp->sdp_name, ctl->mod_modname);
267 267 }
268 268 goto err;
269 269 }
270 270
271 271 while (sdp != NULL) {
272 272 *sdp->sdp_patchpoint = sdp->sdp_patchval;
273 273 sdp = sdp->sdp_next;
274 274 }
275 275 err:
276 276 return (0);
277 277 }
278 278
279 279 /*ARGSUSED*/
280 280 static void
281 281 sdt_disable(void *arg, dtrace_id_t id, void *parg)
282 282 {
283 283 sdt_probe_t *sdp = parg;
284 284 struct modctl *ctl = sdp->sdp_ctl;
285 285
286 286 ctl->mod_nenabled--;
287 287
288 288 if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt)
289 289 goto err;
290 290
291 291 while (sdp != NULL) {
292 292 *sdp->sdp_patchpoint = sdp->sdp_savedval;
293 293 sdp = sdp->sdp_next;
294 294 }
295 295
296 296 err:
297 297 ;
298 298 }
299 299
300 300 /*ARGSUSED*/
301 301 uint64_t
302 302 sdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
303 303 {
304 304 uintptr_t val;
305 305 struct frame *fp = (struct frame *)dtrace_getfp();
306 306 uintptr_t *stack;
307 307 int i;
308 308 #if defined(__amd64)
309 309 /*
310 310 * A total of 6 arguments are passed via registers; any argument with
311 311 * index of 5 or lower is therefore in a register.
312 312 */
313 313 int inreg = 5;
314 314 #endif
315 315
316 316 for (i = 1; i <= aframes; i++) {
317 317 fp = (struct frame *)(fp->fr_savfp);
318 318
319 319 if (fp->fr_savpc == (pc_t)dtrace_invop_callsite) {
320 320 #if !defined(__amd64)
321 321 /*
322 322 * If we pass through the invalid op handler, we will
323 323 * use the pointer that it passed to the stack as the
324 324 * second argument to dtrace_invop() as the pointer to
325 325 * the stack.
326 326 */
327 327 stack = ((uintptr_t **)&fp[1])[1];
328 328 #else
329 329 /*
330 330 * In the case of amd64, we will use the pointer to the
331 331 * regs structure that was pushed when we took the
332 332 * trap. To get this structure, we must increment
333 333 * beyond the frame structure, the calling RIP, and
334 334 * padding stored in dtrace_invop(). If the argument
335 335 * that we're seeking is passed on the stack, we'll
336 336 * pull the true stack pointer out of the saved
337 337 * registers and decrement our argument by the number
338 338 * of arguments passed in registers; if the argument
339 339 * we're seeking is passed in regsiters, we can just
340 340 * load it directly.
341 341 */
342 342 struct regs *rp = (struct regs *)((uintptr_t)&fp[1] +
343 343 sizeof (uintptr_t) * 2);
344 344
345 345 if (argno <= inreg) {
346 346 stack = (uintptr_t *)&rp->r_rdi;
347 347 } else {
348 348 stack = (uintptr_t *)(rp->r_rsp);
349 349 argno -= (inreg + 1);
350 350 }
351 351 #endif
352 352 goto load;
353 353 }
354 354 }
355 355
356 356 /*
357 357 * We know that we did not come through a trap to get into
358 358 * dtrace_probe() -- the provider simply called dtrace_probe()
359 359 * directly. As this is the case, we need to shift the argument
360 360 * that we're looking for: the probe ID is the first argument to
361 361 * dtrace_probe(), so the argument n will actually be found where
362 362 * one would expect to find argument (n + 1).
363 363 */
364 364 argno++;
365 365
366 366 #if defined(__amd64)
367 367 if (argno <= inreg) {
368 368 /*
369 369 * This shouldn't happen. If the argument is passed in a
370 370 * register then it should have been, well, passed in a
371 371 * register...
372 372 */
373 373 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
374 374 return (0);
375 375 }
376 376
377 377 argno -= (inreg + 1);
378 378 #endif
379 379 stack = (uintptr_t *)&fp[1];
380 380
381 381 load:
382 382 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
383 383 val = stack[argno];
384 384 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
385 385
386 386 return (val);
387 387 }
388 388
389 389 static dtrace_pops_t sdt_pops = {
390 390 NULL,
391 391 sdt_provide_module,
392 392 sdt_enable,
393 393 sdt_disable,
394 394 NULL,
395 395 NULL,
396 396 sdt_getargdesc,
397 397 sdt_getarg,
398 398 NULL,
399 399 sdt_destroy
400 400 };
401 401
402 402 /*ARGSUSED*/
403 403 static int
404 404 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
405 405 {
406 406 sdt_provider_t *prov;
407 407
408 408 if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
409 409 0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
410 410 cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
411 411 ddi_remove_minor_node(devi, NULL);
412 412 return (DDI_FAILURE);
413 413 }
414 414
415 415 ddi_report_dev(devi);
416 416 sdt_devi = devi;
417 417
418 418 if (sdt_probetab_size == 0)
419 419 sdt_probetab_size = SDT_PROBETAB_SIZE;
420 420
421 421 sdt_probetab_mask = sdt_probetab_size - 1;
422 422 sdt_probetab =
423 423 kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
424 424 dtrace_invop_add(sdt_invop);
425 425
426 426 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
427 427 uint32_t priv;
428 428
429 429 if (prov->sdtp_priv == DTRACE_PRIV_NONE) {
430 430 priv = DTRACE_PRIV_KERNEL;
431 431 sdt_pops.dtps_mode = NULL;
432 432 } else {
433 433 priv = prov->sdtp_priv;
434 434 ASSERT(priv == DTRACE_PRIV_USER);
435 435 sdt_pops.dtps_mode = sdt_mode;
436 436 }
437 437
438 438 if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
439 439 priv, NULL, &sdt_pops, prov, &prov->sdtp_id) != 0) {
440 440 cmn_err(CE_WARN, "failed to register sdt provider %s",
441 441 prov->sdtp_name);
442 442 }
443 443 }
444 444
445 445 return (DDI_SUCCESS);
446 446 }
447 447
448 448 /*ARGSUSED*/
449 449 static int
450 450 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
451 451 {
452 452 sdt_provider_t *prov;
453 453
454 454 switch (cmd) {
455 455 case DDI_DETACH:
456 456 break;
457 457
458 458 case DDI_SUSPEND:
459 459 return (DDI_SUCCESS);
460 460
461 461 default:
462 462 return (DDI_FAILURE);
463 463 }
464 464
465 465 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
466 466 if (prov->sdtp_id != DTRACE_PROVNONE) {
467 467 if (dtrace_unregister(prov->sdtp_id) != 0)
468 468 return (DDI_FAILURE);
469 469
470 470 prov->sdtp_id = DTRACE_PROVNONE;
471 471 }
472 472 }
473 473
474 474 dtrace_invop_remove(sdt_invop);
475 475 kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
476 476
477 477 return (DDI_SUCCESS);
478 478 }
479 479
480 480 /*ARGSUSED*/
481 481 static int
482 482 sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
483 483 {
484 484 int error;
485 485
486 486 switch (infocmd) {
487 487 case DDI_INFO_DEVT2DEVINFO:
488 488 *result = (void *)sdt_devi;
489 489 error = DDI_SUCCESS;
490 490 break;
491 491 case DDI_INFO_DEVT2INSTANCE:
492 492 *result = (void *)0;
493 493 error = DDI_SUCCESS;
494 494 break;
495 495 default:
496 496 error = DDI_FAILURE;
497 497 }
498 498 return (error);
499 499 }
500 500
501 501 /*ARGSUSED*/
502 502 static int
503 503 sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
504 504 {
505 505 return (0);
506 506 }
507 507
508 508 static struct cb_ops sdt_cb_ops = {
509 509 sdt_open, /* open */
510 510 nodev, /* close */
511 511 nulldev, /* strategy */
512 512 nulldev, /* print */
513 513 nodev, /* dump */
514 514 nodev, /* read */
515 515 nodev, /* write */
516 516 nodev, /* ioctl */
517 517 nodev, /* devmap */
518 518 nodev, /* mmap */
519 519 nodev, /* segmap */
520 520 nochpoll, /* poll */
521 521 ddi_prop_op, /* cb_prop_op */
522 522 0, /* streamtab */
523 523 D_NEW | D_MP /* Driver compatibility flag */
524 524 };
525 525
526 526 static struct dev_ops sdt_ops = {
527 527 DEVO_REV, /* devo_rev, */
528 528 0, /* refcnt */
529 529 sdt_info, /* get_dev_info */
530 530 nulldev, /* identify */
531 531 nulldev, /* probe */
532 532 sdt_attach, /* attach */
533 533 sdt_detach, /* detach */
534 534 nodev, /* reset */
535 535 &sdt_cb_ops, /* driver operations */
536 536 NULL, /* bus operations */
537 537 nodev, /* dev power */
538 538 ddi_quiesce_not_needed, /* quiesce */
539 539 };
540 540
541 541 /*
↓ open down ↓ |
541 lines elided |
↑ open up ↑ |
542 542 * Module linkage information for the kernel.
543 543 */
544 544 static struct modldrv modldrv = {
545 545 &mod_driverops, /* module type (this is a pseudo driver) */
546 546 "Statically Defined Tracing", /* name of module */
547 547 &sdt_ops, /* driver ops */
548 548 };
549 549
550 550 static struct modlinkage modlinkage = {
551 551 MODREV_1,
552 - (void *)&modldrv,
553 - NULL
552 + { (void *)&modldrv, NULL }
554 553 };
555 554
556 555 int
557 556 _init(void)
558 557 {
559 558 return (mod_install(&modlinkage));
560 559 }
561 560
562 561 int
563 562 _info(struct modinfo *modinfop)
564 563 {
565 564 return (mod_info(&modlinkage, modinfop));
566 565 }
567 566
568 567 int
569 568 _fini(void)
570 569 {
571 570 return (mod_remove(&modlinkage));
572 571 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX