profile.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if (p > SAMPLE_BUFFER_SIZE)
  32. return 0;
  33. if (p + count > SAMPLE_BUFFER_SIZE)
  34. count = SAMPLE_BUFFER_SIZE - p;
  35. if (copy_to_user(buf, sample_buffer + p,count))
  36. return -EFAULT;
  37. memset(sample_buffer + p, 0, count);
  38. *ppos += count;
  39. return count;
  40. }
  41. static ssize_t
  42. write_cris_profile(struct file *file, const char __user *buf,
  43. size_t count, loff_t *ppos)
  44. {
  45. sample_buffer_pos = sample_buffer;
  46. memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE);
  47. }
  48. static const struct file_operations cris_proc_profile_operations = {
  49. .read = read_cris_profile,
  50. .write = write_cris_profile,
  51. };
  52. static int
  53. __init init_cris_profile(void)
  54. {
  55. struct proc_dir_entry *entry;
  56. sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
  57. if (!sample_buffer) {
  58. return -ENOMEM;
  59. }
  60. sample_buffer_pos = sample_buffer;
  61. entry = create_proc_entry("system_profile", S_IWUSR | S_IRUGO, NULL);
  62. if (entry) {
  63. entry->proc_fops = &cris_proc_profile_operations;
  64. entry->size = SAMPLE_BUFFER_SIZE;
  65. }
  66. prof_running = 1;
  67. return 0;
  68. }
  69. __initcall(init_cris_profile);