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
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include <elf.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <sys/mman.h>
32
33 #include "_UPT_internal.h"
34
35 static int
get_unwind_info(struct elf_dyn_info * edi,pid_t pid,unw_addr_space_t as,unw_word_t ip)36 get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_word_t ip)
37 {
38
39 struct map_info *map;
40 #if UNW_TARGET_IA64 && defined(__linux__)
41 if (!edi->ktab.start_ip && _Uia64_get_kernel_table (&edi->ktab) < 0)
42 return -UNW_ENOINFO;
43
44 if (edi->ktab.format != -1 && ip >= edi->ktab.start_ip && ip < edi->ktab.end_ip)
45 return 0;
46 #endif
47
48 if ((edi->di_cache.format != -1
49 && ip >= edi->di_cache.start_ip && ip < edi->di_cache.end_ip)
50 #if UNW_TARGET_ARM
51 || (edi->di_debug.format != -1
52 && ip >= edi->di_arm.start_ip && ip < edi->di_arm.end_ip)
53 #endif
54 || (edi->di_debug.format != -1
55 && ip >= edi->di_debug.start_ip && ip < edi->di_debug.end_ip))
56 return 0;
57
58 invalidate_edi(edi);
59
60 /* Add For Cache MAP And ELF */
61 map = tdep_get_elf_image (as, pid, ip);
62 if (map == NULL)
63 return -UNW_ENOINFO;
64
65 if (tdep_find_unwind_table (edi, &map->ei, as, map->path, map->start, map->offset, ip) < 0)
66 return -UNW_ENOINFO;
67 /* Add For Cache MAP And ELF */
68
69 /* This can happen in corner cases where dynamically generated
70 code falls into the same page that contains the data-segment
71 and the page-offset of the code is within the first page of
72 the executable. */
73 if (edi->di_cache.format != -1
74 && (ip < edi->di_cache.start_ip || ip >= edi->di_cache.end_ip))
75 edi->di_cache.format = -1;
76
77 if (edi->di_debug.format != -1
78 && (ip < edi->di_debug.start_ip || ip >= edi->di_debug.end_ip))
79 edi->di_debug.format = -1;
80
81 if (edi->di_cache.format == -1
82 #if UNW_TARGET_ARM
83 && edi->di_arm.format == -1
84 #endif
85 && edi->di_debug.format == -1)
86 return -UNW_ENOINFO;
87
88 return 0;
89 }
90
91 int
_UPT_find_proc_info(unw_addr_space_t as,unw_word_t ip,unw_proc_info_t * pi,int need_unwind_info,void * arg)92 _UPT_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
93 int need_unwind_info, void *arg)
94 {
95 struct UPT_info *ui = arg;
96 int ret = -UNW_ENOINFO;
97
98 if (get_unwind_info (&ui->edi, ui->pid, as, ip) < 0)
99 return -UNW_ENOINFO;
100
101 #if UNW_TARGET_IA64
102 if (ui->edi.ktab.format != -1)
103 {
104 /* The kernel unwind table resides in local memory, so we have
105 to use the local address space to search it. Since
106 _UPT_put_unwind_info() has no easy way of detecting this
107 case, we simply make a copy of the unwind-info, so
108 _UPT_put_unwind_info() can always free() the unwind-info
109 without ill effects. */
110 ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
111 need_unwind_info, arg);
112 if (ret >= 0)
113 {
114 if (!need_unwind_info)
115 pi->unwind_info = NULL;
116 else
117 {
118 void *mem = malloc (pi->unwind_info_size);
119
120 if (!mem)
121 return -UNW_ENOMEM;
122 memcpy (mem, pi->unwind_info, pi->unwind_info_size);
123 pi->unwind_info = mem;
124 }
125 }
126 }
127 #endif
128
129 if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
130 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
131 pi, need_unwind_info, arg);
132
133 if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
134 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
135 need_unwind_info, arg);
136
137 #if UNW_TARGET_ARM
138 if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
139 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
140 need_unwind_info, arg);
141 #endif
142
143 return ret;
144 }
145