8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / librtld_db / demo / common / utils.c
blob5b6c194b3ab050f3a2d8cd78b362e2e534846054
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <libelf.h>
32 #include <sys/param.h>
34 #include "rdb.h"
36 void
37 perr(char *s)
39 perror(s);
40 exit(1);
43 ulong_t
44 hexstr_to_num(const char *str)
46 ulong_t num = 0;
47 size_t i, len = strlen(str);
49 for (i = 0; i < len; i++)
50 if (str[i] >= '0' && str[i] <= '9')
51 num = num * 16 +((int)str[i] - (int)'0');
52 else if (str[i] >= 'a' && str[i] <= 'f')
53 num = num * 16 +((int)str[i] - (int)'a' + 10);
54 else if (str[i] >= 'A' && str[i] <= 'F')
55 num = num * 16 + ((int)str[i] - (int)'A' + 10);
56 return (num);
59 #define STBUFSIZ 1024
61 retc_t
62 proc_string_read(struct ps_prochandle *ph, ulong_t addr, char *buf, int bufsiz)
64 char intbuf[STBUFSIZ];
65 int bufind = 0, intbufind = STBUFSIZ, cont = 1;
66 ssize_t bufbytes = 0;
68 if (lseek(ph->pp_asfd, addr, SEEK_SET) == -1)
69 return (RET_FAILED);
70 while (cont && (bufind < bufsiz)) {
71 if (intbufind >= bufbytes) {
72 if ((bufbytes = read(ph->pp_asfd, intbuf,
73 STBUFSIZ)) == -1)
74 return (RET_FAILED);
75 intbufind = 0;
77 buf[bufind] = intbuf[intbufind];
78 if (buf[bufind] == '\0')
79 return (RET_OK);
80 bufind++;
81 intbufind++;
83 return (RET_FAILED);
86 void
87 print_varstring(struct ps_prochandle *ph, const char *varname)
89 (void) printf("print_varstring: %s\n", varname);
90 if (strcmp(varname, "regs") == 0) {
91 (void) display_all_regs(ph);
92 return;
94 print_mach_varstring(ph, varname);
97 void
98 print_mem(struct ps_prochandle *ph, ulong_t address, int count, char *format)
100 (void) printf("\n%17s:", print_address_ps(ph, address, FLG_PAP_SONAME));
102 if ((*format == 'X') || (*format == 'x')) {
103 int i;
105 for (i = 0; i < count; i++) {
106 unsigned long word;
107 if ((i % 4) == 0)
108 (void) printf("\n 0x%08lx: ", address);
110 if (ps_pread(ph, address, (char *)&word,
111 sizeof (unsigned long)) != PS_OK) {
112 (void) printf("\nfailed to read memory at: "
113 "0x%lx\n", address);
114 return;
116 (void) printf(" 0x%08lx", word);
117 address += 4;
119 (void) putchar('\n');
120 return;
123 if (*format == 'b') {
124 int i;
126 for (i = 0; i < count; i++, address ++) {
127 unsigned char byte;
129 if ((i % 8) == 0)
130 (void) printf("\n 0x%08lx: ", address);
132 if (ps_pread(ph, address, (char *)&byte,
133 sizeof (unsigned char)) != PS_OK) {
134 (void) fprintf(stderr, "\nfailed to read byte "
135 "at: 0x%lx\n", address);
136 return;
138 (void) printf(" %02x", (unsigned)byte);
140 (void) putchar('\n');
141 return;
144 if (*format == 's') {
145 char buf[MAXPATHLEN];
146 if (proc_string_read(ph, address, buf,
147 MAXPATHLEN) != RET_OK) {
148 (void) printf("unable to read string at: %lx\n",
149 address);
150 return;
152 (void) printf(" %s\n", buf);
153 return;