Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 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 | |
| 9 | #define SAMPLE_BUFFER_SIZE 8192 |
| 10 | |
| 11 | static char* sample_buffer; |
| 12 | static char* sample_buffer_pos; |
| 13 | static int prof_running = 0; |
| 14 | |
| 15 | void |
| 16 | cris_profile_sample(struct pt_regs* regs) |
| 17 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 18 | if (!prof_running) |
| 19 | return; |
| 20 | |
| 21 | if (user_mode(regs)) |
| 22 | *(unsigned int*)sample_buffer_pos = current->pid; |
| 23 | else |
| 24 | *(unsigned int*)sample_buffer_pos = 0; |
| 25 | |
| 26 | *(unsigned int*)(sample_buffer_pos + 4) = instruction_pointer(regs); |
| 27 | sample_buffer_pos += 8; |
| 28 | |
| 29 | if (sample_buffer_pos == sample_buffer + SAMPLE_BUFFER_SIZE) |
| 30 | sample_buffer_pos = sample_buffer; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static ssize_t |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 34 | read_cris_profile(struct file *file, char __user *buf, |
| 35 | size_t count, loff_t *ppos) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 36 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 37 | unsigned long p = *ppos; |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 38 | ssize_t ret; |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 39 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 40 | ret = simple_read_from_buffer(buf, count, ppos, sample_buffer, |
| 41 | SAMPLE_BUFFER_SIZE); |
| 42 | if (ret < 0) |
| 43 | return ret; |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 44 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 45 | memset(sample_buffer + p, 0, ret); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 46 | |
Akinobu Mita | ed62f77 | 2008-07-23 21:28:46 -0700 | [diff] [blame] | 47 | return ret; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static ssize_t |
| 51 | write_cris_profile(struct file *file, const char __user *buf, |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 52 | size_t count, loff_t *ppos) |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 53 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 54 | sample_buffer_pos = sample_buffer; |
| 55 | memset(sample_buffer, 0, SAMPLE_BUFFER_SIZE); |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 58 | static const struct file_operations cris_proc_profile_operations = { |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 59 | .read = read_cris_profile, |
| 60 | .write = write_cris_profile, |
| 61 | }; |
| 62 | |
| 63 | static int |
| 64 | __init init_cris_profile(void) |
| 65 | { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 66 | struct proc_dir_entry *entry; |
| 67 | |
| 68 | sample_buffer = kmalloc(SAMPLE_BUFFER_SIZE, GFP_KERNEL); |
| 69 | if (!sample_buffer) { |
| 70 | return -ENOMEM; |
| 71 | } |
| 72 | |
| 73 | sample_buffer_pos = sample_buffer; |
| 74 | |
Denis V. Lunev | c293819 | 2008-04-29 01:02:23 -0700 | [diff] [blame] | 75 | entry = proc_create("system_profile", S_IWUSR | S_IRUGO, NULL, |
| 76 | &cris_proc_profile_operations); |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 77 | if (entry) { |
Cyrill Gorcunov | 542401d | 2007-05-06 14:50:56 -0700 | [diff] [blame] | 78 | entry->size = SAMPLE_BUFFER_SIZE; |
| 79 | } |
| 80 | prof_running = 1; |
| 81 | |
| 82 | return 0; |
Mikael Starvik | 21783c9 | 2005-07-27 11:44:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | __initcall(init_cris_profile); |