17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
520c1c355SRod Evans  * Common Development and Distribution License (the "License").
620c1c355SRod Evans  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2120c1c355SRod Evans 
227c478bd9Sstevel@tonic-gate /*
2320c1c355SRod Evans  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <libelf.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "rdb.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate void
perr(char * s)3720c1c355SRod Evans perr(char *s)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	perror(s);
407c478bd9Sstevel@tonic-gate 	exit(1);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate ulong_t
hexstr_to_num(const char * str)4420c1c355SRod Evans hexstr_to_num(const char *str)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	ulong_t		num = 0;
4720c1c355SRod Evans 	size_t		i, len = strlen(str);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
507c478bd9Sstevel@tonic-gate 		if (str[i] >= '0' && str[i] <= '9')
517c478bd9Sstevel@tonic-gate 			num = num * 16 +((int)str[i] - (int)'0');
527c478bd9Sstevel@tonic-gate 		else if (str[i] >= 'a' && str[i] <= 'f')
537c478bd9Sstevel@tonic-gate 			num = num * 16 +((int)str[i] - (int)'a' + 10);
547c478bd9Sstevel@tonic-gate 		else if (str[i] >= 'A' && str[i] <= 'F')
557c478bd9Sstevel@tonic-gate 			num = num * 16 + ((int)str[i] - (int)'A' + 10);
567c478bd9Sstevel@tonic-gate 	return (num);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	STBUFSIZ	1024
6020c1c355SRod Evans 
617c478bd9Sstevel@tonic-gate retc_t
proc_string_read(struct ps_prochandle * ph,ulong_t addr,char * buf,int bufsiz)6220c1c355SRod Evans proc_string_read(struct ps_prochandle *ph, ulong_t addr, char *buf, int bufsiz)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	char	intbuf[STBUFSIZ];
6520c1c355SRod Evans 	int	bufind = 0, intbufind = STBUFSIZ, cont = 1;
667c478bd9Sstevel@tonic-gate 	ssize_t	bufbytes = 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (lseek(ph->pp_asfd, addr, SEEK_SET) == -1)
697c478bd9Sstevel@tonic-gate 		return (RET_FAILED);
707c478bd9Sstevel@tonic-gate 	while (cont && (bufind < bufsiz)) {
717c478bd9Sstevel@tonic-gate 		if (intbufind >= bufbytes) {
727c478bd9Sstevel@tonic-gate 			if ((bufbytes = read(ph->pp_asfd, intbuf,
737c478bd9Sstevel@tonic-gate 			    STBUFSIZ)) == -1)
747c478bd9Sstevel@tonic-gate 				return (RET_FAILED);
757c478bd9Sstevel@tonic-gate 			intbufind = 0;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 		buf[bufind] = intbuf[intbufind];
787c478bd9Sstevel@tonic-gate 		if (buf[bufind] == '\0')
797c478bd9Sstevel@tonic-gate 			return (RET_OK);
807c478bd9Sstevel@tonic-gate 		bufind++;
817c478bd9Sstevel@tonic-gate 		intbufind++;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	return (RET_FAILED);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate void
print_varstring(struct ps_prochandle * ph,const char * varname)8720c1c355SRod Evans print_varstring(struct ps_prochandle *ph, const char *varname)
887c478bd9Sstevel@tonic-gate {
8920c1c355SRod Evans 	(void) printf("print_varstring: %s\n", varname);
907c478bd9Sstevel@tonic-gate 	if (strcmp(varname, "regs") == 0) {
917c478bd9Sstevel@tonic-gate 		(void) display_all_regs(ph);
927c478bd9Sstevel@tonic-gate 		return;
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	print_mach_varstring(ph, varname);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
print_mem(struct ps_prochandle * ph,ulong_t address,int count,char * format)9820c1c355SRod Evans print_mem(struct ps_prochandle *ph, ulong_t address, int count, char *format)
997c478bd9Sstevel@tonic-gate {
10020c1c355SRod Evans 	(void) printf("\n%17s:", print_address_ps(ph, address, FLG_PAP_SONAME));
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((*format == 'X') || (*format == 'x')) {
1037c478bd9Sstevel@tonic-gate 		int	i;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
1067c478bd9Sstevel@tonic-gate 			unsigned long word;
1077c478bd9Sstevel@tonic-gate 			if ((i % 4) == 0)
10820c1c355SRod Evans 				(void) printf("\n  0x%08lx: ", address);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 			if (ps_pread(ph, address, (char *)&word,
1117c478bd9Sstevel@tonic-gate 			    sizeof (unsigned long)) != PS_OK) {
11220c1c355SRod Evans 				(void) printf("\nfailed to read memory at: "
11320c1c355SRod Evans 				    "0x%lx\n", address);
1147c478bd9Sstevel@tonic-gate 				return;
1157c478bd9Sstevel@tonic-gate 			}
11620c1c355SRod Evans 			(void) printf("  0x%08lx", word);
1177c478bd9Sstevel@tonic-gate 			address += 4;
1187c478bd9Sstevel@tonic-gate 		}
11920c1c355SRod Evans 		(void) putchar('\n');
1207c478bd9Sstevel@tonic-gate 		return;
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (*format == 'b') {
1247c478bd9Sstevel@tonic-gate 		int	i;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++, address ++) {
1277c478bd9Sstevel@tonic-gate 			unsigned char	byte;
12820c1c355SRod Evans 
1297c478bd9Sstevel@tonic-gate 			if ((i % 8) == 0)
13020c1c355SRod Evans 				(void) printf("\n 0x%08lx: ", address);
13120c1c355SRod Evans 
1327c478bd9Sstevel@tonic-gate 			if (ps_pread(ph, address, (char *)&byte,
1337c478bd9Sstevel@tonic-gate 			    sizeof (unsigned char)) != PS_OK) {
13420c1c355SRod Evans 				(void) fprintf(stderr, "\nfailed to read byte "
13520c1c355SRod Evans 				    "at: 0x%lx\n", address);
1367c478bd9Sstevel@tonic-gate 				return;
1377c478bd9Sstevel@tonic-gate 			}
13820c1c355SRod Evans 			(void) printf("  %02x", (unsigned)byte);
1397c478bd9Sstevel@tonic-gate 		}
14020c1c355SRod Evans 		(void) putchar('\n');
1417c478bd9Sstevel@tonic-gate 		return;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (*format == 's') {
1457c478bd9Sstevel@tonic-gate 		char	buf[MAXPATHLEN];
1467c478bd9Sstevel@tonic-gate 		if (proc_string_read(ph, address, buf,
1477c478bd9Sstevel@tonic-gate 		    MAXPATHLEN) != RET_OK) {
14820c1c355SRod Evans 			(void) printf("unable to read string at: %lx\n",
14920c1c355SRod Evans 			    address);
1507c478bd9Sstevel@tonic-gate 			return;
1517c478bd9Sstevel@tonic-gate 		}
15220c1c355SRod Evans 		(void) printf(" %s\n", buf);
1537c478bd9Sstevel@tonic-gate 		return;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate }
156