1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2004 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4   Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
5
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27#include "_UPT_internal.h"
28
29#if HAVE_DECL_PTRACE_POKEDATA || HAVE_TTRACE
30int
31_UPT_access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val,
32		 int write, void *arg)
33{
34  struct UPT_info *ui = arg;
35  if (!ui)
36	return -UNW_EINVAL;
37
38  pid_t pid = ui->pid;
39
40  errno = 0;
41  if (write)
42    {
43      Debug (16, "mem[%lx] <- %lx\n", (long) addr, (long) *val);
44#ifdef HAVE_TTRACE
45#	warning No support for ttrace() yet.
46#else
47      /* ANDROID support update. */
48      ptrace (PTRACE_POKEDATA, pid, (void*) (uintptr_t) addr, (void*) (uintptr_t) *val);
49      /* End of ANDROID update. */
50      if (errno)
51	return -UNW_EINVAL;
52#endif
53    }
54  else
55    {
56#ifdef HAVE_TTRACE
57#	warning No support for ttrace() yet.
58/* ANDROID support update. */
59#elif defined(__mips__) && _MIPS_SIM == _ABIO32
60      /* The assumption is that sizeof(long) == sizeof(unw_word_t).
61         This isn't true for this mips abi, so it requires two reads to get
62         the entire 64 bit value. */
63      long reg1, reg2;
64      reg1 = ptrace (PTRACE_PEEKDATA, pid, (void*) (uintptr_t) addr, 0);
65      if (errno)
66	return -UNW_EINVAL;
67      reg2 = ptrace (PTRACE_PEEKDATA, pid, (void*) (uintptr_t) (addr + sizeof(long)), 0);
68      if (errno)
69	return -UNW_EINVAL;
70      *val = ((unw_word_t)(reg2) << 32) | (uint32_t) reg1;
71#else
72      /* ANDROID support update. */
73      *val = ptrace (PTRACE_PEEKDATA, pid, (void*) addr, 0);
74      /* End of ANDROID update. */
75      if (errno)
76	return -UNW_EINVAL;
77#endif
78/* End of ANDROID update. */
79      Debug (16, "mem[%lx] -> %lx\n", (long) addr, (long) *val);
80    }
81  return 0;
82}
83#elif HAVE_DECL_PT_IO
84int
85_UPT_access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val,
86		 int write, void *arg)
87{
88  struct UPT_info *ui = arg;
89  if (!ui)
90	return -UNW_EINVAL;
91  pid_t pid = ui->pid;
92  struct ptrace_io_desc iod;
93
94  iod.piod_offs = (void *)addr;
95  iod.piod_addr = val;
96  iod.piod_len = sizeof(*val);
97  iod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
98  if (write)
99    Debug (16, "mem[%lx] <- %lx\n", (long) addr, (long) *val);
100  if (ptrace(PT_IO, pid, (caddr_t)&iod, 0) == -1)
101    return -UNW_EINVAL;
102  if (!write)
103     Debug (16, "mem[%lx] -> %lx\n", (long) addr, (long) *val);
104  return 0;
105}
106#else
107#error Fix me
108#endif
109