ftrace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2012 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/compiler.h>
  15. #include <linux/irqflags.h>
  16. #include <linux/percpu.h>
  17. #include <linux/smp.h>
  18. #include <linux/atomic.h>
  19. #include <linux/types.h>
  20. #include <linux/mutex.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/fs.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/err.h>
  25. #include <linux/cache.h>
  26. #include <asm/barrier.h>
  27. #include "internal.h"
  28. static void notrace pstore_ftrace_call(unsigned long ip,
  29. unsigned long parent_ip,
  30. struct ftrace_ops *op,
  31. struct pt_regs *regs)
  32. {
  33. unsigned long flags;
  34. struct pstore_ftrace_record rec = {};
  35. if (unlikely(oops_in_progress))
  36. return;
  37. local_irq_save(flags);
  38. rec.ip = ip;
  39. rec.parent_ip = parent_ip;
  40. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  41. psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec,
  42. 0, sizeof(rec), psinfo);
  43. local_irq_restore(flags);
  44. }
  45. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  46. .func = pstore_ftrace_call,
  47. };
  48. static DEFINE_MUTEX(pstore_ftrace_lock);
  49. static bool pstore_ftrace_enabled;
  50. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  51. size_t count, loff_t *ppos)
  52. {
  53. u8 on;
  54. ssize_t ret;
  55. ret = kstrtou8_from_user(buf, count, 2, &on);
  56. if (ret)
  57. return ret;
  58. mutex_lock(&pstore_ftrace_lock);
  59. if (!on ^ pstore_ftrace_enabled)
  60. goto out;
  61. if (on)
  62. ret = register_ftrace_function(&pstore_ftrace_ops);
  63. else
  64. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  65. if (ret) {
  66. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  67. __func__, on ? "" : "un", ret);
  68. goto err;
  69. }
  70. pstore_ftrace_enabled = on;
  71. out:
  72. ret = count;
  73. err:
  74. mutex_unlock(&pstore_ftrace_lock);
  75. return ret;
  76. }
  77. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  78. size_t count, loff_t *ppos)
  79. {
  80. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  81. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  82. }
  83. static const struct file_operations pstore_knob_fops = {
  84. .open = simple_open,
  85. .read = pstore_ftrace_knob_read,
  86. .write = pstore_ftrace_knob_write,
  87. };
  88. static struct dentry *pstore_ftrace_dir;
  89. void pstore_register_ftrace(void)
  90. {
  91. struct dentry *file;
  92. if (!psinfo->write_buf)
  93. return;
  94. pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
  95. if (!pstore_ftrace_dir) {
  96. pr_err("%s: unable to create pstore directory\n", __func__);
  97. return;
  98. }
  99. file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
  100. NULL, &pstore_knob_fops);
  101. if (!file) {
  102. pr_err("%s: unable to create record_ftrace file\n", __func__);
  103. goto err_file;
  104. }
  105. return;
  106. err_file:
  107. debugfs_remove(pstore_ftrace_dir);
  108. }
  109. void pstore_unregister_ftrace(void)
  110. {
  111. mutex_lock(&pstore_ftrace_lock);
  112. if (pstore_ftrace_enabled) {
  113. unregister_ftrace_function(&pstore_ftrace_ops);
  114. pstore_ftrace_enabled = 0;
  115. }
  116. mutex_unlock(&pstore_ftrace_lock);
  117. debugfs_remove_recursive(pstore_ftrace_dir);
  118. }