Imported Upstream version 1.1
[platform/upstream/libunwind.git] / src / coredump / _UCD_access_reg_linux.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24
25 #include "_UCD_lib.h"
26
27 #include "_UCD_internal.h"
28
29 int
30 _UCD_access_reg (unw_addr_space_t as,
31                                 unw_regnum_t regnum, unw_word_t *valp,
32                                 int write, void *arg)
33 {
34   struct UCD_info *ui = arg;
35
36   if (write)
37     {
38       Debug(0, "write is not supported\n");
39       return -UNW_EINVAL;
40     }
41
42 #if defined(UNW_TARGET_ARM)
43   if (regnum < 0 || regnum >= 16)
44     goto badreg;
45 #elif defined(UNW_TARGET_SH)
46   if (regnum < 0 || regnum > UNW_SH_PR)
47     goto badreg;
48 #else
49 #if defined(UNW_TARGET_MIPS)
50   static const uint8_t remap_regs[] =
51     {
52       [UNW_MIPS_R0]  = EF_REG0,
53       [UNW_MIPS_R1]  = EF_REG1,
54       [UNW_MIPS_R2]  = EF_REG2,
55       [UNW_MIPS_R3]  = EF_REG3,
56       [UNW_MIPS_R4]  = EF_REG4,
57       [UNW_MIPS_R5]  = EF_REG5,
58       [UNW_MIPS_R6]  = EF_REG6,
59       [UNW_MIPS_R7]  = EF_REG7,
60       [UNW_MIPS_R8]  = EF_REG8,
61       [UNW_MIPS_R9]  = EF_REG9,
62       [UNW_MIPS_R10] = EF_REG10,
63       [UNW_MIPS_R11] = EF_REG11,
64       [UNW_MIPS_R12] = EF_REG12,
65       [UNW_MIPS_R13] = EF_REG13,
66       [UNW_MIPS_R14] = EF_REG14,
67       [UNW_MIPS_R15] = EF_REG15,
68       [UNW_MIPS_R16] = EF_REG16,
69       [UNW_MIPS_R17] = EF_REG17,
70       [UNW_MIPS_R18] = EF_REG18,
71       [UNW_MIPS_R19] = EF_REG19,
72       [UNW_MIPS_R20] = EF_REG20,
73       [UNW_MIPS_R21] = EF_REG21,
74       [UNW_MIPS_R22] = EF_REG22,
75       [UNW_MIPS_R23] = EF_REG23,
76       [UNW_MIPS_R24] = EF_REG24,
77       [UNW_MIPS_R25] = EF_REG25,
78       [UNW_MIPS_R28] = EF_REG28,
79       [UNW_MIPS_R29] = EF_REG29,
80       [UNW_MIPS_R30] = EF_REG30,
81       [UNW_MIPS_R31] = EF_REG31,
82       [UNW_MIPS_PC]  = EF_CP0_EPC,
83     };
84 #elif defined(UNW_TARGET_X86)
85   static const uint8_t remap_regs[] =
86     {
87       /* names from libunwind-x86.h */
88       [UNW_X86_EAX]    = offsetof(struct user_regs_struct, eax) / sizeof(long),
89       [UNW_X86_EDX]    = offsetof(struct user_regs_struct, edx) / sizeof(long),
90       [UNW_X86_ECX]    = offsetof(struct user_regs_struct, ecx) / sizeof(long),
91       [UNW_X86_EBX]    = offsetof(struct user_regs_struct, ebx) / sizeof(long),
92       [UNW_X86_ESI]    = offsetof(struct user_regs_struct, esi) / sizeof(long),
93       [UNW_X86_EDI]    = offsetof(struct user_regs_struct, edi) / sizeof(long),
94       [UNW_X86_EBP]    = offsetof(struct user_regs_struct, ebp) / sizeof(long),
95       [UNW_X86_ESP]    = offsetof(struct user_regs_struct, esp) / sizeof(long),
96       [UNW_X86_EIP]    = offsetof(struct user_regs_struct, eip) / sizeof(long),
97       [UNW_X86_EFLAGS] = offsetof(struct user_regs_struct, eflags) / sizeof(long),
98       [UNW_X86_TRAPNO] = offsetof(struct user_regs_struct, orig_eax) / sizeof(long),
99     };
100 #elif defined(UNW_TARGET_X86_64)
101   static const int8_t remap_regs[] =
102     {
103       [UNW_X86_64_RAX]    = offsetof(struct user_regs_struct, rax) / sizeof(long),
104       [UNW_X86_64_RDX]    = offsetof(struct user_regs_struct, rdx) / sizeof(long),
105       [UNW_X86_64_RCX]    = offsetof(struct user_regs_struct, rcx) / sizeof(long),
106       [UNW_X86_64_RBX]    = offsetof(struct user_regs_struct, rbx) / sizeof(long),
107       [UNW_X86_64_RSI]    = offsetof(struct user_regs_struct, rsi) / sizeof(long),
108       [UNW_X86_64_RDI]    = offsetof(struct user_regs_struct, rdi) / sizeof(long),
109       [UNW_X86_64_RBP]    = offsetof(struct user_regs_struct, rbp) / sizeof(long),
110       [UNW_X86_64_RSP]    = offsetof(struct user_regs_struct, rsp) / sizeof(long),
111       [UNW_X86_64_RIP]    = offsetof(struct user_regs_struct, rip) / sizeof(long),
112     };
113 #else
114 #error Port me
115 #endif
116
117   if (regnum < 0 || regnum >= (unw_regnum_t)ARRAY_SIZE(remap_regs))
118     goto badreg;
119
120   regnum = remap_regs[regnum];
121 #endif
122
123   /* pr_reg is a long[] array, but it contains struct user_regs_struct's
124    * image.
125    */
126   Debug(1, "pr_reg[%d]:%ld (0x%lx)\n", regnum,
127                 (long)ui->prstatus->pr_reg[regnum],
128                 (long)ui->prstatus->pr_reg[regnum]
129   );
130   *valp = ui->prstatus->pr_reg[regnum];
131
132   return 0;
133
134 badreg:
135   Debug(0, "bad regnum:%d\n", regnum);
136   return -UNW_EINVAL;
137 }