1*12927SRod.Evans@Sun.COM /*
2*12927SRod.Evans@Sun.COM * CDDL HEADER START
3*12927SRod.Evans@Sun.COM *
4*12927SRod.Evans@Sun.COM * The contents of this file are subject to the terms of the
5*12927SRod.Evans@Sun.COM * Common Development and Distribution License (the "License").
6*12927SRod.Evans@Sun.COM * You may not use this file except in compliance with the License.
7*12927SRod.Evans@Sun.COM *
8*12927SRod.Evans@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12927SRod.Evans@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*12927SRod.Evans@Sun.COM * See the License for the specific language governing permissions
11*12927SRod.Evans@Sun.COM * and limitations under the License.
12*12927SRod.Evans@Sun.COM *
13*12927SRod.Evans@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*12927SRod.Evans@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12927SRod.Evans@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*12927SRod.Evans@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*12927SRod.Evans@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*12927SRod.Evans@Sun.COM *
19*12927SRod.Evans@Sun.COM * CDDL HEADER END
20*12927SRod.Evans@Sun.COM */
21*12927SRod.Evans@Sun.COM
22*12927SRod.Evans@Sun.COM /*
23*12927SRod.Evans@Sun.COM * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24*12927SRod.Evans@Sun.COM */
25*12927SRod.Evans@Sun.COM
26*12927SRod.Evans@Sun.COM #include <stdio.h>
27*12927SRod.Evans@Sun.COM #include <stdlib.h>
28*12927SRod.Evans@Sun.COM #include <unistd.h>
29*12927SRod.Evans@Sun.COM #include <fcntl.h>
30*12927SRod.Evans@Sun.COM #include <string.h>
31*12927SRod.Evans@Sun.COM #include <errno.h>
32*12927SRod.Evans@Sun.COM #include <sys/types.h>
33*12927SRod.Evans@Sun.COM #include <sys/signal.h>
34*12927SRod.Evans@Sun.COM #include <sys/fault.h>
35*12927SRod.Evans@Sun.COM #include <sys/syscall.h>
36*12927SRod.Evans@Sun.COM #include <procfs.h>
37*12927SRod.Evans@Sun.COM #include <sys/auxv.h>
38*12927SRod.Evans@Sun.COM #include <libelf.h>
39*12927SRod.Evans@Sun.COM #include <sys/param.h>
40*12927SRod.Evans@Sun.COM #include <stdarg.h>
41*12927SRod.Evans@Sun.COM
42*12927SRod.Evans@Sun.COM #include "rdb.h"
43*12927SRod.Evans@Sun.COM #include "disasm.h"
44*12927SRod.Evans@Sun.COM
45*12927SRod.Evans@Sun.COM /*
46*12927SRod.Evans@Sun.COM * I don't like this global but it's a work-around for the
47*12927SRod.Evans@Sun.COM * poor disassemble interface for now.
48*12927SRod.Evans@Sun.COM */
49*12927SRod.Evans@Sun.COM static struct ps_prochandle *cur_ph;
50*12927SRod.Evans@Sun.COM
51*12927SRod.Evans@Sun.COM /*
52*12927SRod.Evans@Sun.COM * This routine converts 'address' into it's closest symbol
53*12927SRod.Evans@Sun.COM * representation.
54*12927SRod.Evans@Sun.COM *
55*12927SRod.Evans@Sun.COM * The following flags are used to effect the output:
56*12927SRod.Evans@Sun.COM *
57*12927SRod.Evans@Sun.COM * FLG_PAP_SONAME
58*12927SRod.Evans@Sun.COM * embed the SONAME in the symbol name
59*12927SRod.Evans@Sun.COM * FLG_PAP_NOHEXNAME
60*12927SRod.Evans@Sun.COM * if no symbol found return a null string
61*12927SRod.Evans@Sun.COM * If this flag is not set return a string displaying
62*12927SRod.Evans@Sun.COM * the 'hex' value of address.
63*12927SRod.Evans@Sun.COM * FLG_PAP_PLTDECOM
64*12927SRod.Evans@Sun.COM * decompose the PLT symbol if possible
65*12927SRod.Evans@Sun.COM */
66*12927SRod.Evans@Sun.COM char *
print_address_ps(struct ps_prochandle * ph,ulong_t address,unsigned flags)67*12927SRod.Evans@Sun.COM print_address_ps(struct ps_prochandle *ph, ulong_t address, unsigned flags)
68*12927SRod.Evans@Sun.COM {
69*12927SRod.Evans@Sun.COM static char buf[256];
70*12927SRod.Evans@Sun.COM GElf_Sym sym;
71*12927SRod.Evans@Sun.COM char *str;
72*12927SRod.Evans@Sun.COM ulong_t val;
73*12927SRod.Evans@Sun.COM
74*12927SRod.Evans@Sun.COM if (addr_to_sym(ph, address, &sym, &str) == RET_OK) {
75*12927SRod.Evans@Sun.COM map_info_t *mip;
76*12927SRod.Evans@Sun.COM ulong_t pltbase;
77*12927SRod.Evans@Sun.COM
78*12927SRod.Evans@Sun.COM if (flags & FLG_PAP_SONAME) {
79*12927SRod.Evans@Sun.COM /*
80*12927SRod.Evans@Sun.COM * Embed SOName in symbol name
81*12927SRod.Evans@Sun.COM */
82*12927SRod.Evans@Sun.COM if (mip = addr_to_map(ph, address)) {
83*12927SRod.Evans@Sun.COM (void) strcpy(buf, mip->mi_name);
84*12927SRod.Evans@Sun.COM (void) strcat(buf, ":");
85*12927SRod.Evans@Sun.COM } else
86*12927SRod.Evans@Sun.COM (void) sprintf(buf, "0x%08lx:", address);
87*12927SRod.Evans@Sun.COM } else
88*12927SRod.Evans@Sun.COM buf[0] = '\0';
89*12927SRod.Evans@Sun.COM
90*12927SRod.Evans@Sun.COM if ((flags & FLG_PAP_PLTDECOM) &&
91*12927SRod.Evans@Sun.COM (pltbase = is_plt(ph, address)) != 0) {
92*12927SRod.Evans@Sun.COM rd_plt_info_t rp;
93*12927SRod.Evans@Sun.COM pstatus_t pstatus;
94*12927SRod.Evans@Sun.COM
95*12927SRod.Evans@Sun.COM if (pread(ph->pp_statusfd, &pstatus,
96*12927SRod.Evans@Sun.COM sizeof (pstatus), 0) == -1)
97*12927SRod.Evans@Sun.COM perr("pap: reading pstatus");
98*12927SRod.Evans@Sun.COM
99*12927SRod.Evans@Sun.COM if (rd_plt_resolution(ph->pp_rap, address,
100*12927SRod.Evans@Sun.COM pstatus.pr_lwp.pr_lwpid, pltbase,
101*12927SRod.Evans@Sun.COM &rp) == RD_OK) {
102*12927SRod.Evans@Sun.COM if (rp.pi_flags & RD_FLG_PI_PLTBOUND) {
103*12927SRod.Evans@Sun.COM GElf_Sym _sym;
104*12927SRod.Evans@Sun.COM char *_str;
105*12927SRod.Evans@Sun.COM
106*12927SRod.Evans@Sun.COM if (addr_to_sym(ph, rp.pi_baddr,
107*12927SRod.Evans@Sun.COM &_sym, &_str) == RET_OK) {
108*12927SRod.Evans@Sun.COM (void) snprintf(buf, 256,
109*12927SRod.Evans@Sun.COM "%s0x%lx:plt(%s)",
110*12927SRod.Evans@Sun.COM buf, address, _str);
111*12927SRod.Evans@Sun.COM return (buf);
112*12927SRod.Evans@Sun.COM }
113*12927SRod.Evans@Sun.COM }
114*12927SRod.Evans@Sun.COM }
115*12927SRod.Evans@Sun.COM val = sym.st_value;
116*12927SRod.Evans@Sun.COM (void) snprintf(buf, 256, "%s0x%lx:plt(unbound)+0x%lx",
117*12927SRod.Evans@Sun.COM buf, address, address - val);
118*12927SRod.Evans@Sun.COM return (buf);
119*12927SRod.Evans@Sun.COM } else {
120*12927SRod.Evans@Sun.COM
121*12927SRod.Evans@Sun.COM val = sym.st_value;
122*12927SRod.Evans@Sun.COM
123*12927SRod.Evans@Sun.COM if (val < address)
124*12927SRod.Evans@Sun.COM (void) snprintf(buf, 256, "%s%s+0x%lx", buf,
125*12927SRod.Evans@Sun.COM str, address - val);
126*12927SRod.Evans@Sun.COM else
127*12927SRod.Evans@Sun.COM (void) snprintf(buf, 256, "%s%s", buf, str);
128*12927SRod.Evans@Sun.COM return (buf);
129*12927SRod.Evans@Sun.COM }
130*12927SRod.Evans@Sun.COM } else {
131*12927SRod.Evans@Sun.COM if (flags & FLG_PAP_NOHEXNAME)
132*12927SRod.Evans@Sun.COM buf[0] = '\0';
133*12927SRod.Evans@Sun.COM else
134*12927SRod.Evans@Sun.COM (void) sprintf(buf, "0x%lx", address);
135*12927SRod.Evans@Sun.COM return (buf);
136*12927SRod.Evans@Sun.COM }
137*12927SRod.Evans@Sun.COM }
138*12927SRod.Evans@Sun.COM
139*12927SRod.Evans@Sun.COM char *
print_address(unsigned long address)140*12927SRod.Evans@Sun.COM print_address(unsigned long address)
141*12927SRod.Evans@Sun.COM {
142*12927SRod.Evans@Sun.COM return (print_address_ps(cur_ph, address,
143*12927SRod.Evans@Sun.COM FLG_PAP_SONAME| FLG_PAP_PLTDECOM));
144*12927SRod.Evans@Sun.COM }
145*12927SRod.Evans@Sun.COM
146*12927SRod.Evans@Sun.COM retc_t
disasm_addr(struct ps_prochandle * ph,ulong_t addr,int num_inst)147*12927SRod.Evans@Sun.COM disasm_addr(struct ps_prochandle *ph, ulong_t addr, int num_inst)
148*12927SRod.Evans@Sun.COM {
149*12927SRod.Evans@Sun.COM ulong_t offset, end;
150*12927SRod.Evans@Sun.COM int vers = V8_MODE;
151*12927SRod.Evans@Sun.COM
152*12927SRod.Evans@Sun.COM if (ph->pp_dmodel == PR_MODEL_LP64)
153*12927SRod.Evans@Sun.COM vers = V9_MODE | V9_SGI_MODE;
154*12927SRod.Evans@Sun.COM
155*12927SRod.Evans@Sun.COM for (offset = addr, end = addr + num_inst * 4; offset < end;
156*12927SRod.Evans@Sun.COM offset += 4) {
157*12927SRod.Evans@Sun.COM char *instr_str;
158*12927SRod.Evans@Sun.COM unsigned int instr;
159*12927SRod.Evans@Sun.COM
160*12927SRod.Evans@Sun.COM if (ps_pread(ph, offset, (char *)&instr,
161*12927SRod.Evans@Sun.COM sizeof (unsigned)) != PS_OK)
162*12927SRod.Evans@Sun.COM perror("da: ps_pread");
163*12927SRod.Evans@Sun.COM
164*12927SRod.Evans@Sun.COM cur_ph = ph;
165*12927SRod.Evans@Sun.COM instr_str = disassemble(instr, offset, print_address, 0, 0,
166*12927SRod.Evans@Sun.COM vers);
167*12927SRod.Evans@Sun.COM
168*12927SRod.Evans@Sun.COM (void) printf("%-30s: %s\n", print_address(offset), instr_str);
169*12927SRod.Evans@Sun.COM }
170*12927SRod.Evans@Sun.COM return (RET_OK);
171*12927SRod.Evans@Sun.COM }
172*12927SRod.Evans@Sun.COM
173*12927SRod.Evans@Sun.COM void
disasm(struct ps_prochandle * ph,int num_inst)174*12927SRod.Evans@Sun.COM disasm(struct ps_prochandle *ph, int num_inst)
175*12927SRod.Evans@Sun.COM {
176*12927SRod.Evans@Sun.COM pstatus_t pstat;
177*12927SRod.Evans@Sun.COM
178*12927SRod.Evans@Sun.COM if (pread(ph->pp_statusfd, &pstat, sizeof (pstat), 0) == -1)
179*12927SRod.Evans@Sun.COM perr("disasm: PIOCSTATUS");
180*12927SRod.Evans@Sun.COM
181*12927SRod.Evans@Sun.COM (void) disasm_addr(ph, (ulong_t)pstat.pr_lwp.pr_reg[R_PC], num_inst);
182*12927SRod.Evans@Sun.COM }
183