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
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#include "libunwind_i.h"
27#include "remote.h"
28
29static inline int
30intern_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
48static inline int
49get_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 (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
98PROTECTED int
99unw_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
104  return get_proc_name (tdep_get_as (c), tdep_get_ip (c), buf, buf_len, offp,
105			tdep_get_as_arg (c));
106}
107
108/* ANDROID support update. */
109PROTECTED int
110unw_get_proc_name_by_ip (unw_addr_space_t as, unw_word_t ip, char *buf,
111			 size_t buf_len, unw_word_t *offp, void *as_arg)
112{
113  return get_proc_name (as, ip, buf, buf_len, offp, as_arg);
114}
115/* End of ANDROID update. */
116