Make UEFI boot-platform build again
[haiku.git] / src / libs / libunwind / ptrace / _UPT_find_proc_info.c
blobd2a37eac56bf06bbf22d9448801676adbfb05237
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>
5 This file is part of libunwind.
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:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
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. */
26 #include <elf.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <sys/mman.h>
33 #include "_UPT_internal.h"
35 static int
36 get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_word_t ip)
38 unsigned long segbase, mapoff;
39 char path[PATH_MAX];
41 #if UNW_TARGET_IA64 && defined(__linux)
42 if (!edi->ktab.start_ip && _Uia64_get_kernel_table (&edi->ktab) < 0)
43 return -UNW_ENOINFO;
45 if (edi->ktab.format != -1 && ip >= edi->ktab.start_ip && ip < edi->ktab.end_ip)
46 return 0;
47 #endif
49 if ((edi->di_cache.format != -1
50 && ip >= edi->di_cache.start_ip && ip < edi->di_cache.end_ip)
51 #if UNW_TARGET_ARM
52 || (edi->di_debug.format != -1
53 && ip >= edi->di_arm.start_ip && ip < edi->di_arm.end_ip)
54 #endif
55 || (edi->di_debug.format != -1
56 && ip >= edi->di_debug.start_ip && ip < edi->di_debug.end_ip))
57 return 0;
59 invalidate_edi(edi);
61 if (tdep_get_elf_image (&edi->ei, pid, ip, &segbase, &mapoff, path,
62 sizeof(path)) < 0)
63 return -UNW_ENOINFO;
65 /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
66 which covers the IP we're looking for. */
67 if (tdep_find_unwind_table (edi, as, path, segbase, mapoff, ip) < 0)
68 return -UNW_ENOINFO;
70 /* This can happen in corner cases where dynamically generated
71 code falls into the same page that contains the data-segment
72 and the page-offset of the code is within the first page of
73 the executable. */
74 if (edi->di_cache.format != -1
75 && (ip < edi->di_cache.start_ip || ip >= edi->di_cache.end_ip))
76 edi->di_cache.format = -1;
78 if (edi->di_debug.format != -1
79 && (ip < edi->di_debug.start_ip || ip >= edi->di_debug.end_ip))
80 edi->di_debug.format = -1;
82 if (edi->di_cache.format == -1
83 #if UNW_TARGET_ARM
84 && edi->di_arm.format == -1
85 #endif
86 && edi->di_debug.format == -1)
87 return -UNW_ENOINFO;
89 return 0;
92 int
93 _UPT_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
94 int need_unwind_info, void *arg)
96 struct UPT_info *ui = arg;
97 int ret = -UNW_ENOINFO;
99 if (get_unwind_info (&ui->edi, ui->pid, as, ip) < 0)
100 return -UNW_ENOINFO;
102 #if UNW_TARGET_IA64
103 if (ui->edi.ktab.format != -1)
105 /* The kernel unwind table resides in local memory, so we have
106 to use the local address space to search it. Since
107 _UPT_put_unwind_info() has no easy way of detecting this
108 case, we simply make a copy of the unwind-info, so
109 _UPT_put_unwind_info() can always free() the unwind-info
110 without ill effects. */
111 ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
112 need_unwind_info, arg);
113 if (ret >= 0)
115 if (!need_unwind_info)
116 pi->unwind_info = NULL;
117 else
119 void *mem = malloc (pi->unwind_info_size);
121 if (!mem)
122 return -UNW_ENOMEM;
123 memcpy (mem, pi->unwind_info, pi->unwind_info_size);
124 pi->unwind_info = mem;
128 #endif
130 if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
131 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
132 pi, need_unwind_info, arg);
134 #if UNW_TARGET_ARM
135 if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
136 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
137 need_unwind_info, arg);
138 #endif
140 if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
141 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
142 need_unwind_info, arg);
144 return ret;