Commit | Line | Data |
---|---|---|
0c956676 MD |
1 | /* |
2 | * lttng.c | |
3 | * | |
4 | * LTTng logger ABI | |
5 | * | |
6 | * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/tracepoint.h> | |
25 | #include <linux/uaccess.h> | |
26 | #include <linux/gfp.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/proc_fs.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/mm.h> | |
31 | #include "../wrapper/vmalloc.h" | |
32 | #include "../lttng-events.h" | |
33 | ||
34 | #define TP_MODULE_NOAUTOLOAD | |
35 | #define LTTNG_PACKAGE_BUILD | |
36 | #define CREATE_TRACE_POINTS | |
37 | #define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module | |
38 | #define TRACE_INCLUDE_FILE lttng | |
39 | ||
40 | #include "../instrumentation/events/lttng-module/lttng.h" | |
41 | ||
42 | /* Events written through logger are truncated at 1024 bytes */ | |
43 | #define LTTNG_LOGGER_COUNT_MAX 1024 | |
44 | #define LTTNG_LOGGER_FILE "lttng-logger" | |
45 | ||
46 | static struct proc_dir_entry *lttng_logger_dentry; | |
47 | ||
48 | /** | |
49 | * lttng_logger_write - write a userspace string into the trace system | |
50 | * @file: file pointer | |
51 | * @user_buf: user string | |
52 | * @count: length to copy | |
53 | * @ppos: file position | |
54 | * | |
55 | * Copy a userspace string into a trace event named "lttng:logger". | |
56 | * Copies at most @count bytes into the event "msg" dynamic array. | |
57 | * Truncates the count at LTTNG_LOGGER_COUNT_MAX. Returns the number of | |
58 | * bytes copied from the source. | |
59 | * Return -1 on error, with EFAULT errno. | |
60 | */ | |
61 | static | |
62 | ssize_t lttng_logger_write(struct file *file, const char __user *user_buf, | |
63 | size_t count, loff_t *ppos) | |
64 | { | |
65 | unsigned int nr_pages = 1, i; | |
66 | unsigned long uaddr = (unsigned long) user_buf; | |
67 | struct page *pages[2]; | |
68 | ssize_t written; | |
69 | int ret; | |
70 | ||
71 | /* Truncate count */ | |
72 | if (unlikely(count > LTTNG_LOGGER_COUNT_MAX)) | |
73 | count = LTTNG_LOGGER_COUNT_MAX; | |
74 | ||
75 | /* How many pages are we dealing with ? */ | |
76 | if (unlikely((uaddr & PAGE_MASK) != ((uaddr + count) & PAGE_MASK))) | |
77 | nr_pages = 2; | |
78 | ||
79 | /* Pin userspace pages */ | |
80 | ret = get_user_pages_fast(uaddr, nr_pages, 0, pages); | |
81 | if (unlikely(ret < nr_pages)) { | |
82 | if (ret > 0) { | |
83 | BUG_ON(ret != 1); | |
84 | put_page(pages[0]); | |
85 | } | |
86 | written = -EFAULT; | |
87 | goto end; | |
88 | } | |
89 | ||
90 | /* Trace the event */ | |
91 | trace_lttng_logger(user_buf, count); | |
92 | written = count; | |
93 | *ppos += written; | |
94 | ||
95 | for (i = 0; i < nr_pages; i++) | |
96 | put_page(pages[i]); | |
97 | end: | |
98 | return written; | |
99 | } | |
100 | ||
101 | static const struct file_operations lttng_logger_operations = { | |
102 | .write = lttng_logger_write, | |
103 | }; | |
104 | ||
105 | int __init lttng_logger_init(void) | |
106 | { | |
107 | int ret = 0; | |
108 | ||
109 | wrapper_vmalloc_sync_all(); | |
110 | lttng_logger_dentry = proc_create_data(LTTNG_LOGGER_FILE, | |
111 | S_IRUGO | S_IWUGO, NULL, | |
112 | <tng_logger_operations, NULL); | |
113 | if (!lttng_logger_dentry) { | |
114 | printk(KERN_ERR "Error creating LTTng logger file\n"); | |
115 | ret = -ENOMEM; | |
116 | goto error; | |
117 | } | |
118 | ret = __lttng_events_init__lttng(); | |
119 | if (ret) | |
120 | goto error_events; | |
121 | return ret; | |
122 | ||
123 | error_events: | |
124 | remove_proc_entry("lttng-logger", NULL); | |
125 | error: | |
126 | return ret; | |
127 | } | |
128 | ||
129 | void __exit lttng_logger_exit(void) | |
130 | { | |
131 | __lttng_events_exit__lttng(); | |
132 | if (lttng_logger_dentry) | |
133 | remove_proc_entry("lttng-logger", NULL); | |
134 | } |