profile.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <linux/init.h>
  2. #include <linux/errno.h>
  3. #include <linux/kernel.h>
  4. #include <linux/proc_fs.h>
  5. #include <linux/types.h>
  6. #include <asm/ptrace.h>
  7. #include <asm/uaccess.h>
  8. #define SAMPLE_BUFFER_SIZE 8192
  9. static char* sample_buffer;
  10. static char* sample_buffer_pos;
  11. static int prof_running = 0;
  12. void
  13. 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. }
  46. static const struct file_operations cris_proc_profile_operations = {
  47. .read = read_cris_profile,
  48. .write = write_cris_profile,
  49. };
  50. static int
  51. __init init_cris_profile(void)
  52. {
  53. struct proc_dir_entry *entry;
  54. sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
  55. if (!sample_buffer) {
  56. return -ENOMEM;
  57. }
  58. sample_buffer_pos = sample_buffer;
  59. entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL,
  60. &cris_proc_profile_operations);
  61. if (entry) {
  62. entry->size = SAMPLE_BUFFER_SIZE;
  63. }
  64. prof_running = 1;
  65. return 0;
  66. }
  67. __initcall(init_cris_profile);