File: _UPT_access_reg.c

package info (click to toggle)
libunwind 1.1-3.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,540 kB
  • ctags: 5,665
  • sloc: ansic: 31,521; sh: 18,062; asm: 1,851; makefile: 866; cpp: 118
file content (309 lines) | stat: -rw-r--r-- 7,616 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003-2005 Hewlett-Packard Co
	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
   Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "_UPT_internal.h"

#if UNW_TARGET_IA64
# include <elf.h>
# ifdef HAVE_ASM_PTRACE_OFFSETS_H
#   include <asm/ptrace_offsets.h>
# endif
# include "tdep-ia64/rse.h"
#endif

#if HAVE_DECL_PTRACE_POKEUSER || HAVE_TTRACE
int
_UPT_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
		 int write, void *arg)
{
  struct UPT_info *ui = arg;
  pid_t pid = ui->pid;

#if UNW_DEBUG
  Debug(16, "using pokeuser: reg: %s [%u], val: %lx, write: %d\n", unw_regname(reg), (unsigned) reg, (long) val, write);

  if (write)
    Debug (16, "%s <- %lx\n", unw_regname (reg), (long) *val);
#endif

#if UNW_TARGET_IA64
  if ((unsigned) reg - UNW_IA64_NAT < 32)
    {
      unsigned long nat_bits, mask;

      /* The Linux ptrace represents the statc NaT bits as a single word.  */
      mask = ((unw_word_t) 1) << (reg - UNW_IA64_NAT);
      errno = 0;
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
      nat_bits = ptrace (PTRACE_PEEKUSER, pid, PT_NAT_BITS, 0);
      if (errno)
	goto badreg;
#endif

      if (write)
	{
	  if (*val)
	    nat_bits |= mask;
	  else
	    nat_bits &= ~mask;
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	  errno = 0;
	  ptrace (PTRACE_POKEUSER, pid, PT_NAT_BITS, nat_bits);
	  if (errno)
	    goto badreg;
#endif
	}
      goto out;
    }
  else
    switch (reg)
      {
      case UNW_IA64_GR + 0:
	if (write)
	  goto badreg;
	*val = 0;
	return 0;

      case UNW_REG_IP:
	{
	  unsigned long ip, psr;

	  /* distribute bundle-addr. & slot-number across PT_IIP & PT_IPSR.  */
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	  errno = 0;
	  psr = ptrace (PTRACE_PEEKUSER, pid, PT_CR_IPSR, 0);
	  if (errno)
	    goto badreg;
#endif
	  if (write)
	    {
	      ip = *val & ~0xfUL;
	      psr = (psr & ~0x3UL << 41) | (*val & 0x3);
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	      errno = 0;
	      ptrace (PTRACE_POKEUSER, pid, PT_CR_IIP, ip);
	      ptrace (PTRACE_POKEUSER, pid, PT_CR_IPSR, psr);
	      if (errno)
		goto badreg;
#endif
	    }
	  else
	    {
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	      errno = 0;
	      ip = ptrace (PTRACE_PEEKUSER, pid, PT_CR_IIP, 0);
	      if (errno)
		goto badreg;
#endif
	      *val = ip + ((psr >> 41) & 0x3);
	    }
	  goto out;
	}

      case UNW_IA64_AR_BSPSTORE:
	reg = UNW_IA64_AR_BSP;
	break;

      case UNW_IA64_AR_BSP:
      case UNW_IA64_BSP:
	{
	  unsigned long sof, cfm, bsp;

#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	  /* Account for the fact that ptrace() expects bsp to point
	     _after_ the current register frame.  */
	  errno = 0;
	  cfm = ptrace (PTRACE_PEEKUSER, pid, PT_CFM, 0);
	  if (errno)
	    goto badreg;
#endif
	  sof = (cfm & 0x7f);

	  if (write)
	    {
	      bsp = rse_skip_regs (*val, sof);
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	      errno = 0;
	      ptrace (PTRACE_POKEUSER, pid, PT_AR_BSP, bsp);
	      if (errno)
		goto badreg;
#endif
	    }
	  else
	    {
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	      errno = 0;
	      bsp = ptrace (PTRACE_PEEKUSER, pid, PT_AR_BSP, 0);
	      if (errno)
		goto badreg;
#endif
	      *val = rse_skip_regs (bsp, -sof);
	    }
	  goto out;
	}

      case UNW_IA64_CFM:
	/* If we change CFM, we need to adjust ptrace's notion of bsp
	   accordingly, so that the real bsp remains unchanged.  */
	if (write)
	  {
	    unsigned long new_sof, old_sof, cfm, bsp;

#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	    errno = 0;
	    bsp = ptrace (PTRACE_PEEKUSER, pid, PT_AR_BSP, 0);
	    cfm = ptrace (PTRACE_PEEKUSER, pid, PT_CFM, 0);
#endif
	    if (errno)
	      goto badreg;
	    old_sof = (cfm & 0x7f);
	    new_sof = (*val & 0x7f);
	    if (old_sof != new_sof)
	      {
		bsp = rse_skip_regs (bsp, -old_sof + new_sof);
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
		errno = 0;
		ptrace (PTRACE_POKEUSER, pid, PT_AR_BSP, 0);
		if (errno)
		  goto badreg;
#endif
	      }
#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
	    errno = 0;
	    ptrace (PTRACE_POKEUSER, pid, PT_CFM, *val);
	    if (errno)
	      goto badreg;
#endif
	    goto out;
	  }
	break;
      }
#endif /* End of IA64 */

  if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
    {
#if UNW_DEBUG
      Debug(2, "register out of range: >= %zu / %zu\n", sizeof(_UPT_reg_offset), sizeof(_UPT_reg_offset[0]));
#endif
      errno = EINVAL;
      goto badreg;
    }

#ifdef HAVE_TTRACE
#	warning No support for ttrace() yet.
#else
  errno = 0;
  if (write)
    ptrace (PTRACE_POKEUSER, pid, _UPT_reg_offset[reg], *val);
  else {
#if UNW_DEBUG
    Debug(16, "ptrace PEEKUSER pid: %lu , reg: %lu , offs: %lu\n", (unsigned long)pid, (unsigned long)reg,
        (unsigned long)_UPT_reg_offset[reg]);
#endif
    *val = ptrace (PTRACE_PEEKUSER, pid, _UPT_reg_offset[reg], 0);
  }
  if (errno) {
#if UNW_DEBUG
    Debug(2, "ptrace failure\n");
#endif
    goto badreg;
  }
#endif

#ifdef UNW_TARGET_IA64
 out:
#endif
#if UNW_DEBUG
  if (!write)
    Debug (16, "%s[%u] -> %lx\n", unw_regname (reg), (unsigned) reg, (long) *val);
#endif
  return 0;

 badreg:
  Debug (1, "bad register %s [%u] (error: %s)\n", unw_regname(reg), reg, strerror (errno));
  return -UNW_EBADREG;
}
#elif HAVE_DECL_PT_GETREGS
int
_UPT_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
		 int write, void *arg)
{
  struct UPT_info *ui = arg;
  pid_t pid = ui->pid;
  gregset_t regs;
  char *r;

#if UNW_DEBUG
  Debug(16, "using getregs: reg: %s [%u], val: %lx, write: %u\n", unw_regname(reg), (unsigned) reg, (long) val, write);

  if (write)
    Debug (16, "%s [%u] <- %lx\n", unw_regname (reg), (unsigned) reg, (long) *val);
#endif
  if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
    {
      errno = EINVAL;
      goto badreg;
    }
  r = (char *)&regs + _UPT_reg_offset[reg];
  if (ptrace(PT_GETREGS, pid, (caddr_t)&regs, 0) == -1)
    goto badreg;
  if (write) {
      memcpy(r, val, sizeof(unw_word_t));
      if (ptrace(PT_SETREGS, pid, (caddr_t)&regs, 0) == -1)
        goto badreg;
  } else
      memcpy(val, r, sizeof(unw_word_t));
  return 0;

 badreg:
  Debug (1, "bad register %s [%u] (error: %s)\n", unw_regname(reg), reg, strerror (errno));
  return -UNW_EBADREG;
}
#else
#error Port me
#endif