Commit 2a9c8c36 authored by Masami Hiramatsu's avatar Masami Hiramatsu Committed by Ingo Molnar

perf probe: Add lazy line matching support

Add lazy line matching support for specifying new probes.
This also changes the syntax of perf probe a bit. Now
perf probe accepts one of below probe event definitions.

1) Define event based on function name
 [EVENT=]FUNC[@SRC][:RLN|+OFF|%return|;PTN] [ARG ...]

2) Define event based on source file with line number
 [EVENT=]SRC:ALN [ARG ...]

3) Define event based on source file with lazy pattern
 [EVENT=]SRC;PTN [ARG ...]

- New lazy matching pattern(PTN) follows ';' (semicolon). And it
  must be put the end of the definition.
- So, @SRC is no longer the part which must be put at the end
  of the definition.

Note that ';' (semicolon) can be interpreted as the end of
a command by the shell. This means that you need to quote it.
(anyway you will need to quote the lazy pattern itself too,
because it may contains other sensitive characters, like
'[',']' etc.).

Lazy matching
-------------
The lazy line matching is similar to glob matching except
ignoring spaces in both of pattern and target.

e.g.
'a=*' can matches 'a=b', 'a = b', 'a == b' and so on.

This provides some sort of flexibility and robustness to
probe point definitions against minor code changes.
(for example, actual 10th line of schedule() can be changed
 easily by modifying schedule(), but the same line matching
 'rq=cpu_rq*' may still exist.)

Changes in v3:
 - Cast Dwarf_Addr to uintmax_t for printf-formats.

Changes in v2:
 - Cast Dwarf_Addr to unsigned long long for printf-formats.
Signed-off-by: default avatarMasami Hiramatsu <mhiramat@redhat.com>
Cc: systemtap <systemtap@sources.redhat.com>
Cc: DLE <dle-develop@lists.sourceforge.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: K.Prasad <prasad@linux.vnet.ibm.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
LKML-Reference: <20100225133611.6725.45078.stgit@localhost6.localdomain6>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 5c8d1cbb
...@@ -61,11 +61,19 @@ PROBE SYNTAX ...@@ -61,11 +61,19 @@ PROBE SYNTAX
------------ ------------
Probe points are defined by following syntax. Probe points are defined by following syntax.
"[EVENT=]FUNC[+OFFS|:RLN|%return][@SRC]|SRC:ALN [ARG ...]" 1) Define event based on function name
[EVENT=]FUNC[@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]
2) Define event based on source file with line number
[EVENT=]SRC:ALN [ARG ...]
3) Define event based on source file with lazy pattern
[EVENT=]SRC;PTN [ARG ...]
'EVENT' specifies the name of new event, if omitted, it will be set the name of the probed function. Currently, event group name is set as 'probe'. 'EVENT' specifies the name of new event, if omitted, it will be set the name of the probed function. Currently, event group name is set as 'probe'.
'FUNC' specifies a probed function name, and it may have one of the following options; '+OFFS' is the offset from function entry address in bytes, 'RLN' is the relative-line number from function entry line, and '%return' means that it probes function return. In addition, 'SRC' specifies a source file which has that function. 'FUNC' specifies a probed function name, and it may have one of the following options; '+OFFS' is the offset from function entry address in bytes, ':RLN' is the relative-line number from function entry line, and '%return' means that it probes function return. And ';PTN' means lazy matching pattern (see LAZY MATCHING). Note that ';PTN' must be the end of the probe point definition. In addition, '@SRC' specifies a source file which has that function.
It is also possible to specify a probe point by the source line number by using 'SRC:ALN' syntax, where 'SRC' is the source file path and 'ALN' is the line number. It is also possible to specify a probe point by the source line number or lazy matching by using 'SRC:ALN' or 'SRC;PTN' syntax, where 'SRC' is the source file path, ':ALN' is the line number and ';PTN' is the lazy matching pattern.
'ARG' specifies the arguments of this probe point. You can use the name of local variable, or kprobe-tracer argument format (e.g. $retval, %ax, etc). 'ARG' specifies the arguments of this probe point. You can use the name of local variable, or kprobe-tracer argument format (e.g. $retval, %ax, etc).
LINE SYNTAX LINE SYNTAX
...@@ -81,6 +89,16 @@ and 'ALN2' is end line number in the file. It is also possible to specify how ...@@ -81,6 +89,16 @@ and 'ALN2' is end line number in the file. It is also possible to specify how
many lines to show by using 'NUM'. many lines to show by using 'NUM'.
So, "source.c:100-120" shows lines between 100th to l20th in source.c file. And "func:10+20" shows 20 lines from 10th line of func function. So, "source.c:100-120" shows lines between 100th to l20th in source.c file. And "func:10+20" shows 20 lines from 10th line of func function.
LAZY MATCHING
-------------
The lazy line matching is similar to glob matching but ignoring spaces in both of pattern and target. So this accepts wildcards('*', '?') and character classes(e.g. [a-z], [!A-Z]).
e.g.
'a=*' can matches 'a=b', 'a = b', 'a == b' and so on.
This provides some sort of flexibility and robustness to probe point definitions against minor code changes. For example, actual 10th line of schedule() can be moved easily by modifying schedule(), but the same line matching 'rq=cpu_rq*' may still exist in the function.)
EXAMPLES EXAMPLES
-------- --------
Display which lines in schedule() can be probed: Display which lines in schedule() can be probed:
...@@ -95,6 +113,12 @@ Add a probe on schedule() function 12th line with recording cpu local variable: ...@@ -95,6 +113,12 @@ Add a probe on schedule() function 12th line with recording cpu local variable:
this will add one or more probes which has the name start with "schedule". this will add one or more probes which has the name start with "schedule".
Add probes on lines in schedule() function which calls update_rq_clock().
./perf probe 'schedule;update_rq_clock*'
or
./perf probe --add='schedule;update_rq_clock*'
Delete all probes on schedule(). Delete all probes on schedule().
./perf probe --del='schedule*' ./perf probe --del='schedule*'
......
...@@ -175,22 +175,24 @@ static const struct option options[] = { ...@@ -175,22 +175,24 @@ static const struct option options[] = {
opt_del_probe_event), opt_del_probe_event),
OPT_CALLBACK('a', "add", NULL, OPT_CALLBACK('a', "add", NULL,
#ifdef NO_DWARF_SUPPORT #ifdef NO_DWARF_SUPPORT
"[EVENT=]FUNC[+OFFS|%return] [ARG ...]", "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
#else #else
"[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", "[EVENT=]FUNC[+OFF|%return|:RL|;PT][@SRC]|SRC:AL|SRC;PT"
" [ARG ...]",
#endif #endif
"probe point definition, where\n" "probe point definition, where\n"
"\t\tGROUP:\tGroup name (optional)\n" "\t\tGROUP:\tGroup name (optional)\n"
"\t\tEVENT:\tEvent name\n" "\t\tEVENT:\tEvent name\n"
"\t\tFUNC:\tFunction name\n" "\t\tFUNC:\tFunction name\n"
"\t\tOFFS:\tOffset from function entry (in byte)\n" "\t\tOFF:\tOffset from function entry (in byte)\n"
"\t\t%return:\tPut the probe at function return\n" "\t\t%return:\tPut the probe at function return\n"
#ifdef NO_DWARF_SUPPORT #ifdef NO_DWARF_SUPPORT
"\t\tARG:\tProbe argument (only \n" "\t\tARG:\tProbe argument (only \n"
#else #else
"\t\tSRC:\tSource code path\n" "\t\tSRC:\tSource code path\n"
"\t\tRLN:\tRelative line number from function entry.\n" "\t\tRL:\tRelative line number from function entry.\n"
"\t\tALN:\tAbsolute line number in file.\n" "\t\tAL:\tAbsolute line number in file.\n"
"\t\tPT:\tLazy expression of line code.\n"
"\t\tARG:\tProbe argument (local variable name or\n" "\t\tARG:\tProbe argument (local variable name or\n"
#endif #endif
"\t\t\tkprobe-tracer argument format.)\n", "\t\t\tkprobe-tracer argument format.)\n",
......
...@@ -119,14 +119,14 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -119,14 +119,14 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
char c, nc = 0; char c, nc = 0;
/* /*
* <Syntax> * <Syntax>
* perf probe [EVENT=]SRC:LN * perf probe [EVENT=]SRC[:LN|;PTN]
* perf probe [EVENT=]FUNC[+OFFS|%return][@SRC] * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
* *
* TODO:Group name support * TODO:Group name support
*/ */
ptr = strchr(arg, '='); ptr = strpbrk(arg, ";=@+%");
if (ptr) { /* Event name */ if (ptr && *ptr == '=') { /* Event name */
*ptr = '\0'; *ptr = '\0';
tmp = ptr + 1; tmp = ptr + 1;
ptr = strchr(arg, ':'); ptr = strchr(arg, ':');
...@@ -139,7 +139,7 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -139,7 +139,7 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
arg = tmp; arg = tmp;
} }
ptr = strpbrk(arg, ":+@%"); ptr = strpbrk(arg, ";:+@%");
if (ptr) { if (ptr) {
nc = *ptr; nc = *ptr;
*ptr++ = '\0'; *ptr++ = '\0';
...@@ -156,7 +156,11 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -156,7 +156,11 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
while (ptr) { while (ptr) {
arg = ptr; arg = ptr;
c = nc; c = nc;
ptr = strpbrk(arg, ":+@%"); if (c == ';') { /* Lazy pattern must be the last part */
pp->lazy_line = strdup(arg);
break;
}
ptr = strpbrk(arg, ";:+@%");
if (ptr) { if (ptr) {
nc = *ptr; nc = *ptr;
*ptr++ = '\0'; *ptr++ = '\0';
...@@ -165,13 +169,13 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -165,13 +169,13 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
case ':': /* Line number */ case ':': /* Line number */
pp->line = strtoul(arg, &tmp, 0); pp->line = strtoul(arg, &tmp, 0);
if (*tmp != '\0') if (*tmp != '\0')
semantic_error("There is non-digit charactor" semantic_error("There is non-digit char"
" in line number."); " in line number.");
break; break;
case '+': /* Byte offset from a symbol */ case '+': /* Byte offset from a symbol */
pp->offset = strtoul(arg, &tmp, 0); pp->offset = strtoul(arg, &tmp, 0);
if (*tmp != '\0') if (*tmp != '\0')
semantic_error("There is non-digit charactor" semantic_error("There is non-digit character"
" in offset."); " in offset.");
break; break;
case '@': /* File name */ case '@': /* File name */
...@@ -179,9 +183,6 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -179,9 +183,6 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
semantic_error("SRC@SRC is not allowed."); semantic_error("SRC@SRC is not allowed.");
pp->file = strdup(arg); pp->file = strdup(arg);
DIE_IF(pp->file == NULL); DIE_IF(pp->file == NULL);
if (ptr)
semantic_error("@SRC must be the last "
"option.");
break; break;
case '%': /* Probe places */ case '%': /* Probe places */
if (strcmp(arg, "return") == 0) { if (strcmp(arg, "return") == 0) {
...@@ -196,11 +197,18 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -196,11 +197,18 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
} }
/* Exclusion check */ /* Exclusion check */
if (pp->lazy_line && pp->line)
semantic_error("Lazy pattern can't be used with line number.");
if (pp->lazy_line && pp->offset)
semantic_error("Lazy pattern can't be used with offset.");
if (pp->line && pp->offset) if (pp->line && pp->offset)
semantic_error("Offset can't be used with line number."); semantic_error("Offset can't be used with line number.");
if (!pp->line && pp->file && !pp->function) if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
semantic_error("File always requires line number."); semantic_error("File always requires line number or "
"lazy pattern.");
if (pp->offset && !pp->function) if (pp->offset && !pp->function)
semantic_error("Offset requires an entry function."); semantic_error("Offset requires an entry function.");
...@@ -208,11 +216,13 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) ...@@ -208,11 +216,13 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
if (pp->retprobe && !pp->function) if (pp->retprobe && !pp->function)
semantic_error("Return probe requires an entry function."); semantic_error("Return probe requires an entry function.");
if ((pp->offset || pp->line) && pp->retprobe) if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
semantic_error("Offset/Line can't be used with return probe."); semantic_error("Offset/Line/Lazy pattern can't be used with "
"return probe.");
pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n",
pp->function, pp->file, pp->line, pp->offset, pp->retprobe); pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
pp->lazy_line);
} }
/* Parse perf-probe event definition */ /* Parse perf-probe event definition */
...@@ -456,6 +466,8 @@ static void clear_probe_point(struct probe_point *pp) ...@@ -456,6 +466,8 @@ static void clear_probe_point(struct probe_point *pp)
free(pp->function); free(pp->function);
if (pp->file) if (pp->file)
free(pp->file); free(pp->file);
if (pp->lazy_line)
free(pp->lazy_line);
for (i = 0; i < pp->nr_args; i++) for (i = 0; i < pp->nr_args; i++)
free(pp->args[i]); free(pp->args[i]);
if (pp->args) if (pp->args)
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#include "string.h"
#include "event.h" #include "event.h"
#include "debug.h" #include "debug.h"
#include "util.h" #include "util.h"
...@@ -104,8 +105,67 @@ static int strtailcmp(const char *s1, const char *s2) ...@@ -104,8 +105,67 @@ static int strtailcmp(const char *s1, const char *s2)
return 0; return 0;
} }
/* Find the fileno of the target file. */ /* Line number list operations */
static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
/* Add a line to line number list */
static void line_list__add_line(struct list_head *head, unsigned int line)
{
struct line_node *ln;
struct list_head *p;
/* Reverse search, because new line will be the last one */
list_for_each_entry_reverse(ln, head, list) {
if (ln->line < line) {
p = &ln->list;
goto found;
} else if (ln->line == line) /* Already exist */
return ;
}
/* List is empty, or the smallest entry */
p = head;
found:
pr_debug("line list: add a line %u\n", line);
ln = zalloc(sizeof(struct line_node));
DIE_IF(ln == NULL);
ln->line = line;
INIT_LIST_HEAD(&ln->list);
list_add(&ln->list, p);
}
/* Check if the line in line number list */
static int line_list__has_line(struct list_head *head, unsigned int line)
{
struct line_node *ln;
/* Reverse search, because new line will be the last one */
list_for_each_entry(ln, head, list)
if (ln->line == line)
return 1;
return 0;
}
/* Init line number list */
static void line_list__init(struct list_head *head)
{
INIT_LIST_HEAD(head);
}
/* Free line number list */
static void line_list__free(struct list_head *head)
{
struct line_node *ln;
while (!list_empty(head)) {
ln = list_first_entry(head, struct line_node, list);
list_del(&ln->list);
free(ln);
}
}
/* Dwarf wrappers */
/* Find the realpath of the target file. */
static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
{ {
Dwarf_Files *files; Dwarf_Files *files;
size_t nfiles, i; size_t nfiles, i;
...@@ -113,21 +173,18 @@ static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname) ...@@ -113,21 +173,18 @@ static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
int ret; int ret;
if (!fname) if (!fname)
return -EINVAL; return NULL;
ret = dwarf_getsrcfiles(cu_die, &files, &nfiles); ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
if (ret == 0) { if (ret != 0)
for (i = 0; i < nfiles; i++) { return NULL;
src = dwarf_filesrc(files, i, NULL, NULL);
if (strtailcmp(src, fname) == 0) { for (i = 0; i < nfiles; i++) {
ret = (int)i; /*???: +1 or not?*/ src = dwarf_filesrc(files, i, NULL, NULL);
break; if (strtailcmp(src, fname) == 0)
} break;
}
if (ret)
pr_debug("found fno: %d\n", ret);
} }
return ret; return src;
} }
struct __addr_die_search_param { struct __addr_die_search_param {
...@@ -436,17 +493,109 @@ static void find_probe_point_by_line(struct probe_finder *pf) ...@@ -436,17 +493,109 @@ static void find_probe_point_by_line(struct probe_finder *pf)
} }
} }
/* Find lines which match lazy pattern */
static int find_lazy_match_lines(struct list_head *head,
const char *fname, const char *pat)
{
char *fbuf, *p1, *p2;
int fd, line, nlines = 0;
struct stat st;
fd = open(fname, O_RDONLY);
if (fd < 0)
die("failed to open %s", fname);
DIE_IF(fstat(fd, &st) < 0);
fbuf = malloc(st.st_size + 2);
DIE_IF(fbuf == NULL);
DIE_IF(read(fd, fbuf, st.st_size) < 0);
close(fd);
fbuf[st.st_size] = '\n'; /* Dummy line */
fbuf[st.st_size + 1] = '\0';
p1 = fbuf;
line = 1;
while ((p2 = strchr(p1, '\n')) != NULL) {
*p2 = '\0';
if (strlazymatch(p1, pat)) {
line_list__add_line(head, line);
nlines++;
}
line++;
p1 = p2 + 1;
}
free(fbuf);
return nlines;
}
/* Find probe points from lazy pattern */
static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
{
Dwarf_Lines *lines;
Dwarf_Line *line;
size_t nlines, i;
Dwarf_Addr addr;
Dwarf_Die die_mem;
int lineno;
int ret;
if (list_empty(&pf->lcache)) {
/* Matching lazy line pattern */
ret = find_lazy_match_lines(&pf->lcache, pf->fname,
pf->pp->lazy_line);
if (ret <= 0)
die("No matched lines found in %s.", pf->fname);
}
ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
DIE_IF(ret != 0);
for (i = 0; i < nlines; i++) {
line = dwarf_onesrcline(lines, i);
dwarf_lineno(line, &lineno);
if (!line_list__has_line(&pf->lcache, lineno))
continue;
/* TODO: Get fileno from line, but how? */
if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
continue;
ret = dwarf_lineaddr(line, &addr);
DIE_IF(ret != 0);
if (sp_die) {
/* Address filtering 1: does sp_die include addr? */
if (!dwarf_haspc(sp_die, addr))
continue;
/* Address filtering 2: No child include addr? */
if (die_get_inlinefunc(sp_die, addr, &die_mem))
continue;
}
pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
(int)i, lineno, (unsigned long long)addr);
pf->addr = addr;
show_probe_point(sp_die, pf);
/* Continuing, because target line might be inlined. */
}
/* TODO: deallocate lines, but how? */
}
static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
{ {
struct probe_finder *pf = (struct probe_finder *)data; struct probe_finder *pf = (struct probe_finder *)data;
struct probe_point *pp = pf->pp; struct probe_point *pp = pf->pp;
/* Get probe address */ if (pp->lazy_line)
pf->addr = die_get_entrypc(in_die); find_probe_point_lazy(in_die, pf);
pf->addr += pp->offset; else {
pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr); /* Get probe address */
pf->addr = die_get_entrypc(in_die);
pf->addr += pp->offset;
pr_debug("found inline addr: 0x%jx\n",
(uintmax_t)pf->addr);
show_probe_point(in_die, pf);
}
show_probe_point(in_die, pf);
return DWARF_CB_OK; return DWARF_CB_OK;
} }
...@@ -461,17 +610,21 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) ...@@ -461,17 +610,21 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
die_compare_name(sp_die, pp->function) != 0) die_compare_name(sp_die, pp->function) != 0)
return 0; return 0;
pf->fname = dwarf_decl_file(sp_die);
if (pp->line) { /* Function relative line */ if (pp->line) { /* Function relative line */
pf->fname = dwarf_decl_file(sp_die);
dwarf_decl_line(sp_die, &pf->lno); dwarf_decl_line(sp_die, &pf->lno);
pf->lno += pp->line; pf->lno += pp->line;
find_probe_point_by_line(pf); find_probe_point_by_line(pf);
} else if (!dwarf_func_inline(sp_die)) { } else if (!dwarf_func_inline(sp_die)) {
/* Real function */ /* Real function */
pf->addr = die_get_entrypc(sp_die); if (pp->lazy_line)
pf->addr += pp->offset; find_probe_point_lazy(sp_die, pf);
/* TODO: Check the address in this function */ else {
show_probe_point(sp_die, pf); pf->addr = die_get_entrypc(sp_die);
pf->addr += pp->offset;
/* TODO: Check the address in this function */
show_probe_point(sp_die, pf);
}
} else } else
/* Inlined function: search instances */ /* Inlined function: search instances */
dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf); dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
...@@ -493,7 +646,6 @@ int find_probe_point(int fd, struct probe_point *pp) ...@@ -493,7 +646,6 @@ int find_probe_point(int fd, struct probe_point *pp)
size_t cuhl; size_t cuhl;
Dwarf_Die *diep; Dwarf_Die *diep;
Dwarf *dbg; Dwarf *dbg;
int fno = 0;
dbg = dwarf_begin(fd, DWARF_C_READ); dbg = dwarf_begin(fd, DWARF_C_READ);
if (!dbg) if (!dbg)
...@@ -501,6 +653,7 @@ int find_probe_point(int fd, struct probe_point *pp) ...@@ -501,6 +653,7 @@ int find_probe_point(int fd, struct probe_point *pp)
pp->found = 0; pp->found = 0;
off = 0; off = 0;
line_list__init(&pf.lcache);
/* Loop on CUs (Compilation Unit) */ /* Loop on CUs (Compilation Unit) */
while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
/* Get the DIE(Debugging Information Entry) of this CU */ /* Get the DIE(Debugging Information Entry) of this CU */
...@@ -510,17 +663,19 @@ int find_probe_point(int fd, struct probe_point *pp) ...@@ -510,17 +663,19 @@ int find_probe_point(int fd, struct probe_point *pp)
/* Check if target file is included. */ /* Check if target file is included. */
if (pp->file) if (pp->file)
fno = cu_find_fileno(&pf.cu_die, pp->file); pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
else else
fno = 0; pf.fname = NULL;
if (!pp->file || fno) { if (!pp->file || pf.fname) {
/* Save CU base address (for frame_base) */ /* Save CU base address (for frame_base) */
ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base); ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
if (ret != 0) if (ret != 0)
pf.cu_base = 0; pf.cu_base = 0;
if (pp->function) if (pp->function)
find_probe_point_by_func(&pf); find_probe_point_by_func(&pf);
else if (pp->lazy_line)
find_probe_point_lazy(NULL, &pf);
else { else {
pf.lno = pp->line; pf.lno = pp->line;
find_probe_point_by_line(&pf); find_probe_point_by_line(&pf);
...@@ -528,36 +683,12 @@ int find_probe_point(int fd, struct probe_point *pp) ...@@ -528,36 +683,12 @@ int find_probe_point(int fd, struct probe_point *pp)
} }
off = noff; off = noff;
} }
line_list__free(&pf.lcache);
dwarf_end(dbg); dwarf_end(dbg);
return pp->found; return pp->found;
} }
static void line_range_add_line(struct line_range *lr, unsigned int line)
{
struct line_node *ln;
struct list_head *p;
/* Reverse search, because new line will be the last one */
list_for_each_entry_reverse(ln, &lr->line_list, list) {
if (ln->line < line) {
p = &ln->list;
goto found;
} else if (ln->line == line) /* Already exist */
return ;
}
/* List is empty, or the smallest entry */
p = &lr->line_list;
found:
pr_debug("Debug: add a line %u\n", line);
ln = zalloc(sizeof(struct line_node));
DIE_IF(ln == NULL);
ln->line = line;
INIT_LIST_HEAD(&ln->list);
list_add(&ln->list, p);
}
/* Find line range from its line number */ /* Find line range from its line number */
static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
{ {
...@@ -570,7 +701,7 @@ static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) ...@@ -570,7 +701,7 @@ static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
const char *src; const char *src;
Dwarf_Die die_mem; Dwarf_Die die_mem;
INIT_LIST_HEAD(&lf->lr->line_list); line_list__init(&lf->lr->line_list);
ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines); ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
DIE_IF(ret != 0); DIE_IF(ret != 0);
...@@ -601,7 +732,7 @@ static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) ...@@ -601,7 +732,7 @@ static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
/* Copy real path */ /* Copy real path */
if (!lf->lr->path) if (!lf->lr->path)
lf->lr->path = strdup(src); lf->lr->path = strdup(src);
line_range_add_line(lf->lr, (unsigned int)lineno); line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
} }
/* Update status */ /* Update status */
if (!list_empty(&lf->lr->line_list)) if (!list_empty(&lf->lr->line_list))
...@@ -659,7 +790,6 @@ int find_line_range(int fd, struct line_range *lr) ...@@ -659,7 +790,6 @@ int find_line_range(int fd, struct line_range *lr)
size_t cuhl; size_t cuhl;
Dwarf_Die *diep; Dwarf_Die *diep;
Dwarf *dbg; Dwarf *dbg;
int fno;
dbg = dwarf_begin(fd, DWARF_C_READ); dbg = dwarf_begin(fd, DWARF_C_READ);
if (!dbg) if (!dbg)
...@@ -678,15 +808,14 @@ int find_line_range(int fd, struct line_range *lr) ...@@ -678,15 +808,14 @@ int find_line_range(int fd, struct line_range *lr)
/* Check if target file is included. */ /* Check if target file is included. */
if (lr->file) if (lr->file)
fno = cu_find_fileno(&lf.cu_die, lr->file); lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
else else
fno = 0; lf.fname = 0;
if (!lr->file || fno) { if (!lr->file || lf.fname) {
if (lr->function) if (lr->function)
find_line_range_by_func(&lf); find_line_range_by_func(&lf);
else { else {
lf.fname = lr->file;
lf.lno_s = lr->start; lf.lno_s = lr->start;
if (!lr->end) if (!lr->end)
lf.lno_e = INT_MAX; lf.lno_e = INT_MAX;
......
...@@ -21,6 +21,7 @@ struct probe_point { ...@@ -21,6 +21,7 @@ struct probe_point {
/* Inputs */ /* Inputs */
char *file; /* File name */ char *file; /* File name */
int line; /* Line number */ int line; /* Line number */
char *lazy_line; /* Lazy line pattern */
char *function; /* Function name */ char *function; /* Function name */
int offset; /* Offset bytes */ int offset; /* Offset bytes */
...@@ -74,6 +75,7 @@ struct probe_finder { ...@@ -74,6 +75,7 @@ struct probe_finder {
const char *var; /* Current variable name */ const char *var; /* Current variable name */
char *buf; /* Current output buffer */ char *buf; /* Current output buffer */
int len; /* Length of output buffer */ int len; /* Length of output buffer */
struct list_head lcache; /* Line cache for lazy match */
}; };
struct line_finder { struct line_finder {
......
...@@ -265,21 +265,21 @@ static bool __match_charclass(const char *pat, char c, const char **npat) ...@@ -265,21 +265,21 @@ static bool __match_charclass(const char *pat, char c, const char **npat)
return false; return false;
} }
/** /* Glob/lazy pattern matching */
* strglobmatch - glob expression pattern matching static bool __match_glob(const char *str, const char *pat, bool ignore_space)
* @str: the target string to match
* @pat: the pattern string to match
*
* This returns true if the @str matches @pat. @pat can includes wildcards
* ('*','?') and character classes ([CHARS], complementation and ranges are
* also supported). Also, this supports escape character ('\') to use special
* characters as normal character.
*
* Note: if @pat syntax is broken, this always returns false.
*/
bool strglobmatch(const char *str, const char *pat)
{ {
while (*str && *pat && *pat != '*') { while (*str && *pat && *pat != '*') {
if (ignore_space) {
/* Ignore spaces for lazy matching */
if (isspace(*str)) {
str++;
continue;
}
if (isspace(*pat)) {
pat++;
continue;
}
}
if (*pat == '?') { /* Matches any single character */ if (*pat == '?') { /* Matches any single character */
str++; str++;
pat++; pat++;
...@@ -308,3 +308,32 @@ bool strglobmatch(const char *str, const char *pat) ...@@ -308,3 +308,32 @@ bool strglobmatch(const char *str, const char *pat)
return !*str && !*pat; return !*str && !*pat;
} }
/**
* strglobmatch - glob expression pattern matching
* @str: the target string to match
* @pat: the pattern string to match
*
* This returns true if the @str matches @pat. @pat can includes wildcards
* ('*','?') and character classes ([CHARS], complementation and ranges are
* also supported). Also, this supports escape character ('\') to use special
* characters as normal character.
*
* Note: if @pat syntax is broken, this always returns false.
*/
bool strglobmatch(const char *str, const char *pat)
{
return __match_glob(str, pat, false);
}
/**
* strlazymatch - matching pattern strings lazily with glob pattern
* @str: the target string to match
* @pat: the pattern string to match
*
* This is similar to strglobmatch, except this ignores spaces in
* the target string.
*/
bool strlazymatch(const char *str, const char *pat)
{
return __match_glob(str, pat, true);
}
...@@ -10,6 +10,7 @@ s64 perf_atoll(const char *str); ...@@ -10,6 +10,7 @@ s64 perf_atoll(const char *str);
char **argv_split(const char *str, int *argcp); char **argv_split(const char *str, int *argcp);
void argv_free(char **argv); void argv_free(char **argv);
bool strglobmatch(const char *str, const char *pat); bool strglobmatch(const char *str, const char *pat);
bool strlazymatch(const char *str, const char *pat);
#define _STR(x) #x #define _STR(x) #x
#define STR(x) _STR(x) #define STR(x) _STR(x)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment