ubuntu18.04 vscode kernel module program ERROR: identifier "KBUILD_MODNAME" is undefine - linux

My environment: Ubuntu 18.04, kernel: linux-5.3.0-53 vscode: lastest
Command for fixing linux/module.h not find asm/xxx.h
cd /usr/src/linux-headers-5.3.0-53/include
sudo ln -s asm-generic/ asm
My c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include",
"/usr/src/linux-headers-5.3.0-53/include",
"/usr/src/linux-headers-5.3.0-53-generic/include",
"/usr/src/linux-headers-5.3.0-53/arch/x86/include",
"/usr/src/linux-headers-5.3.0-53/include/uapi",
"/usr/lib/gcc/x86_64-linux-gnu/7.5.0/include"
],
"defines": [
"__GNUC__",
"__KERNEL__"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
My hello_module.c, a simple Linux kernel module program.
//a simple linux kernel module program.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL"); //error here
static int hello_init(void){
printk(KERN_ALERT "hello world\n");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "goodbye world\n");
}
module_init(hello_init);
module_exit(hello_exit);
then ERROR:
identifier "KBUILD_MODNAME" is undefined
How to fix? Please help me..

KBUILD_MODNAME is automatically defined by the Makefile when you build your kernel module, so you will not find it anywhere in the kernel header files.
To stop vscode from complaining about undefined KBUILD_MODNAME identifier, just add a dummy definition to your c_cpp_properties.json file in the "defines" section
"defines": [
"KBUILD_MODNAME=\"hello_module\"",
"__GNUC__",
"__KERNEL__"
],
That should make vscode happy =)
The value you define for KBUILD_MODNAME does not really matter, since it is automatically defined during the make process.
I have not understood if this is your only problem, or if you also have problems to compile the module. If yes let me know and we will figure out how to solve that too!

Related

Need to create node native module using an electron app

I am creating a simple electron app with native modules included. I have included some static libraries sourced from c++ application. Here is my binding.gyp file and the hello.cc file created as mentioned in the https://nodejs.org/dist/latest/docs/api/addons.html
Here is my binding.gyp file
binding.gyp:
"targets": [
{
"target_name": "myext",
"libraries": [
"/Users/rsivaram/logitech/obs-studio/deps/w32-pthreads/libpthreadGC2.a",
"/Users/rsivaram/logitech/obs-studio/build/plugins/obs-x264/libobs-x264-util.a",
"/Users/rsivaram/logitech/obs-studio/build/plugins/mac-syphon/libsyphon-framework.a",
"/Users/rsivaram/logitech/obs-studio/build/deps/file-updater/libfile-updater.a",
"/Users/rsivaram/logitech/obs-studio/build/deps/libcaption/libcaption.a",
"/Users/rsivaram/logitech/obs-studio/build/deps/media-playback/libmedia-playback.a",
],
"cflags!": [ "-fno-exceptions" ],
"cflags": [ "-std=c++11" ],
"cflags_cc!": [ "-fno-exceptions" ]
}
]
}
Here is my hello.cc file
hello.cc:
#include <node.h>
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(
isolate, "world").ToLocalChecked());
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
} // namespace demo
"npm install" works fine, however I m not able to run the application using "npm start".
Errors I m getting
1."identifier "v8" is undefined" in the hello.cc file.
2. "Module did not self-register <abs path of the app>/electron-quick-start/build/Release/myext.node "
How do I create the hello.cc file. Please let me know. Thanks

Failing to insert a custom Kernel module

I'm working on a STM32MP1 target with yocto. In general I can compile a helloworld .ko file and load it into the kernel. But when I want to compile following:
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/types.h>
static struct platform_device *pdev;
static int __init fake_eth_add(void)
{
int inst_id = 0;
pdev = platform_device_alloc("fake-eth", inst_id);
platform_device_add(pdev);
pr_info("fake-eth added");
return 0;
}
static void __exit fake_eth_put(void)
{
pr_info("fake-fake removed");
platform_device_put(pdev);
}
module_init(fake_eth_add);
module_exit(fake_eth_put);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("John Madieu <john.madieu#gmail.com>");
I get following errors:
ERROR: modpost: "platform_device_put" [/home/rdm/ethtest/eth-ins.ko] undefined!
ERROR: modpost: "platform_device_add" [/home/rdm/ethtest/eth-ins.ko] undefined!
ERROR: modpost: "platform_device_alloc" [/home/rdm/ethtest/eth-ins.ko] undefined!
I check if the platform_device_alloc Symbol/Function is defined and it does:
root#txmp-1570:/lib/modules/5.7.1/source/kernel# grep -w platform_device_alloc /proc/kallsyms
c0547cfc T platform_device_alloc
Now I have learned that the symbols must be in the Module.symvers File tagged as EXPORT_SYMBOL_GPL. The symbols were not there, so I added them to the Module.symvers file:
0x00000000 platform_device_alloc vmlinux EXPORT_SYMBOL_GPL
0x00000000 platform_device_add vmlinux EXPORT_SYMBOL_GPL
0x00000000 platform_device_put vmlinux EXPORT_SYMBOL_GPL
Now the code compiles! But when I want to load it with insmod, then I get following error:
root#txmp-1570:/home/rdm/ethtest# insmod eth-ins.ko
insmod: ERROR: could not insert module eth-ins.ko: Unknown symbol in module
And the dmseg logs:
[ 2954.658383] eth_ins: Unknown symbol platform_device_put (err -2)
[ 2954.664194] eth_ins: Unknown symbol platform_device_add (err -2)
[ 2954.672737] eth_ins: Unknown symbol platform_device_alloc (err -2)
What am I missing? Why did I have to append the Module.symvers file by myself? Did I miss something regarding setting up the enviromnent for kernel driver development?

cannot open source file "node.h" in hello.cc

Node Version:v12.1.0
Platform:Mac osx
node-gyp:v5.0.3
I don't know if it's a problem or just a personal question 。as a nodejs addons beginner,I write a
hello.cc file,which as below:
#include <node.h>
#include <v8.h>
using namespace v8;
// 实现􏵹定义的方法
Handle<Value> SayHello(const Arguments &args)
{
HandleScope scope;
return scope.Close(String::New("Hello world!"));
}
// 给传入的目􏵺对象􏱭加sayHello()方法 void Init_Hello(Handle<Object>target) {
target->Set(String::NewSymbol("sayHello"),FunctionTemplate::New(SayHello)->GetFunction());
// 调用NODE_MODULE()方法将注􏵻方法定义􏵼内存中 NODE_MODULE(hello, Init_Hello)
and my binding.gyp is like this:
{
'targets': [
{
'target_name': 'hello', 'sources': [
'src/hello.cc'],
'conditions': [['OS == "mac"',
{
}
]],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}]
}
but the source code show Squiggles
and when I run node-gyp configure build
error shows:
all right version problem.
use nan.h fixed
https://github.com/nodejs/nan

javascript equivalent for python ctypes with node-gyp

I want to transform a python script into a javascript script. My python script loads a dll and use its API.
processings2D = ctypes.CDLL('processings2D.dll')
print(processings2D.ImageProcessor2DCreate())
I try do to the same with node-gyp but my script doesn't find the dll.
console.log(processings2D.ImageProcessor2DCreate());
^
TypeError: Cannot load processings2D.dll library
test.js
var processings2D = require('./build/Release/processings2D.node');
console.log(processings2D.ImageProcessor2DCreate());
addon.cc
#include <nan.h>
#include "processings2D/processings2D.h"
HINSTANCE hDLL = NULL;
typedef int(*Fn)();
void ImageProcessor2DCreate(const Nan::FunctionCallbackInfo<v8::Value>& info) {
hDLL = LoadLibrary("processings2D.dll");
if(!hDLL)
{
Nan::ThrowTypeError("Cannot load processings2D.dll library");
return;
}
Fn fn = (Fn)GetProcAddress(hDLL, "ImageProcessor2DCreate");
if (!fn) {
Nan::ThrowTypeError("Could not load ImageProcessor2DCreate function");
FreeLibrary(hDLL);
return;
}
info.GetReturnValue().Set(Nan::New(fn()));
}
void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("ImageProcessor2DCreate").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(ImageProcessor2DCreate)->GetFunction());
}
NODE_MODULE(twoD, Init)
binding.gyp
{
"targets": [
{
"target_name": "processings2D",
"sources": [
"addon.cc"
],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
dll is in the Release folder /build/Release/processings2D.dll
Am I in the right direction ?
The solution was really simple :
My dll was a 32 bit version, so I should build my module with the 32 bit arch flag and execute my test with the 32 bit version of node.
node-gyp clean configure --arch=ia32 build
"C:\Program Files (x86)\nodejs\node.exe" test.js

How can I write to TTY from a kernel module?

First post to SO, so I'll try to make the question right.
I'm making a simple Linux kernel module with the goal of echoing data back to the TTY shell from where the kernel module was loaded. The problem I having is the kernel "Ooops"-ing with the following message (caught with " watch 'dmesg | tail -50' "). The kernel module's name is Seraphim:
[ 184.222748] SELinux: initialized (dev proc, type proc), uses genfs_contexts
[ 1877.456607] seraphim: module verification failed: signature and/or required key missing - tainting kernel
[ 1877.457321] ------------------
[ 1877.457324] Seraphim started.
[ 1877.457348] BUG: unable to handle kernel NULL pointer dereference at 0000000000000218
[ 1877.457411] IP: [<ffffffffa0012030>] seraphim_entry+0x30/0x1000 [seraphim]
[ 1877.457462] PGD 115a2e067 PUD 10aca8067 PMD 0
[ 1877.457498] Oops: 0000 [#1] SMP
[ 1877.457524] Modules linked in: seraphim(OF+) rfcomm bnep nf_conntrack_netbios_ns nf_conn track_broadcast ipt_MASQUERADE ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge stp llce btable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_ma etc.
The code used for writing data to the TTY terminal follows:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tty.h>
static void printString(char *string) {
struct tty_struct *tty;
tty = current->signal->tty;
if(tty != NULL) {
(tty->driver->ops->write) (tty, string, strlen(string));
}
else
printk("tty equals to zero");
}
What am I doing wrong?
I was following the tutorial at http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf but it was out of date (the kernel I am using is 3.11.10-200 on Fedora 19), so I had to rummage through 3.11.10-200 source files to find the adequate structures.
use tty = get_current_tty(); instead of tty = current->signal->tty;
that's it
you need to lock the tty before accessing it and get_current_tty does it internally
NOTE: get_current_tty is under EXPORT_SYMBOL_GPL, hence your module or code

Resources