ftrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. {
  31. unsigned long flags;
  32. struct pstore_ftrace_record rec = {};
  33. if (unlikely(oops_in_progress))
  34. return;
  35. local_irq_save(flags);
  36. rec.ip = ip;
  37. rec.parent_ip = parent_ip;
  38. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  39. psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec,
  40. sizeof(rec), psinfo);
  41. local_irq_restore(flags);
  42. }
  43. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  44. .func = pstore_ftrace_call,
  45. };
  46. static DEFINE_MUTEX(pstore_ftrace_lock);
  47. static bool pstore_ftrace_enabled;
  48. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  49. size_t count, loff_t *ppos)
  50. {
  51. u8 on;
  52. ssize_t ret;
  53. ret = kstrtou8_from_user(buf, count, 2, &on);
  54. if (ret)
  55. return ret;
  56. mutex_lock(&pstore_ftrace_lock);
  57. if (!on ^ pstore_ftrace_enabled)
  58. goto out;
  59. if (on)
  60. ret = register_ftrace_function(&pstore_ftrace_ops);
  61. else
  62. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  63. if (ret) {
  64. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  65. __func__, on ? "" : "un", ret);
  66. goto err;
  67. }
  68. pstore_ftrace_enabled = on;
  69. out:
  70. ret = count;
  71. err:
  72. mutex_unlock(&pstore_ftrace_lock);
  73. return ret;
  74. }
  75. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  76. size_t count, loff_t *ppos)
  77. {
  78. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  79. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  80. }
  81. static const struct file_operations pstore_knob_fops = {
  82. .open = simple_open,
  83. .read = pstore_ftrace_knob_read,
  84. .write = pstore_ftrace_knob_write,
  85. };
  86. void pstore_register_ftrace(void)
  87. {
  88. struct dentry *dir;
  89. struct dentry *file;
  90. if (!psinfo->write_buf)
  91. return;
  92. dir = debugfs_create_dir("pstore", NULL);
  93. if (!dir) {
  94. pr_err("%s: unable to create pstore directory\n", __func__);
  95. return;
  96. }
  97. file = debugfs_create_file("record_ftrace", 0600, dir, NULL,
  98. &pstore_knob_fops);
  99. if (!file) {
  100. pr_err("%s: unable to create record_ftrace file\n", __func__);
  101. goto err_file;
  102. }
  103. return;
  104. err_file:
  105. debugfs_remove(dir);
  106. }