File: example.c

package info (click to toggle)
eztrace-contrib 1.0.5-3
  • links: PTS, VCS
  • area: contrib
  • in suites: jessie, jessie-kfreebsd
  • size: 4,744 kB
  • ctags: 4,106
  • sloc: ansic: 24,805; sh: 11,403; perl: 1,107; makefile: 851; f90: 295; python: 57; fortran: 38
file content (93 lines) | stat: -rw-r--r-- 2,986 bytes parent folder | download | duplicates (7)
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
/* -*- c-file-style: "GNU" -*- */
/*
 * Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
 * See COPYING in top-level directory.
 */

/* This file contains an alternative libexample.so
 * The functions defined in libexample (example_function1 and
 * example_function2) are redefined so that when the application
 * call one of these functions, the hereby version of it is actually
 * invoked.
 */

/* In order to compile this library, you need to add -ldl 
 */

#include "eztrace.h"
#include "example_ev_codes.h"

/* pointers to the actual functions (located in the original
 * libexample.so)
 */
double (*libexample_function1)(double*, int);
double (*libstatic_example_function)(double*, int);
int (*libexample_function2)(int*, int);

/* redefine example_function1.
 * This version of the function only record some events and calls
 * the original example_function1 function
 */
double example_function1(double *array, int array_size) {
  /* record an event with code EZTRACE_EXAMPLE_FUNCTION1_ENTRY
   * and two values
   */
  EZTRACE_EVENT2(EZTRACE_EXAMPLE_FUNCTION1_ENTRY, array, array_size);

  /* call the actual function (located in the original libexample.so) */
  double ret = libexample_function1(array, array_size);

  /* record another event with code EZTRACE_EXAMPLE_FUNCTION1_ENTRY
   * and two values
   */
  EZTRACE_EVENT2(EZTRACE_EXAMPLE_FUNCTION1_EXIT, array, array_size);
  return ret;
}

double static_example_function(double*array, int array_size) {
  EZTRACE_EVENT2(EZTRACE_STATIC_EXAMPLE_FUNCTION_ENTRY, array, array_size);

  /* call the actual function (located in the original libexample.so) */
  double ret = libstatic_example_function(array, array_size);

  /* record another event with code EZTRACE_EXAMPLE_FUNCTION1_ENTRY
   * and two values
   */
  EZTRACE_EVENT2(EZTRACE_STATIC_EXAMPLE_FUNCTION_EXIT, array, array_size);
  return ret;
}

/* redefine example_function2.
 * This version of the function only record some events and calls
 * the original example_function2 function
 */
int example_function2(int *array, int array_size) {
  EZTRACE_EVENT2(EZTRACE_EXAMPLE_FUNCTION2_ENTRY, array, array_size);
  int ret = libexample_function2(array, array_size);
  EZTRACE_EVENT2(EZTRACE_EXAMPLE_FUNCTION2_EXIT, array, array_size);
  return ret;
}

void __example_init(void) __attribute__ ((constructor));
/* Initialize the current library */
void __example_init(void) {
  /* store the address of the example_function1 in libexample_function1
   * so that the redefined version of this function can call the
   * original one
   */
  INTERCEPT("example_function1", libexample_function1);
  INTERCEPT("example_function2", libexample_function2);

  INTERCEPT("static_example_function", libstatic_example_function);

  /* start event recording */
#ifdef EZTRACE_AUTOSTART
  eztrace_start ();
#endif
}

void __example_conclude(void) __attribute__ ((destructor));
void __example_conclude(void) {
  /* stop event recording */
  eztrace_stop();
}