summaryrefslogtreecommitdiffstats
path: root/runtime/io.c
blob: 16343befe56956e0e9a72e5b0c51c6d2aa4ea733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- linux-c -*- */
/** @file io.c
 * @brief I/O functions
 */

/** Logs data.
 * This function is compatible with printk.  In fact it currently
 * sends all output to vprintk, after sending "STP: ". This allows
 * us to easily detect SystemTap output in the log file.
 *
 * @param fmt A variable number of args.
 * @bug Lines are limited in length by printk buffer.
 * @todo Needs replaced with something much faster that does not
 * use the system log.
 */
void dlog (const char *fmt, ...)
{
  va_list args;
  printk("STP: ");
  va_start(args, fmt);
  vprintk(fmt, args);
  va_end(args);
}


/** Lookup symbol.
 * This simply calls the kernel function kallsyms_lookup().
 * That function is not exported, so this workaround is required.
 * See the kernel source, kernel/kallsyms.c for more information.
 */
static const char * (*_stp_kallsyms_lookup)(unsigned long addr,
			    unsigned long *symbolsize,
			    unsigned long *offset,
			    char **modname, char *namebuf)=(void *)KALLSYMS_LOOKUP;


#define STP_BUF_LEN 8191

/** Static buffer for printing */
static char _stp_pbuf[STP_BUF_LEN+1];
static int _stp_pbuf_len = STP_BUF_LEN;

/** Print into the print buffer.
 * Like printf, except output goes into  _stp_pbuf,
 * which will contain the null-terminated output.
 * Safe because overflowing _stp_pbuf is not allowed.
 * Size is limited by length of print buffer.
 *
 * @param fmt A variable number of args.
 * @note Formatting output should never be done within
 * a probe. Use at module exit time.
 * @sa _stp_print_buf_init
 */

void _stp_print_buf (const char *fmt, ...)
{
  int num;
  va_list args;
  char *buf = _stp_pbuf + STP_BUF_LEN - _stp_pbuf_len;
  va_start(args, fmt);
  num = vscnprintf(buf, _stp_pbuf_len, fmt, args);
  va_end(args);
  if (num > 0)
    _stp_pbuf_len -= num;
}

/** Clear the print buffer.
 * Output from _stp_print_buf() will accumulate in the buffer
 * until this is called.
 */

void _stp_print_buf_init (void)
{
  _stp_pbuf_len = STP_BUF_LEN;
  _stp_pbuf[0] = 0;
}

/** Print addresses symbolically into the print buffer.
 * @param fmt A variable number of args.
 * @param address The address to lookup.
 * @note Formatting output should never be done within
 * a probe. Use at module exit time.
 */

void _stp_print_symbol(const char *fmt, unsigned long address)
{
        char *modname;
        const char *name;
        unsigned long offset, size;
        char namebuf[KSYM_NAME_LEN+1];

        name = _stp_kallsyms_lookup(address, &size, &offset, &modname, namebuf);

        if (!name)
                _stp_print_buf("0x%lx", address);
        else {
	  if (modname)
	    _stp_print_buf("%s+%#lx/%#lx [%s]", name, offset,
			   size, modname);
	  else
	    _stp_print_buf("%s+%#lx/%#lx", name, offset, size);
        }
}

/** Get the current return address.
 * Call from kprobes (not jprobes).
 * @param regs The pt_regs saved by the kprobe.
 * @return The return address saved in esp or rsp.
 * @note i386 and x86_64 only.
 */
 
unsigned long cur_ret_addr (struct pt_regs *regs)
{
#ifdef __x86_64__
  unsigned long *ra = (unsigned long *)regs->rsp;
#else
  unsigned long *ra = (unsigned long *)regs->esp;
#endif
  if (ra)
    return *ra;
  else
    return 0;
}