Gget_proc_name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include "libunwind_i.h"
  22. #include "remote.h"
  23. static inline int
  24. intern_string (unw_addr_space_t as, unw_accessors_t *a,
  25. unw_word_t addr, char *buf, size_t buf_len, void *arg)
  26. {
  27. size_t i;
  28. int ret;
  29. for (i = 0; i < buf_len; ++i)
  30. {
  31. if ((ret = fetch8 (as, a, &addr, (int8_t *) buf + i, arg)) < 0)
  32. return ret;
  33. if (buf[i] == '\0')
  34. return 0; /* copied full string; return success */
  35. }
  36. buf[buf_len - 1] = '\0'; /* ensure string is NUL terminated */
  37. return -UNW_ENOMEM;
  38. }
  39. static inline int
  40. get_proc_name (unw_addr_space_t as, unw_word_t ip,
  41. char *buf, size_t buf_len, unw_word_t *offp, void *arg)
  42. {
  43. unw_accessors_t *a = unw_get_accessors (as);
  44. unw_proc_info_t pi;
  45. int ret;
  46. buf[0] = '\0'; /* always return a valid string, even if it's empty */
  47. ret = unwi_find_dynamic_proc_info (as, ip, &pi, 1, arg);
  48. if (ret == 0)
  49. {
  50. unw_dyn_info_t *di = pi.unwind_info;
  51. if (offp)
  52. *offp = ip - pi.start_ip;
  53. switch (di->format)
  54. {
  55. case UNW_INFO_FORMAT_DYNAMIC:
  56. ret = intern_string (as, a, di->u.pi.name_ptr, buf, buf_len, arg);
  57. break;
  58. case UNW_INFO_FORMAT_TABLE:
  59. case UNW_INFO_FORMAT_REMOTE_TABLE:
  60. /* XXX should we create a fake name, e.g.: "tablenameN",
  61. where N is the index of the function in the table??? */
  62. ret = -UNW_ENOINFO;
  63. break;
  64. default:
  65. ret = -UNW_EINVAL;
  66. break;
  67. }
  68. unwi_put_dynamic_unwind_info (as, &pi, arg);
  69. return ret;
  70. }
  71. if (ret != -UNW_ENOINFO)
  72. return ret;
  73. /* not a dynamic procedure, try to lookup static procedure name: */
  74. if (a->get_proc_name)
  75. return (*a->get_proc_name) (as, ip, buf, buf_len, offp, arg);
  76. return -UNW_ENOINFO;
  77. }
  78. PROTECTED int
  79. unw_get_proc_name (unw_cursor_t *cursor, char *buf, size_t buf_len,
  80. unw_word_t *offp)
  81. {
  82. struct cursor *c = (struct cursor *) cursor;
  83. return get_proc_name (tdep_get_as (c), tdep_get_ip (c), buf, buf_len, offp,
  84. tdep_get_as_arg (c));
  85. }