1a864dc36Sdarran /*
2a864dc36Sdarran * CDDL HEADER START
3a864dc36Sdarran *
4a864dc36Sdarran * The contents of this file are subject to the terms of the
5a864dc36Sdarran * Common Development and Distribution License (the "License").
6a864dc36Sdarran * You may not use this file except in compliance with the License.
7a864dc36Sdarran *
8a864dc36Sdarran * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a864dc36Sdarran * or http://www.opensolaris.org/os/licensing.
10a864dc36Sdarran * See the License for the specific language governing permissions
11a864dc36Sdarran * and limitations under the License.
12a864dc36Sdarran *
13a864dc36Sdarran * When distributing Covered Code, include this CDDL HEADER in each
14a864dc36Sdarran * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a864dc36Sdarran * If applicable, add the following below this CDDL HEADER, with the
16a864dc36Sdarran * fields enclosed by brackets "[]" replaced with your own identifying
17a864dc36Sdarran * information: Portions Copyright [yyyy] [name of copyright owner]
18a864dc36Sdarran *
19a864dc36Sdarran * CDDL HEADER END
20a864dc36Sdarran */
21a864dc36Sdarran
22a864dc36Sdarran /*
23a864dc36Sdarran * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*c0855460Schristos * Copyright (c) 2011, Joyent Inc. All rights reserved.
25a864dc36Sdarran */
26a864dc36Sdarran
27a864dc36Sdarran #pragma ident "%Z%%M% %I% %E% SMI"
28a864dc36Sdarran
29a864dc36Sdarran #include <assert.h>
30a864dc36Sdarran #include <strings.h>
31*c0855460Schristos #ifdef illumos
32a864dc36Sdarran #include <alloca.h>
33bb8023b5Sdarran #endif
34*c0855460Schristos #include <fcntl.h>
35a864dc36Sdarran #include <stdlib.h>
36a864dc36Sdarran #include <stdio.h>
37a864dc36Sdarran
38*c0855460Schristos #include <sys/types.h>
39*c0855460Schristos #include <sys/sysctl.h>
40*c0855460Schristos #include <sys/stat.h>
41*c0855460Schristos
42a864dc36Sdarran #include <dt_parser.h>
43a864dc36Sdarran #include <dt_impl.h>
44a864dc36Sdarran #include <dt_provider.h>
45a864dc36Sdarran #include <dt_module.h>
46a864dc36Sdarran
47a864dc36Sdarran /*
48a864dc36Sdarran * This callback function is installed in a given identifier hash to search for
49a864dc36Sdarran * and apply deferred pragmas that are pending for a given new identifier name.
50a864dc36Sdarran * Multiple pragmas may be pending for a given name; we processs all of them.
51a864dc36Sdarran */
52a864dc36Sdarran /*ARGSUSED*/
53a864dc36Sdarran static void
dt_pragma_apply(dt_idhash_t * dhp,dt_ident_t * idp)54a864dc36Sdarran dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
55a864dc36Sdarran {
56a864dc36Sdarran dt_idhash_t *php;
57a864dc36Sdarran dt_ident_t *pdp;
58a864dc36Sdarran
59a864dc36Sdarran if ((php = yypcb->pcb_pragmas) == NULL)
60a864dc36Sdarran return; /* no pragmas pending for current compilation pass */
61a864dc36Sdarran
62a864dc36Sdarran while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
63a864dc36Sdarran switch (pdp->di_kind) {
64a864dc36Sdarran case DT_IDENT_PRAGAT:
65a864dc36Sdarran idp->di_attr = pdp->di_attr;
66a864dc36Sdarran break;
67a864dc36Sdarran case DT_IDENT_PRAGBN:
68a864dc36Sdarran idp->di_vers = pdp->di_vers;
69a864dc36Sdarran break;
70a864dc36Sdarran }
71a864dc36Sdarran dt_idhash_delete(php, pdp);
72a864dc36Sdarran }
73a864dc36Sdarran }
74a864dc36Sdarran
75a864dc36Sdarran /*
76a864dc36Sdarran * The #pragma attributes directive can be used to reset stability attributes
77a864dc36Sdarran * on a global identifier or inline definition. If the identifier is already
78a864dc36Sdarran * defined, we can just change di_attr. If not, we insert the pragma into a
79a864dc36Sdarran * hash table of the current pcb's deferred pragmas for later processing.
80a864dc36Sdarran */
81a864dc36Sdarran static void
dt_pragma_attributes(const char * prname,dt_node_t * dnp)82a864dc36Sdarran dt_pragma_attributes(const char *prname, dt_node_t *dnp)
83a864dc36Sdarran {
84a864dc36Sdarran dtrace_hdl_t *dtp = yypcb->pcb_hdl;
85*c0855460Schristos dtrace_attribute_t attr, *a = NULL; // XXX: gcc
86a864dc36Sdarran dt_provider_t *pvp;
87a864dc36Sdarran const char *name, *part;
88a864dc36Sdarran dt_ident_t *idp;
89a864dc36Sdarran
90a864dc36Sdarran if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
91a864dc36Sdarran dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
92a864dc36Sdarran xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
93a864dc36Sdarran "<attributes> <ident>\n", prname);
94a864dc36Sdarran }
95a864dc36Sdarran
96a864dc36Sdarran if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
97a864dc36Sdarran xyerror(D_PRAGMA_INVAL, "invalid attributes "
98a864dc36Sdarran "specified by #pragma %s\n", prname);
99a864dc36Sdarran }
100a864dc36Sdarran
101a864dc36Sdarran dnp = dnp->dn_list;
102a864dc36Sdarran name = dnp->dn_string;
103a864dc36Sdarran
104a864dc36Sdarran if (strcmp(name, "provider") == 0) {
105a864dc36Sdarran dnp = dnp->dn_list;
106a864dc36Sdarran name = dnp->dn_string;
107a864dc36Sdarran
108a864dc36Sdarran dnp = dnp->dn_list;
109a864dc36Sdarran part = dnp->dn_string;
110a864dc36Sdarran
111a864dc36Sdarran if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
112a864dc36Sdarran if (strcmp(part, "provider") == 0) {
113a864dc36Sdarran a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
114a864dc36Sdarran } else if (strcmp(part, "module") == 0) {
115a864dc36Sdarran a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
116a864dc36Sdarran } else if (strcmp(part, "function") == 0) {
117a864dc36Sdarran a = &pvp->pv_desc.dtvd_attr.dtpa_func;
118a864dc36Sdarran } else if (strcmp(part, "name") == 0) {
119a864dc36Sdarran a = &pvp->pv_desc.dtvd_attr.dtpa_name;
120a864dc36Sdarran } else if (strcmp(part, "args") == 0) {
121a864dc36Sdarran a = &pvp->pv_desc.dtvd_attr.dtpa_args;
122a864dc36Sdarran } else {
123a864dc36Sdarran xyerror(D_PRAGMA_INVAL, "invalid component "
124a864dc36Sdarran "\"%s\" in attribute #pragma "
125a864dc36Sdarran "for provider %s\n", name, part);
126a864dc36Sdarran }
127a864dc36Sdarran
128a864dc36Sdarran *a = attr;
129a864dc36Sdarran return;
130a864dc36Sdarran }
131a864dc36Sdarran
132a864dc36Sdarran } else if ((idp = dt_idstack_lookup(
133a864dc36Sdarran &yypcb->pcb_globals, name)) != NULL) {
134a864dc36Sdarran
135a864dc36Sdarran if (idp->di_gen != dtp->dt_gen) {
136a864dc36Sdarran xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
137a864dc36Sdarran "entity defined outside program scope\n", prname);
138a864dc36Sdarran }
139a864dc36Sdarran
140a864dc36Sdarran idp->di_attr = attr;
141a864dc36Sdarran return;
142a864dc36Sdarran }
143a864dc36Sdarran
144a864dc36Sdarran if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
145a864dc36Sdarran dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
146a864dc36Sdarran longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
147a864dc36Sdarran
148a864dc36Sdarran idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
149a864dc36Sdarran attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
150a864dc36Sdarran
151a864dc36Sdarran if (idp == NULL)
152a864dc36Sdarran longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
153a864dc36Sdarran
154a864dc36Sdarran if (dtp->dt_globals->dh_defer == NULL)
155a864dc36Sdarran dtp->dt_globals->dh_defer = &dt_pragma_apply;
156a864dc36Sdarran }
157a864dc36Sdarran
158a864dc36Sdarran /*
159a864dc36Sdarran * The #pragma binding directive can be used to reset the version binding
160a864dc36Sdarran * on a global identifier or inline definition. If the identifier is already
161a864dc36Sdarran * defined, we can just change di_vers. If not, we insert the pragma into a
162a864dc36Sdarran * hash table of the current pcb's deferred pragmas for later processing.
163a864dc36Sdarran */
164a864dc36Sdarran static void
dt_pragma_binding(const char * prname,dt_node_t * dnp)165a864dc36Sdarran dt_pragma_binding(const char *prname, dt_node_t *dnp)
166a864dc36Sdarran {
167a864dc36Sdarran dtrace_hdl_t *dtp = yypcb->pcb_hdl;
168a864dc36Sdarran dt_version_t vers;
169a864dc36Sdarran const char *name;
170a864dc36Sdarran dt_ident_t *idp;
171a864dc36Sdarran
172a864dc36Sdarran if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
173a864dc36Sdarran dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
174a864dc36Sdarran xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
175a864dc36Sdarran "\"version\" <ident>\n", prname);
176a864dc36Sdarran }
177a864dc36Sdarran
178a864dc36Sdarran if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
179a864dc36Sdarran xyerror(D_PRAGMA_INVAL, "invalid version string "
180a864dc36Sdarran "specified by #pragma %s\n", prname);
181a864dc36Sdarran }
182a864dc36Sdarran
183a864dc36Sdarran name = dnp->dn_list->dn_string;
184a864dc36Sdarran idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
185a864dc36Sdarran
186a864dc36Sdarran if (idp != NULL) {
187a864dc36Sdarran if (idp->di_gen != dtp->dt_gen) {
188a864dc36Sdarran xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
189a864dc36Sdarran "entity defined outside program scope\n", prname);
190a864dc36Sdarran }
191a864dc36Sdarran idp->di_vers = vers;
192a864dc36Sdarran return;
193a864dc36Sdarran }
194a864dc36Sdarran
195a864dc36Sdarran if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
196a864dc36Sdarran dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
197a864dc36Sdarran longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
198a864dc36Sdarran
199a864dc36Sdarran idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
200a864dc36Sdarran _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
201a864dc36Sdarran
202a864dc36Sdarran if (idp == NULL)
203a864dc36Sdarran longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
204a864dc36Sdarran
205a864dc36Sdarran if (dtp->dt_globals->dh_defer == NULL)
206a864dc36Sdarran dtp->dt_globals->dh_defer = &dt_pragma_apply;
207a864dc36Sdarran }
208a864dc36Sdarran
209*c0855460Schristos static void
dt_pragma_depends_finddep(dtrace_hdl_t * dtp,const char * lname,char * lib,size_t len)210*c0855460Schristos dt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
211*c0855460Schristos size_t len)
212*c0855460Schristos {
213*c0855460Schristos dt_dirpath_t *dirp;
214*c0855460Schristos struct stat sbuf;
215*c0855460Schristos int found = 0;
216*c0855460Schristos
217*c0855460Schristos for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
218*c0855460Schristos dirp = dt_list_next(dirp)) {
219*c0855460Schristos (void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
220*c0855460Schristos
221*c0855460Schristos if (stat(lib, &sbuf) == 0) {
222*c0855460Schristos found = 1;
223*c0855460Schristos break;
224*c0855460Schristos }
225*c0855460Schristos }
226*c0855460Schristos
227*c0855460Schristos if (!found)
228*c0855460Schristos xyerror(D_PRAGMA_DEPEND,
229*c0855460Schristos "failed to find dependency in libpath: %s", lname);
230*c0855460Schristos }
231*c0855460Schristos
232a864dc36Sdarran /*
233a864dc36Sdarran * The #pragma depends_on directive can be used to express a dependency on a
234a864dc36Sdarran * module, provider or library which if not present will cause processing to
235a864dc36Sdarran * abort.
236a864dc36Sdarran */
237a864dc36Sdarran static void
dt_pragma_depends(const char * prname,dt_node_t * cnp)238a864dc36Sdarran dt_pragma_depends(const char *prname, dt_node_t *cnp)
239a864dc36Sdarran {
240a864dc36Sdarran dtrace_hdl_t *dtp = yypcb->pcb_hdl;
241a864dc36Sdarran dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
242*c0855460Schristos int found = 0; // XXX: gcc
243a864dc36Sdarran dt_lib_depend_t *dld;
244a864dc36Sdarran char lib[MAXPATHLEN];
245*c0855460Schristos size_t plen;
246*c0855460Schristos char *provs, *cpy, *tok;
247a864dc36Sdarran
248a864dc36Sdarran if (cnp == NULL || nnp == NULL ||
249a864dc36Sdarran cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
250a864dc36Sdarran xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
251a864dc36Sdarran "<class> <name>\n", prname);
252a864dc36Sdarran }
253a864dc36Sdarran
254*c0855460Schristos if (strcmp(cnp->dn_string, "provider") == 0) {
255*c0855460Schristos /*
256*c0855460Schristos * First try to get the provider list using the
257*c0855460Schristos * debug.dtrace.providers sysctl, since that'll work even if
258*c0855460Schristos * we're not running as root.
259*c0855460Schristos */
260*c0855460Schristos provs = NULL;
261*c0855460Schristos if (sysctlbyname("debug.dtrace.providers", NULL, &plen, NULL, 0) ||
262*c0855460Schristos ((provs = dt_alloc(dtp, plen)) == NULL) ||
263*c0855460Schristos sysctlbyname("debug.dtrace.providers", provs, &plen, NULL, 0))
264a864dc36Sdarran found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
265*c0855460Schristos else {
266*c0855460Schristos found = B_FALSE;
267*c0855460Schristos for (cpy = provs; (tok = strsep(&cpy, " ")) != NULL; )
268*c0855460Schristos if (strcmp(tok, nnp->dn_string) == 0) {
269*c0855460Schristos found = B_TRUE;
270*c0855460Schristos break;
271*c0855460Schristos }
272*c0855460Schristos if (found == B_FALSE)
273*c0855460Schristos found = dt_provider_lookup(dtp,
274*c0855460Schristos nnp->dn_string) != NULL;
275*c0855460Schristos }
276*c0855460Schristos if (provs != NULL)
277*c0855460Schristos dt_free(dtp, provs);
278*c0855460Schristos } else if (strcmp(cnp->dn_string, "module") == 0) {
279a864dc36Sdarran dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
280a864dc36Sdarran found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
281*c0855460Schristos #if defined(__FreeBSD__) || defined(__NetBSD__)
282*c0855460Schristos if (!found) {
283*c0855460Schristos dt_kmodule_t *dkmp = dt_kmodule_lookup(dtp,
284*c0855460Schristos nnp->dn_string);
285*c0855460Schristos found = dkmp != NULL &&
286*c0855460Schristos dt_module_getctf(dtp, dkmp->dkm_module) != NULL;
287*c0855460Schristos }
288*c0855460Schristos #endif
289a864dc36Sdarran } else if (strcmp(cnp->dn_string, "library") == 0) {
290a864dc36Sdarran if (yypcb->pcb_cflags & DTRACE_C_CTL) {
291a864dc36Sdarran assert(dtp->dt_filetag != NULL);
292a864dc36Sdarran
293*c0855460Schristos dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
294*c0855460Schristos sizeof (lib));
295*c0855460Schristos
296a864dc36Sdarran dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
297a864dc36Sdarran dtp->dt_filetag);
298a864dc36Sdarran assert(dld != NULL);
299a864dc36Sdarran
300a864dc36Sdarran if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
301a864dc36Sdarran lib)) != 0) {
302a864dc36Sdarran xyerror(D_PRAGMA_DEPEND,
303a864dc36Sdarran "failed to add dependency %s:%s\n", lib,
304a864dc36Sdarran dtrace_errmsg(dtp, dtrace_errno(dtp)));
305a864dc36Sdarran }
306a864dc36Sdarran } else {
307a864dc36Sdarran /*
308a864dc36Sdarran * By this point we have already performed a topological
309a864dc36Sdarran * sort of the dependencies; we process this directive
310a864dc36Sdarran * as satisfied as long as the dependency was properly
311a864dc36Sdarran * loaded.
312a864dc36Sdarran */
313a864dc36Sdarran if (dtp->dt_filetag == NULL)
314a864dc36Sdarran xyerror(D_PRAGMA_DEPEND, "main program may "
315a864dc36Sdarran "not explicitly depend on a library");
316a864dc36Sdarran
317a864dc36Sdarran dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
318a864dc36Sdarran dtp->dt_filetag);
319a864dc36Sdarran assert(dld != NULL);
320a864dc36Sdarran
321*c0855460Schristos dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
322*c0855460Schristos sizeof (lib));
323a864dc36Sdarran dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
324a864dc36Sdarran lib);
325a864dc36Sdarran assert(dld != NULL);
326a864dc36Sdarran
327a864dc36Sdarran if (!dld->dtld_loaded)
328a864dc36Sdarran xyerror(D_PRAGMA_DEPEND, "program requires "
329a864dc36Sdarran "library \"%s\" which failed to load",
330a864dc36Sdarran lib);
331a864dc36Sdarran }
332a864dc36Sdarran
333a864dc36Sdarran found = B_TRUE;
334a864dc36Sdarran } else {
335a864dc36Sdarran xyerror(D_PRAGMA_INVAL, "invalid class %s "
336a864dc36Sdarran "specified by #pragma %s\n", cnp->dn_string, prname);
337a864dc36Sdarran }
338a864dc36Sdarran
339a864dc36Sdarran if (!found) {
340a864dc36Sdarran xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
341a864dc36Sdarran cnp->dn_string, nnp->dn_string);
342a864dc36Sdarran }
343a864dc36Sdarran }
344a864dc36Sdarran
345a864dc36Sdarran /*
346a864dc36Sdarran * The #pragma error directive can be followed by any list of tokens, which we
347a864dc36Sdarran * just concatenate and print as part of our error message.
348a864dc36Sdarran */
349a864dc36Sdarran static void
dt_pragma_error(const char * prname,dt_node_t * dnp)350a864dc36Sdarran dt_pragma_error(const char *prname, dt_node_t *dnp)
351a864dc36Sdarran {
352a864dc36Sdarran dt_node_t *enp;
353a864dc36Sdarran size_t n = 0;
354a864dc36Sdarran char *s;
355a864dc36Sdarran
356a864dc36Sdarran for (enp = dnp; enp != NULL; enp = enp->dn_list) {
357a864dc36Sdarran if (enp->dn_kind == DT_NODE_IDENT ||
358a864dc36Sdarran enp->dn_kind == DT_NODE_STRING)
359a864dc36Sdarran n += strlen(enp->dn_string) + 1;
360a864dc36Sdarran }
361a864dc36Sdarran
362a864dc36Sdarran s = alloca(n + 1);
363a864dc36Sdarran s[0] = '\0';
364a864dc36Sdarran
365a864dc36Sdarran for (enp = dnp; enp != NULL; enp = enp->dn_list) {
366a864dc36Sdarran if (enp->dn_kind == DT_NODE_IDENT ||
367a864dc36Sdarran enp->dn_kind == DT_NODE_STRING) {
368a864dc36Sdarran (void) strcat(s, enp->dn_string);
369a864dc36Sdarran (void) strcat(s, " ");
370a864dc36Sdarran }
371a864dc36Sdarran }
372a864dc36Sdarran
373a864dc36Sdarran xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
374a864dc36Sdarran }
375a864dc36Sdarran
376a864dc36Sdarran /*ARGSUSED*/
377a864dc36Sdarran static void
dt_pragma_ident(const char * prname,dt_node_t * dnp)378a864dc36Sdarran dt_pragma_ident(const char *prname, dt_node_t *dnp)
379a864dc36Sdarran {
380a864dc36Sdarran /* ignore any #ident or #pragma ident lines */
381a864dc36Sdarran }
382a864dc36Sdarran
383a864dc36Sdarran static void
dt_pragma_option(const char * prname,dt_node_t * dnp)384a864dc36Sdarran dt_pragma_option(const char *prname, dt_node_t *dnp)
385a864dc36Sdarran {
386a864dc36Sdarran dtrace_hdl_t *dtp = yypcb->pcb_hdl;
387a864dc36Sdarran char *opt, *val;
388a864dc36Sdarran
389a864dc36Sdarran if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
390a864dc36Sdarran xyerror(D_PRAGMA_MALFORM,
391a864dc36Sdarran "malformed #pragma %s <option>=<val>\n", prname);
392a864dc36Sdarran }
393a864dc36Sdarran
394a864dc36Sdarran if (dnp->dn_list != NULL) {
395a864dc36Sdarran xyerror(D_PRAGMA_MALFORM,
396a864dc36Sdarran "superfluous arguments specified for #pragma %s\n", prname);
397a864dc36Sdarran }
398a864dc36Sdarran
399a864dc36Sdarran opt = alloca(strlen(dnp->dn_string) + 1);
400a864dc36Sdarran (void) strcpy(opt, dnp->dn_string);
401a864dc36Sdarran
402a864dc36Sdarran if ((val = strchr(opt, '=')) != NULL)
403a864dc36Sdarran *val++ = '\0';
404a864dc36Sdarran
405a864dc36Sdarran if (dtrace_setopt(dtp, opt, val) == -1) {
406a864dc36Sdarran if (val == NULL) {
407a864dc36Sdarran xyerror(D_PRAGMA_OPTSET,
408a864dc36Sdarran "failed to set option '%s': %s\n", opt,
409a864dc36Sdarran dtrace_errmsg(dtp, dtrace_errno(dtp)));
410a864dc36Sdarran } else {
411a864dc36Sdarran xyerror(D_PRAGMA_OPTSET,
412a864dc36Sdarran "failed to set option '%s' to '%s': %s\n",
413a864dc36Sdarran opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
414a864dc36Sdarran }
415a864dc36Sdarran }
416a864dc36Sdarran }
417a864dc36Sdarran
418a864dc36Sdarran /*
419a864dc36Sdarran * The #line directive is used to reset the input line number and to optionally
420a864dc36Sdarran * note the file name for use in error messages. Sun cpp(1) also produces a
421a864dc36Sdarran * third integer token after the filename which is one of the following:
422a864dc36Sdarran *
423a864dc36Sdarran * 0 - line change has nothing to do with an #include file
424a864dc36Sdarran * 1 - line change because we just entered a #include file
425a864dc36Sdarran * 2 - line change because we just exited a #include file
426a864dc36Sdarran *
427a864dc36Sdarran * We use these state tokens to adjust pcb_idepth, which in turn controls
428a864dc36Sdarran * whether type lookups access the global type space or not.
429a864dc36Sdarran */
430a864dc36Sdarran static void
dt_pragma_line(const char * prname,dt_node_t * dnp)431a864dc36Sdarran dt_pragma_line(const char *prname, dt_node_t *dnp)
432a864dc36Sdarran {
433a864dc36Sdarran dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
434a864dc36Sdarran dt_node_t *inp = fnp ? fnp->dn_list : NULL;
435a864dc36Sdarran
436a864dc36Sdarran if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
437a864dc36Sdarran (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
438a864dc36Sdarran (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
439a864dc36Sdarran xyerror(D_PRAGMA_MALFORM, "malformed #%s "
440a864dc36Sdarran "<line> [ [\"file\"] state ]\n", prname);
441a864dc36Sdarran }
442a864dc36Sdarran
443a864dc36Sdarran /*
444a864dc36Sdarran * If a file is specified, free any old pcb_filetag and swap fnp's
445a864dc36Sdarran * dn_string into pcb_filetag as the new filename for error messages.
446a864dc36Sdarran */
447a864dc36Sdarran if (fnp != NULL) {
448a864dc36Sdarran if (yypcb->pcb_filetag != NULL)
449a864dc36Sdarran free(yypcb->pcb_filetag);
450a864dc36Sdarran
451a864dc36Sdarran /*
452a864dc36Sdarran * This is not pretty, but is a necessary evil until we either
453a864dc36Sdarran * write "dpp" or get a useful standalone cpp from DevPro. If
454a864dc36Sdarran * the filename begins with /dev/fd, we know it's the master
455a864dc36Sdarran * input file (see dt_preproc() in dt_cc.c), so just clear the
456a864dc36Sdarran * dt_filetag pointer so error messages refer to the main file.
457a864dc36Sdarran */
458a864dc36Sdarran if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
459a864dc36Sdarran yypcb->pcb_filetag = fnp->dn_string;
460a864dc36Sdarran fnp->dn_string = NULL;
461a864dc36Sdarran } else
462a864dc36Sdarran yypcb->pcb_filetag = NULL;
463a864dc36Sdarran }
464a864dc36Sdarran
465a864dc36Sdarran if (inp != NULL) {
466a864dc36Sdarran if (inp->dn_value == 1)
467a864dc36Sdarran yypcb->pcb_idepth++;
468a864dc36Sdarran else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
469a864dc36Sdarran yypcb->pcb_idepth--;
470a864dc36Sdarran }
471a864dc36Sdarran
472a864dc36Sdarran yylineno = dnp->dn_value;
473a864dc36Sdarran }
474a864dc36Sdarran
475a864dc36Sdarran /*
476a864dc36Sdarran * D compiler pragma types range from control directives to common pragmas to
477a864dc36Sdarran * D custom pragmas, in order of specificity. Similar to gcc, we use #pragma D
478a864dc36Sdarran * as a special prefix for our pragmas so they can be used in mixed headers.
479a864dc36Sdarran */
480a864dc36Sdarran #define DT_PRAGMA_DIR 0 /* pragma directive may be used after naked # */
481a864dc36Sdarran #define DT_PRAGMA_SUB 1 /* pragma directive may be used after #pragma */
482a864dc36Sdarran #define DT_PRAGMA_DCP 2 /* pragma may only be used after #pragma D */
483a864dc36Sdarran
484a864dc36Sdarran static const struct dt_pragmadesc {
485a864dc36Sdarran const char *dpd_name;
486a864dc36Sdarran void (*dpd_func)(const char *, dt_node_t *);
487a864dc36Sdarran int dpd_kind;
488a864dc36Sdarran } dt_pragmas[] = {
489a864dc36Sdarran { "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
490a864dc36Sdarran { "binding", dt_pragma_binding, DT_PRAGMA_DCP },
491a864dc36Sdarran { "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
492a864dc36Sdarran { "error", dt_pragma_error, DT_PRAGMA_DIR },
493a864dc36Sdarran { "ident", dt_pragma_ident, DT_PRAGMA_DIR },
494a864dc36Sdarran { "line", dt_pragma_line, DT_PRAGMA_DIR },
495a864dc36Sdarran { "option", dt_pragma_option, DT_PRAGMA_DCP },
496a864dc36Sdarran { NULL, NULL }
497a864dc36Sdarran };
498a864dc36Sdarran
499a864dc36Sdarran /*
500a864dc36Sdarran * Process a control line #directive by looking up the directive name in our
501a864dc36Sdarran * lookup table and invoking the corresponding function with the token list.
502a864dc36Sdarran * According to K&R[A12.9], we silently ignore null directive lines.
503a864dc36Sdarran */
504a864dc36Sdarran void
dt_pragma(dt_node_t * pnp)505a864dc36Sdarran dt_pragma(dt_node_t *pnp)
506a864dc36Sdarran {
507a864dc36Sdarran const struct dt_pragmadesc *dpd;
508a864dc36Sdarran dt_node_t *dnp;
509a864dc36Sdarran int kind = DT_PRAGMA_DIR;
510a864dc36Sdarran
511a864dc36Sdarran for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
512a864dc36Sdarran if (dnp->dn_kind == DT_NODE_INT) {
513a864dc36Sdarran dt_pragma_line("line", dnp);
514a864dc36Sdarran break;
515a864dc36Sdarran }
516a864dc36Sdarran
517a864dc36Sdarran if (dnp->dn_kind != DT_NODE_IDENT)
518a864dc36Sdarran xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
519a864dc36Sdarran
520a864dc36Sdarran if (kind == DT_PRAGMA_DIR &&
521a864dc36Sdarran strcmp(dnp->dn_string, "pragma") == 0) {
522a864dc36Sdarran kind = DT_PRAGMA_SUB;
523a864dc36Sdarran continue;
524a864dc36Sdarran }
525a864dc36Sdarran
526a864dc36Sdarran if (kind == DT_PRAGMA_SUB &&
527a864dc36Sdarran strcmp(dnp->dn_string, "D") == 0) {
528a864dc36Sdarran kind = DT_PRAGMA_DCP;
529a864dc36Sdarran continue;
530a864dc36Sdarran }
531a864dc36Sdarran
532a864dc36Sdarran for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
533a864dc36Sdarran if (dpd->dpd_kind <= kind &&
534a864dc36Sdarran strcmp(dpd->dpd_name, dnp->dn_string) == 0)
535a864dc36Sdarran break;
536a864dc36Sdarran }
537a864dc36Sdarran
538a864dc36Sdarran yylineno--; /* since we've already seen \n */
539a864dc36Sdarran
540a864dc36Sdarran if (dpd->dpd_name != NULL) {
541a864dc36Sdarran dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
542a864dc36Sdarran yylineno++;
543a864dc36Sdarran break;
544a864dc36Sdarran }
545a864dc36Sdarran
546a864dc36Sdarran switch (kind) {
547a864dc36Sdarran case DT_PRAGMA_DIR:
548a864dc36Sdarran xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
549a864dc36Sdarran "#%s\n", dnp->dn_string);
550a864dc36Sdarran /*NOTREACHED*/
551a864dc36Sdarran case DT_PRAGMA_SUB:
552a864dc36Sdarran break; /* K&R[A12.8] says to ignore unknown pragmas */
553a864dc36Sdarran case DT_PRAGMA_DCP:
554a864dc36Sdarran default:
555a864dc36Sdarran xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
556a864dc36Sdarran dnp->dn_string);
557a864dc36Sdarran }
558a864dc36Sdarran
559a864dc36Sdarran yylineno++;
560a864dc36Sdarran break;
561a864dc36Sdarran }
562a864dc36Sdarran
563a864dc36Sdarran dt_node_list_free(&pnp);
564a864dc36Sdarran }
565