I am trying to create a system call in Linux ubuntu-studio that has the following functionality:
bring virtual memory the larger process and how much bytes it occupies.
below is the implementation code of the system call:
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/sched.h>
struct pid_size{
int pid;
unsigned long size;
};
struct pid_size pid maiorMemoriaVirtual(void)
{
struct task_struct *task;
struct task_struct *maior = NULL;
for_each_process(task)
{
if(maior == NULL)
maior = task;
else if(task->it_virt_value > maior->it_virt_value)
maior = task;
}
struct pid_size retorno;
retorno.pid = maior->pid;
retorno.size = maior->it_virt_value;
return retorno;
}
When compiling to try compiling the kernel got the following error:
kernel/maiorMemoriaVirtual.c: In function ‘sys_maiorMemoriaVirtual’:
kernel/maiorMemoriaVirtual.c:19:21: error: ‘struct task_struct’ has no member named ‘it_virt_value’
else if(task->it_virt_value > maior->it_virt_value)
^
kernel/maiorMemoriaVirtual.c:19:44: error: ‘struct task_struct’ has no member named ‘it_virt_value’
else if(task->it_virt_value > maior->it_virt_value)
^
kernel/maiorMemoriaVirtual.c:23:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
struct pid_size retorno;
^
kernel/maiorMemoriaVirtual.c:25:25: error: ‘struct task_struct’ has no member named ‘it_virt_value’
retorno.size = maior->it_virt_value;
^
make[1]: ** [kernel/maiorMemoriaVirtual.o] Erro 1
I came across a similar problem while working on an assignment and I have realized that there are quite a few changes that have been made to the definition of the 'task_struct' structure.
My guess is that you are looking for accumulated virtual memory usage. You might find this link useful. Accumulated Virtual Memory usage
Link to the task_struct definition in kernel 2.6.36
Related
I'm working on spinlock code in linux kernel source of 4.9.89 version.
After I've done some modification on "include/linux/spinlock.h" and "include/linux/spinlock_types.h", I build the kernel.
But some errors occurred as following:
./include/linux/seqlock.h:406:2: error: unknown type name ‘spinlock_t’
spinlock_t lock;
^
./include/linux/seqlock.h: In function ‘write_seqlock’:
./include/linux/seqlock.h:448:2: error: implicit declaration of function ‘spin_lock’ [-Werror=implicit-function-declaration]
spin_lock(&sl->lock);
^
./include/linux/seqlock.h: In function ‘write_sequnlock’:
./include/linux/seqlock.h:455:2: error: implicit declaration of function ‘spin_unlock’ [-Werror=implicit-function-declaration]
spin_unlock(&sl->lock);
^
./include/linux/seqlock.h: In function ‘write_seqlock_bh’:
./include/linux/seqlock.h:460:2: error: implicit declaration of function ‘spin_lock_bh’ [-Werror=implicit-function-declaration]
spin_lock_bh(&sl->lock);
^
./include/linux/seqlock.h: In function ‘write_sequnlock_bh’:
./include/linux/seqlock.h:467:2: error: implicit declaration of function ‘spin_unlock_bh’ [-Werror=implicit-function-declaration]
spin_unlock_bh(&sl->lock);
^
./include/linux/seqlock.h: In function ‘write_seqlock_irq’:
./include/linux/seqlock.h:472:2: error: implicit declaration of function ‘spin_lock_irq’ [-Werror=implicit-function-declaration]
spin_lock_irq(&sl->lock);
^
./include/linux/seqlock.h: In function ‘write_sequnlock_irq’:
./include/linux/seqlock.h:479:2: error: implicit declaration of function ‘spin_unlock_irq’ [-Werror=implicit-function-declaration]
spin_unlock_irq(&sl->lock);
^
./include/linux/seqlock.h: In function ‘__write_seqlock_irqsave’:
./include/linux/seqlock.h:486:2: error: implicit declaration of function ‘spin_lock_irqsave’ [-Werror=implicit-function-declaration]
spin_lock_irqsave(&sl->lock, flags);
^
./include/linux/seqlock.h: In function ‘write_sequnlock_irqrestore’:
./include/linux/seqlock.h:498:2: error: implicit declaration of function ‘spin_unlock_irqrestore’ [-Werror=implicit-function-declaration]
spin_unlock_irqrestore(&sl->lock, flags);
I did not find anything weird in my code and I'm stuck here.
What I did in the kernel source is that I try to estimate lock holding time and lock waiting time only:
// function modifications
static __always_inline void spin_lock(spinlock_t *lock)
{
struct timeval start, end;
do_gettimeofday(&start);
raw_spin_lock(&lock->rlock);
do_gettimeofday(&end);
atomic_add(&waiting_time, (end.tv_usec - start.tv_usec));
lock->holding_time = end.tv_usec;
}
static __always_inline void spin_unlock(spinlock_t *lock)
{
struct timeval end;
raw_spin_unlock(&lock->rlock);
do_gettimeofday(&end);
atomic_add(&holding_time, (end.tv_usec - lock->holding_time));
}
// macro modification
#define spin_lock_nested(lock, subclass) \
do { \
struct timeval start, end; \
do_gettimeofday(&start); \
raw_spin_lock_nested(spinlock_check(lock), subclass); \
do_gettimeofday(&end); \
atomic_add(&waiting_time, (end.tv_usec - start.tv_usec)); \
lock->holding_time = end.tv_usec; \
} while (0)
// I added 2 atomic values to cumulate the lock time values in spinlock.h file
atomic_t waiting_time;
atomic_t holding_time;
// I added an unsigned int value (holding_time) to estimate lock holding time in spinlock struct
typedef struct spinlock {
union {
struct raw_spinlock rlock;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
struct {
u8 __padding[LOCK_PADSIZE];
struct lockdep_map dep_map;
};
#endif
};
unsigned int holding_time;
} spinlock_t;
Why did the error occur?
I'm waiting for you help.
Thanks!
I'm trying to compile https://sourceforge.net/p/dom-s-orm/code/HEAD/tree/trunk/src/
And whenever I try to compile it I get this error:
[root#localhost dev]# gmake all
(cd objects; gmake)
gmake[1]: Entering directory '/home/burst/dev/objects'
--> Compiling object helper Account from ../output/objects/Account_.cxx
In file included from ../output/objects/Account_.hxx:11:0,
from ../output/objects/Account_.cxx:4:
../build/contrib/DORM/include/Timestamp.hpp: In constructor ‘DORM::Timestamp::Timestamp(const timeval&)’:
../build/contrib/DORM/include/Timestamp.hpp:19:44: error: cannot convert ‘const timeval’ to ‘__time_t {aka long int}’ in initialization
Timestamp(const struct timeval &t): tv{t} {};
^
gmake[1]: *** [../build/gmake/objects.gmk:59: ../output/objects/Account_.o] Error 1
gmake[1]: Leaving directory '/home/burst/dev/objects'
gmake: *** [Makefile:5: all] Error 2
The content of Timestamp.hpp is:
#ifndef DORM__INCLUDE__TIMESTAMP_HPP
#define DORM__INCLUDE__TIMESTAMP_HPP
#include <cppconn/sqlstring.h>
#include <string>
#include <ctime>
#include <sys/time.h>
namespace DORM {
class Timestamp {
public:
struct timeval tv;
Timestamp(): tv{0, 0} {};
Timestamp(const time_t &t): tv{t, 0} {};
Timestamp(const struct timeval &t): tv{t} {};
Timestamp(const std::string time_str);
Timestamp(sql::SQLString time_str);
operator time_t() { return tv.tv_sec; };
operator timeval() { return tv; };
};
}
#endif
Any help is welcomed to be able to fix this.
Thanks a lot!
Regards
Since struct timeval is a simple C struct, it doesn't have a copy constructor. That makes tv{t} in
Timestamp(const struct timeval &t): tv{t} {};
an aggregate initialization (see syntax 2), trying to initialize tv.sec with t. tv.sec is a time_t not a struct timeval, hence the error.
Use parentheses instead like so:
Timestamp(const struct timeval &t): tv(t) {};
I am trying to make a threaded grabber for my OpenCV application. I am unable to figure out why this code doesn't compile. It gives me an error that I believe means that the function call is wrong. However, it is the exact same way how I start a thread using std::thread usually! I want to use std::thread to accomplish it because it will offer more platform-independent compatibility, so please don't tell me to use a platform-specific library. I also want this to be STL-based, so no Boost or DLib. In my main.cpp, I have a working thread application, the code below:
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <mutex>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define read_failure_threshold 512
long grabbers_active = 0;
namespace dev
{
class grabber
{
private:
bool enabled = false;
std::mutex lock;
int capture_mode;
int capture_id;
unsigned long read_failures = 0;
std::string stream;
std::string grabber_name;
cv::Mat image;
public:
void grabber_t()
{
.......[unimportant code]........
}
grabber(std::string name, int captureMode, int captureId, std::string location)
{
.......[unimportant code]........
}
void start()
{
if(!enabled)
{
std::thread grabber_thread(grabber_t);
grabber_thread.detach();
}
enabled = true;
grabbers_active++;
}
cv::Mat getImage()
{
.......[unimportant code]........
}
};
}
[ERRORS:]
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:0:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp: In member function ‘void dev::grabber::start()’:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
std::thread grabber_thread(grabber_t);
^
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: note: candidates are:
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:4:0,
from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:
/usr/include/c++/4.8/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (dev::grabber::*)(); _Args = {}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8/thread:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (dev::grabber::*&&)()’
/usr/include/c++/4.8/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8/thread:128:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.8/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8/thread:122:5: note: candidate expects 0 arguments, 1 provided
make[2]: *** [CMakeFiles/build.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/build.dir/all] Error 2
make: *** [all] Error 2
The error log is also at the end of the code. The only errors I am worried about are the threading ones. The other ones are simple fixes, but require me to have the threading working.
I am in Ubuntu, using g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2. I have C++0x enabled in my CMakeLists.txt. Everything works perfectly in there
My main objective is to figure out why I am getting this error. I have been googling and trying different tricks for many hours, but nothing is working!
Thanks in advanced for your help :)
Change that :
std::thread grabber_thread(grabber_t);
Into that :
std::thread grabber_thread(&grabber::grabber_t, this);
grabber_t is a reference to non-static member function, you need to pass its address, but &grabber_t can't work as you must explicitly qualify name of member function when taking its address, thus resulting in &grabber::grabber_t.
presently i am working on a research project in which we are trying to run a wireless netwok protocal namely aodvuu developed by uppasala university. The protocal was developed in linux kernel version 2.4 x and now we are trying to run it on kernel version 3.8. The following is the corresponding kernel code which is facing problem (kaodv-netlink.c).
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
static void hello_nl_recv_msg(struct sk_buff *skb) {
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
char *msg="Hello from kernel";
int res;
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
msg_size=strlen(msg);
nlh=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */
skb_out = nlmsg_new(msg_size,0);
if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
strncpy(nlmsg_data(nlh),msg,msg_size);
res=nlmsg_unicast(nl_sk,skb_out,pid);
if(res<0)
printk(KERN_INFO "Error while sending bak to user\n");
}
static int __init hello_init(void) {
printk("Entering: %s\n",__FUNCTION__);
/* This is for 3.6 kernels and above.
struct netlink_kernel_cfg cfg = {
.input = hello_nl_recv_msg,
};
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);*/
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL,THIS_MODULE);
if(!nl_sk)
{
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "exiting hello module\n");
netlink_kernel_release(nl_sk);
}
module_init(hello_init); module_exit(hello_exit);
MODULE_LICENSE("GPL");
Error i am getting is
timer_queue.o aodv_socket.o aodv_hello.o aodv_neighbor.o aodv_timeout.o routing_table.o seek_list.o aodv_rreq.o aodv_rrep.o aodv_rerr.o nl.o locality.o
make -C /home/lp3/aodvuu096/lnx KERNEL_DIR=/lib/modules/3.8.0-31-generic/build KCC=gcc XDEFS=-DDEBUG
make[1]: Entering directory `/home/lp3/aodvuu096/lnx'
make -C /lib/modules/3.8.0-31-generic/build M=/home/lp3/aodvuu096/lnx modules
make[2]: Entering directory `/usr/src/linux-headers-3.8.0-31-generic'
CC [M] /home/lp3/aodvuu096/lnx/kaodv-mod.o
CC [M] /home/lp3/aodvuu096/lnx/kaodv-debug.o
CC [M] /home/lp3/aodvuu096/lnx/kaodv-netlink.o
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_init’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:372:21: warning: passing argument 3 of ‘netlink_kernel_create’ from incompatible pointer type [enabled by default]
include/linux/netlink.h:48:1: note: expected ‘struct netlink_kernel_cfg *’ but argument is of type ‘void (*)(struct sk_buff *)’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:372:21: error: too many arguments to function ‘netlink_kernel_create’
include/linux/netlink.h:48:1: note: declared here
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:374:13: error: invalid storage class for function ‘kaodv_netlink_rcv_sk’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_rcv_sk’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:378:14: error: ‘recv_cmd’ undeclared (first use in this function)
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:378:14: note: each undeclared identifier is reported only once for each function it appears in
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:382:1: error: incompatible type for argument 3 of ‘netlink_kernel_create’
include/linux/netlink.h:48:1: note: expected ‘struct netlink_kernel_cfg *’ but argument is of type ‘struct netlink_kernel_cfg’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:386:3: warning: ‘return’ with a value, in function returning void [enabled by default]
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:389:1: warning: ‘return’ with a value, in function returning void [enabled by default]
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_init’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:374:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
make[3]: *** [/home/lp3/aodvuu096/lnx/kaodv-netlink.o] Error 1
make[2]: *** [_module_/home/lp3/aodvuu096/lnx] Error 2
make[2]: Leaving directory `/usr/src/linux-headers-3.8.0-31-generic'
make[1]: *** [kaodv.ko] Error 2
make[1]: Leaving directory `/home/lp3/aodvuu096/lnx'
make: *** [kaodv] Error 2
lp3#lp3-Latitude-E5420:~/aodvuu096$
Comment out this line to fix this error.
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL,THIS_MODULE);
I'm writing a kernel module under Scientific Linux 6.3 x86_64 and I'm looking to use kprobes. In this module, I need access to the first argument of a function on return, so jprobes are out.
I found this very helpful post: Getting function arguments using kprobes
However, when I try accessing regs->rdi inside my probe, the compiler complains with
error: ‘struct pt_regs’ has no member named ‘rdi’
During my module initialization, I run this check with no problems:
#ifndef CONFIG_X86_64
printk(KERN_ALERT "Error: this module only supports x86_64!\n");
return -EINVAL;
#endif
Is there anything else I should be looking at?
uname -r returns 2.6.32-279.14.1.el6.x86_64.debug
Here is a MWE:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/blkdev.h>
static int kprobe_test(struct kprobe *p, struct pt_regs *regs) {
printk(KERN_INFO "rdi: %p\n", regs->rdi);
return 0;
}
static struct kprobe myprobe = {
.pre_handler = NULL,
.post_handler = kprobe_test,
.fault_handler = NULL,
.addr = (kprobe_opcode_t *) generic_make_request,
};
int init_module(void) {
register_kprobe(&myprobe);
return 0;
}
void cleanup_module(void) {
unregister_kprobe(&myprobe);
}
Which results in:
...
/home/user/kmod/kprobe_64_mwe/kprobe_mwe.c:7: error: ‘struct pt_regs’ has no member named ‘rdi’
...
The definition of pt_reg changes when __KERNEL__ is defined. Try using di instead.