xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_pcb.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * DTrace Parsing Control Block
29  *
30  * A DTrace Parsing Control Block (PCB) contains all of the state that is used
31  * by a single pass of the D compiler, other than the global variables used by
32  * lex and yacc.  The routines in this file are used to set up and tear down
33  * PCBs, which are kept on a stack pointed to by the libdtrace global 'yypcb'.
34  * The main engine of the compiler, dt_compile(), is located in dt_cc.c and is
35  * responsible for calling these routines to begin and end a compilation pass.
36  *
37  * Sun's lex/yacc are not MT-safe or re-entrant, but we permit limited nested
38  * use of dt_compile() once the entire parse tree has been constructed but has
39  * not yet executed the "cooking" pass (see dt_cc.c for more information).  The
40  * PCB design also makes it easier to debug (since all global state is kept in
41  * one place) and could permit us to make the D compiler MT-safe or re-entrant
42  * in the future by adding locks to libdtrace or switching to Flex and Bison.
43  */
44 
45 #include <strings.h>
46 #include <stdlib.h>
47 #include <assert.h>
48 
49 #include <dt_impl.h>
50 #include <dt_program.h>
51 #include <dt_provider.h>
52 #include <dt_pcb.h>
53 
54 /*
55  * Initialize the specified PCB by zeroing it and filling in a few default
56  * members, and then pushing it on to the top of the PCB stack and setting
57  * yypcb to point to it.  Increment the current handle's generation count.
58  */
59 void
60 dt_pcb_push(dtrace_hdl_t *dtp, dt_pcb_t *pcb)
61 {
62 	/*
63 	 * Since lex/yacc are not re-entrant and we don't implement state save,
64 	 * assert that if another PCB is active, it is from the same handle and
65 	 * has completed execution of yyparse().  If the first assertion fires,
66 	 * the caller is calling libdtrace without proper MT locking.  If the
67 	 * second assertion fires, dt_compile() is being called recursively
68 	 * from an illegal location in libdtrace, or a dt_pcb_pop() is missing.
69 	 */
70 	if (yypcb != NULL) {
71 		assert(yypcb->pcb_hdl == dtp);
72 		assert(yypcb->pcb_yystate == YYS_DONE);
73 	}
74 
75 	bzero(pcb, sizeof (dt_pcb_t));
76 
77 	dt_scope_create(&pcb->pcb_dstack);
78 	dt_idstack_push(&pcb->pcb_globals, dtp->dt_globals);
79 	dt_irlist_create(&pcb->pcb_ir);
80 
81 	pcb->pcb_hdl = dtp;
82 	pcb->pcb_prev = dtp->dt_pcb;
83 
84 	dtp->dt_pcb = pcb;
85 	dtp->dt_gen++;
86 
87 	yyinit(pcb);
88 }
89 
90 static int
91 dt_pcb_pop_ident(dt_idhash_t *dhp, dt_ident_t *idp, void *arg)
92 {
93 	dtrace_hdl_t *dtp = arg;
94 
95 	if (idp->di_gen == dtp->dt_gen)
96 		dt_idhash_delete(dhp, idp);
97 
98 	return (0);
99 }
100 
101 /*
102  * Pop the topmost PCB from the PCB stack and destroy any data structures that
103  * are associated with it.  If 'err' is non-zero, destroy any intermediate
104  * state that is left behind as part of a compilation that has failed.
105  */
106 void
107 dt_pcb_pop(dtrace_hdl_t *dtp, int err)
108 {
109 	dt_pcb_t *pcb = yypcb;
110 	uint_t i;
111 
112 	assert(pcb != NULL);
113 	assert(pcb == dtp->dt_pcb);
114 
115 	while (pcb->pcb_dstack.ds_next != NULL)
116 		(void) dt_scope_pop();
117 
118 	dt_scope_destroy(&pcb->pcb_dstack);
119 	dt_irlist_destroy(&pcb->pcb_ir);
120 
121 	dt_node_link_free(&pcb->pcb_list);
122 	dt_node_link_free(&pcb->pcb_hold);
123 
124 	if (err != 0) {
125 		dt_xlator_t *dxp, *nxp;
126 		dt_provider_t *pvp, *nvp;
127 
128 		if (pcb->pcb_prog != NULL)
129 			dt_program_destroy(dtp, pcb->pcb_prog);
130 		if (pcb->pcb_stmt != NULL)
131 			dtrace_stmt_destroy(dtp, pcb->pcb_stmt);
132 		if (pcb->pcb_ecbdesc != NULL)
133 			dt_ecbdesc_release(dtp, pcb->pcb_ecbdesc);
134 
135 		for (dxp = dt_list_next(&dtp->dt_xlators); dxp; dxp = nxp) {
136 			nxp = dt_list_next(dxp);
137 			if (dxp->dx_gen == dtp->dt_gen)
138 				dt_xlator_destroy(dtp, dxp);
139 		}
140 
141 		for (pvp = dt_list_next(&dtp->dt_provlist); pvp; pvp = nvp) {
142 			nvp = dt_list_next(pvp);
143 			if (pvp->pv_gen == dtp->dt_gen)
144 				dt_provider_destroy(dtp, pvp);
145 		}
146 
147 		(void) dt_idhash_iter(dtp->dt_aggs, dt_pcb_pop_ident, dtp);
148 		dt_idhash_update(dtp->dt_aggs);
149 
150 		(void) dt_idhash_iter(dtp->dt_globals, dt_pcb_pop_ident, dtp);
151 		dt_idhash_update(dtp->dt_globals);
152 
153 		(void) dt_idhash_iter(dtp->dt_tls, dt_pcb_pop_ident, dtp);
154 		dt_idhash_update(dtp->dt_tls);
155 
156 		(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
157 		(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
158 	}
159 
160 	if (pcb->pcb_pragmas != NULL)
161 		dt_idhash_destroy(pcb->pcb_pragmas);
162 	if (pcb->pcb_locals != NULL)
163 		dt_idhash_destroy(pcb->pcb_locals);
164 	if (pcb->pcb_idents != NULL)
165 		dt_idhash_destroy(pcb->pcb_idents);
166 	if (pcb->pcb_inttab != NULL)
167 		dt_inttab_destroy(pcb->pcb_inttab);
168 	if (pcb->pcb_strtab != NULL)
169 		dt_strtab_destroy(pcb->pcb_strtab);
170 	if (pcb->pcb_regs != NULL)
171 		dt_regset_destroy(pcb->pcb_regs);
172 
173 	for (i = 0; i < pcb->pcb_asxreflen; i++)
174 		dt_free(dtp, pcb->pcb_asxrefs[i]);
175 
176 	dt_free(dtp, pcb->pcb_asxrefs);
177 	dt_difo_free(dtp, pcb->pcb_difo);
178 
179 	free(pcb->pcb_filetag);
180 	free(pcb->pcb_sflagv);
181 
182 	dtp->dt_pcb = pcb->pcb_prev;
183 	bzero(pcb, sizeof (dt_pcb_t));
184 	yyinit(dtp->dt_pcb);
185 }
186