Move headers under include/
[lttng-modules.git] / probes / lttng.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
0c956676
MD
3 * lttng.c
4 *
5 * LTTng logger ABI
6 *
7 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0c956676
MD
8 */
9
10#include <linux/module.h>
11#include <linux/tracepoint.h>
12#include <linux/uaccess.h>
13#include <linux/gfp.h>
14#include <linux/fs.h>
15#include <linux/proc_fs.h>
16#include <linux/slab.h>
17#include <linux/mm.h>
4d328377 18#include <linux/miscdevice.h>
b5304713 19#include <lttng/lttng-events.h>
0c956676
MD
20
21#define TP_MODULE_NOAUTOLOAD
22#define LTTNG_PACKAGE_BUILD
23#define CREATE_TRACE_POINTS
c075712b 24#define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
0c956676 25#define TRACE_INCLUDE_FILE lttng
3bc29f0a 26#define LTTNG_INSTRUMENTATION
0c956676 27
156a3977 28#include <instrumentation/events/lttng-module/lttng.h>
0c956676
MD
29
30/* Events written through logger are truncated at 1024 bytes */
31#define LTTNG_LOGGER_COUNT_MAX 1024
32#define LTTNG_LOGGER_FILE "lttng-logger"
33
20591cf7
MD
34DEFINE_TRACE(lttng_logger);
35
0c956676
MD
36static struct proc_dir_entry *lttng_logger_dentry;
37
38/**
39 * lttng_logger_write - write a userspace string into the trace system
40 * @file: file pointer
41 * @user_buf: user string
42 * @count: length to copy
43 * @ppos: file position
44 *
45 * Copy a userspace string into a trace event named "lttng:logger".
46 * Copies at most @count bytes into the event "msg" dynamic array.
47 * Truncates the count at LTTNG_LOGGER_COUNT_MAX. Returns the number of
48 * bytes copied from the source.
49 * Return -1 on error, with EFAULT errno.
50 */
51static
52ssize_t lttng_logger_write(struct file *file, const char __user *user_buf,
53 size_t count, loff_t *ppos)
54{
467f3a48 55 int nr_pages = 1, i;
0c956676
MD
56 unsigned long uaddr = (unsigned long) user_buf;
57 struct page *pages[2];
58 ssize_t written;
59 int ret;
60
61 /* Truncate count */
62 if (unlikely(count > LTTNG_LOGGER_COUNT_MAX))
63 count = LTTNG_LOGGER_COUNT_MAX;
64
65 /* How many pages are we dealing with ? */
66 if (unlikely((uaddr & PAGE_MASK) != ((uaddr + count) & PAGE_MASK)))
67 nr_pages = 2;
68
69 /* Pin userspace pages */
70 ret = get_user_pages_fast(uaddr, nr_pages, 0, pages);
71 if (unlikely(ret < nr_pages)) {
72 if (ret > 0) {
73 BUG_ON(ret != 1);
74 put_page(pages[0]);
75 }
76 written = -EFAULT;
77 goto end;
78 }
79
80 /* Trace the event */
81 trace_lttng_logger(user_buf, count);
82 written = count;
83 *ppos += written;
84
85 for (i = 0; i < nr_pages; i++)
86 put_page(pages[i]);
87end:
88 return written;
89}
90
91static const struct file_operations lttng_logger_operations = {
92 .write = lttng_logger_write,
93};
94
059de147
MJ
95static const struct proc_ops lttng_logger_proc_ops = {
96 .proc_write = lttng_logger_write,
97};
059de147 98
4d328377
SG
99static struct miscdevice logger_dev = {
100 .minor = MISC_DYNAMIC_MINOR,
101 .name = "lttng-logger",
102 .mode = 0666,
103 .fops = &lttng_logger_operations
104};
105
0c956676
MD
106int __init lttng_logger_init(void)
107{
108 int ret = 0;
109
4d328377
SG
110 /* /dev/lttng-logger */
111 ret = misc_register(&logger_dev);
112 if (ret) {
113 printk(KERN_ERR "Error creating LTTng logger device\n");
114 goto error;
115 }
116
117 /* /proc/lttng-logger */
0c956676
MD
118 lttng_logger_dentry = proc_create_data(LTTNG_LOGGER_FILE,
119 S_IRUGO | S_IWUGO, NULL,
059de147 120 &lttng_logger_proc_ops, NULL);
0c956676 121 if (!lttng_logger_dentry) {
4d328377 122 printk(KERN_ERR "Error creating LTTng logger proc file\n");
0c956676 123 ret = -ENOMEM;
4d328377 124 goto error_proc;
0c956676 125 }
4d328377
SG
126
127 /* Init */
0c956676
MD
128 ret = __lttng_events_init__lttng();
129 if (ret)
130 goto error_events;
131 return ret;
132
133error_events:
134 remove_proc_entry("lttng-logger", NULL);
4d328377
SG
135error_proc:
136 misc_deregister(&logger_dev);
0c956676
MD
137error:
138 return ret;
139}
140
32fe46fb 141void lttng_logger_exit(void)
0c956676
MD
142{
143 __lttng_events_exit__lttng();
144 if (lttng_logger_dentry)
145 remove_proc_entry("lttng-logger", NULL);
4d328377 146 misc_deregister(&logger_dev);
0c956676 147}
This page took 0.040071 seconds and 4 git commands to generate.