Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_pcb.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* DTrace Parsing Control Block30*31* A DTrace Parsing Control Block (PCB) contains all of the state that is used32* by a single pass of the D compiler, other than the global variables used by33* lex and yacc. The routines in this file are used to set up and tear down34* PCBs, which are kept on a stack pointed to by the libdtrace global 'yypcb'.35* The main engine of the compiler, dt_compile(), is located in dt_cc.c and is36* responsible for calling these routines to begin and end a compilation pass.37*38* Sun's lex/yacc are not MT-safe or re-entrant, but we permit limited nested39* use of dt_compile() once the entire parse tree has been constructed but has40* not yet executed the "cooking" pass (see dt_cc.c for more information). The41* PCB design also makes it easier to debug (since all global state is kept in42* one place) and could permit us to make the D compiler MT-safe or re-entrant43* in the future by adding locks to libdtrace or switching to Flex and Bison.44*/4546#include <strings.h>47#include <stdlib.h>48#include <assert.h>4950#include <dt_impl.h>51#include <dt_program.h>52#include <dt_provider.h>53#include <dt_pcb.h>5455/*56* Initialize the specified PCB by zeroing it and filling in a few default57* members, and then pushing it on to the top of the PCB stack and setting58* yypcb to point to it. Increment the current handle's generation count.59*/60void61dt_pcb_push(dtrace_hdl_t *dtp, dt_pcb_t *pcb)62{63/*64* Since lex/yacc are not re-entrant and we don't implement state save,65* assert that if another PCB is active, it is from the same handle and66* has completed execution of yyparse(). If the first assertion fires,67* the caller is calling libdtrace without proper MT locking. If the68* second assertion fires, dt_compile() is being called recursively69* from an illegal location in libdtrace, or a dt_pcb_pop() is missing.70*/71if (yypcb != NULL) {72assert(yypcb->pcb_hdl == dtp);73assert(yypcb->pcb_yystate == YYS_DONE);74}7576bzero(pcb, sizeof (dt_pcb_t));7778dt_scope_create(&pcb->pcb_dstack);79dt_idstack_push(&pcb->pcb_globals, dtp->dt_globals);80dt_irlist_create(&pcb->pcb_ir);8182pcb->pcb_hdl = dtp;83pcb->pcb_prev = dtp->dt_pcb;8485dtp->dt_pcb = pcb;86dtp->dt_gen++;8788yyinit(pcb);89}9091static int92dt_pcb_pop_ident(dt_idhash_t *dhp, dt_ident_t *idp, void *arg)93{94dtrace_hdl_t *dtp = arg;9596if (idp->di_gen == dtp->dt_gen)97dt_idhash_delete(dhp, idp);9899return (0);100}101102/*103* Pop the topmost PCB from the PCB stack and destroy any data structures that104* are associated with it. If 'err' is non-zero, destroy any intermediate105* state that is left behind as part of a compilation that has failed.106*/107void108dt_pcb_pop(dtrace_hdl_t *dtp, int err)109{110dt_pcb_t *pcb = yypcb;111uint_t i;112113assert(pcb != NULL);114assert(pcb == dtp->dt_pcb);115116while (pcb->pcb_dstack.ds_next != NULL)117(void) dt_scope_pop();118119dt_scope_destroy(&pcb->pcb_dstack);120dt_irlist_destroy(&pcb->pcb_ir);121122dt_node_link_free(&pcb->pcb_list);123dt_node_link_free(&pcb->pcb_hold);124125if (err != 0) {126dt_xlator_t *dxp, *nxp;127dt_provider_t *pvp, *nvp;128129if (pcb->pcb_prog != NULL)130dt_program_destroy(dtp, pcb->pcb_prog);131if (pcb->pcb_stmt != NULL)132dtrace_stmt_destroy(dtp, pcb->pcb_stmt);133if (pcb->pcb_ecbdesc != NULL)134dt_ecbdesc_release(dtp, pcb->pcb_ecbdesc);135136for (dxp = dt_list_next(&dtp->dt_xlators); dxp; dxp = nxp) {137nxp = dt_list_next(dxp);138if (dxp->dx_gen == dtp->dt_gen)139dt_xlator_destroy(dtp, dxp);140}141142for (pvp = dt_list_next(&dtp->dt_provlist); pvp; pvp = nvp) {143nvp = dt_list_next(pvp);144if (pvp->pv_gen == dtp->dt_gen)145dt_provider_destroy(dtp, pvp);146}147148(void) dt_idhash_iter(dtp->dt_aggs, dt_pcb_pop_ident, dtp);149dt_idhash_update(dtp->dt_aggs);150151(void) dt_idhash_iter(dtp->dt_globals, dt_pcb_pop_ident, dtp);152dt_idhash_update(dtp->dt_globals);153154(void) dt_idhash_iter(dtp->dt_tls, dt_pcb_pop_ident, dtp);155dt_idhash_update(dtp->dt_tls);156157(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);158(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);159}160161if (pcb->pcb_pragmas != NULL)162dt_idhash_destroy(pcb->pcb_pragmas);163if (pcb->pcb_locals != NULL)164dt_idhash_destroy(pcb->pcb_locals);165if (pcb->pcb_idents != NULL)166dt_idhash_destroy(pcb->pcb_idents);167if (pcb->pcb_inttab != NULL)168dt_inttab_destroy(pcb->pcb_inttab);169if (pcb->pcb_strtab != NULL)170dt_strtab_destroy(pcb->pcb_strtab);171if (pcb->pcb_regs != NULL)172dt_regset_destroy(pcb->pcb_regs);173174for (i = 0; i < pcb->pcb_asxreflen; i++)175dt_free(dtp, pcb->pcb_asxrefs[i]);176177dt_free(dtp, pcb->pcb_asxrefs);178dt_difo_free(dtp, pcb->pcb_difo);179180free(pcb->pcb_filetag);181free(pcb->pcb_sflagv);182183dtp->dt_pcb = pcb->pcb_prev;184bzero(pcb, sizeof (dt_pcb_t));185yyinit(dtp->dt_pcb);186}187188189