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