os-linux.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2004 Hewlett-Packard Co
  3. Copyright (C) 2007 David Mosberger-Tang
  4. Contributed by David Mosberger-Tang <dmosberger@gmail.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #ifndef os_linux_h
  23. #define os_linux_h
  24. struct map_iterator
  25. {
  26. off_t offset;
  27. int fd;
  28. size_t buf_size;
  29. char *buf;
  30. char *buf_end;
  31. char *path;
  32. };
  33. static inline char *
  34. ltoa (char *buf, long val)
  35. {
  36. char *cp = buf, tmp;
  37. ssize_t i, len;
  38. do
  39. {
  40. *cp++ = '0' + (val % 10);
  41. val /= 10;
  42. }
  43. while (val);
  44. /* reverse the order of the digits: */
  45. len = cp - buf;
  46. --cp;
  47. for (i = 0; i < len / 2; ++i)
  48. {
  49. tmp = buf[i];
  50. buf[i] = cp[-i];
  51. cp[-i] = tmp;
  52. }
  53. return buf + len;
  54. }
  55. static inline int
  56. maps_init (struct map_iterator *mi, pid_t pid)
  57. {
  58. #if !HAVE_SGX
  59. char path[sizeof ("/proc/0123456789/maps")], *cp;
  60. memcpy (path, "/proc/", 6);
  61. cp = ltoa (path + 6, pid);
  62. assert (cp + 6 < path + sizeof (path));
  63. memcpy (cp, "/maps", 6);
  64. mi->fd = open (path, O_RDONLY);
  65. if (mi->fd >= 0)
  66. {
  67. /* Try to allocate a page-sized buffer. */
  68. mi->buf_size = getpagesize ();
  69. cp = mmap (0, mi->buf_size, PROT_READ | PROT_WRITE,
  70. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  71. if (cp == MAP_FAILED)
  72. {
  73. close(mi->fd);
  74. mi->fd = -1;
  75. return -1;
  76. }
  77. else
  78. {
  79. mi->offset = 0;
  80. mi->buf = mi->buf_end = cp + mi->buf_size;
  81. return 0;
  82. }
  83. }
  84. #else
  85. return -1;
  86. #endif
  87. }
  88. static inline char *
  89. skip_whitespace (char *cp)
  90. {
  91. if (!cp)
  92. return NULL;
  93. while (*cp == ' ' || *cp == '\t')
  94. ++cp;
  95. return cp;
  96. }
  97. static inline char *
  98. scan_hex (char *cp, unsigned long *valp)
  99. {
  100. unsigned long num_digits = 0, digit, val = 0;
  101. cp = skip_whitespace (cp);
  102. if (!cp)
  103. return NULL;
  104. while (1)
  105. {
  106. digit = *cp;
  107. if ((digit - '0') <= 9)
  108. digit -= '0';
  109. else if ((digit - 'a') < 6)
  110. digit -= 'a' - 10;
  111. else if ((digit - 'A') < 6)
  112. digit -= 'A' - 10;
  113. else
  114. break;
  115. val = (val << 4) | digit;
  116. ++num_digits;
  117. ++cp;
  118. }
  119. if (!num_digits)
  120. return NULL;
  121. *valp = val;
  122. return cp;
  123. }
  124. static inline char *
  125. scan_dec (char *cp, unsigned long *valp)
  126. {
  127. unsigned long num_digits = 0, digit, val = 0;
  128. if (!(cp = skip_whitespace (cp)))
  129. return NULL;
  130. while (1)
  131. {
  132. digit = *cp;
  133. if ((digit - '0') <= 9)
  134. {
  135. digit -= '0';
  136. ++cp;
  137. }
  138. else
  139. break;
  140. val = (10 * val) + digit;
  141. ++num_digits;
  142. }
  143. if (!num_digits)
  144. return NULL;
  145. *valp = val;
  146. return cp;
  147. }
  148. static inline char *
  149. scan_char (char *cp, char *valp)
  150. {
  151. if (!cp)
  152. return NULL;
  153. *valp = *cp;
  154. /* don't step over NUL terminator */
  155. if (*cp)
  156. ++cp;
  157. return cp;
  158. }
  159. /* Scan a string delimited by white-space. Fails on empty string or
  160. if string is doesn't fit in the specified buffer. */
  161. static inline char *
  162. scan_string (char *cp, char *valp, size_t buf_size)
  163. {
  164. size_t i = 0;
  165. if (!(cp = skip_whitespace (cp)))
  166. return NULL;
  167. while (*cp != ' ' && *cp != '\t' && *cp != '\0')
  168. {
  169. if ((valp != NULL) && (i < buf_size - 1))
  170. valp[i++] = *cp;
  171. ++cp;
  172. }
  173. if (i == 0 || i >= buf_size)
  174. return NULL;
  175. valp[i] = '\0';
  176. return cp;
  177. }
  178. static inline int
  179. maps_next (struct map_iterator *mi,
  180. unsigned long *low, unsigned long *high, unsigned long *offset)
  181. {
  182. char perm[16], dash = 0, colon = 0, *cp;
  183. unsigned long major, minor, inum;
  184. ssize_t i, nread;
  185. if (mi->fd < 0)
  186. return 0;
  187. while (1)
  188. {
  189. ssize_t bytes_left = mi->buf_end - mi->buf;
  190. char *eol = NULL;
  191. for (i = 0; i < bytes_left; ++i)
  192. {
  193. if (mi->buf[i] == '\n')
  194. {
  195. eol = mi->buf + i;
  196. break;
  197. }
  198. else if (mi->buf[i] == '\0')
  199. break;
  200. }
  201. if (!eol)
  202. {
  203. /* copy down the remaining bytes, if any */
  204. if (bytes_left > 0)
  205. memmove (mi->buf_end - mi->buf_size, mi->buf, bytes_left);
  206. mi->buf = mi->buf_end - mi->buf_size;
  207. nread = read (mi->fd, mi->buf + bytes_left,
  208. mi->buf_size - bytes_left);
  209. if (nread <= 0)
  210. return 0;
  211. else if ((size_t) (nread + bytes_left) < mi->buf_size)
  212. {
  213. /* Move contents to the end of the buffer so we
  214. maintain the invariant that all bytes between
  215. mi->buf and mi->buf_end are valid. */
  216. memmove (mi->buf_end - nread - bytes_left, mi->buf,
  217. nread + bytes_left);
  218. mi->buf = mi->buf_end - nread - bytes_left;
  219. }
  220. eol = mi->buf + bytes_left + nread - 1;
  221. for (i = bytes_left; i < bytes_left + nread; ++i)
  222. if (mi->buf[i] == '\n')
  223. {
  224. eol = mi->buf + i;
  225. break;
  226. }
  227. }
  228. cp = mi->buf;
  229. mi->buf = eol + 1;
  230. *eol = '\0';
  231. /* scan: "LOW-HIGH PERM OFFSET MAJOR:MINOR INUM PATH" */
  232. cp = scan_hex (cp, low);
  233. cp = scan_char (cp, &dash);
  234. cp = scan_hex (cp, high);
  235. cp = scan_string (cp, perm, sizeof (perm));
  236. cp = scan_hex (cp, offset);
  237. cp = scan_hex (cp, &major);
  238. cp = scan_char (cp, &colon);
  239. cp = scan_hex (cp, &minor);
  240. cp = scan_dec (cp, &inum);
  241. cp = mi->path = skip_whitespace (cp);
  242. if (!cp)
  243. continue;
  244. cp = scan_string (cp, NULL, 0);
  245. if (dash != '-' || colon != ':')
  246. continue; /* skip line with unknown or bad format */
  247. return 1;
  248. }
  249. return 0;
  250. }
  251. static inline void
  252. maps_close (struct map_iterator *mi)
  253. {
  254. #if !HAVE_SGX
  255. if (mi->fd < 0)
  256. return;
  257. close (mi->fd);
  258. mi->fd = -1;
  259. if (mi->buf)
  260. {
  261. munmap (mi->buf_end - mi->buf_size, mi->buf_size);
  262. mi->buf = mi->buf_end = 0;
  263. }
  264. #endif
  265. }
  266. #endif /* os_linux_h */