0

I am getting used to writing eBPF code as of now and want to avoid using pointers in my BPF text due to how difficult it is to get a correct output out of it. Using strtok() seems to be out of the question due to all of the example codes requiring pointers. I also want to expand it to CSV files in the future since this is a means of practice for me. I was able to find another user's code here but it gives me an error with the BCC terminal due to the one pointer.

char str[256];
bpf_probe_read_user(&str, sizeof(str), (void *)PT_REGS_RC(ctx));
char token[] = strtok(str, ",");

char input[] ="first second third forth";
char delimiter[] = " ";
char firstWord, *secondWord, *remainder, *context;

int inputLength = strlen(input);
char *inputCopy = (char*) calloc(inputLength + 1, sizeof(char));
strncpy(inputCopy, input, inputLength);

str = strtok_r (inputCopy, delimiter, &context);
secondWord = strtok_r (NULL, delimiter, &context);
remainder = context;

getchar();
free(inputCopy);
Zarif Rahman
  • 79
  • 1
  • 8
  • So what do you plan to use instead of pointers? – Miguel Sandoval May 30 '21 at 02:12
  • It's pointers or nothing... A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) Reading both you will understand that a pointer is nothing but a normal variable that holds a memory address as its value. They are not that complicated... – David C. Rankin May 30 '21 at 02:28
  • Given that strings in `C` are pointers to characters, it's hard to imagine you'll be able to avoid using pointers. – Mark Saving May 30 '21 at 03:10
  • Rather than avoiding pointers, you should learn how they work and embrace them. – William Pursell May 30 '21 at 03:19
  • I am perfectly fine using pointers, it’s just the fact that the BCC compiler absolutely does not like pointers within bpf text. I was hoping there would be something other than using pointers since I’m kinda new to C. – Zarif Rahman May 30 '21 at 03:40
  • Could you add the terminal error? – Miguel Sandoval May 30 '21 at 04:14
  • @Mark Saving, You could have an array of chars rather than a pointer to one. But as soon as you index that array (e.g. `s[i]`), you involve a pointer (since it's a shorthand for `*(s + i)`). So your point stands: **This is truly impossible to do without pointers.** – ikegami May 30 '21 at 05:10

1 Answers1

-1

Pointers are powerful, and you wont be able to avoid them for very long. The time you invest in learning them is definitively worth it.

Here is an example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
    Extracts the word with the index "n" in the string "str".
    Words are delimited by a blank space or the end of the string.
}*/
char *getWord(char *str, int n)
{
    int words = 0;
    int length = 0;
    int beginIndex = 0;
    int endIndex = 0;
    char currentchar;
    while ((currentchar = str[endIndex++]) != '\0')
    {
        if (currentchar == ' ')
        {
            if (n == words)
                break;
            if (length > 0)
                words++;
            length = 0;
            beginIndex = endIndex;
            continue;
        }
        length++;
    }
    
    if (n == words)
    {
        char *result = malloc(sizeof(char) * length + 1);
        if (result == NULL)
        {
            printf("Error while allocating memory!\n");
            exit(1);
        }
        memcpy(result, str + beginIndex, length);
        result[length] = '\0';
        return result;
    }else
        return NULL;
}

You can easily use the function:

int main(int argc, char *argv[])
{
    char string[] = "Pointers are cool!";
    char *word = getWord(string, 2);
    printf("The third word is: '%s'\n", word);
    free(word); //Don't forget to de-allocate the memory!
    return 0;
}
  • I should’ve been more clear with my post but the BCC kernel does not like pointers at all. My original goal was to write code with strtok() but it’s been a general pain in the butt writing the eBPF code with it. – Zarif Rahman May 30 '21 at 03:44
  • Hmm.. have you tried the code I provided? Could you show us the issue? – Miguel Sandoval May 30 '21 at 04:06