profile.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <linux/init.h>
  2. #include <linux/errno.h>
  3. #include <linux/kernel.h>
  4. #include <linux/proc_fs.h>
  5. #include <linux/slab.h>
  6. #include <linux/types.h>
  7. #include <asm/ptrace.h>
  8. #include <asm/uaccess.h>
  9. #define SAMPLE_BUFFER_SIZE 8192
  10. static char *sample_buffer;
  11. static char *sample_buffer_pos;
  12. static int prof_running = 0;
  13. void cris_profile_sample(struct pt_regs *regs)
  14. {
  15. if (!prof_running)
  16. return;
  17. if (user_mode(regs))
  18. *(unsigned int*)sample_buffer_pos = current->pid;
  19. else
  20. *(unsigned int*)sample_buffer_pos = 0;
  21. *(unsigned int *)(sample_buffer_pos + 4) = instruction_pointer(regs);
  22. sample_buffer_pos += 8;
  23. if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE)
  24. sample_buffer_pos = sample_buffer;
  25. }
  26. static ssize_t
  27. read_cris_profile(struct file *file, char __user *buf,
  28. size_t count, loff_t *ppos)
  29. {
  30. unsigned long p = *ppos;
  31. ssize_t ret;
  32. ret = simple_read_from_buffer(buf, count, ppos, sample_buffer,
  33. SAMPLE_BUFFER_SIZE);
  34. if (ret < 0)
  35. return ret;
  36. memset(sample_buffer + p, 0, ret);
  37. return ret;
  38. }
  39. static ssize_t
  40. write_cris_profile(struct file *file, const char __user *buf,
  41. size_t count, loff_t *ppos)
  42. {
  43. sample_buffer_pos = sample_buffer;
  44. memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE);
  45. return count < SAMPLE_BUFFER_SIZE ? count : SAMPLE_BUFFER_SIZE;
  46. }
  47. static const struct file_operations cris_proc_profile_operations = {
  48. .read = read_cris_profile,
  49. .write = write_cris_profile,
  50. .llseek = default_llseek,
  51. };
  52. static int __init init_cris_profile(void)
  53. {
  54. struct proc_dir_entry *entry;
  55. sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
  56. if (!sample_buffer) {
  57. return -ENOMEM;
  58. }
  59. sample_buffer_pos = sample_buffer;
  60. entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL,
  61. &cris_proc_profile_operations);
  62. if (entry) {
  63. proc_set_size(entry, SAMPLE_BUFFER_SIZE);
  64. }
  65. prof_running = 1;
  66. return 0;
  67. }
  68. __initcall(init_cris_profile);