Re: [PATCH bpf-next] selftests/bpf: Add read_trace_pipe_iter function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Apr 09, 2024 at 02:36:01PM +0200, Jiri Olsa wrote:

SNIP

> +int read_trace_pipe_iter(void (*cb)(const char *str, void *data), void *data, int iter)
> +{
> +	char *buf = NULL;
> +	FILE *fp = NULL;
> +	size_t buflen;
> +
> +	if (access(TRACEFS_PIPE, F_OK) == 0)
> +		fp = fopen(TRACEFS_PIPE, "r");
> +	else
> +		fp = fopen(DEBUGFS_PIPE, "r");
> +	if (!fp)
> +		return -1;
> +
> +	 /* We do not want to wait forever when iter is specified. */
> +	if (iter)
> +		fcntl(fileno(fp), F_SETFL, O_NONBLOCK);
> +
> +	while (getline(&buf, &buflen, fp) >= 0 || errno == EAGAIN) {
> +		cb(buf, data);
> +		if (iter && !(--iter))
> +			break;
> +	}

hm, using this in some other changes shows that the original code
is not completely right and we need this change as well:

+       while ((n = getline(&buf, &buflen, fp) >= 0) || errno == EAGAIN) {
+               if (n > 0)
+                       cb(buf, data);

I'll send new version

jirka

> +
> +	free(buf);
> +	if (fp)
> +		fclose(fp);
> +	return 0;
> +}
> +
> +static void trace_pipe_cb(const char *str, void *data)
> +{
> +	printf("%s", str);
> +}
> +
> +void read_trace_pipe(void)
> +{
> +	read_trace_pipe_iter(trace_pipe_cb, NULL, 0);
> +}
> diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
> index d1ed71789049..2ce873c9f9aa 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.h
> +++ b/tools/testing/selftests/bpf/trace_helpers.h
> @@ -33,6 +33,8 @@ struct ksym *search_kallsyms_custom_local(struct ksyms *ksyms, const void *p1,
>  int kallsyms_find(const char *sym, unsigned long long *addr);
>  
>  void read_trace_pipe(void);
> +int read_trace_pipe_iter(void (*cb)(const char *str, void *data),
> +			 void *data, int iter);
>  
>  ssize_t get_uprobe_offset(const void *addr);
>  ssize_t get_rel_offset(uintptr_t addr);
> -- 
> 2.44.0
> 




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux