So I'm developing something on eBPF.
I needed to use the unistd.h header because I wanted to use sleep function.
However, I realized that when I type in
#include <unistd.h>
these headers don't get included and I would get an error saying:
warning : implicit delcaration of function 'sleep' is invalid in C99 [-Wimplicit-Function-declaration] sleep (1);
I thought I have done something wrong somewhere else on the code so I tried to include that header on the example from the tutorial and it didn't work that way neither.
So from the code I have attached below, I tried to put a one second of delay before the message would be published.
Has anyone had a same issue and have somehow found a way to use that header inside the c code?
I would very much appreciate it if someone could land me some help!
Thanks a million!
I tried updating the kernel and moved all the header files to ёusr/local/includeё
directory because it said on the internet that this is the place where the compiler first checks for headers but still didn't work.
So here's the code I tried but didn't work.
from bcc import BPF
BPF(text = 'int kprobe__sys_clone(void *ctx)
{
#include <unistd.h>
sleep(1);
bpf_trace_printk("Hello World!\\n");
return 0;
}
').trace_print()
I guess what you want is something like, https://github.com/iovisor/bcc/blob/master/examples/networking/xdp/xdp_drop_count.py
Search "sleep" in that code.
As #Ctx said, the function happens when clone syscall is triggered. There is no point to sleep() there, neither can you do it in the kernel calling sleep().
You might want to understand the above example to see how it sets intervals to print stuff. Hope that helps.
Related
I'm using the CUnit framework for the way it displays the testing results. (I'm a programming & S.O. newbie so step by step answers really appreciated).
Is there any way I can use the same CUnit framework for when I'm testing for functions that I expect to exit()? It doesnt seem so to me, but I'm keen to ask anyway - it would display the pass/fail result along with my other CUnit tests so its ideal.
If not, I've been looking at other noob-friendly solutions (such as this SO post), but I cannot use GOTO/setjmp/longjmp. The solution also needs to be portable.
I'm using Mac & gcc command line to run this code.
EDIT
One of the suggested solutions is to use C Pre-Processor (CPP) Directive /"mocking", which looks ideal? I have used the below code in my test.c file:
#define ERROR(PHRASE) {fprintf(stderr,"Fatal Error %s occurred in %s, line %d\n",PHRASE, FILE, LINE); exit(2);}
#ifdef ERROR(PHRASE)
#define ERROR(PHRASE) {printf("In test phase");}
#endif
#ifndef ERROR(PHRASE #define ERROR(PHRASE) {printf("Not In test phase");}
#endif
Here is the error message that the terminal gives me:
test.c:30:9: warning: 'ERROR' macro redefined [-Wmacro-redefined]
#define ERROR(PHRASE) {printf("In test phase");}
^
test.c:26:9: note: previous definition is here
#define ERROR(PHRASE) {fprintf(stderr,"Fatal Error %s occured in %s, lin...
^
test.c:32:14: warning: extra tokens at end of #ifndef directive
[-Wextra-tokens]
#ifndef ERROR(PHRASE) {printf("Not In test phase");}
Removing the (PHRASE) still gives the same errors.
EDIT
If helpful for anyone else, mocking using the #ifdef was the easiest way to solve this issue in the end. This website was helpful too.
Just so you know what to search for, what you want to do is "mock" the exit() call. The basic idea is to choose a different implementation for the exit function, generally at compile time. Frankly, C doesn't make this particularly easy, but there are some options with varying levels of portability and intrusiveness.
This article describes something that is pretty portable, but also fairly intrusive. Basically, you use macros and/or function pointers to toggle back and forth, which means modifying your code a bit, but honestly it's not that big of a deal.
For something potentially less intrusive but also much less portable, this article has a couple of ideas (I believe both would work on MacOS). Here you get the linker to redirect the exit() call to another function, which you provide. The good news is that it doesn't require any modifications to your code. The bad news is that it requires you to gain the cooperation of the linker, and won't work everywhere (LD_PRELOAD won't work on Windows, and AFAIK --wrap requires GNU ld or something compatible).
One aspect that might be considered if there are issues/increased effort with regards to testing is if there's any scope to change the program being tested in some way that would help with testing without significantly increasing the complexity of the code.
In this case, is there scope to replace the calls to exit() with error return codes from functions, such that the callers can do things such as tidy up, or log state, before actually exiting? If so, this both simplifies testing and is likely to simplify fault-finding when the code is actually used in release/production, as it can be quite tricky to work out why a program just ups and dies on you, especially if the code is tucked away in a library function!
If you want to do something non-intrusive, you can run the function under test as a separate process. You start this with CreateProcess (Windows) or fork (and possibly execv on Max and Linux. Your test code then use wait to test the exit code and pass if the created process exits correctly.
I have used sound and no sound function in turbo C++, but I cannot get it to work in code blocks compiler.
Program:
int main()
{
sound();
delay();
nosound();
return 0;
}
I have added definition for delay and it's working in other programs.
When I am compiling this code I am getting error as ::undefined reference to sound and no sound.
How can I make this work? Or is there a different solution I should use?
You can use "Beep" function which is in windows.h header file.
In Beep function you have to give 2 parameters , first is for frequency and second is duration in milliseconds.
You can '\a' which gives alert sound, '\a' should be placed in printf.
I'm doing this right now! But i'm using C, dunno if for C++ is the same.
The only thing i know is that you have to include the library dos.h to make it work.
the function is Beep(soundfrequency,delay);
give it a try with Beep(2000,2000);
Good luck :)
I am trying to create a new system call in Minix 3.3. At first i just want to create simple printmsg() call that will write "Hello World" on screen.
I looked various tutorials on internet and still couldn't find out solution.
I defined my sys call number in callnr.h like this #define PM_PRINTMSG (PM BASE + 48) and i increased number of sys calls #define NR_PM_CALLS 49.
In table.c I added CALL(PM_PRINTMSG) = doprintmsg.
In proto.h I described function prototype `int do_printmsg(void);
Function implementation is written in misc.c. I added #include <stdio.h> and made Hello World function int do printmsg(){ printf("I am a system call"); return 0; }
When I test my system call in user program _syscall(PM_PROC_NR, PM_PRINTMSG, &m); I don't get any errors but no printf is displayed.
So, is it possible to printf messages from system calls since i had to add <stdio.h> myself in misc.c or i missed some steps. I forgot to mention that i go in /usr/src/releasetools and type make services and make install respectively to recompile kernel.
I figured out what was the problem, so i will post answer if someone needs this in future. I did everything well in this example but i failed to compile kernel.
The location was correct which is usr/src/releasetools, but command needed is make hdboot. Also i figured out my PC somehow wasnt working well with this virtual machines and i had many errors while compiling even though i didn't change anything. When i switched to laptop everything worked fine.
My conclusion is sometimes there is just something wrong on your machine so you should try and test problems on different ones
In my opinion, with the continuous evolution of MINIX 3 and its series, it will be wise to only follow the developer's guide directly from the minix3.org website here
Although you managed to solve the problem yourself, the latest version of MINIX3 (MINIX 3.4) will follow a more advanced and suitable approach.
Please visit the link to learn more.
Many regards.
Ola
I have just dug up a bug in some code we're working with* that was failing due to the following situation:
Assert(SomeVitalFunction(foo) == OK)
This worked fine all the time the DEBUG macros were #defined:
#ifdef DEBUG
#define Assert(x) if((x) == 0){/*some error handling*/}
#else
#define Assert(x)
#endif
But when we #undef'd DEBUG it has the effect of deleting the vital function call from the code.
I can't for the life of me work out how that could ever work with DEBUG #undef'd, and it seems a bad idea generally to put any sort of function call inside an assert like this.
Have I missed something?
* = Edit to clarify following Carpetsmoker's comment: The code comes from a particularly backward cabal of Elbonian code slaves, our task has been to hack, slash, shave, polish, sanitize and apply lipstick to the thing.
You have missed nothing.
Asserts should always be written as if they could disappear at the flick of a compiler switch.
You can call functions that take a relatively long time to complete inside an assert (for example analysing the integrity of a data structure), because the function call will not be present in the release build. The flip side of this is that you cannot call functions that are necessary for correct operation.
It depends upon what SomeVitalFunction is doing. If it has no interesting side-effects, it is ok to use it inside an assert. But if calling or not calling SomeVitalFunction is essential to the program, it is a bug.
For instance, on POSIX, kill(2) with a 0 signal is only useful to test if a process is living. I would imagine that you might be sometimes tempted to use
assert(kill(sompid, 0) == 0); // process sompid exists
assuming that you always suppose that the process sompid is still running.
Likewise, you might use assert(hash_table_count(htbl)>0); to check that some hash table htbl is not empty.
BTW, notice that assert(3) is documented as being ignored if you compile with -DNDEBUG preprocessor option (not if -DDEBUG is not given).
I am trying to embed a plplot graphics inside a gtk window at OSX. I used plplotcanvas but so far i have no success at all. At the wiki there is one example (http://archive.tcltk.co.kr/doc/plplot-html-5.9.4/gui.html) but i can find nothing else. The version i am using is 5.10.0.
Anyone knows how to do it or any advise?. At the examples on the wiki the compiler can not find plplotcanvas.h after change the libraries the compiler says the same for every function:
plem.c:62:2: warning: implicit declaration of function 'plplot_canvas_plwid' is invalid in C99 [-Wimplicit-function-declaration]
plplot_canvas_plwid(canvas,2);
It is the same with every function of plplotcanvas. Thanks in advance guys.
Either you got a version mismatch of libs and API you use, a typo, or you simply forgot to include appropriate headers.
For a more detailed diagnosis you need to post some (relevant) code chunks.
There are plenty of examples for plplot usage, each screenshot links to a full code example