summaryrefslogtreecommitdiffstats
path: root/runtime/runtime.h
blob: e077c29a7573c81701c67cbde5fe3b3d67d69a9a (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* main header file
 * Copyright (C) 2005 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#ifndef _RUNTIME_H_
#define _RUNTIME_H_
/** @file runtime.h
 * @brief Main include file for runtime functions.
 */

#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/init.h>
#include <linux/hash.h>
#include <linux/string.h>
#include <linux/kprobes.h>
#include <linux/proc_fs.h>
#include <linux/vmalloc.h>
#include <linux/time.h>
#include <linux/spinlock.h>
#include <linux/hardirq.h>
#include <asm/uaccess.h>
#include <linux/kallsyms.h>

#ifdef DEBUG
/** Prints debug line.
 * This function prints a debug message immediately to stpd. 
 * If the last character is not a newline, then one is added. 
 * @param args A variable number of args in a format like printf.
 * @ingroup io
 */
static void _stp_dbug (char *func, int line, const char *fmt, ...);
#define dbug(args...) _stp_dbug(__FUNCTION__, __LINE__, args)
#define kbug(args...) {printk("%s:%d ",__FUNCTION__, __LINE__); printk(args); }
#else
#define dbug(args...) ;
#define kbug(args...) ;
#endif /* DEBUG */

/* atomic globals */
static atomic_t _stp_transport_failures = ATOMIC_INIT (0);

#ifdef STP_RELAYFS
static struct
{
	atomic_t ____cacheline_aligned_in_smp seq;
} _stp_seq = { ATOMIC_INIT (0) };

#define _stp_seq_inc() (atomic_inc_return(&_stp_seq.seq))
#endif /* RELAYFS */

#include "print.c"
#include "string.c"
#include "arith.c"
#include "copy.c"
#include "sym.h"


/************* Module Stuff ********************/
static int (*_stp_kta)(unsigned long addr);
static const char * (*_stp_kallsyms_lookup)(unsigned long addr,
					    unsigned long *symbolsize,
					    unsigned long *offset,
					    char **modname, char *namebuf);

/* TEST_MODE is always defined by systemtap */
#ifdef TEST_MODE
#define SYSTEMTAP 1
#endif

#ifdef SYSTEMTAP
/* This implementation is used if stap_[num_]symbols are available. */
static const char * _stp_kallsyms_lookup_tabled (unsigned long addr,
						 unsigned long *symbolsize,
						 unsigned long *offset,
						 char **modname,
						 char *namebuf)
{
  unsigned begin = 0;
  unsigned end = stap_num_symbols;
  /*const*/ struct stap_symbol* s;

  /* binary search on index [begin,end) */
  do
    {
      unsigned mid = (begin + end) / 2;
      if (addr < stap_symbols[mid].addr)
	end = mid;
      else
	begin = mid;
    } while (begin + 1 < end);
  /* result index in $begin, guaranteed between [0,stap_num_symbols) */

  s = & stap_symbols [begin];
  if (addr < s->addr)
    return NULL;
  else
    {
      if (offset) *offset = addr - s->addr;
      if (modname) *modname = (char *) s->modname;
      if (symbolsize)
	{
	  if ((begin + 1) < stap_num_symbols)
	    *symbolsize = stap_symbols[begin+1].addr - s->addr;
	  else
	    *symbolsize = 0;
	  // NB: This is only a heuristic.  Sometimes there are large
	  // gaps between text areas of modules.
	}
      if (namebuf)
	{
	  strlcpy (namebuf, s->symbol, KSYM_NAME_LEN+1);
	  return namebuf;
	}
      else
	return s->symbol;
    }
}
#endif

int init_module (void)
{
  _stp_kta = (int (*)(unsigned long))kallsyms_lookup_name("__kernel_text_address");

#ifdef SYSTEMTAP
  if (stap_num_symbols > 0)
    _stp_kallsyms_lookup = & _stp_kallsyms_lookup_tabled;
  else
#endif
    _stp_kallsyms_lookup = (const char * (*)(unsigned long,unsigned long *,unsigned long *,char **,char *))
      kallsyms_lookup_name("kallsyms_lookup");

  return _stp_transport_init();
}

int probe_start(void);

void cleanup_module(void)
{
  _stp_transport_close();
}

MODULE_LICENSE("GPL");

#endif /* _RUNTIME_H_ */