Print this page
OS-3280 need a way to specify the root of a native system in the lx brand
OS-3279 lx brand should allow delegated datasets
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>


  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  25  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <sys/types.h>
  29 #include <sys/modctl.h>
  30 #include <sys/systeminfo.h>
  31 #include <sys/resource.h>
  32 
  33 #include <libelf.h>
  34 #include <strings.h>
  35 #include <alloca.h>
  36 #include <limits.h>
  37 #include <unistd.h>
  38 #include <stdlib.h>
  39 #include <stdio.h>
  40 #include <fcntl.h>
  41 #include <errno.h>
  42 #include <assert.h>

  43 
  44 #define _POSIX_PTHREAD_SEMANTICS
  45 #include <dirent.h>
  46 #undef  _POSIX_PTHREAD_SEMANTICS
  47 
  48 #include <dt_impl.h>
  49 #include <dt_program.h>
  50 #include <dt_module.h>
  51 #include <dt_printf.h>
  52 #include <dt_string.h>
  53 #include <dt_provider.h>
  54 
  55 /*
  56  * Stability and versioning definitions.  These #defines are used in the tables
  57  * of identifiers below to fill in the attribute and version fields associated
  58  * with each identifier.  The DT_ATTR_* macros are a convenience to permit more
  59  * concise declarations of common attributes such as Stable/Stable/Common.  The
  60  * DT_VERS_* macros declare the encoded integer values of all versions used so
  61  * far.  DT_VERS_LATEST must correspond to the latest version value among all
  62  * versions exported by the D compiler.  DT_VERS_STRING must be an ASCII string


 668 const dtrace_attribute_t _dtrace_typattr = {
 669         DTRACE_STABILITY_PRIVATE,
 670         DTRACE_STABILITY_PRIVATE,
 671         DTRACE_CLASS_UNKNOWN
 672 };
 673 
 674 const dtrace_attribute_t _dtrace_prvattr = {
 675         DTRACE_STABILITY_PRIVATE,
 676         DTRACE_STABILITY_PRIVATE,
 677         DTRACE_CLASS_UNKNOWN
 678 };
 679 
 680 const dtrace_pattr_t _dtrace_prvdesc = {
 681 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 682 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 683 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 684 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 685 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 686 };
 687 
 688 const char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
 689 const char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
 690 
 691 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
 692 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
 693 
 694 int _dtrace_strbuckets = 211;   /* default number of hash buckets (prime) */
 695 int _dtrace_intbuckets = 256;   /* default number of integer buckets (Pof2) */
 696 uint_t _dtrace_strsize = 256;   /* default size of string intrinsic type */
 697 uint_t _dtrace_stkindent = 14;  /* default whitespace indent for stack/ustack */
 698 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
 699 uint_t _dtrace_pidlrulim = 8;   /* default number of pid handles to cache */
 700 size_t _dtrace_bufsize = 512;   /* default dt_buf_create() size */
 701 int _dtrace_argmax = 32;        /* default maximum number of probe arguments */
 702 
 703 int _dtrace_debug = 0;          /* debug messages enabled (off) */
 704 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
 705 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
 706 
 707 typedef struct dt_fdlist {
 708         int *df_fds;            /* array of provider driver file descriptors */
 709         uint_t df_ents;         /* number of valid elements in df_fds[] */


 809         if (rv < 0 || rv > len)
 810                 (void) snprintf(buf, len, "%s", "Unknown");
 811 
 812         while ((p = strchr(p, '.')) != NULL)
 813                 *p++ = '_';
 814 
 815         return (buf);
 816 }
 817 
 818 static dtrace_hdl_t *
 819 dt_vopen(int version, int flags, int *errp,
 820     const dtrace_vector_t *vector, void *arg)
 821 {
 822         dtrace_hdl_t *dtp = NULL;
 823         int dtfd = -1, ftfd = -1, fterr = 0;
 824         dtrace_prog_t *pgp;
 825         dt_module_t *dmp;
 826         dt_provmod_t *provmod = NULL;
 827         int i, err;
 828         struct rlimit rl;


 829 
 830         const dt_intrinsic_t *dinp;
 831         const dt_typedef_t *dtyp;
 832         const dt_ident_t *idp;
 833 
 834         dtrace_typeinfo_t dtt;
 835         ctf_funcinfo_t ctc;
 836         ctf_arinfo_t ctr;
 837 
 838         dt_fdlist_t df = { NULL, 0, 0 };
 839 
 840         char isadef[32], utsdef[32];
 841         char s1[64], s2[64];
 842 
 843         if (version <= 0)
 844                 return (set_open_errno(dtp, errp, EINVAL));
 845 
 846         if (version > DTRACE_VERSION)
 847                 return (set_open_errno(dtp, errp, EDT_VERSION));
 848 


 941         dtp->dt_oflags = flags;
 942         dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
 943         dtp->dt_linkmode = DT_LINK_KERNEL;
 944         dtp->dt_linktype = DT_LTYP_ELF;
 945         dtp->dt_xlatemode = DT_XL_STATIC;
 946         dtp->dt_stdcmode = DT_STDC_XA;
 947         dtp->dt_encoding = DT_ENCODING_UNSET;
 948         dtp->dt_version = version;
 949         dtp->dt_fd = dtfd;
 950         dtp->dt_ftfd = ftfd;
 951         dtp->dt_fterr = fterr;
 952         dtp->dt_cdefs_fd = -1;
 953         dtp->dt_ddefs_fd = -1;
 954         dtp->dt_stdout_fd = -1;
 955         dtp->dt_modbuckets = _dtrace_strbuckets;
 956         dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
 957         dtp->dt_provbuckets = _dtrace_strbuckets;
 958         dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
 959         dt_proc_init(dtp);
 960         dtp->dt_vmax = DT_VERS_LATEST;








 961         dtp->dt_cpp_path = strdup(_dtrace_defcpp);

 962         dtp->dt_cpp_argv = malloc(sizeof (char *));
 963         dtp->dt_cpp_argc = 1;
 964         dtp->dt_cpp_args = 1;
 965         dtp->dt_ld_path = strdup(_dtrace_defld);
 966         dtp->dt_provmod = provmod;
 967         dtp->dt_vector = vector;
 968         dtp->dt_varg = arg;
 969         dt_dof_init(dtp);
 970         (void) uname(&dtp->dt_uts);
 971 
 972         if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
 973             dtp->dt_procs == NULL || dtp->dt_proc_env == NULL ||
 974             dtp->dt_ld_path == NULL || dtp->dt_cpp_path == NULL ||
 975             dtp->dt_cpp_argv == NULL)
 976                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 977 
 978         for (i = 0; i < DTRACEOPT_MAX; i++)
 979                 dtp->dt_options[i] = DTRACEOPT_UNSET;
 980 
 981         dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
 982 
 983         (void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
 984             (uint_t)(sizeof (void *) * NBBY));
 985 


1290         /*
1291          * Load hard-wired inlines into the definition cache by calling the
1292          * compiler on the raw definition string defined above.
1293          */
1294         if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1295             DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1296                 dt_dprintf("failed to load hard-wired definitions: %s\n",
1297                     dtrace_errmsg(dtp, dtrace_errno(dtp)));
1298                 return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1299         }
1300 
1301         dt_program_destroy(dtp, pgp);
1302 
1303         /*
1304          * Set up the default DTrace library path.  Once set, the next call to
1305          * dt_compile() will compile all the libraries.  We intentionally defer
1306          * library processing to improve overhead for clients that don't ever
1307          * compile, and to provide better error reporting (because the full
1308          * reporting of compiler errors requires dtrace_open() to succeed).
1309          */
1310         if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)



1311                 return (set_open_errno(dtp, errp, dtp->dt_errno));
1312 



1313         return (dtp);
1314 }
1315 
1316 dtrace_hdl_t *
1317 dtrace_open(int version, int flags, int *errp)
1318 {
1319         return (dt_vopen(version, flags, errp, NULL, NULL));
1320 }
1321 
1322 dtrace_hdl_t *
1323 dtrace_vopen(int version, int flags, int *errp,
1324     const dtrace_vector_t *vector, void *arg)
1325 {
1326         return (dt_vopen(version, flags, errp, vector, arg));
1327 }
1328 
1329 void
1330 dtrace_close(dtrace_hdl_t *dtp)
1331 {
1332         dt_ident_t *idp, *ndp;




  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  25  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <sys/types.h>
  29 #include <sys/modctl.h>
  30 #include <sys/systeminfo.h>
  31 #include <sys/resource.h>
  32 
  33 #include <libelf.h>
  34 #include <strings.h>
  35 #include <alloca.h>
  36 #include <limits.h>
  37 #include <unistd.h>
  38 #include <stdlib.h>
  39 #include <stdio.h>
  40 #include <fcntl.h>
  41 #include <errno.h>
  42 #include <assert.h>
  43 #include <zone.h>
  44 
  45 #define _POSIX_PTHREAD_SEMANTICS
  46 #include <dirent.h>
  47 #undef  _POSIX_PTHREAD_SEMANTICS
  48 
  49 #include <dt_impl.h>
  50 #include <dt_program.h>
  51 #include <dt_module.h>
  52 #include <dt_printf.h>
  53 #include <dt_string.h>
  54 #include <dt_provider.h>
  55 
  56 /*
  57  * Stability and versioning definitions.  These #defines are used in the tables
  58  * of identifiers below to fill in the attribute and version fields associated
  59  * with each identifier.  The DT_ATTR_* macros are a convenience to permit more
  60  * concise declarations of common attributes such as Stable/Stable/Common.  The
  61  * DT_VERS_* macros declare the encoded integer values of all versions used so
  62  * far.  DT_VERS_LATEST must correspond to the latest version value among all
  63  * versions exported by the D compiler.  DT_VERS_STRING must be an ASCII string


 669 const dtrace_attribute_t _dtrace_typattr = {
 670         DTRACE_STABILITY_PRIVATE,
 671         DTRACE_STABILITY_PRIVATE,
 672         DTRACE_CLASS_UNKNOWN
 673 };
 674 
 675 const dtrace_attribute_t _dtrace_prvattr = {
 676         DTRACE_STABILITY_PRIVATE,
 677         DTRACE_STABILITY_PRIVATE,
 678         DTRACE_CLASS_UNKNOWN
 679 };
 680 
 681 const dtrace_pattr_t _dtrace_prvdesc = {
 682 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 683 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 684 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 685 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 686 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 687 };
 688 
 689 const char *_dtrace_defcpp = "/usr/lib/cpp"; /* default cpp(1) to invoke */
 690 const char *_dtrace_defld = "/usr/bin/ld";   /* default ld(1) to invoke */
 691 
 692 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
 693 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
 694 
 695 int _dtrace_strbuckets = 211;   /* default number of hash buckets (prime) */
 696 int _dtrace_intbuckets = 256;   /* default number of integer buckets (Pof2) */
 697 uint_t _dtrace_strsize = 256;   /* default size of string intrinsic type */
 698 uint_t _dtrace_stkindent = 14;  /* default whitespace indent for stack/ustack */
 699 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
 700 uint_t _dtrace_pidlrulim = 8;   /* default number of pid handles to cache */
 701 size_t _dtrace_bufsize = 512;   /* default dt_buf_create() size */
 702 int _dtrace_argmax = 32;        /* default maximum number of probe arguments */
 703 
 704 int _dtrace_debug = 0;          /* debug messages enabled (off) */
 705 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
 706 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
 707 
 708 typedef struct dt_fdlist {
 709         int *df_fds;            /* array of provider driver file descriptors */
 710         uint_t df_ents;         /* number of valid elements in df_fds[] */


 810         if (rv < 0 || rv > len)
 811                 (void) snprintf(buf, len, "%s", "Unknown");
 812 
 813         while ((p = strchr(p, '.')) != NULL)
 814                 *p++ = '_';
 815 
 816         return (buf);
 817 }
 818 
 819 static dtrace_hdl_t *
 820 dt_vopen(int version, int flags, int *errp,
 821     const dtrace_vector_t *vector, void *arg)
 822 {
 823         dtrace_hdl_t *dtp = NULL;
 824         int dtfd = -1, ftfd = -1, fterr = 0;
 825         dtrace_prog_t *pgp;
 826         dt_module_t *dmp;
 827         dt_provmod_t *provmod = NULL;
 828         int i, err;
 829         struct rlimit rl;
 830         const char *zroot;
 831         char *libpath = NULL;
 832 
 833         const dt_intrinsic_t *dinp;
 834         const dt_typedef_t *dtyp;
 835         const dt_ident_t *idp;
 836 
 837         dtrace_typeinfo_t dtt;
 838         ctf_funcinfo_t ctc;
 839         ctf_arinfo_t ctr;
 840 
 841         dt_fdlist_t df = { NULL, 0, 0 };
 842 
 843         char isadef[32], utsdef[32];
 844         char s1[64], s2[64];
 845 
 846         if (version <= 0)
 847                 return (set_open_errno(dtp, errp, EINVAL));
 848 
 849         if (version > DTRACE_VERSION)
 850                 return (set_open_errno(dtp, errp, EDT_VERSION));
 851 


 944         dtp->dt_oflags = flags;
 945         dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
 946         dtp->dt_linkmode = DT_LINK_KERNEL;
 947         dtp->dt_linktype = DT_LTYP_ELF;
 948         dtp->dt_xlatemode = DT_XL_STATIC;
 949         dtp->dt_stdcmode = DT_STDC_XA;
 950         dtp->dt_encoding = DT_ENCODING_UNSET;
 951         dtp->dt_version = version;
 952         dtp->dt_fd = dtfd;
 953         dtp->dt_ftfd = ftfd;
 954         dtp->dt_fterr = fterr;
 955         dtp->dt_cdefs_fd = -1;
 956         dtp->dt_ddefs_fd = -1;
 957         dtp->dt_stdout_fd = -1;
 958         dtp->dt_modbuckets = _dtrace_strbuckets;
 959         dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
 960         dtp->dt_provbuckets = _dtrace_strbuckets;
 961         dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
 962         dt_proc_init(dtp);
 963         dtp->dt_vmax = DT_VERS_LATEST;
 964         zroot = zone_get_nroot();
 965         if (zroot != NULL) {
 966                 (void) asprintf(&dtp->dt_ld_path, "%s/%s", zroot,
 967                     _dtrace_defld);
 968                 (void) asprintf(&dtp->dt_cpp_path, "%s/%s", zroot,
 969                     _dtrace_defcpp);
 970         } else {
 971                 dtp->dt_ld_path = strdup(_dtrace_defld);
 972                 dtp->dt_cpp_path = strdup(_dtrace_defcpp);
 973         }
 974         dtp->dt_cpp_argv = malloc(sizeof (char *));
 975         dtp->dt_cpp_argc = 1;
 976         dtp->dt_cpp_args = 1;

 977         dtp->dt_provmod = provmod;
 978         dtp->dt_vector = vector;
 979         dtp->dt_varg = arg;
 980         dt_dof_init(dtp);
 981         (void) uname(&dtp->dt_uts);
 982 
 983         if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
 984             dtp->dt_procs == NULL || dtp->dt_proc_env == NULL ||
 985             dtp->dt_ld_path == NULL || dtp->dt_cpp_path == NULL ||
 986             dtp->dt_cpp_argv == NULL)
 987                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 988 
 989         for (i = 0; i < DTRACEOPT_MAX; i++)
 990                 dtp->dt_options[i] = DTRACEOPT_UNSET;
 991 
 992         dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
 993 
 994         (void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
 995             (uint_t)(sizeof (void *) * NBBY));
 996 


1301         /*
1302          * Load hard-wired inlines into the definition cache by calling the
1303          * compiler on the raw definition string defined above.
1304          */
1305         if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1306             DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1307                 dt_dprintf("failed to load hard-wired definitions: %s\n",
1308                     dtrace_errmsg(dtp, dtrace_errno(dtp)));
1309                 return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1310         }
1311 
1312         dt_program_destroy(dtp, pgp);
1313 
1314         /*
1315          * Set up the default DTrace library path.  Once set, the next call to
1316          * dt_compile() will compile all the libraries.  We intentionally defer
1317          * library processing to improve overhead for clients that don't ever
1318          * compile, and to provide better error reporting (because the full
1319          * reporting of compiler errors requires dtrace_open() to succeed).
1320          */
1321         if (zroot != NULL)
1322                 (void) asprintf(&libpath, "%s/%s", zroot, _dtrace_libdir);
1323         if (dtrace_setopt(dtp, "libdir",
1324             libpath != NULL ? libpath : _dtrace_libdir) != 0)
1325                 return (set_open_errno(dtp, errp, dtp->dt_errno));
1326 
1327         if (libpath != NULL)
1328                 free(libpath);
1329 
1330         return (dtp);
1331 }
1332 
1333 dtrace_hdl_t *
1334 dtrace_open(int version, int flags, int *errp)
1335 {
1336         return (dt_vopen(version, flags, errp, NULL, NULL));
1337 }
1338 
1339 dtrace_hdl_t *
1340 dtrace_vopen(int version, int flags, int *errp,
1341     const dtrace_vector_t *vector, void *arg)
1342 {
1343         return (dt_vopen(version, flags, errp, vector, arg));
1344 }
1345 
1346 void
1347 dtrace_close(dtrace_hdl_t *dtp)
1348 {
1349         dt_ident_t *idp, *ndp;