1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <unistd.h> 30*0Sstevel@tonic-gate #include <strings.h> 31*0Sstevel@tonic-gate #include <stdlib.h> 32*0Sstevel@tonic-gate #include <errno.h> 33*0Sstevel@tonic-gate #include <assert.h> 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <dt_impl.h> 36*0Sstevel@tonic-gate #include <dt_printf.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate dtrace_prog_t * 39*0Sstevel@tonic-gate dtrace_program_create(dtrace_hdl_t *dtp) 40*0Sstevel@tonic-gate { 41*0Sstevel@tonic-gate dtrace_prog_t *pgp = calloc(1, sizeof (dtrace_prog_t)); 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate if (pgp != NULL) 44*0Sstevel@tonic-gate dt_list_append(&dtp->dt_programs, pgp); 45*0Sstevel@tonic-gate else 46*0Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate return (pgp); 49*0Sstevel@tonic-gate } 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate void 52*0Sstevel@tonic-gate dtrace_program_destroy(dtrace_hdl_t *dtp, dtrace_prog_t *pgp) 53*0Sstevel@tonic-gate { 54*0Sstevel@tonic-gate dt_stmt_t *stp, *next; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) { 57*0Sstevel@tonic-gate next = dt_list_next(stp); 58*0Sstevel@tonic-gate dtrace_stmt_destroy(stp->ds_desc); 59*0Sstevel@tonic-gate free(stp); 60*0Sstevel@tonic-gate } 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate dt_list_delete(&dtp->dt_programs, pgp); 63*0Sstevel@tonic-gate free(pgp); 64*0Sstevel@tonic-gate } 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /*ARGSUSED*/ 67*0Sstevel@tonic-gate void 68*0Sstevel@tonic-gate dtrace_program_info(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 69*0Sstevel@tonic-gate dtrace_proginfo_t *pip) 70*0Sstevel@tonic-gate { 71*0Sstevel@tonic-gate dt_stmt_t *stp; 72*0Sstevel@tonic-gate dtrace_actdesc_t *ap; 73*0Sstevel@tonic-gate dtrace_ecbdesc_t *last = NULL; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate if (pip == NULL) 76*0Sstevel@tonic-gate return; 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate bzero(pip, sizeof (dtrace_proginfo_t)); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate if (dt_list_next(&pgp->dp_stmts) != NULL) { 81*0Sstevel@tonic-gate pip->dpi_descattr = _dtrace_maxattr; 82*0Sstevel@tonic-gate pip->dpi_stmtattr = _dtrace_maxattr; 83*0Sstevel@tonic-gate } else { 84*0Sstevel@tonic-gate pip->dpi_descattr = _dtrace_defattr; 85*0Sstevel@tonic-gate pip->dpi_stmtattr = _dtrace_defattr; 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate for (stp = dt_list_next(&pgp->dp_stmts); stp; stp = dt_list_next(stp)) { 89*0Sstevel@tonic-gate dtrace_ecbdesc_t *edp = stp->ds_desc->dtsd_ecbdesc; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate if (edp == last) 92*0Sstevel@tonic-gate continue; 93*0Sstevel@tonic-gate last = edp; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate pip->dpi_descattr = 96*0Sstevel@tonic-gate dt_attr_min(stp->ds_desc->dtsd_descattr, pip->dpi_descattr); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate pip->dpi_stmtattr = 99*0Sstevel@tonic-gate dt_attr_min(stp->ds_desc->dtsd_stmtattr, pip->dpi_stmtattr); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate /* 102*0Sstevel@tonic-gate * If there aren't any actions, account for the fact that 103*0Sstevel@tonic-gate * recording the epid will generate a record. 104*0Sstevel@tonic-gate */ 105*0Sstevel@tonic-gate if (edp->dted_action == NULL) 106*0Sstevel@tonic-gate pip->dpi_recgens++; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) { 109*0Sstevel@tonic-gate if (ap->dtad_kind == DTRACEACT_SPECULATE) { 110*0Sstevel@tonic-gate pip->dpi_speculations++; 111*0Sstevel@tonic-gate continue; 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate if (DTRACEACT_ISAGG(ap->dtad_kind)) { 115*0Sstevel@tonic-gate pip->dpi_recgens -= ap->dtad_arg; 116*0Sstevel@tonic-gate pip->dpi_aggregates++; 117*0Sstevel@tonic-gate continue; 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate if (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind)) 121*0Sstevel@tonic-gate continue; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate if (ap->dtad_kind == DTRACEACT_DIFEXPR && 124*0Sstevel@tonic-gate ap->dtad_difo->dtdo_rtype.dtdt_kind == 125*0Sstevel@tonic-gate DIF_TYPE_CTF && 126*0Sstevel@tonic-gate ap->dtad_difo->dtdo_rtype.dtdt_size == 0) 127*0Sstevel@tonic-gate continue; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate pip->dpi_recgens++; 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate int 135*0Sstevel@tonic-gate dtrace_program_exec(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 136*0Sstevel@tonic-gate dtrace_proginfo_t *pip) 137*0Sstevel@tonic-gate { 138*0Sstevel@tonic-gate void *dof; 139*0Sstevel@tonic-gate int n, err; 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate dtrace_program_info(dtp, pgp, pip); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate if ((dof = dtrace_dof_create(dtp, pgp, DTRACE_D_STRIP)) == NULL) 144*0Sstevel@tonic-gate return (-1); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate n = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof); 147*0Sstevel@tonic-gate dtrace_dof_destroy(dtp, dof); 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if (n == -1) { 150*0Sstevel@tonic-gate switch (errno) { 151*0Sstevel@tonic-gate case EINVAL: 152*0Sstevel@tonic-gate err = EDT_DIFINVAL; 153*0Sstevel@tonic-gate break; 154*0Sstevel@tonic-gate case EFAULT: 155*0Sstevel@tonic-gate err = EDT_DIFFAULT; 156*0Sstevel@tonic-gate break; 157*0Sstevel@tonic-gate case E2BIG: 158*0Sstevel@tonic-gate err = EDT_DIFSIZE; 159*0Sstevel@tonic-gate break; 160*0Sstevel@tonic-gate default: 161*0Sstevel@tonic-gate err = errno; 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate return (dt_set_errno(dtp, err)); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate if (pip != NULL) 168*0Sstevel@tonic-gate pip->dpi_matches += n; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate return (0); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate void 174*0Sstevel@tonic-gate dtrace_ecbdesc_hold(dtrace_ecbdesc_t *edp) 175*0Sstevel@tonic-gate { 176*0Sstevel@tonic-gate edp->dted_refcnt++; 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate void 180*0Sstevel@tonic-gate dtrace_ecbdesc_release(dtrace_ecbdesc_t *edp) 181*0Sstevel@tonic-gate { 182*0Sstevel@tonic-gate dtrace_difo_t *dp; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate if (--edp->dted_refcnt > 0) 185*0Sstevel@tonic-gate return; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate if ((dp = edp->dted_pred.dtpdd_difo) != NULL) 188*0Sstevel@tonic-gate dtrace_difo_release(dp); 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate assert(edp->dted_action == NULL); 191*0Sstevel@tonic-gate free(edp); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate dtrace_ecbdesc_t * 195*0Sstevel@tonic-gate dtrace_ecbdesc_create(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate dtrace_ecbdesc_t *edp; 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate if ((edp = malloc(sizeof (dtrace_ecbdesc_t))) == NULL) { 200*0Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 201*0Sstevel@tonic-gate return (NULL); 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate bzero(edp, sizeof (dtrace_ecbdesc_t)); 205*0Sstevel@tonic-gate edp->dted_probe = *pdp; 206*0Sstevel@tonic-gate dtrace_ecbdesc_hold(edp); 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate return (edp); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate dtrace_stmtdesc_t * 212*0Sstevel@tonic-gate dtrace_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp) 213*0Sstevel@tonic-gate { 214*0Sstevel@tonic-gate dtrace_stmtdesc_t *sdp; 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate if ((sdp = malloc(sizeof (dtrace_stmtdesc_t))) == NULL) { 217*0Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 218*0Sstevel@tonic-gate return (NULL); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate bzero(sdp, sizeof (dtrace_stmtdesc_t)); 222*0Sstevel@tonic-gate dtrace_ecbdesc_hold(edp); 223*0Sstevel@tonic-gate sdp->dtsd_ecbdesc = edp; 224*0Sstevel@tonic-gate sdp->dtsd_descattr = _dtrace_defattr; 225*0Sstevel@tonic-gate sdp->dtsd_stmtattr = _dtrace_defattr; 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate return (sdp); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate dtrace_actdesc_t * 231*0Sstevel@tonic-gate dtrace_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate dtrace_actdesc_t *new; 234*0Sstevel@tonic-gate dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate if ((new = malloc(sizeof (dtrace_actdesc_t))) == NULL) { 237*0Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 238*0Sstevel@tonic-gate return (NULL); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate if (sdp->dtsd_action_last != NULL) { 242*0Sstevel@tonic-gate assert(sdp->dtsd_action != NULL); 243*0Sstevel@tonic-gate assert(sdp->dtsd_action_last->dtad_next == NULL); 244*0Sstevel@tonic-gate sdp->dtsd_action_last->dtad_next = new; 245*0Sstevel@tonic-gate } else { 246*0Sstevel@tonic-gate dtrace_actdesc_t *ap = edp->dted_action; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate assert(sdp->dtsd_action == NULL); 249*0Sstevel@tonic-gate sdp->dtsd_action = new; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate while (ap != NULL && ap->dtad_next != NULL) 252*0Sstevel@tonic-gate ap = ap->dtad_next; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate if (ap == NULL) 255*0Sstevel@tonic-gate edp->dted_action = new; 256*0Sstevel@tonic-gate else 257*0Sstevel@tonic-gate ap->dtad_next = new; 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate sdp->dtsd_action_last = new; 261*0Sstevel@tonic-gate bzero(new, sizeof (dtrace_actdesc_t)); 262*0Sstevel@tonic-gate new->dtad_uarg = (uintptr_t)sdp; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate return (new); 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate int 268*0Sstevel@tonic-gate dtrace_stmt_add(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, dtrace_stmtdesc_t *sdp) 269*0Sstevel@tonic-gate { 270*0Sstevel@tonic-gate dt_stmt_t *stp = malloc(sizeof (dt_stmt_t)); 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate if (stp == NULL) 273*0Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate dt_list_append(&pgp->dp_stmts, stp); 276*0Sstevel@tonic-gate stp->ds_desc = sdp; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate return (0); 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate int 282*0Sstevel@tonic-gate dtrace_stmt_iter(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 283*0Sstevel@tonic-gate dtrace_stmt_f *func, void *data) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate dt_stmt_t *stp, *next; 286*0Sstevel@tonic-gate int status = 0; 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) { 289*0Sstevel@tonic-gate next = dt_list_next(stp); 290*0Sstevel@tonic-gate if ((status = func(dtp, pgp, stp->ds_desc, data)) != 0) 291*0Sstevel@tonic-gate break; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate return (status); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate void 298*0Sstevel@tonic-gate dtrace_stmt_destroy(dtrace_stmtdesc_t *sdp) 299*0Sstevel@tonic-gate { 300*0Sstevel@tonic-gate dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate /* 303*0Sstevel@tonic-gate * We need to remove any actions that we have on this ECB, and 304*0Sstevel@tonic-gate * remove our hold on the ECB itself. 305*0Sstevel@tonic-gate */ 306*0Sstevel@tonic-gate if (sdp->dtsd_action != NULL) { 307*0Sstevel@tonic-gate dtrace_actdesc_t *last = sdp->dtsd_action_last; 308*0Sstevel@tonic-gate dtrace_actdesc_t *ap, *next; 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate assert(last != NULL); 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) { 313*0Sstevel@tonic-gate if (ap == sdp->dtsd_action) 314*0Sstevel@tonic-gate break; 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate if (ap->dtad_next == sdp->dtsd_action) 317*0Sstevel@tonic-gate break; 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate assert(ap != NULL); 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate if (ap == edp->dted_action) { 323*0Sstevel@tonic-gate edp->dted_action = last->dtad_next; 324*0Sstevel@tonic-gate } else { 325*0Sstevel@tonic-gate ap->dtad_next = last->dtad_next; 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate /* 329*0Sstevel@tonic-gate * We have now removed our action list from its ECB; we can 330*0Sstevel@tonic-gate * safely destroy the list. 331*0Sstevel@tonic-gate */ 332*0Sstevel@tonic-gate last->dtad_next = NULL; 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate for (ap = sdp->dtsd_action; ap != NULL; ap = next) { 335*0Sstevel@tonic-gate dtrace_difo_t *dp; 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate assert(ap->dtad_uarg == (uintptr_t)sdp); 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate if ((dp = ap->dtad_difo) != NULL) 340*0Sstevel@tonic-gate dtrace_difo_release(dp); 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate next = ap->dtad_next; 343*0Sstevel@tonic-gate free(ap); 344*0Sstevel@tonic-gate } 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate if (sdp->dtsd_fmtdata != NULL) 348*0Sstevel@tonic-gate dt_printf_destroy(sdp->dtsd_fmtdata); 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate dtrace_ecbdesc_release(sdp->dtsd_ecbdesc); 351*0Sstevel@tonic-gate free(sdp); 352*0Sstevel@tonic-gate } 353