I would like to load some text from a file and print it out using eBPF. Is such a thing even possible? I did something similar using bpf_probe_read but I'm wondering if there is a simpler way of doing something like this by just giving it a location? I want to try expanding this by using CSVs for instance as a means of practice.
#!/usr/bin/python3
# BPF PROGRAM
bpfprogram = """
static void helloworld() {
bpf_trace_printk("Hello World!\\n");
}
int helloworld2(void *ctx)
{
helloWorld();
return 0;
}
"""
b = BPF(text=bpfprogram)
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="helloworld")
b.trace_print()
Related
I'm writing some common DML code that contains a fairly complex method, something like:
saved uint32 checksum_ini;
method calculate_checksum(bytes_t data) -> (uint32 sum) {
uint32 result = checksum_ini;
for (int i = 0; i < data.size; ++i) {
result = f(result, data.data[i]);
}
return result;
}
My device calls the function indirectly by reading and writing some registers, which makes it cumbersome to unit test all corner cases of the checksum algorithm.
How can I efficiently write a unit test for my checksum implementation?
One approach is to create a dedicated test module, say test-checksum, containing a test device, say test_checksum_dev, that imports only your common code, and exposes the calculate_checksum method to Python, where it is easy to write tests. This is done in two steps: First, expose the method to C:
dml 1.4;
device test_checksum_dev;
import "checksum-common.dml";
// Make DML method calculate_checksum available as extern C symbol "calculate_checksum"
// The signature will be:
// uint64 calculate_checksum(conf_object_t *obj, bytes_t data)
export calculate_checksum as "calculate_checksum";
The second step is to expose it to Python. Create checksum.h:
#ifndef CHECKSUM_H
#define CHECKSUM_H
#include <simics/base/types.h>
#include <simics/pywrap.h>
extern uint32 calculate_checksum(conf_object_t *obj, bytes_t data);
#endif /* CHECKSUM_H */
(if you also add header %{ #include "checksum.h" %} to the DML file, you will get a hard check that signatures stay consistent).
Now add the header file to IFACE_FILES in your module makefile to create a Python wrapping:
SRC_FILES = test-checksum.dml
IFACE_FILES = checksum.h
include $(MODULE_MAKEFILE)
You can now call the DML method directly from your test:
SIM_load_module('test-checksum')
from simmod.test_checksum.checksum import calculate_checksum
obj = SIM_create_object('test_checksum_dev', 'dev', checksum_ini=0xdeadbeef)
assert calculate_checksum(obj, b'hello world') == (0xda39ba47).to_bytes(4, 'little')
[programming//in this code i am able only to enter one
value through scan F() FUNCTION although i have used loop up to 5 but when i enter first value program automatically ends whats wrong please answer???][1]
code:
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[5];
int j=0;
if(j<5){
printf("enter\n");
scanf("%d",&arr[j]);
printf("well enter next");
int y;
y=arr[j];
if (y<5)
{ printf("value:%d",arr[j]);
}
j++;
}
return 0;
}
I think you need to figure this out on your own, but let me help you get started.
Let me show you how to store 5 integers taken from the keyboard and you'll have to learn the rest. C is a hard language to start with, so don't be discouraged, I'm still a beginner too.
I will also use some code formatting that I like. There are probably better formats out there but definitely think about how the code looks on the screen.
practice.c
#include<stdio.h>
int main() {
int arr[5];
int j=0;
printf("Enter the first integer:");
while (j<5) {
scanf("%d",&arr[j]);
printf("Next:");
j++;
}
/* I'd then practice printing out the 5 integers.
* Then you can figure out how to stop the loop based on some logical condition.
*/
return 0;
}
Compile with:
gcc -Wall -o practice practice.c
So I've been trying to access the iphdr using eBPF.
static inline int parse_ipv4(void *data, u64 nh_off, void *data_end) {
struct iphdr *iph = data + nh_off;
if ((void*)&iph[1] > data_end)
return 0;
return iph->protocol;
}
When I use the code above in the eBPF function, it works fine like :
if (h_proto == htons(ETH_P_IP)){
index = parse_ipv4(data, nh_off, data_end);
Like this, calling parse_ipv4 function works.
However, if I try to access the ipheader directly without using the function, it doesn't work.
if (h_proto == htons(ETH_P_IP)){
index = parse_ipv4(data, nh_off, data_end);
struct iphdr *iph2 = sizeof(*eth) + nh_off;
}
This gives me an error : HINT: The invalid mem access 'inv' error can happen if you try to dereference memory without first using bpf_probe_read() to copy it to the BPF stack. Sometimes the bpf_probe_read is automatic by the bcc rewriter, other times you'll need to be explicit.
and fails to activate.
Thank you so much in advance!
Unless I misunderstand your program, the following:
struct iphdr *iph2 = sizeof(*eth) + nh_off;
looks erroneous. Instead, iph2 should be something like data + nh_off, just as in your function, no? If you set it to the sum of two sizes, without any base address, then you try to access data at an arbitrary memory location (something like 0x28 I guess), which of course is not permitted.
In Unity3D, I want to load position data from text file.
Here is an example of the text file.
data_01.txt
1; -5; -10
data_ 02.txt
2; 2; 5
data_03.txt
3; 2; 4
...............
all files are 1 line.
I'd like to input these data into a object.
I want to load text file in 30 text files per 1 second.
There is code saving position data.
public void Start()
{
try
{
System.IO.StreamWriter saveFile = new System.IO.StreamWriter("savefile.txt", false);
Transform[] transforms = GameObject.FindObjectsOfType(typeof(Transform)) as Transform[];
foreach (Transform t in transforms)
{
this.saveFile.WriteLine(t.position);
}
}
catch (System.Exception ex)
{
Debug.Log(ex.Message);
}
finally
{
saveFile.Close();
}
}
How can I implement code loading position data in Unity continuously?
You could write some code where you use System.IO and use that to read the text file. Then you could use Regex to split the string at a semicolon. Here is some example code. The code is written on my iPhone so it may not be perfect.
using System.io
using System.Regex
using unityengine;
StreamReader reader = new StreamReader("file name");
String read = reader.Read();
String[] points = Regex.Split(read,";");
//now you can use the points array to visualize your data
You will probably need to put this In a for loop to get all of your files but this should work. Also tell me about any mistakes in my code and I will try to fix them.
I am starter in os Deving and manage to make a bootloader and then a kernel.I cam successfully jumped to protected mode and transfer the control to kernel.I able to write single characters but printing string is not working.This is my printString() function.
void printString(char * message[]){
int i;
for(i = 0; message[i] != '\0'; i++)
{
print(message[i]);
}
}
And My print Character function is here
void print(char *character){
unsigned char *vidmem = (unsigned char *) VIDEO_ADDRESS;
int offset; //Variable which hold the offset where we want to print our character
offset = GetCursor(); //Setting our offset to current cursor position
vidmem[offset+1] = character;
vidmem[offset+2] = 0x0f;
SetCursor(offset+2);
}
and this is call to function
printString("manoj");
Please help me I am a starter in os deving
I would recommend keeping track of the X and Y coordinates as (static) globals, and using them for offsets into memory. Also, it shouldn't be offset+1 and offset+2, but rather offset and offset+1. This is in addition to what tangrs said in his answer.
A good tutorial for learning how to print to the screen can be found at http://www.jamesmolloy.co.uk/tutorial_html/3.-The%20Screen.html - he goes into great detail about how to print things. It also is a good place to start learning about OSDev, along with the OSDev forums at http://forum.osdev.org/index.php.
There's several things wrong with your functions
Firstly, your print function takes a pointer to a character where it looks like you want the character itself.
Secondly, your printString function is really taking a pointer to pointer to char which isn't what you want if you're calling the printString function like printString("Hello World");.
Your compiler should have warned you about these.
Your code should looks something like this
void printString(char * message){
// ...
}
void print(char character){
// ...
vidmem[offset+1] = character;
// ...
}