summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/demo_rdb/common/dis.c
blob: e08236f739e35cfb2c5dca4d329d705778938091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <procfs.h>
#include <sys/auxv.h>
#include <libelf.h>
#include <sys/param.h>
#include <stdarg.h>

#include "rdb.h"
#include "disasm.h"

/*
 * I don't like this global but it's a work-around for the
 * poor disassemble interface for now.
 */
static struct ps_prochandle	*cur_ph;

/*
 * This routine converts 'address' into it's closest symbol
 * representation.
 *
 * The following flags are used to effect the output:
 *
 *	FLG_PAP_SONAME
 *		embed the SONAME in the symbol name
 *	FLG_PAP_NOHEXNAME
 *		if no symbol found return a null string
 *		If this flag is not set return a string displaying
 *		the 'hex' value of address.
 *	FLG_PAP_PLTDECOM
 *		decompose the PLT symbol if possible
 */
char *
print_address_ps(struct ps_prochandle *ph, ulong_t address, unsigned flags)
{
	static char	buf[256];
	GElf_Sym	sym;
	char		*str;
	ulong_t		val;

	if (addr_to_sym(ph, address, &sym, &str) == RET_OK) {
		map_info_t	*mip;
		ulong_t		pltbase;

		if (flags & FLG_PAP_SONAME) {
			/*
			 * Embed SOName in symbol name
			 */
			if ((mip = addr_to_map(ph, address)) != 0) {
				(void) strcpy(buf, mip->mi_name);
				(void) strcat(buf, ":");
			} else
				(void) sprintf(buf, "0x%08lx:", address);
		} else
			buf[0] = '\0';

		if ((flags & FLG_PAP_PLTDECOM) &&
		    (pltbase = is_plt(ph, address)) != 0) {
			rd_plt_info_t	rp;
			pstatus_t	pstatus;

			if (pread(ph->pp_statusfd, &pstatus,
			    sizeof (pstatus), 0) == -1)
				perr("pap: reading pstatus");

			if (rd_plt_resolution(ph->pp_rap, address,
			    pstatus.pr_lwp.pr_lwpid, pltbase,
			    &rp) == RD_OK) {
				if (rp.pi_flags & RD_FLG_PI_PLTBOUND) {
					GElf_Sym	_sym;
					char		*_str;

					if (addr_to_sym(ph, rp.pi_baddr,
					    &_sym, &_str) == RET_OK) {
						(void) snprintf(buf, 256,
						    "%s0x%lx:plt(%s)",
						    buf, address, _str);
						return (buf);
					}
				}
			}
			val = sym.st_value;
			(void) snprintf(buf, 256, "%s0x%lx:plt(unbound)+0x%lx",
			    buf, address, address - val);
			return (buf);
		} else {

			val = sym.st_value;

			if (val < address)
				(void) snprintf(buf, 256, "%s%s+0x%lx", buf,
				    str, address - val);
			else
				(void) snprintf(buf, 256, "%s%s", buf, str);
			return (buf);
		}
	} else {
		if (flags & FLG_PAP_NOHEXNAME)
			buf[0] = '\0';
		else
			(void) sprintf(buf, "0x%lx", address);
		return (buf);
	}
}

char *
print_address(unsigned long address)
{
	return (print_address_ps(cur_ph, address,
	    FLG_PAP_SONAME| FLG_PAP_PLTDECOM));
}

retc_t
disasm_addr(struct ps_prochandle *ph, ulong_t addr, int num_inst)
{
	ulong_t 	offset, end;
	int		vers = V8_MODE;

	if (ph->pp_dmodel == PR_MODEL_LP64)
		vers = V9_MODE | V9_SGI_MODE;

	for (offset = addr, end = addr + num_inst * 4; offset < end;
	    offset += 4) {
		char		*instr_str;
		unsigned int	instr;

		if (ps_pread(ph, offset, (char *)&instr,
		    sizeof (unsigned)) != PS_OK)
			perror("da: ps_pread");

		cur_ph = ph;
		instr_str = disassemble(instr, offset, print_address, 0, 0,
		    vers);

		(void) printf("%-30s: %s\n", print_address(offset), instr_str);
	}
	return (RET_OK);
}

void
disasm(struct ps_prochandle *ph, int num_inst)
{
	pstatus_t	pstat;

	if (pread(ph->pp_statusfd, &pstat, sizeof (pstat), 0) == -1)
		perr("disasm: PIOCSTATUS");

	(void) disasm_addr(ph, (ulong_t)pstat.pr_lwp.pr_reg[R_PC], num_inst);
}