profile.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  14. cris_profile_sample(struct pt_regs* regs)
  15. {
  16. if (!prof_running)
  17. return;
  18. if (user_mode(regs))
  19. *(unsigned int*)sample_buffer_pos = current->pid;
  20. else
  21. *(unsigned int*)sample_buffer_pos = 0;
  22. *(unsigned int*)(sample_buffer_pos + 4) = instruction_pointer(regs);
  23. sample_buffer_pos += 8;
  24. if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE)
  25. sample_buffer_pos = sample_buffer;
  26. }
  27. static ssize_t
  28. read_cris_profile(struct file *file, char __user *buf,
  29. size_t count, loff_t *ppos)
  30. {
  31. unsigned long p = *ppos;
  32. ssize_t ret;
  33. ret = simple_read_from_buffer(buf, count, ppos, sample_buffer,
  34. SAMPLE_BUFFER_SIZE);
  35. if (ret < 0)
  36. return ret;
  37. memset(sample_buffer + p, 0, ret);
  38. return ret;
  39. }
  40. static ssize_t
  41. write_cris_profile(struct file *file, const char __user *buf,
  42. size_t count, loff_t *ppos)
  43. {
  44. sample_buffer_pos = sample_buffer;
  45. memset(sample_buffer, 0, 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. };
  51. static int
  52. __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. entry->size = SAMPLE_BUFFER_SIZE;
  64. }
  65. prof_running = 1;
  66. return 0;
  67. }
  68. __initcall(init_cris_profile);