profile.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, size_t count, loff_t *ppos)
  28. {
  29. unsigned long p = *ppos;
  30. if (p > SAMPLE_BUFFER_SIZE)
  31. return 0;
  32. if (p + count > SAMPLE_BUFFER_SIZE)
  33. count = SAMPLE_BUFFER_SIZE - p;
  34. if (copy_to_user(buf, sample_buffer + p,count))
  35. return -EFAULT;
  36. memset(sample_buffer + p, 0, count);
  37. *ppos += count;
  38. return count;
  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 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 = (char*)kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL);
  56. sample_buffer_pos = sample_buffer;
  57. entry = create_proc_entry("system_profile", S_IWUSR | S_IRUGO, NULL);
  58. if (entry) {
  59. entry->proc_fops = &cris_proc_profile_operations;
  60. entry->size = SAMPLE_BUFFER_SIZE;
  61. }
  62. prof_running = 1;
  63. return 0;
  64. }
  65. __initcall(init_cris_profile);