xref: /illumos-gate/usr/src/cmd/sgs/demo_rdb/common/dis.c (revision 1a065e93eee983124652c3eb0cfdcb4776cd89ab)
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 *
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 
74 	if (addr_to_sym(ph, address, &sym, &str) == RET_OK) {
75 		map_info_t	*mip;
76 		ulong_t		pltbase;
77 
78 		if (flags & FLG_PAP_SONAME) {
79 			/*
80 			 * Embed SOName in symbol name
81 			 */
82 			if ((mip = addr_to_map(ph, address)) != 0) {
83 				(void) strcpy(buf, mip->mi_name);
84 				(void) strcat(buf, ":");
85 			} else
86 				(void) sprintf(buf, "0x%08lx:", address);
87 		} else
88 			buf[0] = '\0';
89 
90 		if ((flags & FLG_PAP_PLTDECOM) &&
91 		    (pltbase = is_plt(ph, address)) != 0) {
92 			rd_plt_info_t	rp;
93 			pstatus_t	pstatus;
94 
95 			if (pread(ph->pp_statusfd, &pstatus,
96 			    sizeof (pstatus), 0) == -1)
97 				perr("pap: reading pstatus");
98 
99 			if (rd_plt_resolution(ph->pp_rap, address,
100 			    pstatus.pr_lwp.pr_lwpid, pltbase,
101 			    &rp) == RD_OK) {
102 				if (rp.pi_flags & RD_FLG_PI_PLTBOUND) {
103 					GElf_Sym	_sym;
104 					char		*_str;
105 
106 					if (addr_to_sym(ph, rp.pi_baddr,
107 					    &_sym, &_str) == RET_OK) {
108 						(void) snprintf(buf, 256,
109 						    "%s0x%lx:plt(%s)",
110 						    buf, address, _str);
111 						return (buf);
112 					}
113 				}
114 			}
115 			val = sym.st_value;
116 			(void) snprintf(buf, 256, "%s0x%lx:plt(unbound)+0x%lx",
117 			    buf, address, address - val);
118 			return (buf);
119 		} else {
120 
121 			val = sym.st_value;
122 
123 			if (val < address)
124 				(void) snprintf(buf, 256, "%s%s+0x%lx", buf,
125 				    str, address - val);
126 			else
127 				(void) snprintf(buf, 256, "%s%s", buf, str);
128 			return (buf);
129 		}
130 	} else {
131 		if (flags & FLG_PAP_NOHEXNAME)
132 			buf[0] = '\0';
133 		else
134 			(void) sprintf(buf, "0x%lx", address);
135 		return (buf);
136 	}
137 }
138 
139 char *
140 print_address(unsigned long address)
141 {
142 	return (print_address_ps(cur_ph, address,
143 	    FLG_PAP_SONAME| FLG_PAP_PLTDECOM));
144 }
145 
146 retc_t
147 disasm_addr(struct ps_prochandle *ph, ulong_t addr, int num_inst)
148 {
149 	ulong_t 	offset, end;
150 	int		vers = V8_MODE;
151 
152 	if (ph->pp_dmodel == PR_MODEL_LP64)
153 		vers = V9_MODE | V9_SGI_MODE;
154 
155 	for (offset = addr, end = addr + num_inst * 4; offset < end;
156 	    offset += 4) {
157 		char		*instr_str;
158 		unsigned int	instr;
159 
160 		if (ps_pread(ph, offset, (char *)&instr,
161 		    sizeof (unsigned)) != PS_OK)
162 			perror("da: ps_pread");
163 
164 		cur_ph = ph;
165 		instr_str = disassemble(instr, offset, print_address, 0, 0,
166 		    vers);
167 
168 		(void) printf("%-30s: %s\n", print_address(offset), instr_str);
169 	}
170 	return (RET_OK);
171 }
172 
173 void
174 disasm(struct ps_prochandle *ph, int num_inst)
175 {
176 	pstatus_t	pstat;
177 
178 	if (pread(ph->pp_statusfd, &pstat, sizeof (pstat), 0) == -1)
179 		perr("disasm: PIOCSTATUS");
180 
181 	(void) disasm_addr(ph, (ulong_t)pstat.pr_lwp.pr_reg[R_PC], num_inst);
182 }
183