All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jesper Dangaard Brouer <brouer@redhat.com>
To: Adel Fuchs <adelfuchs@gmail.com>
Cc: "xdp-newbies@vger.kernel.org" <xdp-newbies@vger.kernel.org>,
	"iovisor-dev@lists.iovisor.org" <iovisor-dev@lists.iovisor.org>,
	brouer@redhat.com
Subject: Re: Error with printk and bpf_trace_printk
Date: Thu, 1 Jun 2017 16:35:15 +0200	[thread overview]
Message-ID: <20170601163515.1de24539@redhat.com> (raw)
In-Reply-To: <CAErYV9FFf+70vzpp+0qu0G0PPN0pKd7rfu7CJZ_b-fUrtqjkCw@mail.gmail.com>

On Thu, 1 Jun 2017 14:10:01 +0300
Adel Fuchs <adelfuchs@gmail.com> wrote:

> Hi Jesper,
> I tried adding your solution, bpf_debug,  and I'm now able to run the
> program with no errors but the trace_pipe file stays empty.
> I just added this to my program:
> 
> #ifdef DEBUG
> /* Only use this for debug output. Notice output from bpf_trace_printk()
> * end-up in /sys/kernel/debug/tracing/trace_pipe
> */
> #define bpf_debug(fmt, ...) \
> ({ \
> char ____fmt[] = fmt; \
> bpf_trace_printk(____fmt, sizeof(____fmt), \
> ##__VA_ARGS__); \
> })
> #else
> #define bpf_debug(fmt, ...) { } while (0)
> #endif
> 
> 
> And added a printing command:
> bpf_debug("hi");
> 
> Do you know what's the problem?

I assume you are doing:
 sudo cat /sys/kernel/debug/tracing/trace_pipe

The problem could be that the kernel need to be compiled with the right
trace config options... does anybody on the list(s) know? 

(p.s. removed netdev list cc)
-- 
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer



> On Tue, May 30, 2017 at 3:24 PM, Jesper Dangaard Brouer
> <brouer@redhat.com> wrote:
> >
> > Notice, there are two mailing lists (Cc'ed) that you should likely ask
> > these kind of questions on (instead of netdev), depending on if this is
> > mostly related to bpf (iovisor-dev@lists.iovisor.org) or somehow
> > related to XDP (xdp-newbies@vger.kernel.org).
> >
> > See my answer inlined below:
> >
> > On Sun, 28 May 2017 17:48:20 +0300 Adel Fuchs <adelfuchs@gmail.com> wrote:
> >  
> >> I have a working eBPF program, and I'm trying to add outputs to it.
> >> I'm not able to use both printk and bpf_trace_printk functions. I get
> >> this error:
> >>
> >> ELF contains non-map related relo data in entry 0 pointing to section
> >> 8! Compiler bug?!
> >>
> >> Prog section 'ingress' rejected: Invalid argument (22)!
> >>  - Type:         3
> >>  - Instructions: 16 (0 over limit)
> >>  - License:      GPL
> >>
> >> Verifier analysis:
> >>
> >> 0: (bf) r6 = r1
> >> 1: (18) r1 = 0x0
> >> 3: (85) call bpf_unspec#0
> >> unknown func bpf_unspec#0
> >>
> >> Error fetching program/map!
> >> Failed to retrieve (e)BPF data!
> >>
> >> Are there certain "includes" that I need to add?
> >> In addition, I'm not sure I'm using the function correctly. I just
> >> wrote: printk("hi")  
> >
> > You obviously cannot call printk directly from and eBPF program.
> > I wonder how you got this compiling...
> >
> > As you hinted yourself, you should be using: bpf_trace_printk().
> > But it is actually tricky to use... and not much help is around to
> > figure this out.
> >
> > First of all the output end-up in this file: /sys/kernel/debug/tracing/trace_pipe
> > Remember to read the output use 'cat' like:
> >
> >  sudo cat /sys/kernel/debug/tracing/trace_pipe
> >
> > And only the first process to read the output gets the output...
> >
> >
> > I deduct you are using the TC/iproute2 examples:
> >  https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/tree/examples/bpf
> >
> > Next gotcha is that, you need to provide the char* string in a very
> > special way to make this compile correctly.  The iproute2 provide a
> > helper define called "printt()" in include/bpf_api.h for this:
> >
> > #ifndef printt
> > # define printt(fmt, ...)                                               \
> >         ({                                                              \
> >                 char ____fmt[] = fmt;                                   \
> >                 trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__);  \
> >         })
> > #endif
> >
> > Or see my solution here:
> > [1] https://github.com/netoptimizer/prototype-kernel/blob/master/kernel/samples/bpf/xdp_ddos01_blacklist_kern.c#L86:L99
> >
> >
> > Another gotcha I've experienced is that if you format the string
> > incorrectly, or use a modifier like %X, which bpf_trace_printk() does
> > not seem to understand, then you "hear-nothing"...  Also experienced if
> > using more than 3 arguments, then it fails or also go silent. Be
> > careful when using this somewhat "flaky" debug facility.
> >
> > Do remember these bpf_trace_printk() should only be used for debugging,
> > as it is very slow...
> > --
> > Best regards,
> >   Jesper Dangaard Brouer
> >   MSc.CS, Principal Kernel Engineer at Red Hat
> >   LinkedIn: http://www.linkedin.com/in/brouer  

      reply	other threads:[~2017-06-01 14:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-28 14:48 Error with printk and bpf_trace_printk Adel Fuchs
2017-05-30 12:24 ` Jesper Dangaard Brouer
2017-05-30 12:24   ` Jesper Dangaard Brouer via iovisor-dev
2017-06-01 11:10   ` Adel Fuchs
2017-06-01 11:10     ` Adel Fuchs via iovisor-dev
2017-06-01 14:35     ` Jesper Dangaard Brouer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170601163515.1de24539@redhat.com \
    --to=brouer@redhat.com \
    --cc=adelfuchs@gmail.com \
    --cc=iovisor-dev@lists.iovisor.org \
    --cc=xdp-newbies@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.