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 <fcntl.h>
28*12927SRod.Evans@Sun.COM #include <unistd.h>
29*12927SRod.Evans@Sun.COM #include <stdlib.h>
30*12927SRod.Evans@Sun.COM #include <string.h>
31*12927SRod.Evans@Sun.COM #include <libelf.h>
32*12927SRod.Evans@Sun.COM #include <sys/param.h>
33*12927SRod.Evans@Sun.COM
34*12927SRod.Evans@Sun.COM #include "rdb.h"
35*12927SRod.Evans@Sun.COM
36*12927SRod.Evans@Sun.COM void
perr(char * s)37*12927SRod.Evans@Sun.COM perr(char *s)
38*12927SRod.Evans@Sun.COM {
39*12927SRod.Evans@Sun.COM perror(s);
40*12927SRod.Evans@Sun.COM exit(1);
41*12927SRod.Evans@Sun.COM }
42*12927SRod.Evans@Sun.COM
43*12927SRod.Evans@Sun.COM ulong_t
hexstr_to_num(const char * str)44*12927SRod.Evans@Sun.COM hexstr_to_num(const char *str)
45*12927SRod.Evans@Sun.COM {
46*12927SRod.Evans@Sun.COM ulong_t num = 0;
47*12927SRod.Evans@Sun.COM size_t i, len = strlen(str);
48*12927SRod.Evans@Sun.COM
49*12927SRod.Evans@Sun.COM for (i = 0; i < len; i++)
50*12927SRod.Evans@Sun.COM if (str[i] >= '0' && str[i] <= '9')
51*12927SRod.Evans@Sun.COM num = num * 16 +((int)str[i] - (int)'0');
52*12927SRod.Evans@Sun.COM else if (str[i] >= 'a' && str[i] <= 'f')
53*12927SRod.Evans@Sun.COM num = num * 16 +((int)str[i] - (int)'a' + 10);
54*12927SRod.Evans@Sun.COM else if (str[i] >= 'A' && str[i] <= 'F')
55*12927SRod.Evans@Sun.COM num = num * 16 + ((int)str[i] - (int)'A' + 10);
56*12927SRod.Evans@Sun.COM return (num);
57*12927SRod.Evans@Sun.COM }
58*12927SRod.Evans@Sun.COM
59*12927SRod.Evans@Sun.COM #define STBUFSIZ 1024
60*12927SRod.Evans@Sun.COM
61*12927SRod.Evans@Sun.COM retc_t
proc_string_read(struct ps_prochandle * ph,ulong_t addr,char * buf,int bufsiz)62*12927SRod.Evans@Sun.COM proc_string_read(struct ps_prochandle *ph, ulong_t addr, char *buf, int bufsiz)
63*12927SRod.Evans@Sun.COM {
64*12927SRod.Evans@Sun.COM char intbuf[STBUFSIZ];
65*12927SRod.Evans@Sun.COM int bufind = 0, intbufind = STBUFSIZ, cont = 1;
66*12927SRod.Evans@Sun.COM ssize_t bufbytes = 0;
67*12927SRod.Evans@Sun.COM
68*12927SRod.Evans@Sun.COM if (lseek(ph->pp_asfd, addr, SEEK_SET) == -1)
69*12927SRod.Evans@Sun.COM return (RET_FAILED);
70*12927SRod.Evans@Sun.COM while (cont && (bufind < bufsiz)) {
71*12927SRod.Evans@Sun.COM if (intbufind >= bufbytes) {
72*12927SRod.Evans@Sun.COM if ((bufbytes = read(ph->pp_asfd, intbuf,
73*12927SRod.Evans@Sun.COM STBUFSIZ)) == -1)
74*12927SRod.Evans@Sun.COM return (RET_FAILED);
75*12927SRod.Evans@Sun.COM intbufind = 0;
76*12927SRod.Evans@Sun.COM }
77*12927SRod.Evans@Sun.COM buf[bufind] = intbuf[intbufind];
78*12927SRod.Evans@Sun.COM if (buf[bufind] == '\0')
79*12927SRod.Evans@Sun.COM return (RET_OK);
80*12927SRod.Evans@Sun.COM bufind++;
81*12927SRod.Evans@Sun.COM intbufind++;
82*12927SRod.Evans@Sun.COM }
83*12927SRod.Evans@Sun.COM return (RET_FAILED);
84*12927SRod.Evans@Sun.COM }
85*12927SRod.Evans@Sun.COM
86*12927SRod.Evans@Sun.COM void
print_varstring(struct ps_prochandle * ph,const char * varname)87*12927SRod.Evans@Sun.COM print_varstring(struct ps_prochandle *ph, const char *varname)
88*12927SRod.Evans@Sun.COM {
89*12927SRod.Evans@Sun.COM (void) printf("print_varstring: %s\n", varname);
90*12927SRod.Evans@Sun.COM if (strcmp(varname, "regs") == 0) {
91*12927SRod.Evans@Sun.COM (void) display_all_regs(ph);
92*12927SRod.Evans@Sun.COM return;
93*12927SRod.Evans@Sun.COM }
94*12927SRod.Evans@Sun.COM print_mach_varstring(ph, varname);
95*12927SRod.Evans@Sun.COM }
96*12927SRod.Evans@Sun.COM
97*12927SRod.Evans@Sun.COM void
print_mem(struct ps_prochandle * ph,ulong_t address,int count,char * format)98*12927SRod.Evans@Sun.COM print_mem(struct ps_prochandle *ph, ulong_t address, int count, char *format)
99*12927SRod.Evans@Sun.COM {
100*12927SRod.Evans@Sun.COM (void) printf("\n%17s:", print_address_ps(ph, address, FLG_PAP_SONAME));
101*12927SRod.Evans@Sun.COM
102*12927SRod.Evans@Sun.COM if ((*format == 'X') || (*format == 'x')) {
103*12927SRod.Evans@Sun.COM int i;
104*12927SRod.Evans@Sun.COM
105*12927SRod.Evans@Sun.COM for (i = 0; i < count; i++) {
106*12927SRod.Evans@Sun.COM unsigned long word;
107*12927SRod.Evans@Sun.COM if ((i % 4) == 0)
108*12927SRod.Evans@Sun.COM (void) printf("\n 0x%08lx: ", address);
109*12927SRod.Evans@Sun.COM
110*12927SRod.Evans@Sun.COM if (ps_pread(ph, address, (char *)&word,
111*12927SRod.Evans@Sun.COM sizeof (unsigned long)) != PS_OK) {
112*12927SRod.Evans@Sun.COM (void) printf("\nfailed to read memory at: "
113*12927SRod.Evans@Sun.COM "0x%lx\n", address);
114*12927SRod.Evans@Sun.COM return;
115*12927SRod.Evans@Sun.COM }
116*12927SRod.Evans@Sun.COM (void) printf(" 0x%08lx", word);
117*12927SRod.Evans@Sun.COM address += 4;
118*12927SRod.Evans@Sun.COM }
119*12927SRod.Evans@Sun.COM (void) putchar('\n');
120*12927SRod.Evans@Sun.COM return;
121*12927SRod.Evans@Sun.COM }
122*12927SRod.Evans@Sun.COM
123*12927SRod.Evans@Sun.COM if (*format == 'b') {
124*12927SRod.Evans@Sun.COM int i;
125*12927SRod.Evans@Sun.COM
126*12927SRod.Evans@Sun.COM for (i = 0; i < count; i++, address ++) {
127*12927SRod.Evans@Sun.COM unsigned char byte;
128*12927SRod.Evans@Sun.COM
129*12927SRod.Evans@Sun.COM if ((i % 8) == 0)
130*12927SRod.Evans@Sun.COM (void) printf("\n 0x%08lx: ", address);
131*12927SRod.Evans@Sun.COM
132*12927SRod.Evans@Sun.COM if (ps_pread(ph, address, (char *)&byte,
133*12927SRod.Evans@Sun.COM sizeof (unsigned char)) != PS_OK) {
134*12927SRod.Evans@Sun.COM (void) fprintf(stderr, "\nfailed to read byte "
135*12927SRod.Evans@Sun.COM "at: 0x%lx\n", address);
136*12927SRod.Evans@Sun.COM return;
137*12927SRod.Evans@Sun.COM }
138*12927SRod.Evans@Sun.COM (void) printf(" %02x", (unsigned)byte);
139*12927SRod.Evans@Sun.COM }
140*12927SRod.Evans@Sun.COM (void) putchar('\n');
141*12927SRod.Evans@Sun.COM return;
142*12927SRod.Evans@Sun.COM }
143*12927SRod.Evans@Sun.COM
144*12927SRod.Evans@Sun.COM if (*format == 's') {
145*12927SRod.Evans@Sun.COM char buf[MAXPATHLEN];
146*12927SRod.Evans@Sun.COM if (proc_string_read(ph, address, buf,
147*12927SRod.Evans@Sun.COM MAXPATHLEN) != RET_OK) {
148*12927SRod.Evans@Sun.COM (void) printf("unable to read string at: %lx\n",
149*12927SRod.Evans@Sun.COM address);
150*12927SRod.Evans@Sun.COM return;
151*12927SRod.Evans@Sun.COM }
152*12927SRod.Evans@Sun.COM (void) printf(" %s\n", buf);
153*12927SRod.Evans@Sun.COM return;
154*12927SRod.Evans@Sun.COM }
155*12927SRod.Evans@Sun.COM }
156