2 * Dynamic Binary Instrumentation Module based on KProbes
3 * writer/debugfs_writer.c
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Vyacheslav Cherkashin <v.cherkashin@samsung.com>
26 #include <linux/module.h>
27 #include <linux/debugfs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <asm/uaccess.h>
31 #include <driver/swap_debugfs.h>
32 #include "swap_writer_module.h"
33 #include "event_filter.h"
36 /* ============================================================================
38 * ============================================================================
40 static char *common_buf = NULL;
41 enum { subbuf_size = 8*1024 };
42 enum { common_buf_size = subbuf_size * NR_CPUS };
44 static int init_buffer(void)
46 common_buf = vmalloc(common_buf_size);
48 return common_buf ? 0 : -ENOMEM;
51 static void exit_buffer(void)
57 static void *get_current_buf(void)
59 return common_buf + subbuf_size * get_cpu();
62 static void put_current_buf(void)
71 /* ============================================================================
73 * ============================================================================
75 static ssize_t write_raw(struct file *file, const char __user *user_buf,
76 size_t count, loff_t *ppos)
81 if (count > subbuf_size)
84 buf = get_current_buf();
85 if (copy_from_user(buf, user_buf, count)) {
90 ret = raw_msg(buf, count);
97 static const struct file_operations fops_raw = {
100 .llseek = default_llseek
107 /* ============================================================================
108 * === FOPS_AVAILABLE_FILTERS ===
109 * ============================================================================
117 static void func_for_read(struct ev_filter *f, void *data)
119 struct read_buf *rbuf = (struct read_buf *)data;
120 int len = strlen(f->name);
122 if (rbuf->end - rbuf->ptr < len + 2)
125 if (rbuf->ptr != rbuf->begin) {
130 memcpy(rbuf->ptr, f->name, len);
134 static ssize_t read_af(struct file *file, char __user *user_buf,
135 size_t count, loff_t *ppos)
138 struct read_buf rbuf = {
141 .end = buf + sizeof(buf)
144 event_filter_on_each(func_for_read, (void *)&rbuf);
149 return simple_read_from_buffer(user_buf, count, ppos,
150 rbuf.begin, rbuf.ptr - rbuf.begin);
153 static const struct file_operations fops_available_filters = {
154 .owner = THIS_MODULE,
156 .llseek = default_llseek
163 /* ============================================================================
164 * === FOPS_FILTER ===
165 * ============================================================================
167 static ssize_t read_filter(struct file *file, char __user *user_buf,
168 size_t count, loff_t *ppos)
170 const char *name = event_filter_get();
171 int len = strlen(name);
175 buf = kmalloc(len + 2, GFP_KERNEL);
176 memcpy(buf, name, len);
181 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len + 2);
187 static ssize_t write_filter(struct file *file, const char __user *user_buf,
188 size_t count, loff_t *ppos)
191 char buf[len], name[len];
195 buf_size = min(count, (size_t)(len - 1));
196 if (copy_from_user(buf, user_buf, buf_size))
200 ret = sscanf(buf, "%31s", name);
204 ret = event_filter_set(name);
211 static const struct file_operations fops_filter = {
212 .owner = THIS_MODULE,
214 .write = write_filter,
215 .llseek = default_llseek
222 /* ============================================================================
224 * ============================================================================
226 static struct dentry *writer_dir = NULL;
228 void exit_debugfs_writer(void)
231 debugfs_remove_recursive(writer_dir);
238 int init_debugfs_writer(void)
241 struct dentry *swap_dir, *dentry;
247 swap_dir = get_swap_debugfs_dir();
248 if (swap_dir == NULL)
251 writer_dir = debugfs_create_dir("writer", swap_dir);
252 if (writer_dir == NULL)
255 dentry = debugfs_create_file("raw", 0600, writer_dir, NULL, &fops_raw);
259 dentry = debugfs_create_file("available_filters", 0600, writer_dir, NULL, &fops_available_filters);
263 dentry = debugfs_create_file("filter", 0600, writer_dir, NULL, &fops_filter);
270 exit_debugfs_writer();