ktrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <xfs.h>
  19. static kmem_zone_t *ktrace_hdr_zone;
  20. static kmem_zone_t *ktrace_ent_zone;
  21. static int ktrace_zentries;
  22. void
  23. ktrace_init(int zentries)
  24. {
  25. ktrace_zentries = zentries;
  26. ktrace_hdr_zone = kmem_zone_init(sizeof(ktrace_t),
  27. "ktrace_hdr");
  28. ASSERT(ktrace_hdr_zone);
  29. ktrace_ent_zone = kmem_zone_init(ktrace_zentries
  30. * sizeof(ktrace_entry_t),
  31. "ktrace_ent");
  32. ASSERT(ktrace_ent_zone);
  33. }
  34. void
  35. ktrace_uninit(void)
  36. {
  37. kmem_zone_destroy(ktrace_hdr_zone);
  38. kmem_zone_destroy(ktrace_ent_zone);
  39. }
  40. /*
  41. * ktrace_alloc()
  42. *
  43. * Allocate a ktrace header and enough buffering for the given
  44. * number of entries.
  45. */
  46. ktrace_t *
  47. ktrace_alloc(int nentries, unsigned int __nocast sleep)
  48. {
  49. ktrace_t *ktp;
  50. ktrace_entry_t *ktep;
  51. ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep);
  52. if (ktp == (ktrace_t*)NULL) {
  53. /*
  54. * KM_SLEEP callers don't expect failure.
  55. */
  56. if (sleep & KM_SLEEP)
  57. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  58. return NULL;
  59. }
  60. /*
  61. * Special treatment for buffers with the ktrace_zentries entries
  62. */
  63. if (nentries == ktrace_zentries) {
  64. ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone,
  65. sleep);
  66. } else {
  67. ktep = (ktrace_entry_t*)kmem_zalloc((nentries * sizeof(*ktep)),
  68. sleep | KM_LARGE);
  69. }
  70. if (ktep == NULL) {
  71. /*
  72. * KM_SLEEP callers don't expect failure.
  73. */
  74. if (sleep & KM_SLEEP)
  75. panic("ktrace_alloc: NULL memory on KM_SLEEP request!");
  76. kmem_free(ktp, sizeof(*ktp));
  77. return NULL;
  78. }
  79. spinlock_init(&(ktp->kt_lock), "kt_lock");
  80. ktp->kt_entries = ktep;
  81. ktp->kt_nentries = nentries;
  82. ktp->kt_index = 0;
  83. ktp->kt_rollover = 0;
  84. return ktp;
  85. }
  86. /*
  87. * ktrace_free()
  88. *
  89. * Free up the ktrace header and buffer. It is up to the caller
  90. * to ensure that no-one is referencing it.
  91. */
  92. void
  93. ktrace_free(ktrace_t *ktp)
  94. {
  95. int entries_size;
  96. if (ktp == (ktrace_t *)NULL)
  97. return;
  98. spinlock_destroy(&ktp->kt_lock);
  99. /*
  100. * Special treatment for the Vnode trace buffer.
  101. */
  102. if (ktp->kt_nentries == ktrace_zentries) {
  103. kmem_zone_free(ktrace_ent_zone, ktp->kt_entries);
  104. } else {
  105. entries_size = (int)(ktp->kt_nentries * sizeof(ktrace_entry_t));
  106. kmem_free(ktp->kt_entries, entries_size);
  107. }
  108. kmem_zone_free(ktrace_hdr_zone, ktp);
  109. }
  110. /*
  111. * Enter the given values into the "next" entry in the trace buffer.
  112. * kt_index is always the index of the next entry to be filled.
  113. */
  114. void
  115. ktrace_enter(
  116. ktrace_t *ktp,
  117. void *val0,
  118. void *val1,
  119. void *val2,
  120. void *val3,
  121. void *val4,
  122. void *val5,
  123. void *val6,
  124. void *val7,
  125. void *val8,
  126. void *val9,
  127. void *val10,
  128. void *val11,
  129. void *val12,
  130. void *val13,
  131. void *val14,
  132. void *val15)
  133. {
  134. static DEFINE_SPINLOCK(wrap_lock);
  135. unsigned long flags;
  136. int index;
  137. ktrace_entry_t *ktep;
  138. ASSERT(ktp != NULL);
  139. /*
  140. * Grab an entry by pushing the index up to the next one.
  141. */
  142. spin_lock_irqsave(&wrap_lock, flags);
  143. index = ktp->kt_index;
  144. if (++ktp->kt_index == ktp->kt_nentries)
  145. ktp->kt_index = 0;
  146. spin_unlock_irqrestore(&wrap_lock, flags);
  147. if (!ktp->kt_rollover && index == ktp->kt_nentries - 1)
  148. ktp->kt_rollover = 1;
  149. ASSERT((index >= 0) && (index < ktp->kt_nentries));
  150. ktep = &(ktp->kt_entries[index]);
  151. ktep->val[0] = val0;
  152. ktep->val[1] = val1;
  153. ktep->val[2] = val2;
  154. ktep->val[3] = val3;
  155. ktep->val[4] = val4;
  156. ktep->val[5] = val5;
  157. ktep->val[6] = val6;
  158. ktep->val[7] = val7;
  159. ktep->val[8] = val8;
  160. ktep->val[9] = val9;
  161. ktep->val[10] = val10;
  162. ktep->val[11] = val11;
  163. ktep->val[12] = val12;
  164. ktep->val[13] = val13;
  165. ktep->val[14] = val14;
  166. ktep->val[15] = val15;
  167. }
  168. /*
  169. * Return the number of entries in the trace buffer.
  170. */
  171. int
  172. ktrace_nentries(
  173. ktrace_t *ktp)
  174. {
  175. if (ktp == NULL) {
  176. return 0;
  177. }
  178. return (ktp->kt_rollover ? ktp->kt_nentries : ktp->kt_index);
  179. }
  180. /*
  181. * ktrace_first()
  182. *
  183. * This is used to find the start of the trace buffer.
  184. * In conjunction with ktrace_next() it can be used to
  185. * iterate through the entire trace buffer. This code does
  186. * not do any locking because it is assumed that it is called
  187. * from the debugger.
  188. *
  189. * The caller must pass in a pointer to a ktrace_snap
  190. * structure in which we will keep some state used to
  191. * iterate through the buffer. This state must not touched
  192. * by any code outside of this module.
  193. */
  194. ktrace_entry_t *
  195. ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp)
  196. {
  197. ktrace_entry_t *ktep;
  198. int index;
  199. int nentries;
  200. if (ktp->kt_rollover)
  201. index = ktp->kt_index;
  202. else
  203. index = 0;
  204. ktsp->ks_start = index;
  205. ktep = &(ktp->kt_entries[index]);
  206. nentries = ktrace_nentries(ktp);
  207. index++;
  208. if (index < nentries) {
  209. ktsp->ks_index = index;
  210. } else {
  211. ktsp->ks_index = 0;
  212. if (index > nentries)
  213. ktep = NULL;
  214. }
  215. return ktep;
  216. }
  217. /*
  218. * ktrace_next()
  219. *
  220. * This is used to iterate through the entries of the given
  221. * trace buffer. The caller must pass in the ktrace_snap_t
  222. * structure initialized by ktrace_first(). The return value
  223. * will be either a pointer to the next ktrace_entry or NULL
  224. * if all of the entries have been traversed.
  225. */
  226. ktrace_entry_t *
  227. ktrace_next(
  228. ktrace_t *ktp,
  229. ktrace_snap_t *ktsp)
  230. {
  231. int index;
  232. ktrace_entry_t *ktep;
  233. index = ktsp->ks_index;
  234. if (index == ktsp->ks_start) {
  235. ktep = NULL;
  236. } else {
  237. ktep = &ktp->kt_entries[index];
  238. }
  239. index++;
  240. if (index == ktrace_nentries(ktp)) {
  241. ktsp->ks_index = 0;
  242. } else {
  243. ktsp->ks_index = index;
  244. }
  245. return ktep;
  246. }
  247. /*
  248. * ktrace_skip()
  249. *
  250. * Skip the next "count" entries and return the entry after that.
  251. * Return NULL if this causes us to iterate past the beginning again.
  252. */
  253. ktrace_entry_t *
  254. ktrace_skip(
  255. ktrace_t *ktp,
  256. int count,
  257. ktrace_snap_t *ktsp)
  258. {
  259. int index;
  260. int new_index;
  261. ktrace_entry_t *ktep;
  262. int nentries = ktrace_nentries(ktp);
  263. index = ktsp->ks_index;
  264. new_index = index + count;
  265. while (new_index >= nentries) {
  266. new_index -= nentries;
  267. }
  268. if (index == ktsp->ks_start) {
  269. /*
  270. * We've iterated around to the start, so we're done.
  271. */
  272. ktep = NULL;
  273. } else if ((new_index < index) && (index < ktsp->ks_index)) {
  274. /*
  275. * We've skipped past the start again, so we're done.
  276. */
  277. ktep = NULL;
  278. ktsp->ks_index = ktsp->ks_start;
  279. } else {
  280. ktep = &(ktp->kt_entries[new_index]);
  281. new_index++;
  282. if (new_index == nentries) {
  283. ktsp->ks_index = 0;
  284. } else {
  285. ktsp->ks_index = new_index;
  286. }
  287. }
  288. return ktep;
  289. }