mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-08-18 09:07:43 +08:00
添加了kernel/sample的代码...
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := cn_test
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
MODCFLAGS:=-O6 -Wall -DMODULE -D__KERNEL__ -DLINUX -I$(PWD) -std=c99
|
||||
|
||||
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #print_vmarea.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
|
||||
test :
|
||||
sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* cn_test.c
|
||||
*
|
||||
* 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#define pr_fmt(fmt) "cn_test: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/moduleparam.h>
|
||||
#include <linux/skbuff.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/timer.h>
|
||||
|
||||
#include <linux/connector.h>
|
||||
|
||||
static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
|
||||
static char cn_test_name[] = "cn_test";
|
||||
static struct sock *nls;
|
||||
static struct timer_list cn_test_timer;
|
||||
|
||||
static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
|
||||
{
|
||||
pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
|
||||
__func__, jiffies, msg->id.idx, msg->id.val,
|
||||
msg->seq, msg->ack, msg->len,
|
||||
msg->len ? (char *)msg->data : "");
|
||||
}
|
||||
|
||||
/*
|
||||
* Do not remove this function even if no one is using it as
|
||||
* this is an example of how to get notifications about new
|
||||
* connector user registration
|
||||
*/
|
||||
#if 0
|
||||
static int cn_test_want_notify(void)
|
||||
{
|
||||
struct cn_ctl_msg *ctl;
|
||||
struct cn_notify_req *req;
|
||||
struct cn_msg *msg = NULL;
|
||||
int size, size0;
|
||||
struct sk_buff *skb;
|
||||
struct nlmsghdr *nlh;
|
||||
u32 group = 1;
|
||||
|
||||
size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
|
||||
|
||||
size = NLMSG_SPACE(size0);
|
||||
|
||||
skb = alloc_skb(size, GFP_ATOMIC);
|
||||
if (!skb) {
|
||||
pr_err("failed to allocate new skb with size=%u\n", size);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
|
||||
if (!nlh) {
|
||||
kfree_skb(skb);
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
msg = nlmsg_data(nlh);
|
||||
|
||||
memset(msg, 0, size0);
|
||||
|
||||
msg->id.idx = -1;
|
||||
msg->id.val = -1;
|
||||
msg->seq = 0x123;
|
||||
msg->ack = 0x345;
|
||||
msg->len = size0 - sizeof(*msg);
|
||||
|
||||
ctl = (struct cn_ctl_msg *)(msg + 1);
|
||||
|
||||
ctl->idx_notify_num = 1;
|
||||
ctl->val_notify_num = 2;
|
||||
ctl->group = group;
|
||||
ctl->len = msg->len - sizeof(*ctl);
|
||||
|
||||
req = (struct cn_notify_req *)(ctl + 1);
|
||||
|
||||
/*
|
||||
* Idx.
|
||||
*/
|
||||
req->first = cn_test_id.idx;
|
||||
req->range = 10;
|
||||
|
||||
/*
|
||||
* Val 0.
|
||||
*/
|
||||
req++;
|
||||
req->first = cn_test_id.val;
|
||||
req->range = 10;
|
||||
|
||||
/*
|
||||
* Val 1.
|
||||
*/
|
||||
req++;
|
||||
req->first = cn_test_id.val + 20;
|
||||
req->range = 10;
|
||||
|
||||
NETLINK_CB(skb).dst_group = ctl->group;
|
||||
//netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
|
||||
netlink_unicast(nls, skb, 0, 0);
|
||||
|
||||
pr_info("request was sent: group=0x%x\n", ctl->group);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static u32 cn_test_timer_counter;
|
||||
static void cn_test_timer_func(unsigned long __data)
|
||||
{
|
||||
struct cn_msg *m;
|
||||
char data[32];
|
||||
|
||||
pr_debug("%s: timer fired with data %lu\n", __func__, __data);
|
||||
|
||||
m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
|
||||
if (m) {
|
||||
|
||||
memcpy(&m->id, &cn_test_id, sizeof(m->id));
|
||||
m->seq = cn_test_timer_counter;
|
||||
m->len = sizeof(data);
|
||||
|
||||
m->len =
|
||||
scnprintf(data, sizeof(data), "counter = %u",
|
||||
cn_test_timer_counter) + 1;
|
||||
|
||||
memcpy(m + 1, data, m->len);
|
||||
|
||||
cn_netlink_send(m, 0, GFP_ATOMIC);
|
||||
kfree(m);
|
||||
}
|
||||
|
||||
cn_test_timer_counter++;
|
||||
|
||||
mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
|
||||
}
|
||||
|
||||
static int cn_test_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
|
||||
if (err)
|
||||
goto err_out;
|
||||
cn_test_id.val++;
|
||||
err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
|
||||
if (err) {
|
||||
cn_del_callback(&cn_test_id);
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
setup_timer(&cn_test_timer, cn_test_timer_func, 0);
|
||||
mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
|
||||
|
||||
pr_info("initialized with id={%u.%u}\n",
|
||||
cn_test_id.idx, cn_test_id.val);
|
||||
|
||||
return 0;
|
||||
|
||||
err_out:
|
||||
if (nls && nls->sk_socket)
|
||||
sock_release(nls->sk_socket);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void cn_test_fini(void)
|
||||
{
|
||||
del_timer_sync(&cn_test_timer);
|
||||
cn_del_callback(&cn_test_id);
|
||||
cn_test_id.val--;
|
||||
cn_del_callback(&cn_test_id);
|
||||
if (nls && nls->sk_socket)
|
||||
sock_release(nls->sk_socket);
|
||||
}
|
||||
|
||||
module_init(cn_test_init);
|
||||
module_exit(cn_test_fini);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
|
||||
MODULE_DESCRIPTION("Connector's test module");
|
||||
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* ucon.c
|
||||
*
|
||||
* Copyright (c) 2004+ Evgeniy Polyakov <zbr@ioremap.net>
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <asm/types.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/poll.h>
|
||||
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <linux/connector.h>
|
||||
|
||||
#define DEBUG
|
||||
#define NETLINK_CONNECTOR 11
|
||||
|
||||
/* Hopefully your userspace connector.h matches this kernel */
|
||||
#define CN_TEST_IDX CN_NETLINK_USERS + 3
|
||||
#define CN_TEST_VAL 0x456
|
||||
|
||||
#ifdef DEBUG
|
||||
#define ulog(f, a...) fprintf(stdout, f, ##a)
|
||||
#else
|
||||
#define ulog(f, a...) do {} while (0)
|
||||
#endif
|
||||
|
||||
static int need_exit;
|
||||
static __u32 seq;
|
||||
|
||||
static int netlink_send(int s, struct cn_msg *msg)
|
||||
{
|
||||
struct nlmsghdr *nlh;
|
||||
unsigned int size;
|
||||
int err;
|
||||
char buf[128];
|
||||
struct cn_msg *m;
|
||||
|
||||
size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
|
||||
|
||||
nlh = (struct nlmsghdr *)buf;
|
||||
nlh->nlmsg_seq = seq++;
|
||||
nlh->nlmsg_pid = getpid();
|
||||
nlh->nlmsg_type = NLMSG_DONE;
|
||||
nlh->nlmsg_len = size;
|
||||
nlh->nlmsg_flags = 0;
|
||||
|
||||
m = NLMSG_DATA(nlh);
|
||||
#if 0
|
||||
ulog("%s: [%08x.%08x] len=%u, seq=%u, ack=%u.\n",
|
||||
__func__, msg->id.idx, msg->id.val, msg->len, msg->seq, msg->ack);
|
||||
#endif
|
||||
memcpy(m, msg, sizeof(*m) + msg->len);
|
||||
|
||||
err = send(s, nlh, size, 0);
|
||||
if (err == -1)
|
||||
ulog("Failed to send: %s [%d].\n",
|
||||
strerror(errno), errno);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
printf(
|
||||
"Usage: ucon [options] [output file]\n"
|
||||
"\n"
|
||||
"\t-h\tthis help screen\n"
|
||||
"\t-s\tsend buffers to the test module\n"
|
||||
"\n"
|
||||
"The default behavior of ucon is to subscribe to the test module\n"
|
||||
"and wait for state messages. Any ones received are dumped to the\n"
|
||||
"specified output file (or stdout). The test module is assumed to\n"
|
||||
"have an id of {%u.%u}\n"
|
||||
"\n"
|
||||
"If you get no output, then verify the cn_test module id matches\n"
|
||||
"the expected id above.\n"
|
||||
, CN_TEST_IDX, CN_TEST_VAL
|
||||
);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int s;
|
||||
char buf[1024];
|
||||
int len;
|
||||
struct nlmsghdr *reply;
|
||||
struct sockaddr_nl l_local;
|
||||
struct cn_msg *data;
|
||||
FILE *out;
|
||||
time_t tm;
|
||||
struct pollfd pfd;
|
||||
bool send_msgs = false;
|
||||
|
||||
while ((s = getopt(argc, argv, "hs")) != -1) {
|
||||
switch (s) {
|
||||
case 's':
|
||||
send_msgs = true;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
usage();
|
||||
return 0;
|
||||
|
||||
default:
|
||||
/* getopt() outputs an error for us */
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (argc != optind) {
|
||||
out = fopen(argv[optind], "a+");
|
||||
if (!out) {
|
||||
ulog("Unable to open %s for writing: %s\n",
|
||||
argv[1], strerror(errno));
|
||||
out = stdout;
|
||||
}
|
||||
} else
|
||||
out = stdout;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
|
||||
if (s == -1) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
l_local.nl_family = AF_NETLINK;
|
||||
l_local.nl_groups = -1; /* bitmask of requested groups */
|
||||
l_local.nl_pid = 0;
|
||||
|
||||
ulog("subscribing to %u.%u\n", CN_TEST_IDX, CN_TEST_VAL);
|
||||
|
||||
if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
|
||||
perror("bind");
|
||||
close(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
{
|
||||
int on = 0x57; /* Additional group number */
|
||||
setsockopt(s, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &on, sizeof(on));
|
||||
}
|
||||
#endif
|
||||
if (send_msgs) {
|
||||
int i, j;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
data = (struct cn_msg *)buf;
|
||||
|
||||
data->id.idx = CN_TEST_IDX;
|
||||
data->id.val = CN_TEST_VAL;
|
||||
data->seq = seq++;
|
||||
data->ack = 0;
|
||||
data->len = 0;
|
||||
|
||||
for (j=0; j<10; ++j) {
|
||||
for (i=0; i<1000; ++i) {
|
||||
len = netlink_send(s, data);
|
||||
}
|
||||
|
||||
ulog("%d messages have been sent to %08x.%08x.\n", i, data->id.idx, data->id.val);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
pfd.fd = s;
|
||||
|
||||
while (!need_exit) {
|
||||
pfd.events = POLLIN;
|
||||
pfd.revents = 0;
|
||||
switch (poll(&pfd, 1, -1)) {
|
||||
case 0:
|
||||
need_exit = 1;
|
||||
break;
|
||||
case -1:
|
||||
if (errno != EINTR) {
|
||||
need_exit = 1;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (need_exit)
|
||||
break;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
len = recv(s, buf, sizeof(buf), 0);
|
||||
if (len == -1) {
|
||||
perror("recv buf");
|
||||
close(s);
|
||||
return -1;
|
||||
}
|
||||
reply = (struct nlmsghdr *)buf;
|
||||
|
||||
switch (reply->nlmsg_type) {
|
||||
case NLMSG_ERROR:
|
||||
fprintf(out, "Error message received.\n");
|
||||
fflush(out);
|
||||
break;
|
||||
case NLMSG_DONE:
|
||||
data = (struct cn_msg *)NLMSG_DATA(reply);
|
||||
|
||||
time(&tm);
|
||||
fprintf(out, "%.24s : [%x.%x] [%08u.%08u].\n",
|
||||
ctime(&tm), data->id.idx, data->id.val, data->seq, data->ack);
|
||||
fflush(out);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := kdb_hello
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
MODCFLAGS:=-O6 -Wall -DMODULE -D__KERNEL__ -DLINUX -I$(PWD) -std=c99
|
||||
|
||||
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #print_vmarea.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
|
||||
test :
|
||||
sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Created by: Jason Wessel <jason.wessel@windriver.com>
|
||||
*
|
||||
* Copyright (c) 2010 Wind River Systems, Inc. All Rights Reserved.
|
||||
*
|
||||
* This file is licensed under the terms of the GNU General Public
|
||||
* License version 2. This program is licensed "as is" without any
|
||||
* warranty of any kind, whether express or implied.
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
#include <linux/kdb.h>
|
||||
|
||||
/*
|
||||
* All kdb shell command call backs receive argc and argv, where
|
||||
* argv[0] is the command the end user typed
|
||||
*/
|
||||
static int kdb_hello_cmd(int argc, const char **argv)
|
||||
{
|
||||
if (argc > 1)
|
||||
return KDB_ARGCOUNT;
|
||||
|
||||
if (argc)
|
||||
kdb_printf("Hello %s.\n", argv[1]);
|
||||
else
|
||||
kdb_printf("Hello world!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int __init kdb_hello_cmd_init(void)
|
||||
{
|
||||
/*
|
||||
* Registration of a dynamically added kdb command is done with
|
||||
* kdb_register() with the arguments being:
|
||||
* 1: The name of the shell command
|
||||
* 2: The function that processes the command
|
||||
* 3: Description of the usage of any arguments
|
||||
* 4: Descriptive text when you run help
|
||||
* 5: Number of characters to complete the command
|
||||
* 0 == type the whole command
|
||||
* 1 == match both "g" and "go" for example
|
||||
*/
|
||||
kdb_register("hello", kdb_hello_cmd, "[string]",
|
||||
"Say Hello World or Hello [string]", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit kdb_hello_cmd_exit(void)
|
||||
{
|
||||
kdb_unregister("hello");
|
||||
}
|
||||
|
||||
module_init(kdb_hello_cmd_init);
|
||||
module_exit(kdb_hello_cmd_exit);
|
||||
|
||||
MODULE_AUTHOR("WindRiver");
|
||||
MODULE_DESCRIPTION("KDB example to add a hello command");
|
||||
MODULE_LICENSE("GPL");
|
||||
@@ -0,0 +1,78 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := trace-events-sample
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
MODCFLAGS:=-O6 -Wall -DMODULE -D__KERNEL__ -DLINUX -I$(PWD) -std=c99
|
||||
|
||||
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #print_vmarea.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
|
||||
test :
|
||||
sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kthread.h>
|
||||
|
||||
/*
|
||||
* Any file that uses trace points, must include the header.
|
||||
* But only one file, must include the header by defining
|
||||
* CREATE_TRACE_POINTS first. This will make the C code that
|
||||
* creates the handles for the trace points.
|
||||
*/
|
||||
#define CREATE_TRACE_POINTS
|
||||
#include "trace-events-sample.h"
|
||||
|
||||
static const char *random_strings[] = {
|
||||
"Mother Goose",
|
||||
"Snoopy",
|
||||
"Gandalf",
|
||||
"Frodo",
|
||||
"One ring to rule them all"
|
||||
};
|
||||
|
||||
static void simple_thread_func(int cnt)
|
||||
{
|
||||
int array[6];
|
||||
int len = cnt % 5;
|
||||
int i;
|
||||
|
||||
set_current_state(TASK_INTERRUPTIBLE);
|
||||
schedule_timeout(HZ);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
array[i] = i + 1;
|
||||
array[i] = 0;
|
||||
|
||||
/* Silly tracepoints */
|
||||
trace_foo_bar("hello", cnt, array, random_strings[len],
|
||||
tsk_cpus_allowed(current));
|
||||
|
||||
trace_foo_with_template_simple("HELLO", cnt);
|
||||
|
||||
trace_foo_bar_with_cond("Some times print", cnt);
|
||||
|
||||
trace_foo_with_template_cond("prints other times", cnt);
|
||||
|
||||
trace_foo_with_template_print("I have to be different", cnt);
|
||||
}
|
||||
|
||||
static int simple_thread(void *arg)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
while (!kthread_should_stop())
|
||||
simple_thread_func(cnt++);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct task_struct *simple_tsk;
|
||||
static struct task_struct *simple_tsk_fn;
|
||||
|
||||
static void simple_thread_func_fn(int cnt)
|
||||
{
|
||||
set_current_state(TASK_INTERRUPTIBLE);
|
||||
schedule_timeout(HZ);
|
||||
|
||||
/* More silly tracepoints */
|
||||
trace_foo_bar_with_fn("Look at me", cnt);
|
||||
trace_foo_with_template_fn("Look at me too", cnt);
|
||||
}
|
||||
|
||||
static int simple_thread_fn(void *arg)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
while (!kthread_should_stop())
|
||||
simple_thread_func_fn(cnt++);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEFINE_MUTEX(thread_mutex);
|
||||
|
||||
int foo_bar_reg(void)
|
||||
{
|
||||
pr_info("Starting thread for foo_bar_fn\n");
|
||||
/*
|
||||
* We shouldn't be able to start a trace when the module is
|
||||
* unloading (there's other locks to prevent that). But
|
||||
* for consistency sake, we still take the thread_mutex.
|
||||
*/
|
||||
mutex_lock(&thread_mutex);
|
||||
simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
|
||||
mutex_unlock(&thread_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void foo_bar_unreg(void)
|
||||
{
|
||||
pr_info("Killing thread for foo_bar_fn\n");
|
||||
/* protect against module unloading */
|
||||
mutex_lock(&thread_mutex);
|
||||
if (simple_tsk_fn)
|
||||
kthread_stop(simple_tsk_fn);
|
||||
simple_tsk_fn = NULL;
|
||||
mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
static int __init trace_event_init(void)
|
||||
{
|
||||
simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
|
||||
if (IS_ERR(simple_tsk))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit trace_event_exit(void)
|
||||
{
|
||||
kthread_stop(simple_tsk);
|
||||
mutex_lock(&thread_mutex);
|
||||
if (simple_tsk_fn)
|
||||
kthread_stop(simple_tsk_fn);
|
||||
simple_tsk_fn = NULL;
|
||||
mutex_unlock(&thread_mutex);
|
||||
}
|
||||
|
||||
module_init(trace_event_init);
|
||||
module_exit(trace_event_exit);
|
||||
|
||||
MODULE_AUTHOR("Steven Rostedt");
|
||||
MODULE_DESCRIPTION("trace-events-sample");
|
||||
MODULE_LICENSE("GPL");
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,78 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := trace-printk
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
MODCFLAGS:=-O6 -Wall -DMODULE -D__KERNEL__ -DLINUX -I$(PWD) -std=c99
|
||||
|
||||
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #print_vmarea.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
|
||||
test :
|
||||
sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kthread.h>
|
||||
#include <linux/irq_work.h>
|
||||
|
||||
/* Must not be static to force gcc to consider these non constant */
|
||||
char *trace_printk_test_global_str =
|
||||
"This is a dynamic string that will use trace_puts\n";
|
||||
|
||||
char *trace_printk_test_global_str_irq =
|
||||
"(irq) This is a dynamic string that will use trace_puts\n";
|
||||
|
||||
char *trace_printk_test_global_str_fmt =
|
||||
"%sThis is a %s that will use trace_printk\n";
|
||||
|
||||
static struct irq_work irqwork;
|
||||
|
||||
static void trace_printk_irq_work(struct irq_work *work)
|
||||
{
|
||||
trace_printk("(irq) This is a static string that will use trace_bputs\n");
|
||||
trace_printk(trace_printk_test_global_str_irq);
|
||||
|
||||
trace_printk("(irq) This is a %s that will use trace_bprintk()\n",
|
||||
"static string");
|
||||
|
||||
trace_printk(trace_printk_test_global_str_fmt,
|
||||
"(irq) ", "dynamic string");
|
||||
}
|
||||
|
||||
static int __init trace_printk_init(void)
|
||||
{
|
||||
init_irq_work(&irqwork, trace_printk_irq_work);
|
||||
|
||||
trace_printk("This is a static string that will use trace_bputs\n");
|
||||
trace_printk(trace_printk_test_global_str);
|
||||
|
||||
/* Kick off printing in irq context */
|
||||
irq_work_queue(&irqwork);
|
||||
|
||||
trace_printk("This is a %s that will use trace_bprintk()\n",
|
||||
"static string");
|
||||
|
||||
trace_printk(trace_printk_test_global_str_fmt, "", "dynamic string");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit trace_printk_exit(void)
|
||||
{
|
||||
}
|
||||
|
||||
module_init(trace_printk_init);
|
||||
module_exit(trace_printk_exit);
|
||||
|
||||
MODULE_AUTHOR("Steven Rostedt");
|
||||
MODULE_DESCRIPTION("trace-printk");
|
||||
MODULE_LICENSE("GPL");
|
||||
Reference in New Issue
Block a user