• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2001-2005 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 "libunwind_i.h"
27 #include "remote.h"
28 
29 static inline int
intern_string(unw_addr_space_t as,unw_accessors_t * a,unw_word_t addr,char * buf,size_t buf_len,void * arg)30 intern_string (unw_addr_space_t as, unw_accessors_t *a,
31                unw_word_t addr, char *buf, size_t buf_len, void *arg)
32 {
33   size_t i;
34   int ret;
35 
36   for (i = 0; i < buf_len; ++i)
37     {
38       if ((ret = fetch8 (as, a, &addr, (int8_t *) buf + i, arg)) < 0)
39         return ret;
40 
41       if (buf[i] == '\0')
42         return 0;               /* copied full string; return success */
43     }
44   buf[buf_len - 1] = '\0';      /* ensure string is NUL terminated */
45   return -UNW_ENOMEM;
46 }
47 
48 static inline int
get_proc_name(unw_addr_space_t as,unw_word_t ip,char * buf,size_t buf_len,unw_word_t * offp,void * arg)49 get_proc_name (unw_addr_space_t as, unw_word_t ip,
50                char *buf, size_t buf_len, unw_word_t *offp, void *arg)
51 {
52   unw_accessors_t *a = unw_get_accessors_int (as);
53   unw_proc_info_t pi;
54   int ret;
55 
56   buf[0] = '\0';        /* always return a valid string, even if it's empty */
57 
58   ret = unwi_find_dynamic_proc_info (as, ip, &pi, 1, arg);
59   if (ret == 0)
60     {
61       unw_dyn_info_t *di = pi.unwind_info;
62 
63       if (offp)
64         *offp = ip - pi.start_ip;
65 
66       switch (di->format)
67         {
68         case UNW_INFO_FORMAT_DYNAMIC:
69           ret = intern_string (as, a, di->u.pi.name_ptr, buf, buf_len, arg);
70           break;
71 
72         case UNW_INFO_FORMAT_TABLE:
73         case UNW_INFO_FORMAT_REMOTE_TABLE:
74           /* XXX should we create a fake name, e.g.: "tablenameN",
75              where N is the index of the function in the table??? */
76           ret = -UNW_ENOINFO;
77           break;
78 
79         default:
80           ret = -UNW_EINVAL;
81           break;
82         }
83       unwi_put_dynamic_unwind_info (as, &pi, arg);
84       return ret;
85     }
86 
87   if (ret != -UNW_ENOINFO)
88     return ret;
89 
90   /* not a dynamic procedure, try to lookup static procedure name: */
91 
92   if (a->get_proc_name)
93     return (*a->get_proc_name) (as, ip, buf, buf_len, offp, arg);
94 
95   return -UNW_ENOINFO;
96 }
97 
98 int
unw_get_proc_name(unw_cursor_t * cursor,char * buf,size_t buf_len,unw_word_t * offp)99 unw_get_proc_name (unw_cursor_t *cursor, char *buf, size_t buf_len,
100                    unw_word_t *offp)
101 {
102   struct cursor *c = (struct cursor *) cursor;
103   unw_word_t ip;
104   int error;
105 
106   ip = tdep_get_ip (c);
107 #if !defined(__ia64__)
108   if (c->dwarf.use_prev_instr)
109     {
110 #if defined(__arm__)
111       /* On arm, the least bit denotes thumb/arm mode, clear it. */
112       ip &= ~(unw_word_t)0x1;
113 #endif
114       --ip;
115     }
116 
117 
118 #endif
119   error = get_proc_name (tdep_get_as (c), ip, buf, buf_len, offp,
120                          tdep_get_as_arg (c));
121 #if !defined(__ia64__)
122   if (c->dwarf.use_prev_instr && offp != NULL && error == 0)
123     *offp += 1;
124 #endif
125   return error;
126 }
127