PageRenderTime 63ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/usr/src/lib/libdtrace/sparc/dt_isadep.c

https://github.com/richlowe/illumos-gate
C | 338 lines | 225 code | 55 blank | 58 comment | 72 complexity | 687dc9c98662ce449cebd088a0586b53 MD5 | raw file
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License, Version 1.0 only
  6. * (the "License"). You may not use this file except in compliance
  7. * with the License.
  8. *
  9. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10. * or http://www.opensolaris.org/os/licensing.
  11. * See the License for the specific language governing permissions
  12. * and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL HEADER in each
  15. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16. * If applicable, add the following below this CDDL HEADER, with the
  17. * fields enclosed by brackets "[]" replaced with your own identifying
  18. * information: Portions Copyright [yyyy] [name of copyright owner]
  19. *
  20. * CDDL HEADER END
  21. */
  22. /*
  23. * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
  24. * Use is subject to license terms.
  25. */
  26. #pragma ident "%Z%%M% %I% %E% SMI"
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <libgen.h>
  32. #include <dt_impl.h>
  33. #include <dt_pid.h>
  34. #define OP(x) ((x) >> 30)
  35. #define OP2(x) (((x) >> 22) & 0x07)
  36. #define COND(x) (((x) >> 25) & 0x0f)
  37. #define A(x) (((x) >> 29) & 0x01)
  38. #define OP_BRANCH 0
  39. #define OP2_BPcc 0x1
  40. #define OP2_Bicc 0x2
  41. #define OP2_BPr 0x3
  42. #define OP2_FBPfcc 0x5
  43. #define OP2_FBfcc 0x6
  44. /*ARGSUSED*/
  45. int
  46. dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
  47. fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
  48. {
  49. ftp->ftps_type = DTFTP_ENTRY;
  50. ftp->ftps_pc = (uintptr_t)symp->st_value;
  51. ftp->ftps_size = (size_t)symp->st_size;
  52. ftp->ftps_noffs = 1;
  53. ftp->ftps_offs[0] = 0;
  54. if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
  55. dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
  56. strerror(errno));
  57. return (dt_set_errno(dtp, errno));
  58. }
  59. return (1);
  60. }
  61. int
  62. dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
  63. fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
  64. {
  65. uint32_t *text;
  66. int i;
  67. int srdepth = 0;
  68. if ((text = malloc(symp->st_size + 4)) == NULL) {
  69. dt_dprintf("mr sparkle: malloc() failed\n");
  70. return (DT_PROC_ERR);
  71. }
  72. if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
  73. dt_dprintf("mr sparkle: Pread() failed\n");
  74. free(text);
  75. return (DT_PROC_ERR);
  76. }
  77. /*
  78. * Leave a dummy instruction in the last slot to simplify edge
  79. * conditions.
  80. */
  81. text[symp->st_size / 4] = 0;
  82. ftp->ftps_type = DTFTP_RETURN;
  83. ftp->ftps_pc = symp->st_value;
  84. ftp->ftps_size = symp->st_size;
  85. ftp->ftps_noffs = 0;
  86. for (i = 0; i < symp->st_size / 4; i++) {
  87. /*
  88. * If we encounter an existing tracepoint, query the
  89. * kernel to find out the instruction that was
  90. * replaced at this spot.
  91. */
  92. while (text[i] == FASTTRAP_INSTR) {
  93. fasttrap_instr_query_t instr;
  94. instr.ftiq_pid = Pstatus(P)->pr_pid;
  95. instr.ftiq_pc = symp->st_value + i * 4;
  96. if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_GETINSTR,
  97. &instr) != 0) {
  98. if (errno == ESRCH || errno == ENOENT) {
  99. if (Pread(P, &text[i], 4,
  100. instr.ftiq_pc) != 4) {
  101. dt_dprintf("mr sparkle: "
  102. "Pread() failed\n");
  103. free(text);
  104. return (DT_PROC_ERR);
  105. }
  106. continue;
  107. }
  108. free(text);
  109. dt_dprintf("mr sparkle: getinstr query "
  110. "failed: %s\n", strerror(errno));
  111. return (DT_PROC_ERR);
  112. }
  113. text[i] = instr.ftiq_instr;
  114. break;
  115. }
  116. /* save */
  117. if ((text[i] & 0xc1f80000) == 0x81e00000) {
  118. srdepth++;
  119. continue;
  120. }
  121. /* restore */
  122. if ((text[i] & 0xc1f80000) == 0x81e80000) {
  123. srdepth--;
  124. continue;
  125. }
  126. if (srdepth > 0) {
  127. /* ret */
  128. if (text[i] == 0x81c7e008)
  129. goto is_ret;
  130. /* return */
  131. if (text[i] == 0x81cfe008)
  132. goto is_ret;
  133. /* call or jmpl w/ restore in the slot */
  134. if (((text[i] & 0xc0000000) == 0x40000000 ||
  135. (text[i] & 0xc1f80000) == 0x81c00000) &&
  136. (text[i + 1] & 0xc1f80000) == 0x81e80000)
  137. goto is_ret;
  138. /* call to one of the stret routines */
  139. if ((text[i] & 0xc0000000) == 0x40000000) {
  140. int32_t disp = text[i] << 2;
  141. uint64_t dest = ftp->ftps_pc + i * 4 + disp;
  142. dt_dprintf("dest = %llx\n", (u_longlong_t)dest);
  143. if (dest == stret[0] || dest == stret[1] ||
  144. dest == stret[2] || dest == stret[3])
  145. goto is_ret;
  146. }
  147. } else {
  148. /* external call */
  149. if ((text[i] & 0xc0000000) == 0x40000000) {
  150. int32_t dst = text[i] << 2;
  151. dst += i * 4;
  152. if ((uintptr_t)dst >= (uintptr_t)symp->st_size)
  153. goto is_ret;
  154. }
  155. /* jmpl into %g0 -- this includes the retl pseudo op */
  156. if ((text[i] & 0xfff80000) == 0x81c00000)
  157. goto is_ret;
  158. /* external branch -- possible return site */
  159. if (OP(text[i]) == OP_BRANCH) {
  160. int32_t dst;
  161. int baa;
  162. switch (OP2(text[i])) {
  163. case OP2_BPcc:
  164. dst = text[i] & 0x7ffff;
  165. dst <<= 13;
  166. dst >>= 11;
  167. baa = COND(text[i]) == 8 && A(text[i]);
  168. break;
  169. case OP2_Bicc:
  170. dst = text[i] & 0x3fffff;
  171. dst <<= 10;
  172. dst >>= 8;
  173. baa = COND(text[i]) == 8 && A(text[i]);
  174. break;
  175. case OP2_BPr:
  176. dst = (((text[i]) >> 6) & 0xc000) |
  177. ((text[i]) & 0x3fff);
  178. dst <<= 16;
  179. dst >>= 14;
  180. baa = 0;
  181. break;
  182. case OP2_FBPfcc:
  183. dst = text[i] & 0x7ffff;
  184. dst <<= 13;
  185. dst >>= 11;
  186. baa = COND(text[i]) == 8 && A(text[i]);
  187. break;
  188. case OP2_FBfcc:
  189. dst = text[i] & 0x3fffff;
  190. dst <<= 10;
  191. dst >>= 8;
  192. baa = COND(text[i]) == 8 && A(text[i]);
  193. break;
  194. default:
  195. continue;
  196. }
  197. dst += i * 4;
  198. /*
  199. * Interpret branches outside of the function's
  200. * bounds as potential return sites. If the
  201. * branch is a ba,a don't skip the instruction
  202. * in the delay slot.
  203. */
  204. if ((uintptr_t)dst >=
  205. (uintptr_t)symp->st_size) {
  206. if (baa)
  207. goto is_ret_baa;
  208. else
  209. goto is_ret;
  210. }
  211. }
  212. }
  213. continue;
  214. is_ret:
  215. i++;
  216. is_ret_baa:
  217. dt_dprintf("return at offset %x\n", i * 4);
  218. ftp->ftps_offs[ftp->ftps_noffs++] = i * 4;
  219. }
  220. free(text);
  221. if (ftp->ftps_noffs > 0) {
  222. if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
  223. dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
  224. strerror(errno));
  225. return (dt_set_errno(dtp, errno));
  226. }
  227. }
  228. return (ftp->ftps_noffs);
  229. }
  230. /*ARGSUSED*/
  231. int
  232. dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
  233. fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
  234. {
  235. if (off & 0x3)
  236. return (DT_PROC_ALIGN);
  237. ftp->ftps_type = DTFTP_OFFSETS;
  238. ftp->ftps_pc = (uintptr_t)symp->st_value;
  239. ftp->ftps_size = (size_t)symp->st_size;
  240. ftp->ftps_noffs = 1;
  241. ftp->ftps_offs[0] = off;
  242. if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
  243. dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
  244. strerror(errno));
  245. return (dt_set_errno(dtp, errno));
  246. }
  247. return (1);
  248. }
  249. /*ARGSUSED*/
  250. int
  251. dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
  252. fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
  253. {
  254. ulong_t i;
  255. ftp->ftps_type = DTFTP_OFFSETS;
  256. ftp->ftps_pc = (uintptr_t)symp->st_value;
  257. ftp->ftps_size = (size_t)symp->st_size;
  258. ftp->ftps_noffs = 0;
  259. /*
  260. * If we're matching against everything, just iterate through each
  261. * instruction in the function, otherwise look for matching offset
  262. * names by constructing the string and comparing it against the
  263. * pattern.
  264. */
  265. if (strcmp("*", pattern) == 0) {
  266. for (i = 0; i < symp->st_size; i += 4) {
  267. ftp->ftps_offs[ftp->ftps_noffs++] = i;
  268. }
  269. } else {
  270. char name[sizeof (i) * 2 + 1];
  271. for (i = 0; i < symp->st_size; i += 4) {
  272. (void) sprintf(name, "%lx", i);
  273. if (gmatch(name, pattern))
  274. ftp->ftps_offs[ftp->ftps_noffs++] = i;
  275. }
  276. }
  277. if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
  278. dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
  279. strerror(errno));
  280. return (dt_set_errno(dtp, errno));
  281. }
  282. return (ftp->ftps_noffs);
  283. }