-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer_sync.c
234 lines (200 loc) · 5.58 KB
/
buffer_sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @file buffer_sync.c
*
* @remark Copyright (C) 2006-2015 RotateRight, LLC
* @remark Copyright 2002 OProfile authors
* @remark Based on Oprofile's implementation.
* @remark Read the file COPYING
*/
#include <linux/mm.h>
#include <linux/workqueue.h>
#include <linux/notifier.h>
#include <linux/dcookies.h>
#include <linux/profile.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/jiffies.h>
#include <linux/sched.h>
#include <linux/version.h>
#include "rrnotify.h"
#include "rrnotify_stats.h"
#include "event_buffer.h"
#include "buffer_sync.h"
/* The task is on its way out. A sync of the buffer means we can catch
* any remaining samples for this task.
*/
static int task_exit_notify(struct notifier_block * self, unsigned long val, void * data)
{
struct task_struct * task = data;
sync_buffer(task);
return 0;
}
static struct notifier_block task_exit_nb = {
.notifier_call = task_exit_notify,
};
int sync_start(void)
{
int err;
err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
return err;
}
void sync_stop(void)
{
profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
}
/* Optimisation. We can manage without taking the dcookie sem
* because we cannot reach this code without at least one
* dcookie user still being registered (namely, the reader
* of the event buffer). */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
static inline unsigned long fast_get_dcookie(struct path * path)
{
unsigned long cookie;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
if (path->dentry->d_flags & DCACHE_COOKIE)
#else
if (path->dentry->d_cookie)
#endif
return (unsigned long)path->dentry;
get_dcookie(path, &cookie);
return cookie;
}
#else
static inline unsigned long fast_get_dcookie(struct dentry * dentry,
struct vfsmount * vfsmnt)
{
unsigned long cookie;
if (dentry->d_cookie)
return (unsigned long)dentry;
get_dcookie(dentry, vfsmnt, &cookie);
return cookie;
}
#endif
static void release_mm(struct mm_struct * mm)
{
if (!mm)
return;
up_read(&mm->mmap_sem);
mmput(mm);
}
static struct mm_struct * take_tasks_mm(struct task_struct * task)
{
struct mm_struct * mm = get_task_mm(task);
if (mm)
down_read(&mm->mmap_sem);
return mm;
}
static void add_escape_code(int code)
{
add_event_entry(RR_ESCAPE_CODE);
add_event_entry(code);
}
static void add_task_thread_info(struct task_struct * task)
{
unsigned long utime, stime;
struct timespec end_time;
add_escape_code(RRNOTIFY_THREAD_INFO_BEGIN);
// Write the task group id and the thread id
add_event_entry(task->tgid);
add_event_entry(task->pid);
// Write out the user time and the system time
utime = jiffies_to_usecs(task->utime);
stime = jiffies_to_usecs(task->stime);
add_event_entry(utime);
add_event_entry(stime);
// Write out the start time
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
add_event_entry(task->real_start_time/1000000000);
add_event_entry(task->real_start_time);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
add_event_entry(task->real_start_time.tv_sec);
add_event_entry(task->real_start_time.tv_nsec);
#else
add_event_entry(task->start_time.tv_sec);
add_event_entry(task->start_time.tv_nsec);
#endif
// Write out the end time
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
ktime_get_ts(&end_time);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
do_posix_clock_monotonic_gettime(&end_time);
#else
end_time = current_kernel_time();
#endif
add_event_entry(end_time.tv_sec);
add_event_entry(end_time.tv_nsec);
add_escape_code(RRNOTIFY_THREAD_INFO_END);
}
static void add_task_module_info(struct task_struct * task)
{
struct mm_struct *mm = take_tasks_mm(task);
// module info is variable-length - calculate total length in entries first
unsigned long moduleCount = 0;
if(mm) {
struct vm_area_struct * vma;
for (vma = mm->mmap; vma; vma = vma->vm_next) {
if (vma->vm_file && (vma->vm_flags & VM_EXEC)) {
moduleCount++;
} else {
continue;
}
}
} else {
atomic_inc(&rrnotify_stats.sample_lost_no_mm);
}
add_escape_code(RRNOTIFY_MODULE_LIST_BEGIN);
add_event_entry(moduleCount); // number of module entries
if(mm) {
unsigned long cookie = RR_NO_COOKIE;
off_t offset = 0;
struct vm_area_struct * vma;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
// fixup removal of VM_EXECUTABLE flag in linux 3.7.0 and later
// https://lkml.org/lkml/2012/3/31/42
#define VM_EXECUTABLE 0x00001000
unsigned long vm_flags = 0;
unsigned long app_cookie = RR_NO_COOKIE;
if (mm->exe_file) {
app_cookie = fast_get_dcookie(&mm->exe_file->f_path);
}
#endif // >= 3.7.0
for (vma = mm->mmap; vma; vma = vma->vm_next) {
if (vma->vm_file && (vma->vm_flags & VM_EXEC)) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
cookie = fast_get_dcookie(&vma->vm_file->f_path);
#else
cookie = fast_get_dcookie(vma->vm_file->f_dentry,
vma->vm_file->f_vfsmnt);
#endif
offset = vma->vm_pgoff << PAGE_SHIFT;
add_event_entry(vma->vm_start);
add_event_entry(vma->vm_end);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
vm_flags = vma->vm_flags;
if(cookie == app_cookie) {
vm_flags |= VM_EXECUTABLE;
}
add_event_entry(vm_flags);
#else
add_event_entry(vma->vm_flags);
#endif // >= 3.7.0
add_event_entry(cookie);
add_event_entry(offset);
} else {
continue;
}
}
}
release_mm(mm);
add_escape_code(RRNOTIFY_MODULE_LIST_END);
}
void sync_buffer(struct task_struct * task)
{
down(&buffer_sem);
atomic_inc(&rrnotify_stats.event_received);
add_escape_code(RRNOTIFY_RECORD_BEGIN);
add_task_thread_info(task);
add_task_module_info(task);
add_escape_code(RRNOTIFY_RECORD_END);
up(&buffer_sem);
}