HPCToolkit
monitor_preload.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 /****************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // Implements monitoring that is launched with a preloaded library,
54 // something that only works for dynamically linked applications.
55 // The library intercepts the beginning execution point of the
56 // process and then starts monitoring. When the process exits,
57 // control will be transferred back to this library where profiling
58 // is finalized.
59 //
60 // Description:
61 // [The set of functions, macros, etc. defined in the file]
62 //
63 // Author:
64 // Written by John Mellor-Crummey and Nathan Tallent, Rice University.
65 //
66 *****************************************************************************/
67 
68 /************************** System Include Files ****************************/
69 
70 #include <stdlib.h>
71 #include <stdarg.h> /* va_arg */
72 #include <string.h> /* memset */
73 #include <errno.h>
74 #include <pthread.h>
75 
76 #ifndef __USE_GNU
77 # define __USE_GNU /* must define on Linux to get RTLD_NEXT from <dlfcn.h> */
78 # define SELF_DEFINED__USE_GNU
79 #endif
80 
81 #include <dlfcn.h>
82 
83 #undef __USE_GNU
84 
85 /**************************** User Include Files ****************************/
86 
87 #include "monitor.h"
88 #include "hpcrun.h"
89 
90 
91 /**************************** Forward Declarations **************************/
92 
93 /****************************************************************************/
94 
95 
96 void
98 {
99  init_library();
100  /* process initialized with interception of libc_start_main */
101 }
102 
103 
104 void
106 {
107  /* process finalized with libc_start_main */
108  fini_library();
109 }
110 
111 
112 void
113 monitor_init_process(const char* name, int* argc, char** argv,
114  const unsigned pid)
115 {
116  hpcrun_cmd = name; /* command is also in /proc/pid/cmdline */
117  if (opt_debug >= 1) {
118  fprintf(stderr, "Init process callback from monitor received\n");
119  }
120  init_process();
121 }
122 
123 
124 void
126 {
127  if (opt_debug >= 1) {
128  fprintf(stderr, "Fini process callback from monitor received\n");
129  }
130  fini_process();
131 }
132 
133 
134 void
136 {
137  int rval;
138  if ((rval = PAPI_thread_init(pthread_self)) != PAPI_OK) {
139  DIEx("error: PAPI error (%d): %s.", rval, PAPI_strerror(rval));
140  }
141 }
142 
143 
144 void*
146 {
147  if (opt_debug >= 1) {
148  fprintf(stderr, "init_thread(TID=0x%ux) callback from monitor received\n", tid);
149  }
150  return((void *)init_thread(1));
151 }
152 
153 
154 void
156 {
157  if (opt_debug >= 1) {
158  fprintf(stderr, "fini_thread(TID=0x%lx) callback from monitor received\n", (long)pthread_self());
159  }
160  fini_thread((hpcrun_profiles_desc_t **)(&data), 1 /*is_thread*/);
161 }
162 
163 
164 void
165 monitor_dlopen(char* lib)
166 {
167  if (opt_debug >= 1) {
168  fprintf(stderr, "dlopen(%s) callback from monitor received\n", lib);
169  }
170  /* update profile tables */
171  //handle_dlopen();
172 }
173 
174 /****************************************************************************/
void monitor_fini_process()
void monitor_dlopen(char *lib)
void init_library()
Definition: monitor.c:164
void fini_thread(hpcrun_profiles_desc_t **profdesc, int is_thread)
Definition: monitor.c:1491
void monitor_init_library()
void monitor_fini_library()
void monitor_fini_thread(void *data)
static __thread u32 tid
const char * hpcrun_cmd
Definition: monitor.c:136
static __thread u32 pid
void fini_process()
Definition: monitor.c:1466
void monitor_init_process(const char *name, int *argc, char **argv, const unsigned pid)
#define DIEx(fmt,...)
Definition: monitor.h:139
void monitor_init_thread_support(void)
void fini_library()
Definition: monitor.c:183
hpcrun_profiles_desc_t * init_thread(int is_thread)
Definition: monitor.c:510
void init_process()
Definition: monitor.c:430
int opt_debug
Definition: monitor.c:126
void * monitor_init_thread(unsigned tid)