/*
* probe-finder.c : C expression to kprobe event converter
*
* Written by Masami Hiramatsu <mhiramat@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "event.h"
#include "debug.h"
#include "util.h"
#include "probe-finder.h"
/* Dwarf_Die Linkage to parent Die */
struct die_link {
struct die_link *parent; /* Parent die */
Dwarf_Die die; /* Current die */
};
static Dwarf_Debug __dw_debug;
static Dwarf_Error __dw_error;
/*
* Generic dwarf analysis helpers
*/
#define X86_32_MAX_REGS 8
const char *x86_32_regs_table[X86_32_MAX_REGS] = {
"%ax",
"%cx",
"%dx",
"%bx",
"$stack", /* Stack address instead of %sp */
"%bp",
"%si",
"%di",
};
#define X86_64_MAX_REGS 16
const char *x86_64_regs_table[X86_64_MAX_REGS] = {
"%ax",
"%dx",
"%cx",
"%bx",
"%si",
"%di",
"%bp",
"%sp",
"%r8",
"%r9",
"%r10",
"%r11",
"%r12",
"%r13",
"%r14",
"%r15",
};
/* TODO: switching by dwarf address size */
#ifdef __x86_64__
#define ARCH_MAX_REGS X86_64_MAX_REGS
#define arch_regs_table x86_64_regs_table
#else
#define ARCH_MAX_REGS X86_32_MAX_REGS
#define arch_regs_table x86_32_regs_table
#endif
/* Return architecture dependent register string (for kprobe-tracer) */
static const char *get_arch_regstr(unsigned int n)
{
return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
}
/*
* Compare the tail of two strings.
* Return 0 if whole of either string is same as another's tail part.
*/
static int strtailcmp(const char *s1, const char *s2)
{
int i1 = strlen(s1);
int i2 = strlen(s2);
while (--i1 >= 0 && --i2 >= 0) {
if (s1[i1] != s2[i2])
return s1[i1] - s2[i2];
}
return 0;
}
/* Find the fileno of the target file. */
static Dwarf_Unsigned cu_find_fileno(Dwarf_Die cu_die, const char *fname)
{
Dwarf_Signed cnt, i;
Dwarf_Unsigned found = 0;
char **srcs;
int ret;
if (!fname)
return 0;
ret = dwarf_srcfiles(cu_die, &srcs, &cnt, &__dw_error);
if (ret == DW_DLV_OK) {
for (i = 0; i < cnt && !found; i++) {
if (strtailcmp(srcs[i], fname) == 0)
found = i + 1;
dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
}
for (; i < cnt; i++)
dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
dwarf_dealloc(__dw_debug, srcs, DW_DLA_LIST);
}
if (found)
pr_debug("found fno: %d\n", (int)found);
return found;
}
static int cu_get_filename(Dwarf_Die cu_die, Dwarf_Unsigned fno, char **buf)
{
Dwarf_Signed cnt, i;
char **srcs;
int ret = 0;
if (!buf || !fno)
return -EINVAL;
ret = dwarf_srcfiles(cu_die, &srcs, &cnt, &__dw_error);
if (ret == DW_DLV_OK) {
if ((Dwarf_Unsigned)cnt > fno - 1) {
*buf = strdup(srcs[fno - 1]);
ret = 0;
pr_debug("found filename: %s\n", *buf);
} else
ret = -ENOENT;
for (i = 0; i < cnt; i++)
dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
dwarf_dealloc(__dw_debug, srcs, DW_DLA_LIST);
} else
ret = -EINVAL;
return ret;
}
/* Compare diename and tname */
static int die_compare_name(Dwarf_Die dw_die, const char *tname)
{
char *name;
int ret;
ret = dwarf_diename(dw_die, &name, &__dw_error);
DIE_IF(ret == DW_DLV_ERROR);
if (ret == DW_DLV_OK) {
ret = strcmp(tname, name);
dwarf_dealloc(__dw_debug, name, DW_DLA_STRING);
} else
ret = -1;
return ret;
}