int main( void ) { int err; dtrace_hdl_t * dh = dtrace_open(DTRACE_VERSION, 0, &err); if (dh == NULL) { printf("Cannot open dtrace library: %s\n", dtrace_errmsg(dh, err)); return -1; } // Set a few necessary options dtrace_setopt(dh, "strsize", "4096"); dtrace_setopt(dh, "bufsize", "4m"); // Compile my program const char * script = "syscall::*exit*:entry { printf(\"%s %d\\n\", execname, curpsinfo->pr_pid); }"; dtrace_prog_t *prog = dtrace_program_strcompile(dh, script, DTRACE_PROBESPEC_NAME, 0, 0, NULL); if( prog == NULL ) { printf("Cannot compile program: %s\n", dtrace_errmsg(dh, err)); return -1; } // Add buffered output handler, as this is the only way I can get data out of Dtrace? if (dtrace_handle_buffered(dh, dtrace_dcmdbuffered, NULL) == -1) { printf("Couldn't add buffered handler"); return -1; } dtrace_proginfo_t info; if( dtrace_program_exec(dh, prog, &info) == -1 ) { printf("Cannot exec program: %s\n", dtrace_errmsg(dh, err)); return -1; } if( dtrace_go(dh) == -1 ) { printf("Cannot go: %s\n", dtrace_errmsg(NULL, err)); return -1; } while(1) { dtrace_sleep(dh); switch (dtrace_work(dh, NULL, chew, chewrec, NULL)) { case DTRACE_WORKSTATUS_DONE: if (dtrace_stop(dh) == -1) printf("couldn't stop tracing"); break; break; case DTRACE_WORKSTATUS_OKAY: break; case DTRACE_WORKSTATUS_ERROR: printf("WARNING: DTRACE_WORKSTATUS_ERROR!\n"); break; default: printf("Unknown return value from dtrace_work()\n"); } } dtrace_close(dh); return 0; }
static void compile_str(dtrace_cmd_t *dcp) { char *p; if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg, dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL) dfatal("invalid probe specifier %s", dcp->dc_arg); if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL) *p = '\0'; /* crop name for reporting */ dcp->dc_desc = "description"; dcp->dc_name = dcp->dc_arg; }
static void dprog_compile(void) { dtrace_prog_t *prog; dtrace_proginfo_t info; if (g_opt_V) { (void) fprintf(stderr, "%s: vvvv D program vvvv\n", g_pname); (void) fputs(g_prog, stderr); (void) fprintf(stderr, "%s: ^^^^ D program ^^^^\n", g_pname); } if ((prog = dtrace_program_strcompile(g_dtp, g_prog, DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL) dfatal("failed to compile program"); if (dtrace_program_exec(g_dtp, prog, &info) == -1) dfatal("failed to enable probes"); }
int dtrace_handle_err(dtrace_hdl_t *dtp, dtrace_handle_err_f *hdlr, void *arg) { dtrace_prog_t *pgp = NULL; dt_stmt_t *stp; dtrace_ecbdesc_t *edp; /* * We don't currently support multiple error handlers. */ if (dtp->dt_errhdlr != NULL) return (dt_set_errno(dtp, EALREADY)); /* * If the DTRACEOPT_GRABANON is enabled, the anonymous enabling will * already have a dtrace:::ERROR probe enabled; save 'hdlr' and 'arg' * but do not bother compiling and enabling _dt_errprog. */ if (dtp->dt_options[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) goto out; if ((pgp = dtrace_program_strcompile(dtp, _dt_errprog, DTRACE_PROBESPEC_NAME, DTRACE_C_ZDEFS, 0, NULL)) == NULL) return (dt_set_errno(dtp, dtrace_errno(dtp))); stp = dt_list_next(&pgp->dp_stmts); assert(stp != NULL); edp = stp->ds_desc->dtsd_ecbdesc; assert(edp != NULL); edp->dted_uarg = DT_ECB_ERROR; out: dtp->dt_errhdlr = hdlr; dtp->dt_errarg = arg; dtp->dt_errprog = pgp; return (0); }