dt_dof.c

来自「Sun Solaris 10 中的 DTrace 组件的源代码。请参看: htt」· C语言 代码 · 共 1,107 行 · 第 1/3 页

C
1,107 行
字号
	dt_dprintf("allocated DOF buffer of size %lu bytes\n",	    (ulong_t)ddo.ddo_fsize);	bzero(ddo.ddo_buf, ddo.ddo_fsize);	hp = (dof_hdr_t *)(uintptr_t)ddo.ddo_buf;	secbase = (dof_sec_t *)((uintptr_t)ddo.ddo_buf + sizeof (dof_hdr_t));	ddo.ddo_secp = secbase;	ddo.ddo_lptr = (uchar_t *)(secbase + ddo.ddo_secs);	if (dtrace_dof_hdr(dtp, hp) != 0) {		free(ddo.ddo_buf);		return (NULL);	}	hp->dofh_secnum = ddo.ddo_secs;	hp->dofh_loadsz = ddo.ddo_lsize;	hp->dofh_filesz = ddo.ddo_fsize;	if (ddo.ddo_usize != 0) {		ddo.ddo_uptr = (uchar_t *)		    ddo.ddo_buf + roundup(ddo.ddo_lsize, 64);	} else {		ddo.ddo_uptr = (uchar_t *)		    ddo.ddo_buf + ddo.ddo_lsize;	}	/*	 * The program-level string table is the last section and sits at the	 * end of our data buffer.  Set up pointers and indices for it.	 */	strsec = ddo.ddo_secs - 1;	strtab = (char *)ddo.ddo_buf + stroff;	strp = strtab + 1; /* initial byte is '\0' */	/*	 * Allocate an array of section indices for tracking DIFOHDR sections	 * for each ECBDESC.  Reset ddo_secs to zero so we can keep track of	 * section header indices as we fill in each section's header and data.	 */	actsecs = alloca(sizeof (dof_secidx_t) * maxacts);	ddo.ddo_secs = 0;	/*	 * Now iterate through the statements again, this time copying the	 * actual data into our buffer and filling in the section headers.	 */	stp = dt_list_next(&pgp->dp_stmts);	last = NULL;	for (; stp != NULL; stp = dt_list_next(stp)) {		dof_secidx_t probesec = DOF_SECIDX_NONE;		dof_secidx_t prdsec = DOF_SECIDX_NONE;		dof_secidx_t actsec = DOF_SECIDX_NONE;		dtrace_stmtdesc_t *sdp = stp->ds_desc;		dof_probedesc_t *dofp;		dof_ecbdesc_t *dofe;		dof_actdesc_t *dofa;		uint_t nacts = 0;		uint_t nactsecs = 0;		if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)			continue; /* same ecb as previous statement */		pdp = &edp->dted_probe;		last = edp;		/*		 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,		 * and copy each of the probe description strings into the		 * program-level string table we previously set up.		 */		ddo.ddo_lptr = (uchar_t *)		    roundup((uintptr_t)ddo.ddo_lptr, sizeof (dof_secidx_t));		ddo.ddo_secp->dofs_type = DOF_SECT_PROBEDESC;		ddo.ddo_secp->dofs_align = sizeof (dof_secidx_t);		ddo.ddo_secp->dofs_flags = DOF_SECF_LOAD;		ddo.ddo_secp->dofs_entsize = sizeof (dof_probedesc_t);		ddo.ddo_secp->dofs_offset = (size_t)		    (ddo.ddo_lptr - ddo.ddo_buf);		ddo.ddo_secp->dofs_size = sizeof (dof_probedesc_t);		probesec = ddo.ddo_secs++;		ddo.ddo_secp++;		dofp = (dof_probedesc_t *)(uintptr_t)ddo.ddo_lptr;		dofp->dofp_strtab = strsec;		if (pdp->dtpd_provider[0] != '\0') {			dofp->dofp_provider = (dof_stridx_t)(strp - strtab);			(void) strcpy(strp, pdp->dtpd_provider);			strp += strlen(strp) + 1;		} else			dofp->dofp_provider = 0;		if (pdp->dtpd_mod[0] != '\0') {			dofp->dofp_mod = (dof_stridx_t)(strp - strtab);			(void) strcpy(strp, pdp->dtpd_mod);			strp += strlen(strp) + 1;		} else			dofp->dofp_mod = 0;		if (pdp->dtpd_func[0] != '\0') {			dofp->dofp_func = (dof_stridx_t)(strp - strtab);			(void) strcpy(strp, pdp->dtpd_func);			strp += strlen(strp) + 1;		} else			dofp->dofp_func = 0;		if (pdp->dtpd_name[0] != '\0') {			dofp->dofp_name = (dof_stridx_t)(strp - strtab);			(void) strcpy(strp, pdp->dtpd_name);			strp += strlen(strp) + 1;		} else			dofp->dofp_name = 0;		ddo.ddo_lptr += sizeof (dof_probedesc_t);		/*		 * If there is a predicate DIFO associated with the ecbdesc,		 * write out the DIFO sections and save the DIFO section index.		 */		if (edp->dted_pred.dtpdd_difo != NULL)			prdsec = dof_difo_save(&ddo, edp->dted_pred.dtpdd_difo);		/*		 * Iterate through the action list generating sections for each		 * DIFO referenced by an actdesc.  We accumulate the indices of		 * the corresponding DOF_SECT_DIFOHDR sections in actsecs[].		 */		for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {			if (ap->dtad_difo != NULL) {				actsecs[nactsecs++] =				    dof_difo_save(&ddo, ap->dtad_difo);			}			nacts++;		}		if (nacts != 0) {			/*			 * Iterate through the action list again filling in the			 * actual DOF_SECT_ACTDESC.  This section is an array			 * of descriptions where each one is filled in by			 * copying the relevant fields of the dtrace_actdesc_t,			 * except for the DIFO, for which we grab the saved			 * section index of the DOF_SECT_DIFOHDR that we saved			 * in actsecs[] from the action loop above.			 */			ddo.ddo_lptr = (uchar_t *)			    roundup((uintptr_t)ddo.ddo_lptr, sizeof (uint64_t));			ddo.ddo_secp->dofs_type = DOF_SECT_ACTDESC;			ddo.ddo_secp->dofs_align = sizeof (uint64_t);			ddo.ddo_secp->dofs_flags = DOF_SECF_LOAD;			ddo.ddo_secp->dofs_entsize = sizeof (dof_actdesc_t);			ddo.ddo_secp->dofs_offset =			    (size_t)(ddo.ddo_lptr - ddo.ddo_buf);			ddo.ddo_secp->dofs_size =			    sizeof (dof_actdesc_t) * nacts;			actsec = ddo.ddo_secs++;			ddo.ddo_secp++;			dofa = (dof_actdesc_t *)(uintptr_t)ddo.ddo_lptr;			nactsecs = 0;			next = stp;			strndx = 0;			for (ap = edp->dted_action; ap != NULL;			    ap = ap->dtad_next) {				if (ap->dtad_difo != NULL)					dofa->dofa_difo = actsecs[nactsecs++];				else					dofa->dofa_difo = DOF_SECIDX_NONE;				if (sdp != NULL && ap == sdp->dtsd_action) {					void *fmt = sdp->dtsd_fmtdata;					size_t len;					/*					 * This is the first action in a					 * statement.  If it has format data,					 * that data must be formatted and					 * printed to the string section.					 */					if (fmt != NULL) {						len = dtrace_printf_format(dtp,						    fmt, NULL, 0);						(void) dtrace_printf_format(dtp,						    fmt, strp, len + 1);						strndx = (dof_stridx_t)						    (strp - strtab);						strp += len + 1;						dofa->dofa_arg = strndx;					} else {						strndx = 0;					}					if ((next = dt_list_next(next)) != NULL)						sdp = next->ds_desc;					else						sdp = NULL;				}				if (strndx != 0) {					dofa->dofa_arg = strndx;					dofa->dofa_strtab = strsec;				} else {					dofa->dofa_arg = ap->dtad_arg;					dofa->dofa_strtab = DOF_SECIDX_NONE;				}				dofa->dofa_kind = ap->dtad_kind;				dofa->dofa_ntuple = ap->dtad_ntuple;				dofa->dofa_uarg = ap->dtad_uarg;				dofa++;			}			ddo.ddo_lptr += sizeof (dof_actdesc_t) * nacts;		}		/*		 * Now finally, add the DOF_SECT_ECBDESC referencing all the		 * previously created sub-sections.		 */		ddo.ddo_lptr = (uchar_t *)		    roundup((uintptr_t)ddo.ddo_lptr, sizeof (uint64_t));		ddo.ddo_secp->dofs_type = DOF_SECT_ECBDESC;		ddo.ddo_secp->dofs_align = sizeof (uint64_t);		ddo.ddo_secp->dofs_flags = DOF_SECF_LOAD;		ddo.ddo_secp->dofs_entsize = 0;		ddo.ddo_secp->dofs_offset = (size_t)		    (ddo.ddo_lptr - ddo.ddo_buf);		ddo.ddo_secp->dofs_size = sizeof (dof_ecbdesc_t);		ddo.ddo_secs++;		ddo.ddo_secp++;		dofe = (dof_ecbdesc_t *)(uintptr_t)ddo.ddo_lptr;		dofe->dofe_probes = probesec;		dofe->dofe_pred = prdsec;		dofe->dofe_actions = actsec;		dofe->dofe_pad = 0;		dofe->dofe_uarg = edp->dted_uarg;		ddo.ddo_lptr += sizeof (dof_ecbdesc_t);	}	if (flags & DTRACE_D_PROBES) {		uint_t h;		dt_provider_t *pvp;		for (h = 0; h < dtp->dt_provbuckets; h++) {			for (pvp = dtp->dt_provs[h]; pvp != NULL;			    pvp = pvp->pv_next) {				if (dof_provider_save(dtp, &ddo, pvp) != 0) {					free(ddo.ddo_buf);					return (NULL);				}			}		}	}	assert((size_t)(ddo.ddo_lptr - ddo.ddo_buf) == stroff);	ddo.ddo_secp->dofs_type = DOF_SECT_STRTAB;	ddo.ddo_secp->dofs_align = sizeof (char);	ddo.ddo_secp->dofs_flags = DOF_SECF_LOAD;	ddo.ddo_secp->dofs_entsize = 0;	ddo.ddo_secp->dofs_offset = stroff;	ddo.ddo_secp->dofs_size = ddo.ddo_strs + 1;	ddo.ddo_secs++;	ddo.ddo_secp++;	ddo.ddo_lptr += ddo.ddo_strs + 1;	assert(ddo.ddo_lptr <= ddo.ddo_buf + hp->dofh_loadsz);	assert(ddo.ddo_uptr <= ddo.ddo_buf + hp->dofh_filesz);	assert(ddo.ddo_secs == hp->dofh_secnum);	assert(ddo.ddo_secp == secbase + hp->dofh_secnum);	return (ddo.ddo_buf);}void *dtrace_getopt_dof(dtrace_hdl_t *dtp){	dof_hdr_t *dof;	dof_sec_t *sec;	dof_optdesc_t *dofo;	int i, nopts = 0, len = sizeof (dof_hdr_t) +	    roundup(sizeof (dof_sec_t), sizeof (uint64_t));	for (i = 0; i < DTRACEOPT_MAX; i++) {		if (dtp->dt_options[i] != DTRACEOPT_UNSET)			nopts++;	}	len += sizeof (dof_optdesc_t) * nopts;	if ((dof = malloc(len)) == NULL) {		(void) dt_set_errno(dtp, EDT_NOMEM);		return (NULL);	}	bzero(dof, len);	if (dtrace_dof_hdr(dtp, dof) != 0) {		free(dof);		return (NULL);	}	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */	dof->dofh_loadsz = len;	dof->dofh_filesz = len;	/*	 * Fill in the option section header...	 */	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));	sec->dofs_type = DOF_SECT_OPTDESC;	sec->dofs_align = sizeof (uint64_t);	sec->dofs_flags = DOF_SECF_LOAD;	sec->dofs_entsize = sizeof (dof_optdesc_t);	dofo = (dof_optdesc_t *)((uintptr_t)sec +	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));	sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;	sec->dofs_size = sizeof (dof_optdesc_t) * nopts;	for (i = 0; i < DTRACEOPT_MAX; i++) {		if (dtp->dt_options[i] == DTRACEOPT_UNSET)			continue;		dofo->dofo_option = i;		dofo->dofo_strtab = DOF_SECIDX_NONE;		dofo->dofo_value = dtp->dt_options[i];		dofo++;	}	return (dof);}void *dtrace_geterr_dof(dtrace_hdl_t *dtp){	if (dtp->dt_errprog != NULL)		return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));	(void) dt_set_errno(dtp, EDT_BADERROR);	return (NULL);}voiddtrace_dof_destroy(void *dof){	free(dof);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?