e0ea83ebb1
Summary of changes: - libdtrace changeset: 12902:3bb859a7330c user: Bryan Cantrill <Bryan.Cantrill@Sun.COM> date: Fri Jul 23 17:34:02 2010 -0700 summary: 6679140 asymmetric alloc/dealloc activity can induce dynamic variable drops changeset: 12692:4341b447c069 user: Ali Bahrami <Ali.Bahrami@Oracle.COM> date: Thu Jun 24 18:16:42 2010 -0600 summary: 6916796 OSnet mapfiles should use version 2 link-editor syntax changeset: 12507:501806a754d2 user: Alan Maguire <Alan.Maguire@Sun.COM> date: Thu May 27 17:29:51 2010 -0400 summary: PSARC 2010/106 DTrace TCP and UDP providers changeset: 11798:1e7f1f154004 user: Roger A. Faulkner <Roger.Faulkner@Sun.COM> date: Sun Feb 28 18:42:20 2010 -0800 summary: PSARC 2009/657 delete obsolete system call traps changeset: 11466:d60272412fb0 user: Roger A. Faulkner <Roger.Faulkner@Sun.COM> date: Mon Jan 11 17:42:13 2010 -0800 summary: 6915578 MUTEX_HELD() and RW_LOCK_HELD() macros should be available to Posix threads changeset: 11237:0d23e47ed228 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 03 13:39:19 2009 +0000 summary: 6795386 macro arguments and globbing in DTrace probe descriptions don't mix changeset: 10791:944abfb5b345 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Wed Oct 14 11:25:23 2009 +0100 summary: 6886953 large symbols lead to stack exhaustion changeset: 10326:8e3fbeec2d76 user: Siddheshwar Mahesh <Siddheshwar.Mahesh@Sun.COM> date: Mon Aug 17 13:26:49 2009 -0500 summary: 6868411 NFS provider generates error on ci_remote on RDMA operations changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 10044:2643c1cd9e2a user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Mon Jul 06 21:19:41 2009 -0400 summary: 6855027 iscsit.d breaks dtrace in osol based on snv_117 changeset: 9900:1b86d65a4f9e user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Thu Jun 18 13:16:39 2009 -0600 summary: 6851224 elf_getshnum() and elf_getshstrndx() incompatible with 2002 ELF gABI agreement changeset: 9885:a3d5e9d9e779 user: Robert Mastors <Robert.Mastors@Sun.COM> date: Tue Jun 16 15:25:25 2009 -0500 summary: 6711844 assert: vp->v_shrlocks == 0L, file: ../../common/fs/vnode.c, line: 2333 changeset: 9881:741c9e4e094c user: Charles Ting <Charles.Ting@Sun.COM> date: Tue Jun 16 14:51:40 2009 -0400 summary: 6849606 SRP DTrace Probe for xfer-done misses completion of READ transfers changeset: 9829:e8059fcaee97 user: Charles Ting <Charles.Ting@Sun.COM> date: Tue Jun 09 10:11:35 2009 -0400 summary: 6804431 Add Dtrace probes to SRPT changeset: 9812:a2990074321f user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Mon Jun 08 09:49:48 2009 -0400 summary: 6847237 The iscsit.d DTrace translator should include iscsi.d for the definition of iscsiinfo_t changeset: 9721:4f7e194c7c37 user: Priya Krishnan <Priya.Krishnan@Sun.COM> date: Tue May 26 10:40:43 2009 -0400 summary: 6809997 COMSTAR iscsi target DTrace Provider needed changeset: 9625:8aa5731291b4 user: Sam Cramer <Sam.Cramer@Sun.COM> date: Wed May 13 17:10:06 2009 -0700 summary: 6840354 "/usr/lib/dtrace/fc.d", line 59: syntax error near "fct_local_port_t" changeset: 9609:8874cc8d5e3f user: Sam Cramer <Sam.Cramer@Sun.COM> date: Mon May 11 21:02:27 2009 -0700 summary: 6809580 fct DTrace providers needed for qlt changeset: 9578:c4b38ec17f4e user: Sam Cramer <Sam.Cramer@Sun.COM> date: Fri May 08 12:12:40 2009 -0700 summary: 6809580 fct DTrace providers needed for qlt changeset: 9531:dc8924ef7839 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Mon May 04 11:48:15 2009 -0700 summary: 6730130 dtrace missing printf handler for stdev changeset: 9389:750ed3471e90 user: Vamsi Nagineni <Vamsi.Krishna@Sun.COM> date: Fri Apr 17 06:26:47 2009 -0700 summary: 6812050 dtrace should translate curpsinfo->pr_contract changeset: 9085:ff7eb0bace56 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Wed Mar 18 13:28:28 2009 -0600 summary: 6813909 generalize eh_frame support to non-amd64 platforms changeset: 8803:8c01b39012c9 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Fri Feb 13 07:13:13 2009 +0000 summary: PSARC 2008/480 DTrace CPC Provider changeset: 8744:03d5725cda56 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Tue Feb 10 09:38:02 2009 -0700 summary: 6798660 Cadmium .NOT file processing problem with CWD relative file paths changeset: 8337:079ecc003ca6 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 11 11:26:47 2008 +0000 summary: 6750659 drti.o crashes app due to corrupt environment changeset: 7991:d3b751ef3d85 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Nov 03 10:26:23 2008 +0000 summary: 6738982 Representative thread after DTrace stop() action is incorrect changeset: 7208:568549b138d8 user: vv149972 date: Mon Jul 28 23:14:31 2008 -0700 summary: 6696397 NFS v3 provider reports all UDP clients as 0.0.0.0 changeset: 6878:360e73ea6b0c user: brendan date: Fri Jun 13 19:06:55 2008 -0700 summary: PSARC 2008/302 DTrace IP Provider changeset: 6554:b5817e112852 user: ahl date: Mon May 05 14:38:24 2008 -0700 summary: 6677812 race between dtrace activities in non-local zones ---- - ctf tools: changeset: 12177:800b7f847f1e user: Surya Prakki <Surya.Prakki@Sun.COM> date: Sun Apr 18 23:59:57 2010 -0700 summary: 6941452 ctfconvert fails on VLAs with code generated by SS12u1 changeset: 11432:c1c450bf62f2 user: John Levon <john.levon@sun.com> date: Tue Jan 05 06:57:53 2010 -0800 summary: 6905711 anonymous and empty SOUs crash ctfconvert changeset: 11227:cd2ac59c39f2 user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Wed Dec 02 15:37:55 2009 -0700 summary: 6900241 ld should track SHT_GROUP sections by symbol name, not section name changeset: 10380:5394a7172e1f user: Ali Bahrami <Ali.Bahrami@Sun.COM> date: Tue Aug 25 13:51:43 2009 -0600 summary: 6866605 SUNWonbld ELF analysis tools need overhaul (fix ctfmerge/libc dependency) changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 10206:51f52702df72 user: John Levon <john.levon@sun.com> date: Wed Jul 29 14:36:30 2009 -0700 summary: 6854065 CTF tools should error out given 1024+-member structures changeset: 7230:429b4f7acf1a user: sn199410 date: Wed Jul 30 16:10:30 2008 -0700 summary: 6575435 ctf tools cannot handle C99 VLAs ("variable length arrays") changeset: 6936:72189fcd99e4 user: sommerfe date: Sun Jun 22 09:13:44 2008 -0700 summary: 6716983 left-for-dead ctfmerge worker threads awake to take out maker ---- - dtrace command utility: changeset: 12507:501806a754d2 user: Alan Maguire <Alan.Maguire@Sun.COM> date: Thu May 27 17:29:51 2010 -0400 summary: PSARC 2010/106 DTrace TCP and UDP providers changeset: 11838:32bb5d254240 user: Liane Praza <Liane.Praza@Sun.COM> date: Tue Mar 02 19:29:26 2010 -0700 summary: PSARC 2010/067 Interim modernization updates changeset: 11270:47a962fe7b45 user: Frank Van Der Linden <Frank.Vanderlinden@Sun.COM> date: Mon Dec 07 13:47:36 2009 -0800 summary: 6907170 fix for 6875656 left out updates to DTrace test suite changeset: 11237:0d23e47ed228 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 03 13:39:19 2009 +0000 summary: 6795386 macro arguments and globbing in DTrace probe descriptions don't mix changeset: 11153:dec430d20576 user: Frank Van Der Linden <Frank.Vanderlinden@Sun.COM> date: Sun Nov 22 19:22:26 2009 -0800 summary: 6875656 xdt needs to support more XenTrace probes changeset: 11102:b91faef0c984 user: Gavin Maltby <Gavin.Maltby@Sun.COM> date: Thu Nov 19 15:28:11 2009 +1100 summary: PSARC/2009/554 door_xcreate - extended door creation interface for private doors changeset: 11066:cebb50cbe4f9 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Fri Nov 13 01:32:32 2009 -0800 summary: PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling changeset: 10791:944abfb5b345 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Wed Oct 14 11:25:23 2009 +0100 summary: 6886953 large symbols lead to stack exhaustion changeset: 10207:87c40ea3fc4b user: jmcp <James.McPherson@Sun.COM> date: Wed Jul 29 16:56:03 2009 -0700 summary: 6864230 hiho, hiho, it'ch chtime for CH to go changeset: 9531:dc8924ef7839 user: Rafael Vanoni <rafael.vanoni@sun.com> date: Mon May 04 11:48:15 2009 -0700 summary: 6730130 dtrace missing printf handler for stdev changeset: 9397:e667d620a75c user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Apr 20 07:58:44 2009 +0100 summary: 6806023 cpc provider event name validation needs to be a bit tighter changeset: 8803:8c01b39012c9 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Fri Feb 13 07:13:13 2009 +0000 summary: PSARC 2008/480 DTrace CPC Provider changeset: 8605:0189cb9c5358 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Jan 22 12:09:13 2009 +0000 summary: 6749441 intrstat(1M) shows zeroed values after suspend/resume changeset: 8337:079ecc003ca6 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Thu Dec 11 11:26:47 2008 +0000 summary: 6750659 drti.o crashes app due to corrupt environment changeset: 8287:771477e4b843 user: John Sonnenschein <John.Sonnenschein@Sun.COM> date: Fri Dec 05 19:08:38 2008 -0800 summary: PSARC 2005/462 Removal of Perl 5.6.1 from Solaris 11 changeset: 7991:d3b751ef3d85 user: Jonathan Haslam <Jonathan.Haslam@Sun.COM> date: Mon Nov 03 10:26:23 2008 +0000 summary: 6738982 Representative thread after DTrace stop() action is incorrect changeset: 7502:da077e5d991e user: Aruna Ramakrishna <aruna@cs.umn.edu> date: Sat Sep 06 05:36:02 2008 -0400 summary: 6706947 tcp_trace should be replaced with dtrace probes. changeset: 7484:a48e950bad22 user: Tom Erickson <tomee@eng.sun.com> date: Wed Sep 03 15:14:25 2008 -0700 summary: 6737926 getAggregate() method fails to specify anonymous aggregation explicitly changeset: 7299:d9a056040774 user: John Beck <John.Beck@Sun.COM> date: Thu Aug 07 12:44:26 2008 -0700 summary: 6734627 protocmp complains about opt/SUNWdtrt/README after TW -> Hg switch changeset: 6998:58787ea78303 user: brendan date: Tue Jul 01 18:28:22 2008 -0700 summary: 6721426 tst.sdtargs.d passes despite dtrace "invalid address" error changeset: 6878:360e73ea6b0c user: brendan date: Fri Jun 13 19:06:55 2008 -0700 summary: PSARC 2008/302 DTrace IP Provider changeset: 6670:1961a43f2335 user: tariq date: Tue May 20 15:08:16 2008 -0700 summary: 6685348 Hypervisor event provider for DTrace changeset: 6554:b5817e112852 user: ahl date: Mon May 05 14:38:24 2008 -0700 summary: 6677812 race between dtrace activities in non-local zones changeset: 6543:465433824d87 user: rie date: Fri May 02 15:01:06 2008 -0700 summary: 6683064 check_rtime could do with some spring cleaning
1314 lines
34 KiB
C
1314 lines
34 KiB
C
/*
|
|
* CDDL HEADER START
|
|
*
|
|
* The contents of this file are subject to the terms of the
|
|
* Common Development and Distribution License (the "License").
|
|
* You may not use this file except in compliance with the License.
|
|
*
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
* See the License for the specific language governing permissions
|
|
* and limitations under the License.
|
|
*
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
*
|
|
* CDDL HEADER END
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/modctl.h>
|
|
#include <sys/kobj.h>
|
|
#include <sys/kobj_impl.h>
|
|
#include <sys/sysmacros.h>
|
|
#include <sys/elf.h>
|
|
#include <sys/task.h>
|
|
|
|
#include <unistd.h>
|
|
#include <project.h>
|
|
#include <strings.h>
|
|
#include <stdlib.h>
|
|
#include <libelf.h>
|
|
#include <limits.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <dirent.h>
|
|
|
|
#include <dt_strtab.h>
|
|
#include <dt_module.h>
|
|
#include <dt_impl.h>
|
|
|
|
static const char *dt_module_strtab; /* active strtab for qsort callbacks */
|
|
|
|
static void
|
|
dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
|
|
{
|
|
dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
|
|
uint_t h;
|
|
|
|
assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
|
|
|
|
dsp->ds_symid = id;
|
|
h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
|
|
dsp->ds_next = dmp->dm_symbuckets[h];
|
|
dmp->dm_symbuckets[h] = dmp->dm_symfree++;
|
|
}
|
|
|
|
static uint_t
|
|
dt_module_syminit32(dt_module_t *dmp)
|
|
{
|
|
#if STT_NUM != (STT_TLS + 1)
|
|
#error "STT_NUM has grown. update dt_module_syminit32()"
|
|
#endif
|
|
|
|
const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
|
|
const char *base = dmp->dm_strtab.cts_data;
|
|
size_t ss_size = dmp->dm_strtab.cts_size;
|
|
uint_t i, n = dmp->dm_nsymelems;
|
|
uint_t asrsv = 0;
|
|
|
|
for (i = 0; i < n; i++, sym++) {
|
|
const char *name = base + sym->st_name;
|
|
uchar_t type = ELF32_ST_TYPE(sym->st_info);
|
|
|
|
if (type >= STT_NUM || type == STT_SECTION)
|
|
continue; /* skip sections and unknown types */
|
|
|
|
if (sym->st_name == 0 || sym->st_name >= ss_size)
|
|
continue; /* skip null or invalid names */
|
|
|
|
if (sym->st_value != 0 &&
|
|
(ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
|
|
asrsv++; /* reserve space in the address map */
|
|
|
|
dt_module_symhash_insert(dmp, name, i);
|
|
}
|
|
|
|
return (asrsv);
|
|
}
|
|
|
|
static uint_t
|
|
dt_module_syminit64(dt_module_t *dmp)
|
|
{
|
|
#if STT_NUM != (STT_TLS + 1)
|
|
#error "STT_NUM has grown. update dt_module_syminit64()"
|
|
#endif
|
|
|
|
const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
|
|
const char *base = dmp->dm_strtab.cts_data;
|
|
size_t ss_size = dmp->dm_strtab.cts_size;
|
|
uint_t i, n = dmp->dm_nsymelems;
|
|
uint_t asrsv = 0;
|
|
|
|
for (i = 0; i < n; i++, sym++) {
|
|
const char *name = base + sym->st_name;
|
|
uchar_t type = ELF64_ST_TYPE(sym->st_info);
|
|
|
|
if (type >= STT_NUM || type == STT_SECTION)
|
|
continue; /* skip sections and unknown types */
|
|
|
|
if (sym->st_name == 0 || sym->st_name >= ss_size)
|
|
continue; /* skip null or invalid names */
|
|
|
|
if (sym->st_value != 0 &&
|
|
(ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
|
|
asrsv++; /* reserve space in the address map */
|
|
|
|
dt_module_symhash_insert(dmp, name, i);
|
|
}
|
|
|
|
return (asrsv);
|
|
}
|
|
|
|
/*
|
|
* Sort comparison function for 32-bit symbol address-to-name lookups. We sort
|
|
* symbols by value. If values are equal, we prefer the symbol that is
|
|
* non-zero sized, typed, not weak, or lexically first, in that order.
|
|
*/
|
|
static int
|
|
dt_module_symcomp32(const void *lp, const void *rp)
|
|
{
|
|
Elf32_Sym *lhs = *((Elf32_Sym **)lp);
|
|
Elf32_Sym *rhs = *((Elf32_Sym **)rp);
|
|
|
|
if (lhs->st_value != rhs->st_value)
|
|
return (lhs->st_value > rhs->st_value ? 1 : -1);
|
|
|
|
if ((lhs->st_size == 0) != (rhs->st_size == 0))
|
|
return (lhs->st_size == 0 ? 1 : -1);
|
|
|
|
if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
|
|
(ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
|
|
return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
|
|
|
|
if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
|
|
(ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
|
|
return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
|
|
|
|
return (strcmp(dt_module_strtab + lhs->st_name,
|
|
dt_module_strtab + rhs->st_name));
|
|
}
|
|
|
|
/*
|
|
* Sort comparison function for 64-bit symbol address-to-name lookups. We sort
|
|
* symbols by value. If values are equal, we prefer the symbol that is
|
|
* non-zero sized, typed, not weak, or lexically first, in that order.
|
|
*/
|
|
static int
|
|
dt_module_symcomp64(const void *lp, const void *rp)
|
|
{
|
|
Elf64_Sym *lhs = *((Elf64_Sym **)lp);
|
|
Elf64_Sym *rhs = *((Elf64_Sym **)rp);
|
|
|
|
if (lhs->st_value != rhs->st_value)
|
|
return (lhs->st_value > rhs->st_value ? 1 : -1);
|
|
|
|
if ((lhs->st_size == 0) != (rhs->st_size == 0))
|
|
return (lhs->st_size == 0 ? 1 : -1);
|
|
|
|
if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
|
|
(ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
|
|
return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
|
|
|
|
if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
|
|
(ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
|
|
return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
|
|
|
|
return (strcmp(dt_module_strtab + lhs->st_name,
|
|
dt_module_strtab + rhs->st_name));
|
|
}
|
|
|
|
static void
|
|
dt_module_symsort32(dt_module_t *dmp)
|
|
{
|
|
Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
|
|
Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
|
|
const dt_sym_t *dsp = dmp->dm_symchains + 1;
|
|
uint_t i, n = dmp->dm_symfree;
|
|
|
|
for (i = 1; i < n; i++, dsp++) {
|
|
Elf32_Sym *sym = symtab + dsp->ds_symid;
|
|
if (sym->st_value != 0 &&
|
|
(ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
|
|
*sympp++ = sym;
|
|
}
|
|
|
|
dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
|
|
assert(dmp->dm_aslen <= dmp->dm_asrsv);
|
|
|
|
dt_module_strtab = dmp->dm_strtab.cts_data;
|
|
qsort(dmp->dm_asmap, dmp->dm_aslen,
|
|
sizeof (Elf32_Sym *), dt_module_symcomp32);
|
|
dt_module_strtab = NULL;
|
|
}
|
|
|
|
static void
|
|
dt_module_symsort64(dt_module_t *dmp)
|
|
{
|
|
Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
|
|
Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
|
|
const dt_sym_t *dsp = dmp->dm_symchains + 1;
|
|
uint_t i, n = dmp->dm_symfree;
|
|
|
|
for (i = 1; i < n; i++, dsp++) {
|
|
Elf64_Sym *sym = symtab + dsp->ds_symid;
|
|
if (sym->st_value != 0 &&
|
|
(ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
|
|
*sympp++ = sym;
|
|
}
|
|
|
|
dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
|
|
assert(dmp->dm_aslen <= dmp->dm_asrsv);
|
|
|
|
dt_module_strtab = dmp->dm_strtab.cts_data;
|
|
qsort(dmp->dm_asmap, dmp->dm_aslen,
|
|
sizeof (Elf64_Sym *), dt_module_symcomp64);
|
|
dt_module_strtab = NULL;
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
|
|
{
|
|
if (dst != NULL) {
|
|
dst->st_name = src->st_name;
|
|
dst->st_info = src->st_info;
|
|
dst->st_other = src->st_other;
|
|
dst->st_shndx = src->st_shndx;
|
|
dst->st_value = src->st_value;
|
|
dst->st_size = src->st_size;
|
|
}
|
|
|
|
return (dst);
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
|
|
{
|
|
if (dst != NULL)
|
|
bcopy(src, dst, sizeof (GElf_Sym));
|
|
|
|
return (dst);
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symname32(dt_module_t *dmp, const char *name,
|
|
GElf_Sym *symp, uint_t *idp)
|
|
{
|
|
const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
|
|
const char *strtab = dmp->dm_strtab.cts_data;
|
|
|
|
const Elf32_Sym *sym;
|
|
const dt_sym_t *dsp;
|
|
uint_t i, h;
|
|
|
|
if (dmp->dm_nsymelems == 0)
|
|
return (NULL);
|
|
|
|
h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
|
|
|
|
for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
|
|
dsp = &dmp->dm_symchains[i];
|
|
sym = symtab + dsp->ds_symid;
|
|
|
|
if (strcmp(name, strtab + sym->st_name) == 0) {
|
|
if (idp != NULL)
|
|
*idp = dsp->ds_symid;
|
|
return (dt_module_symgelf32(sym, symp));
|
|
}
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symname64(dt_module_t *dmp, const char *name,
|
|
GElf_Sym *symp, uint_t *idp)
|
|
{
|
|
const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
|
|
const char *strtab = dmp->dm_strtab.cts_data;
|
|
|
|
const Elf64_Sym *sym;
|
|
const dt_sym_t *dsp;
|
|
uint_t i, h;
|
|
|
|
if (dmp->dm_nsymelems == 0)
|
|
return (NULL);
|
|
|
|
h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
|
|
|
|
for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
|
|
dsp = &dmp->dm_symchains[i];
|
|
sym = symtab + dsp->ds_symid;
|
|
|
|
if (strcmp(name, strtab + sym->st_name) == 0) {
|
|
if (idp != NULL)
|
|
*idp = dsp->ds_symid;
|
|
return (dt_module_symgelf64(sym, symp));
|
|
}
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
|
|
GElf_Sym *symp, uint_t *idp)
|
|
{
|
|
const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
|
|
const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
|
|
const Elf32_Sym *sym;
|
|
|
|
uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
|
|
Elf32_Addr v;
|
|
|
|
if (dmp->dm_aslen == 0)
|
|
return (NULL);
|
|
|
|
while (hi - lo > 1) {
|
|
mid = (lo + hi) / 2;
|
|
if (addr >= asmap[mid]->st_value)
|
|
lo = mid;
|
|
else
|
|
hi = mid;
|
|
}
|
|
|
|
i = addr < asmap[hi]->st_value ? lo : hi;
|
|
sym = asmap[i];
|
|
v = sym->st_value;
|
|
|
|
/*
|
|
* If the previous entry has the same value, improve our choice. The
|
|
* order of equal-valued symbols is determined by the comparison func.
|
|
*/
|
|
while (i-- != 0 && asmap[i]->st_value == v)
|
|
sym = asmap[i];
|
|
|
|
if (addr - sym->st_value < MAX(sym->st_size, 1)) {
|
|
if (idp != NULL)
|
|
*idp = (uint_t)(sym - symtab);
|
|
return (dt_module_symgelf32(sym, symp));
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
static GElf_Sym *
|
|
dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
|
|
GElf_Sym *symp, uint_t *idp)
|
|
{
|
|
const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
|
|
const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
|
|
const Elf64_Sym *sym;
|
|
|
|
uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
|
|
Elf64_Addr v;
|
|
|
|
if (dmp->dm_aslen == 0)
|
|
return (NULL);
|
|
|
|
while (hi - lo > 1) {
|
|
mid = (lo + hi) / 2;
|
|
if (addr >= asmap[mid]->st_value)
|
|
lo = mid;
|
|
else
|
|
hi = mid;
|
|
}
|
|
|
|
i = addr < asmap[hi]->st_value ? lo : hi;
|
|
sym = asmap[i];
|
|
v = sym->st_value;
|
|
|
|
/*
|
|
* If the previous entry has the same value, improve our choice. The
|
|
* order of equal-valued symbols is determined by the comparison func.
|
|
*/
|
|
while (i-- != 0 && asmap[i]->st_value == v)
|
|
sym = asmap[i];
|
|
|
|
if (addr - sym->st_value < MAX(sym->st_size, 1)) {
|
|
if (idp != NULL)
|
|
*idp = (uint_t)(sym - symtab);
|
|
return (dt_module_symgelf64(sym, symp));
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
static const dt_modops_t dt_modops_32 = {
|
|
dt_module_syminit32,
|
|
dt_module_symsort32,
|
|
dt_module_symname32,
|
|
dt_module_symaddr32
|
|
};
|
|
|
|
static const dt_modops_t dt_modops_64 = {
|
|
dt_module_syminit64,
|
|
dt_module_symsort64,
|
|
dt_module_symname64,
|
|
dt_module_symaddr64
|
|
};
|
|
|
|
dt_module_t *
|
|
dt_module_create(dtrace_hdl_t *dtp, const char *name)
|
|
{
|
|
uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
|
|
dt_module_t *dmp;
|
|
|
|
for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
|
|
if (strcmp(dmp->dm_name, name) == 0)
|
|
return (dmp);
|
|
}
|
|
|
|
if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
|
|
return (NULL); /* caller must handle allocation failure */
|
|
|
|
bzero(dmp, sizeof (dt_module_t));
|
|
(void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
|
|
dt_list_append(&dtp->dt_modlist, dmp);
|
|
dmp->dm_next = dtp->dt_mods[h];
|
|
dtp->dt_mods[h] = dmp;
|
|
dtp->dt_nmods++;
|
|
|
|
if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
|
|
dmp->dm_ops = &dt_modops_64;
|
|
else
|
|
dmp->dm_ops = &dt_modops_32;
|
|
|
|
return (dmp);
|
|
}
|
|
|
|
dt_module_t *
|
|
dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
|
|
{
|
|
uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
|
|
dt_module_t *dmp;
|
|
|
|
for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
|
|
if (strcmp(dmp->dm_name, name) == 0)
|
|
return (dmp);
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
/*ARGSUSED*/
|
|
dt_module_t *
|
|
dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
|
|
{
|
|
return (ctfp ? ctf_getspecific(ctfp) : NULL);
|
|
}
|
|
|
|
static int
|
|
dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
|
|
{
|
|
const char *s;
|
|
size_t shstrs;
|
|
GElf_Shdr sh;
|
|
Elf_Data *dp;
|
|
Elf_Scn *sp;
|
|
|
|
if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
|
|
return (dt_set_errno(dtp, EDT_NOTLOADED));
|
|
|
|
for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
|
|
if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
|
|
(s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
|
|
continue; /* skip any malformed sections */
|
|
|
|
if (sh.sh_type == ctsp->cts_type &&
|
|
sh.sh_entsize == ctsp->cts_entsize &&
|
|
strcmp(s, ctsp->cts_name) == 0)
|
|
break; /* section matches specification */
|
|
}
|
|
|
|
/*
|
|
* If the section isn't found, return success but leave cts_data set
|
|
* to NULL and cts_size set to zero for our caller.
|
|
*/
|
|
if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
|
|
return (0);
|
|
|
|
ctsp->cts_data = dp->d_buf;
|
|
ctsp->cts_size = dp->d_size;
|
|
|
|
dt_dprintf("loaded %s [%s] (%lu bytes)\n",
|
|
dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
|
|
|
|
return (0);
|
|
}
|
|
|
|
int
|
|
dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
|
|
{
|
|
if (dmp->dm_flags & DT_DM_LOADED)
|
|
return (0); /* module is already loaded */
|
|
|
|
dmp->dm_ctdata.cts_name = ".SUNW_ctf";
|
|
dmp->dm_ctdata.cts_type = SHT_PROGBITS;
|
|
dmp->dm_ctdata.cts_flags = 0;
|
|
dmp->dm_ctdata.cts_data = NULL;
|
|
dmp->dm_ctdata.cts_size = 0;
|
|
dmp->dm_ctdata.cts_entsize = 0;
|
|
dmp->dm_ctdata.cts_offset = 0;
|
|
|
|
dmp->dm_symtab.cts_name = ".symtab";
|
|
dmp->dm_symtab.cts_type = SHT_SYMTAB;
|
|
dmp->dm_symtab.cts_flags = 0;
|
|
dmp->dm_symtab.cts_data = NULL;
|
|
dmp->dm_symtab.cts_size = 0;
|
|
dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
|
|
sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
|
|
dmp->dm_symtab.cts_offset = 0;
|
|
|
|
dmp->dm_strtab.cts_name = ".strtab";
|
|
dmp->dm_strtab.cts_type = SHT_STRTAB;
|
|
dmp->dm_strtab.cts_flags = 0;
|
|
dmp->dm_strtab.cts_data = NULL;
|
|
dmp->dm_strtab.cts_size = 0;
|
|
dmp->dm_strtab.cts_entsize = 0;
|
|
dmp->dm_strtab.cts_offset = 0;
|
|
|
|
/*
|
|
* Attempt to load the module's CTF section, symbol table section, and
|
|
* string table section. Note that modules may not contain CTF data:
|
|
* this will result in a successful load_sect but data of size zero.
|
|
* We will then fail if dt_module_getctf() is called, as shown below.
|
|
*/
|
|
if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
|
|
dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
|
|
dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
|
|
dt_module_unload(dtp, dmp);
|
|
return (-1); /* dt_errno is set for us */
|
|
}
|
|
|
|
/*
|
|
* Allocate the hash chains and hash buckets for symbol name lookup.
|
|
* This is relatively simple since the symbol table is of fixed size
|
|
* and is known in advance. We allocate one extra element since we
|
|
* use element indices instead of pointers and zero is our sentinel.
|
|
*/
|
|
dmp->dm_nsymelems =
|
|
dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
|
|
|
|
dmp->dm_nsymbuckets = _dtrace_strbuckets;
|
|
dmp->dm_symfree = 1; /* first free element is index 1 */
|
|
|
|
dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
|
|
dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
|
|
|
|
if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
|
|
dt_module_unload(dtp, dmp);
|
|
return (dt_set_errno(dtp, EDT_NOMEM));
|
|
}
|
|
|
|
bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
|
|
bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
|
|
|
|
/*
|
|
* Iterate over the symbol table data buffer and insert each symbol
|
|
* name into the name hash if the name and type are valid. Then
|
|
* allocate the address map, fill it in, and sort it.
|
|
*/
|
|
dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
|
|
|
|
dt_dprintf("hashed %s [%s] (%u symbols)\n",
|
|
dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
|
|
|
|
if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
|
|
dt_module_unload(dtp, dmp);
|
|
return (dt_set_errno(dtp, EDT_NOMEM));
|
|
}
|
|
|
|
dmp->dm_ops->do_symsort(dmp);
|
|
|
|
dt_dprintf("sorted %s [%s] (%u symbols)\n",
|
|
dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
|
|
|
|
dmp->dm_flags |= DT_DM_LOADED;
|
|
return (0);
|
|
}
|
|
|
|
ctf_file_t *
|
|
dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
|
|
{
|
|
const char *parent;
|
|
dt_module_t *pmp;
|
|
ctf_file_t *pfp;
|
|
int model;
|
|
|
|
if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
|
|
return (dmp->dm_ctfp);
|
|
|
|
if (dmp->dm_ops == &dt_modops_64)
|
|
model = CTF_MODEL_LP64;
|
|
else
|
|
model = CTF_MODEL_ILP32;
|
|
|
|
/*
|
|
* If the data model of the module does not match our program data
|
|
* model, then do not permit CTF from this module to be opened and
|
|
* returned to the compiler. If we support mixed data models in the
|
|
* future for combined kernel/user tracing, this can be removed.
|
|
*/
|
|
if (dtp->dt_conf.dtc_ctfmodel != model) {
|
|
(void) dt_set_errno(dtp, EDT_DATAMODEL);
|
|
return (NULL);
|
|
}
|
|
|
|
if (dmp->dm_ctdata.cts_size == 0) {
|
|
(void) dt_set_errno(dtp, EDT_NOCTF);
|
|
return (NULL);
|
|
}
|
|
|
|
dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
|
|
&dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
|
|
|
|
if (dmp->dm_ctfp == NULL) {
|
|
(void) dt_set_errno(dtp, EDT_CTF);
|
|
return (NULL);
|
|
}
|
|
|
|
(void) ctf_setmodel(dmp->dm_ctfp, model);
|
|
ctf_setspecific(dmp->dm_ctfp, dmp);
|
|
|
|
if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
|
|
if ((pmp = dt_module_create(dtp, parent)) == NULL ||
|
|
(pfp = dt_module_getctf(dtp, pmp)) == NULL) {
|
|
if (pmp == NULL)
|
|
(void) dt_set_errno(dtp, EDT_NOMEM);
|
|
goto err;
|
|
}
|
|
|
|
if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
|
|
dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
|
|
(void) dt_set_errno(dtp, EDT_CTF);
|
|
goto err;
|
|
}
|
|
}
|
|
|
|
dt_dprintf("loaded CTF container for %s (%p)\n",
|
|
dmp->dm_name, (void *)dmp->dm_ctfp);
|
|
|
|
return (dmp->dm_ctfp);
|
|
|
|
err:
|
|
ctf_close(dmp->dm_ctfp);
|
|
dmp->dm_ctfp = NULL;
|
|
return (NULL);
|
|
}
|
|
|
|
/*ARGSUSED*/
|
|
void
|
|
dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
|
|
{
|
|
ctf_close(dmp->dm_ctfp);
|
|
dmp->dm_ctfp = NULL;
|
|
|
|
bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
|
|
bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
|
|
bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
|
|
|
|
if (dmp->dm_symbuckets != NULL) {
|
|
free(dmp->dm_symbuckets);
|
|
dmp->dm_symbuckets = NULL;
|
|
}
|
|
|
|
if (dmp->dm_symchains != NULL) {
|
|
free(dmp->dm_symchains);
|
|
dmp->dm_symchains = NULL;
|
|
}
|
|
|
|
if (dmp->dm_asmap != NULL) {
|
|
free(dmp->dm_asmap);
|
|
dmp->dm_asmap = NULL;
|
|
}
|
|
|
|
dmp->dm_symfree = 0;
|
|
dmp->dm_nsymbuckets = 0;
|
|
dmp->dm_nsymelems = 0;
|
|
dmp->dm_asrsv = 0;
|
|
dmp->dm_aslen = 0;
|
|
|
|
dmp->dm_text_va = NULL;
|
|
dmp->dm_text_size = 0;
|
|
dmp->dm_data_va = NULL;
|
|
dmp->dm_data_size = 0;
|
|
dmp->dm_bss_va = NULL;
|
|
dmp->dm_bss_size = 0;
|
|
|
|
if (dmp->dm_extern != NULL) {
|
|
dt_idhash_destroy(dmp->dm_extern);
|
|
dmp->dm_extern = NULL;
|
|
}
|
|
|
|
(void) elf_end(dmp->dm_elf);
|
|
dmp->dm_elf = NULL;
|
|
|
|
dmp->dm_flags &= ~DT_DM_LOADED;
|
|
}
|
|
|
|
void
|
|
dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
|
|
{
|
|
uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
|
|
dt_module_t **dmpp = &dtp->dt_mods[h];
|
|
|
|
dt_list_delete(&dtp->dt_modlist, dmp);
|
|
assert(dtp->dt_nmods != 0);
|
|
dtp->dt_nmods--;
|
|
|
|
/*
|
|
* Now remove this module from its hash chain. We expect to always
|
|
* find the module on its hash chain, so in this loop we assert that
|
|
* we don't run off the end of the list.
|
|
*/
|
|
while (*dmpp != dmp) {
|
|
dmpp = &((*dmpp)->dm_next);
|
|
assert(*dmpp != NULL);
|
|
}
|
|
|
|
*dmpp = dmp->dm_next;
|
|
|
|
dt_module_unload(dtp, dmp);
|
|
free(dmp);
|
|
}
|
|
|
|
/*
|
|
* Insert a new external symbol reference into the specified module. The new
|
|
* symbol will be marked as undefined and is assigned a symbol index beyond
|
|
* any existing cached symbols from this module. We use the ident's di_data
|
|
* field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
|
|
*/
|
|
dt_ident_t *
|
|
dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
|
|
const char *name, const dtrace_typeinfo_t *tip)
|
|
{
|
|
dtrace_syminfo_t *sip;
|
|
dt_ident_t *idp;
|
|
uint_t id;
|
|
|
|
if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
|
|
"extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
|
|
(void) dt_set_errno(dtp, EDT_NOMEM);
|
|
return (NULL);
|
|
}
|
|
|
|
if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
|
|
(void) dt_set_errno(dtp, EDT_SYMOFLOW);
|
|
return (NULL);
|
|
}
|
|
|
|
if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
|
|
(void) dt_set_errno(dtp, EDT_NOMEM);
|
|
return (NULL);
|
|
}
|
|
|
|
idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
|
|
_dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
|
|
|
|
if (idp == NULL) {
|
|
(void) dt_set_errno(dtp, EDT_NOMEM);
|
|
free(sip);
|
|
return (NULL);
|
|
}
|
|
|
|
sip->dts_object = dmp->dm_name;
|
|
sip->dts_name = idp->di_name;
|
|
sip->dts_id = idp->di_id;
|
|
|
|
idp->di_data = sip;
|
|
idp->di_ctfp = tip->dtt_ctfp;
|
|
idp->di_type = tip->dtt_type;
|
|
|
|
return (idp);
|
|
}
|
|
|
|
const char *
|
|
dt_module_modelname(dt_module_t *dmp)
|
|
{
|
|
if (dmp->dm_ops == &dt_modops_64)
|
|
return ("64-bit");
|
|
else
|
|
return ("32-bit");
|
|
}
|
|
|
|
/*
|
|
* Update our module cache by adding an entry for the specified module 'name'.
|
|
* We create the dt_module_t and populate it using /system/object/<name>/.
|
|
*/
|
|
static void
|
|
dt_module_update(dtrace_hdl_t *dtp, const char *name)
|
|
{
|
|
char fname[MAXPATHLEN];
|
|
struct stat64 st;
|
|
int fd, err, bits;
|
|
|
|
dt_module_t *dmp;
|
|
const char *s;
|
|
size_t shstrs;
|
|
GElf_Shdr sh;
|
|
Elf_Data *dp;
|
|
Elf_Scn *sp;
|
|
|
|
(void) snprintf(fname, sizeof (fname),
|
|
"%s/%s/object", OBJFS_ROOT, name);
|
|
|
|
if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
|
|
(dmp = dt_module_create(dtp, name)) == NULL) {
|
|
dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
|
|
(void) close(fd);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Since the module can unload out from under us (and /system/object
|
|
* will return ENOENT), tell libelf to cook the entire file now and
|
|
* then close the underlying file descriptor immediately. If this
|
|
* succeeds, we know that we can continue safely using dmp->dm_elf.
|
|
*/
|
|
dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
|
|
err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
|
|
(void) close(fd);
|
|
|
|
if (dmp->dm_elf == NULL || err == -1 ||
|
|
elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
|
|
dt_dprintf("failed to load %s: %s\n",
|
|
fname, elf_errmsg(elf_errno()));
|
|
dt_module_destroy(dtp, dmp);
|
|
return;
|
|
}
|
|
|
|
switch (gelf_getclass(dmp->dm_elf)) {
|
|
case ELFCLASS32:
|
|
dmp->dm_ops = &dt_modops_32;
|
|
bits = 32;
|
|
break;
|
|
case ELFCLASS64:
|
|
dmp->dm_ops = &dt_modops_64;
|
|
bits = 64;
|
|
break;
|
|
default:
|
|
dt_dprintf("failed to load %s: unknown ELF class\n", fname);
|
|
dt_module_destroy(dtp, dmp);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Iterate over the section headers locating various sections of
|
|
* interest and use their attributes to flesh out the dt_module_t.
|
|
*/
|
|
for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
|
|
if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
|
|
(s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
|
|
continue; /* skip any malformed sections */
|
|
|
|
if (strcmp(s, ".text") == 0) {
|
|
dmp->dm_text_size = sh.sh_size;
|
|
dmp->dm_text_va = sh.sh_addr;
|
|
} else if (strcmp(s, ".data") == 0) {
|
|
dmp->dm_data_size = sh.sh_size;
|
|
dmp->dm_data_va = sh.sh_addr;
|
|
} else if (strcmp(s, ".bss") == 0) {
|
|
dmp->dm_bss_size = sh.sh_size;
|
|
dmp->dm_bss_va = sh.sh_addr;
|
|
} else if (strcmp(s, ".info") == 0 &&
|
|
(dp = elf_getdata(sp, NULL)) != NULL) {
|
|
bcopy(dp->d_buf, &dmp->dm_info,
|
|
MIN(sh.sh_size, sizeof (dmp->dm_info)));
|
|
} else if (strcmp(s, ".filename") == 0 &&
|
|
(dp = elf_getdata(sp, NULL)) != NULL) {
|
|
(void) strlcpy(dmp->dm_file,
|
|
dp->d_buf, sizeof (dmp->dm_file));
|
|
}
|
|
}
|
|
|
|
dmp->dm_flags |= DT_DM_KERNEL;
|
|
dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
|
|
|
|
if (dmp->dm_info.objfs_info_primary)
|
|
dmp->dm_flags |= DT_DM_PRIMARY;
|
|
|
|
dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
|
|
bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
|
|
}
|
|
|
|
/*
|
|
* Unload all the loaded modules and then refresh the module cache with the
|
|
* latest list of loaded modules and their address ranges.
|
|
*/
|
|
void
|
|
dtrace_update(dtrace_hdl_t *dtp)
|
|
{
|
|
dt_module_t *dmp;
|
|
DIR *dirp;
|
|
|
|
for (dmp = dt_list_next(&dtp->dt_modlist);
|
|
dmp != NULL; dmp = dt_list_next(dmp))
|
|
dt_module_unload(dtp, dmp);
|
|
|
|
/*
|
|
* Open /system/object and attempt to create a libdtrace module for
|
|
* each kernel module that is loaded on the current system.
|
|
*/
|
|
if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
|
|
(dirp = opendir(OBJFS_ROOT)) != NULL) {
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
if (dp->d_name[0] != '.')
|
|
dt_module_update(dtp, dp->d_name);
|
|
}
|
|
|
|
(void) closedir(dirp);
|
|
}
|
|
|
|
/*
|
|
* Look up all the macro identifiers and set di_id to the latest value.
|
|
* This code collaborates with dt_lex.l on the use of di_id. We will
|
|
* need to implement something fancier if we need to support non-ints.
|
|
*/
|
|
dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
|
|
dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
|
|
dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
|
|
dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
|
|
dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
|
|
dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
|
|
dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
|
|
dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
|
|
dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
|
|
dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
|
|
|
|
/*
|
|
* Cache the pointers to the modules representing the base executable
|
|
* and the run-time linker in the dtrace client handle. Note that on
|
|
* x86 krtld is folded into unix, so if we don't find it, use unix
|
|
* instead.
|
|
*/
|
|
dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
|
|
dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
|
|
if (dtp->dt_rtld == NULL)
|
|
dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
|
|
|
|
/*
|
|
* If this is the first time we are initializing the module list,
|
|
* remove the module for genunix from the module list and then move it
|
|
* to the front of the module list. We do this so that type and symbol
|
|
* queries encounter genunix and thereby optimize for the common case
|
|
* in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
|
|
*/
|
|
if (dtp->dt_exec != NULL &&
|
|
dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
|
|
dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
|
|
dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
|
|
}
|
|
}
|
|
|
|
static dt_module_t *
|
|
dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
|
|
{
|
|
int err = EDT_NOMOD;
|
|
dt_module_t *dmp;
|
|
|
|
switch ((uintptr_t)object) {
|
|
case (uintptr_t)DTRACE_OBJ_EXEC:
|
|
dmp = dtp->dt_exec;
|
|
break;
|
|
case (uintptr_t)DTRACE_OBJ_RTLD:
|
|
dmp = dtp->dt_rtld;
|
|
break;
|
|
case (uintptr_t)DTRACE_OBJ_CDEFS:
|
|
dmp = dtp->dt_cdefs;
|
|
break;
|
|
case (uintptr_t)DTRACE_OBJ_DDEFS:
|
|
dmp = dtp->dt_ddefs;
|
|
break;
|
|
default:
|
|
dmp = dt_module_create(dtp, object);
|
|
err = EDT_NOMEM;
|
|
}
|
|
|
|
if (dmp == NULL)
|
|
(void) dt_set_errno(dtp, err);
|
|
|
|
return (dmp);
|
|
}
|
|
|
|
/*
|
|
* Exported interface to look up a symbol by name. We return the GElf_Sym and
|
|
* complete symbol information for the matching symbol.
|
|
*/
|
|
int
|
|
dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
|
|
GElf_Sym *symp, dtrace_syminfo_t *sip)
|
|
{
|
|
dt_module_t *dmp;
|
|
dt_ident_t *idp;
|
|
uint_t n, id;
|
|
GElf_Sym sym;
|
|
|
|
uint_t mask = 0; /* mask of dt_module flags to match */
|
|
uint_t bits = 0; /* flag bits that must be present */
|
|
|
|
if (object != DTRACE_OBJ_EVERY &&
|
|
object != DTRACE_OBJ_KMODS &&
|
|
object != DTRACE_OBJ_UMODS) {
|
|
if ((dmp = dt_module_from_object(dtp, object)) == NULL)
|
|
return (-1); /* dt_errno is set for us */
|
|
|
|
if (dt_module_load(dtp, dmp) == -1)
|
|
return (-1); /* dt_errno is set for us */
|
|
n = 1;
|
|
|
|
} else {
|
|
if (object == DTRACE_OBJ_KMODS)
|
|
mask = bits = DT_DM_KERNEL;
|
|
else if (object == DTRACE_OBJ_UMODS)
|
|
mask = DT_DM_KERNEL;
|
|
|
|
dmp = dt_list_next(&dtp->dt_modlist);
|
|
n = dtp->dt_nmods;
|
|
}
|
|
|
|
if (symp == NULL)
|
|
symp = &sym;
|
|
|
|
for (; n > 0; n--, dmp = dt_list_next(dmp)) {
|
|
if ((dmp->dm_flags & mask) != bits)
|
|
continue; /* failed to match required attributes */
|
|
|
|
if (dt_module_load(dtp, dmp) == -1)
|
|
continue; /* failed to load symbol table */
|
|
|
|
if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
|
|
if (sip != NULL) {
|
|
sip->dts_object = dmp->dm_name;
|
|
sip->dts_name = (const char *)
|
|
dmp->dm_strtab.cts_data + symp->st_name;
|
|
sip->dts_id = id;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
if (dmp->dm_extern != NULL &&
|
|
(idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
|
|
if (symp != &sym) {
|
|
symp->st_name = (uintptr_t)idp->di_name;
|
|
symp->st_info =
|
|
GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
|
|
symp->st_other = 0;
|
|
symp->st_shndx = SHN_UNDEF;
|
|
symp->st_value = 0;
|
|
symp->st_size =
|
|
ctf_type_size(idp->di_ctfp, idp->di_type);
|
|
}
|
|
|
|
if (sip != NULL) {
|
|
sip->dts_object = dmp->dm_name;
|
|
sip->dts_name = idp->di_name;
|
|
sip->dts_id = idp->di_id;
|
|
}
|
|
|
|
return (0);
|
|
}
|
|
}
|
|
|
|
return (dt_set_errno(dtp, EDT_NOSYM));
|
|
}
|
|
|
|
/*
|
|
* Exported interface to look up a symbol by address. We return the GElf_Sym
|
|
* and complete symbol information for the matching symbol.
|
|
*/
|
|
int
|
|
dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
|
|
GElf_Sym *symp, dtrace_syminfo_t *sip)
|
|
{
|
|
dt_module_t *dmp;
|
|
uint_t id;
|
|
const dtrace_vector_t *v = dtp->dt_vector;
|
|
|
|
if (v != NULL)
|
|
return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
|
|
|
|
for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
|
|
dmp = dt_list_next(dmp)) {
|
|
if (addr - dmp->dm_text_va < dmp->dm_text_size ||
|
|
addr - dmp->dm_data_va < dmp->dm_data_size ||
|
|
addr - dmp->dm_bss_va < dmp->dm_bss_size)
|
|
break;
|
|
}
|
|
|
|
if (dmp == NULL)
|
|
return (dt_set_errno(dtp, EDT_NOSYMADDR));
|
|
|
|
if (dt_module_load(dtp, dmp) == -1)
|
|
return (-1); /* dt_errno is set for us */
|
|
|
|
if (symp != NULL) {
|
|
if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
|
|
return (dt_set_errno(dtp, EDT_NOSYMADDR));
|
|
}
|
|
|
|
if (sip != NULL) {
|
|
sip->dts_object = dmp->dm_name;
|
|
|
|
if (symp != NULL) {
|
|
sip->dts_name = (const char *)
|
|
dmp->dm_strtab.cts_data + symp->st_name;
|
|
sip->dts_id = id;
|
|
} else {
|
|
sip->dts_name = NULL;
|
|
sip->dts_id = 0;
|
|
}
|
|
}
|
|
|
|
return (0);
|
|
}
|
|
|
|
int
|
|
dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
|
|
dtrace_typeinfo_t *tip)
|
|
{
|
|
dtrace_typeinfo_t ti;
|
|
dt_module_t *dmp;
|
|
int found = 0;
|
|
ctf_id_t id;
|
|
uint_t n;
|
|
int justone;
|
|
|
|
uint_t mask = 0; /* mask of dt_module flags to match */
|
|
uint_t bits = 0; /* flag bits that must be present */
|
|
|
|
if (object != DTRACE_OBJ_EVERY &&
|
|
object != DTRACE_OBJ_KMODS &&
|
|
object != DTRACE_OBJ_UMODS) {
|
|
if ((dmp = dt_module_from_object(dtp, object)) == NULL)
|
|
return (-1); /* dt_errno is set for us */
|
|
|
|
if (dt_module_load(dtp, dmp) == -1)
|
|
return (-1); /* dt_errno is set for us */
|
|
n = 1;
|
|
justone = 1;
|
|
|
|
} else {
|
|
if (object == DTRACE_OBJ_KMODS)
|
|
mask = bits = DT_DM_KERNEL;
|
|
else if (object == DTRACE_OBJ_UMODS)
|
|
mask = DT_DM_KERNEL;
|
|
|
|
dmp = dt_list_next(&dtp->dt_modlist);
|
|
n = dtp->dt_nmods;
|
|
justone = 0;
|
|
}
|
|
|
|
if (tip == NULL)
|
|
tip = &ti;
|
|
|
|
for (; n > 0; n--, dmp = dt_list_next(dmp)) {
|
|
if ((dmp->dm_flags & mask) != bits)
|
|
continue; /* failed to match required attributes */
|
|
|
|
/*
|
|
* If we can't load the CTF container, continue on to the next
|
|
* module. If our search was scoped to only one module then
|
|
* return immediately leaving dt_errno unmodified.
|
|
*/
|
|
if (dt_module_getctf(dtp, dmp) == NULL) {
|
|
if (justone)
|
|
return (-1);
|
|
continue;
|
|
}
|
|
|
|
/*
|
|
* Look up the type in the module's CTF container. If our
|
|
* match is a forward declaration tag, save this choice in
|
|
* 'tip' and keep going in the hope that we will locate the
|
|
* underlying structure definition. Otherwise just return.
|
|
*/
|
|
if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
|
|
tip->dtt_object = dmp->dm_name;
|
|
tip->dtt_ctfp = dmp->dm_ctfp;
|
|
tip->dtt_type = id;
|
|
|
|
if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
|
|
dmp->dm_ctfp, id)) != CTF_K_FORWARD)
|
|
return (0);
|
|
|
|
found++;
|
|
}
|
|
}
|
|
|
|
if (found == 0)
|
|
return (dt_set_errno(dtp, EDT_NOTYPE));
|
|
|
|
return (0);
|
|
}
|
|
|
|
int
|
|
dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
|
|
const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
|
|
{
|
|
dt_module_t *dmp;
|
|
|
|
tip->dtt_object = NULL;
|
|
tip->dtt_ctfp = NULL;
|
|
tip->dtt_type = CTF_ERR;
|
|
|
|
if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
|
|
return (dt_set_errno(dtp, EDT_NOMOD));
|
|
|
|
if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
|
|
dt_ident_t *idp =
|
|
dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
|
|
|
|
if (idp == NULL)
|
|
return (dt_set_errno(dtp, EDT_NOSYM));
|
|
|
|
tip->dtt_ctfp = idp->di_ctfp;
|
|
tip->dtt_type = idp->di_type;
|
|
|
|
} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
|
|
if (dt_module_getctf(dtp, dmp) == NULL)
|
|
return (-1); /* errno is set for us */
|
|
|
|
tip->dtt_ctfp = dmp->dm_ctfp;
|
|
tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
|
|
|
|
if (tip->dtt_type == CTF_ERR) {
|
|
dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
|
|
return (dt_set_errno(dtp, EDT_CTF));
|
|
}
|
|
|
|
} else {
|
|
tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
|
|
tip->dtt_type = DT_FPTR_TYPE(dtp);
|
|
}
|
|
|
|
tip->dtt_object = dmp->dm_name;
|
|
return (0);
|
|
}
|
|
|
|
static dtrace_objinfo_t *
|
|
dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
|
|
{
|
|
dto->dto_name = dmp->dm_name;
|
|
dto->dto_file = dmp->dm_file;
|
|
dto->dto_id = dmp->dm_modid;
|
|
dto->dto_flags = 0;
|
|
|
|
if (dmp->dm_flags & DT_DM_KERNEL)
|
|
dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
|
|
if (dmp->dm_flags & DT_DM_PRIMARY)
|
|
dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
|
|
|
|
dto->dto_text_va = dmp->dm_text_va;
|
|
dto->dto_text_size = dmp->dm_text_size;
|
|
dto->dto_data_va = dmp->dm_data_va;
|
|
dto->dto_data_size = dmp->dm_data_size;
|
|
dto->dto_bss_va = dmp->dm_bss_va;
|
|
dto->dto_bss_size = dmp->dm_bss_size;
|
|
|
|
return (dto);
|
|
}
|
|
|
|
int
|
|
dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
|
|
{
|
|
const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
|
|
dtrace_objinfo_t dto;
|
|
int rv;
|
|
|
|
for (; dmp != NULL; dmp = dt_list_next(dmp)) {
|
|
if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
|
|
return (rv);
|
|
}
|
|
|
|
return (0);
|
|
}
|
|
|
|
int
|
|
dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
|
|
{
|
|
dt_module_t *dmp;
|
|
|
|
if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
|
|
object == DTRACE_OBJ_UMODS || dto == NULL)
|
|
return (dt_set_errno(dtp, EINVAL));
|
|
|
|
if ((dmp = dt_module_from_object(dtp, object)) == NULL)
|
|
return (-1); /* dt_errno is set for us */
|
|
|
|
if (dt_module_load(dtp, dmp) == -1)
|
|
return (-1); /* dt_errno is set for us */
|
|
|
|
(void) dt_module_info(dmp, dto);
|
|
return (0);
|
|
}
|