os-freebsd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
  3. This file is part of libunwind.
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #include <sys/mman.h>
  23. #include <sys/sysctl.h>
  24. #include <sys/user.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include "libunwind_i.h"
  28. static void *
  29. get_mem(size_t sz)
  30. {
  31. void *res;
  32. res = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
  33. if (res == MAP_FAILED)
  34. return (NULL);
  35. return (res);
  36. }
  37. static void
  38. free_mem(void *ptr, size_t sz)
  39. {
  40. munmap(ptr, sz);
  41. }
  42. static int
  43. get_pid_by_tid(int tid)
  44. {
  45. int mib[3], error;
  46. size_t len, len1;
  47. char *buf;
  48. struct kinfo_proc *kv;
  49. int i, pid;
  50. len = 0;
  51. mib[0] = CTL_KERN;
  52. mib[1] = KERN_PROC;
  53. mib[2] = KERN_PROC_ALL;
  54. error = sysctl(mib, 3, NULL, &len, NULL, 0);
  55. if (error == -1)
  56. return (-1);
  57. len1 = len * 4 / 3;
  58. buf = get_mem(len1);
  59. if (buf == NULL)
  60. return (-1);
  61. len = len1;
  62. error = sysctl(mib, 3, buf, &len, NULL, 0);
  63. if (error == -1) {
  64. free_mem(buf, len1);
  65. return (-1);
  66. }
  67. pid = -1;
  68. for (i = 0, kv = (struct kinfo_proc *)buf; i < len / sizeof(*kv);
  69. i++, kv++) {
  70. if (kv->ki_tid == tid) {
  71. pid = kv->ki_pid;
  72. break;
  73. }
  74. }
  75. free_mem(buf, len1);
  76. return (pid);
  77. }
  78. PROTECTED int
  79. tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
  80. unsigned long *segbase, unsigned long *mapoff, char *path, size_t pathlen)
  81. {
  82. int mib[4], error, ret;
  83. size_t len, len1;
  84. char *buf, *bp, *eb;
  85. struct kinfo_vmentry *kv;
  86. len = 0;
  87. mib[0] = CTL_KERN;
  88. mib[1] = KERN_PROC;
  89. mib[2] = KERN_PROC_VMMAP;
  90. mib[3] = pid;
  91. error = sysctl(mib, 4, NULL, &len, NULL, 0);
  92. if (error == -1) {
  93. if (errno == ESRCH) {
  94. mib[3] = get_pid_by_tid(pid);
  95. if (mib[3] != -1)
  96. error = sysctl(mib, 4, NULL, &len, NULL, 0);
  97. if (error == -1)
  98. return (-UNW_EUNSPEC);
  99. } else
  100. return (-UNW_EUNSPEC);
  101. }
  102. len1 = len * 4 / 3;
  103. buf = get_mem(len1);
  104. if (buf == NULL)
  105. return (-UNW_EUNSPEC);
  106. len = len1;
  107. error = sysctl(mib, 4, buf, &len, NULL, 0);
  108. if (error == -1) {
  109. free_mem(buf, len1);
  110. return (-UNW_EUNSPEC);
  111. }
  112. ret = -UNW_EUNSPEC;
  113. for (bp = buf, eb = buf + len; bp < eb; bp += kv->kve_structsize) {
  114. kv = (struct kinfo_vmentry *)(uintptr_t)bp;
  115. if (ip < kv->kve_start || ip >= kv->kve_end)
  116. continue;
  117. if (kv->kve_type != KVME_TYPE_VNODE)
  118. continue;
  119. *segbase = kv->kve_start;
  120. *mapoff = kv->kve_offset;
  121. if (path)
  122. {
  123. strncpy(path, kv->kve_path, pathlen);
  124. }
  125. ret = elf_map_image (ei, kv->kve_path);
  126. break;
  127. }
  128. free_mem(buf, len1);
  129. return (ret);
  130. }