Imported Upstream version 1.3.1
[platform/upstream/libunwind.git] / src / ptrace / _UPT_find_proc_info.c
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
36 get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_word_t ip)
37 {
38   unsigned long segbase, mapoff;
39   char path[PATH_MAX];
40
41 #if UNW_TARGET_IA64 && defined(__linux)
42   if (!edi->ktab.start_ip && _Uia64_get_kernel_table (&edi->ktab) < 0)
43     return -UNW_ENOINFO;
44
45   if (edi->ktab.format != -1 && ip >= edi->ktab.start_ip && ip < edi->ktab.end_ip)
46     return 0;
47 #endif
48
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;
58
59   invalidate_edi(edi);
60
61   if (tdep_get_elf_image (&edi->ei, pid, ip, &segbase, &mapoff, path,
62                           sizeof(path)) < 0)
63     return -UNW_ENOINFO;
64
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;
69
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;
77
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;
81
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;
88
89   return 0;
90 }
91
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)
95 {
96   struct UPT_info *ui = arg;
97   int ret = -UNW_ENOINFO;
98
99   if (get_unwind_info (&ui->edi, ui->pid, as, ip) < 0)
100     return -UNW_ENOINFO;
101
102 #if UNW_TARGET_IA64
103   if (ui->edi.ktab.format != -1)
104     {
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)
114         {
115           if (!need_unwind_info)
116             pi->unwind_info = NULL;
117           else
118             {
119               void *mem = malloc (pi->unwind_info_size);
120
121               if (!mem)
122                 return -UNW_ENOMEM;
123               memcpy (mem, pi->unwind_info, pi->unwind_info_size);
124               pi->unwind_info = mem;
125             }
126         }
127     }
128 #endif
129
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);
133
134   if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
135     ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
136                                     need_unwind_info, arg);
137
138 #if UNW_TARGET_ARM
139   if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
140     ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
141                                     need_unwind_info, arg);
142 #endif
143
144   return ret;
145 }