如何理解Systemtap脚本中的“$ location”?

时间:2014-11-12 09:00:31

标签: linux linux-kernel kernel systemtap

Systemtap脚本:

# Array to hold the list of drop points we find
global locations

# Note when we turn the monitor on and off
probe begin { printf("Monitoring for dropped packets\n") }
probe end { printf("Stopping dropped packet monitor\n") }

# increment a drop counter for every location we drop at
#probe kernel.trace("kfree_skb") { locations[$location] <<< 1 }

# Every 5 seconds report our drop locations
probe timer.sec(5)
{
        printf("\n")
        foreach (l in locations-) {
                printf("%d packets dropped at location %p\n",
                           @count(locations[l]), l)
        }
        delete locations
}

和kfree_skb()的源代码是:

void kfree_skb(struct sk_buff *skb)
{
    if (unlikely(!skb))
        return;
    if (likely(atomic_read(&skb->users) == 1))
        smp_rmb();
    else if (likely(!atomic_dec_and_test(&skb->users)))
        return;
    trace_kfree_skb(skb, __builtin_return_address(0));
    __kfree_skb(skb);
}

我只是想知道$location来自哪里?和
$locationkfree_skb()之间的关系是什么?
谢谢。

1 个答案:

答案 0 :(得分:0)

根据stap.1手册页:

   Many types of probe points provide context variables, which are
   run-time values, safely extracted from the kernel or userspace
   program being probed.  These are pre‐ fixed with the $
   character.  The CONTEXT VARIABLES section in stapprobes(3stap)
   lists what is available for each type of probe point.

根据stapprobes.3stap手册页:

KERNEL TRACEPOINTS

   This family of probe points hooks up to static probing
   tracepoints inserted into the kernel or modules.  [...]

   Tracepoint probes look like: kernel.trace("name").  The
   tracepoint name string, which may contain the usual wildcard
   characters, is matched against the names defined by the kernel
   developers in the tracepoint header files.

   The handler associated with a tracepoint-based probe may read
   the optional parame‐ ters specified at the macro call site.
   [...] For example, the tracepoint probe kernel.trace("sched_switch")
   provides the parameters $rq, $prev, and $next.  [...]

   The name of the tracepoint is available in $$name, and a string
   of name=value pairs for all parameters of the tracepoint is
   available in $$vars or $$parms.

根据linux内核源代码:

% cd net/core
% git grep trace_kfree_skb

dev.c: [...]
drop_monitor.c: [...]
skbuff.c: [...]

% cd ../../include/trace/events
% git grep -A5 'TRACE_EVENT.*kfree_skb'

skb.h:TRACE_EVENT(kfree_skb,
skb.h-
skb.h-  TP_PROTO(struct sk_buff *skb, void *location),
skb.h-
skb.h-  TP_ARGS(skb, location),
skb.h-