Skip to content

Commit

Permalink
libbpf-tools/trace_helpers: Add a utility to split and convert string
Browse files Browse the repository at this point in the history
Add a utility API to split and convert strings for argument parsing.

Also, apply the API usage to offcputime and profile to remove duplicates.
  • Loading branch information
ekyooo authored and yonghong-song committed Oct 20, 2024
1 parent 280d822 commit 86d006a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
27 changes: 4 additions & 23 deletions libbpf-tools/offcputime.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,6 @@ static const struct argp_option opts[] = {
{},
};

static int split_pidstr(char *s, char* delim, int max_split, pid_t *pids)
{
char *pid;
int nr = 0;

errno = 0;
pid = strtok(s, delim);
while (pid) {
if (nr >= max_split)
return -ENOBUFS;

pids[nr++] = strtol(pid, NULL, 10);
if (errno)
return -errno;

pid = strtok(NULL, delim);
}

return 0;
}

static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
static int pos_args;
Expand All @@ -114,7 +93,8 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
env.verbose = true;
break;
case 'p':
ret = split_pidstr(strdup(arg), ",", MAX_PID_NR, env.pids);
ret = split_convert(strdup(arg), ",", env.pids, sizeof(env.pids),
sizeof(pid_t), str_to_int);
if (ret) {
if (ret == -ENOBUFS)
fprintf(stderr, "the number of pid is too big, please "
Expand All @@ -126,7 +106,8 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
}
break;
case 't':
ret = split_pidstr(strdup(arg), ",", MAX_TID_NR, env.tids);
ret = split_convert(strdup(arg), ",", env.tids, sizeof(env.tids),
sizeof(pid_t), str_to_int);
if (ret) {
if (ret == -ENOBUFS)
fprintf(stderr, "the number of tid is too big, please "
Expand Down
27 changes: 4 additions & 23 deletions libbpf-tools/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,6 @@ struct syms_cache *syms_cache;
struct syms *syms;
static char syminfo[SYM_INFO_LEN];

static int split_pidstr(char *s, char* sep, int max_split, pid_t *pids)
{
char *pid;
int nr = 0;

errno = 0;
pid = strtok(s, sep);
while (pid) {
if (nr >= max_split)
return -ENOBUFS;

pids[nr++] = strtol(pid, NULL, 10);
if (errno)
return -errno;

pid = strtok(NULL, ",");
}

return 0;
}

static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
static int pos_args;
Expand All @@ -166,7 +145,8 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
env.verbose = true;
break;
case 'p':
ret = split_pidstr(strdup(arg), ",", MAX_PID_NR, env.pids);
ret = split_convert(strdup(arg), ",", env.pids, sizeof(env.pids),
sizeof(pid_t), str_to_int);
if (ret) {
if (ret == -ENOBUFS)
fprintf(stderr, "the number of pid is too big, please "
Expand All @@ -178,7 +158,8 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
}
break;
case 'L':
ret = split_pidstr(strdup(arg), ",", MAX_TID_NR, env.tids);
ret = split_convert(strdup(arg), ",", env.tids, sizeof(env.tids),
sizeof(pid_t), str_to_int);
if (ret) {
if (ret == -ENOBUFS)
fprintf(stderr, "the number of tid is too big, please "
Expand Down
42 changes: 42 additions & 0 deletions libbpf-tools/trace_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1247,3 +1247,45 @@ bool probe_ringbuf()
close(map_fd);
return true;
}

int split_convert(char *s, const char* delim, void *elems, size_t elems_size,
size_t elem_size, convert_fn_t convert)
{
char *token;
int ret;
char *pos = (char *)elems;

if (!s || !delim || !elems)
return -EINVAL;

token = strtok(s, delim);
while (token) {
if (pos + elem_size > (char*)elems + elems_size)
return -ENOBUFS;

ret = convert(token, pos);
if (ret)
return -ret;

pos += elem_size;
token = strtok(NULL, delim);
}

return 0;
}

int str_to_int(const char *src, void *dest)
{
errno = 0;
*(int*)dest = strtol(src, NULL, 10);

return errno;
}

int str_to_long(const char *src, void *dest)
{
errno = 0;
*(long*)dest = strtol(src, NULL, 10);

return errno;
}
12 changes: 12 additions & 0 deletions libbpf-tools/trace_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ bool module_btf_exists(const char *mod);
bool probe_tp_btf(const char *name);
bool probe_ringbuf();

typedef int (*convert_fn_t)(const char *src, void *dest);
int split_convert(char *s, const char* delim, void *elems, size_t elems_size,
size_t elem_size, convert_fn_t convert);
/*
* Implementations of convert_fn_t.
* This can be replaced with a user-defined callback function.
*/
/* converts a string to an integer */
int str_to_int(const char *src, void *dest);
/* converts a string to a long integer */
int str_to_long(const char *src, void *dest);

#endif /* __TRACE_HELPERS_H */

0 comments on commit 86d006a

Please sign in to comment.