88 #define STAND_ALONE_CLIENT 92 #define SAMPLE_SOURCES(...) zero_fcn() 93 #define dup2(...) zero_fcn() 94 #define hpcrun_set_disabled() 95 #define monitor_real_exit exit 96 #define monitor_real_fork fork 97 #define monitor_real_execve execve 98 #define monitor_sigaction(...) 0 99 int zero_fcn(
void) {
return 0; }
104 #include <sys/types.h> 105 #include <sys/mman.h> 106 #include <sys/stat.h> 107 #include <sys/wait.h> 119 #if !defined(STAND_ALONE_CLIENT) 133 #define SERVER_MEM_LIMIT 140 134 #define MIN_NUM_QUERIES 12 138 #define END_OF_FILE -2 176 while (len < count) {
177 ret =
read(fd, ((
char *) buf) + len, count - len);
178 if (ret < 0 && errno != EINTR) {
203 while (len < count) {
204 ret =
write(fd, ((
const char *) buf) + len, count - len);
205 if (ret < 0 && errno != EINTR) {
225 memset(mesg, 0,
sizeof(*mesg));
262 #if defined(_SC_PAGESIZE) 263 long ans = sysconf(_SC_PAGESIZE);
273 return ((size + pagesize - 1)/pagesize) *
pagesize;
285 prot = PROT_READ | PROT_WRITE;
286 #if defined(MAP_ANONYMOUS) 287 flags = MAP_PRIVATE | MAP_ANONYMOUS;
289 flags = MAP_PRIVATE | MAP_ANON;
292 return mmap(
NULL, size, prot, flags, -1, 0);
306 TMSG(SYSTEM_SERVER,
"caught SIGPIPE: system server must have exited");
337 TMSG(SYSTEM_SERVER,
"syserv shutdown");
345 int sendfd[2], recvfd[2];
346 bool sampling_is_running;
359 if (pipe(sendfd) != 0 || pipe(recvfd) != 0) {
360 EMSG(
"SYSTEM_SERVER ERROR: syserv launch failed: pipe failed");
367 if (sampling_is_running) {
370 pid = monitor_real_fork();
376 EMSG(
"SYSTEM_SERVER ERROR: syserv launch failed: fork failed");
391 warn(
"dup of log fd onto stdout failed");
394 warn(
"dup of log fd onto stderr failed");
399 char fdin_str[10], fdout_str[10];
400 sprintf(fdin_str,
"%d", sendfd[0]);
401 sprintf(fdout_str,
"%d", recvfd[1]);
406 arglist[3] = fdin_str;
407 arglist[4] = fdout_str;
411 err(1,
"hpcrun system server: exec(%s) failed",
server);
427 TMSG(SYSTEM_SERVER,
"syserv launch: success, server: %d", (
int)
server_pid);
430 if (sampling_is_running) {
442 TMSG(SYSTEM_SERVER,
"syserv init");
444 server = getenv(
"HPCRUN_FNBOUNDS_CMD");
446 EMSG(
"SYSTEM_SERVER ERROR: unable to get HPCRUN_FNBOUNDS_CMD");
451 char *str = getenv(
"HPCRUN_SERVER_MEMSIZE");
453 if (str ==
NULL || sscanf(str,
"%ld", &size) < 1) {
459 EMSG(
"SYSTEM_SERVER ERROR: unable to install handler for SIGPIPE");
480 EEMSG(
"hpcrun: unable to launch the hpcfnbounds server.\n" 481 "hpcrun: check that hpctoolkit is properly configured with dyninst\n" 482 "and its prereqs (boost, elfutils, libdwarf, bzip, libz, lzma).");
483 monitor_real_exit(1);
499 TMSG(SYSTEM_SERVER,
"syserv fini");
517 EMSG(
"SYSTEM_SERVER ERROR: passed NULL pointer to %s", __func__);
525 TMSG(SYSTEM_SERVER,
"query %d: %s", query, fname);
531 size_t len = strlen(fname) + 1;
535 TMSG(SYSTEM_SERVER,
"restart server");
541 EMSG(
"SYSTEM_SERVER ERROR: unable to restart system server");
551 EMSG(
"SYSTEM_SERVER ERROR: lost contact with server");
556 EMSG(
"SYSTEM_SERVER ERROR: lost contact with server");
561 EMSG(
"SYSTEM_SERVER ERROR: query failed: %s", fname);
568 size_t num_bytes = mesg.
len *
sizeof(
void *);
571 if (addr == MAP_FAILED) {
575 EMSG(
"SYSTEM_SERVER ERROR: mmap failed");
580 EMSG(
"SYSTEM_SERVER ERROR: lost contact with server");
589 EMSG(
"SYSTEM_SERVER ERROR: lost contact with server");
594 EMSG(
"SYSTEM_SERVER ERROR: query failed: %s", fname);
602 TMSG(SYSTEM_SERVER,
"addr: %p, symbols: %ld, offset: 0x%lx, reloc: %d",
605 TMSG(SYSTEM_SERVER,
"server memsize: %ld Meg", fnb_info.
memsize / 1024);
611 EMSG(
"SYSTEM_SERVER: warning: memory usage: %ld Meg",
616 EMSG(
"SYSTEM_SERVER: warning: memory usage: %ld Meg, restart server",
633 TMSG(SYSTEM_SERVER,
"query variable for %s", fname);
642 #ifdef STAND_ALONE_CLIENT 644 #define BUF_SIZE 2000 655 printf(
"\nfnbounds> ");
659 char *new_line = strchr(fname,
'\n');
660 if (new_line !=
NULL) {
671 printf(
" %p\n", addr[k]);
673 printf(
"num symbols = %ld, offset = 0x%lx, reloc = %d\n",
677 if (munmap(addr, fnb_hdr.
mmap_size) != 0) {
678 err(1,
"munmap failed");
686 main(
int argc,
char *argv[])
688 struct sigaction act;
691 errx(1,
"usage: client /path/to/fnbounds");
695 memset(&act, 0,
sizeof(act));
696 act.sa_handler = SIG_IGN;
697 sigemptyset(&act.sa_mask);
698 if (sigaction(SIGPIPE, &act,
NULL) != 0) {
699 err(1,
"sigaction failed on SIGPIPE");
703 errx(1,
"fnbounds server failed");
705 printf(
"server: %s\n",
server);
707 printf(
"connected\n");
717 #endif // STAND_ALONE_CLIENT
ssize_t MONITOR_EXT_WRAP_NAME() write(int fd, const void *buf, size_t count)
static int launch_server(void)
void * hpcrun_syserv_query_specific(const char *fname, struct fnbounds_file_header *fh, int query)
#define SAMPLE_SOURCES(op,...)
static int read_mesg(struct syserv_mesg *mesg)
static void * mmap_anon(size_t size)
void * hpcrun_syserv_query(const char *fname, struct fnbounds_file_header *fh)
static void shutdown_server(void)
static size_t page_align(size_t size)
int messages_logfile_fd(void)
static int write_all(int fd, const void *buf, size_t count)
void hpcrun_set_disabled(void)
static int read_all(int fd, void *buf, size_t count)
void hpcrun_syserv_fini(void)
int main(int argc, char *argv[])
int hpcrun_syserv_init(void)
void * hpcrun_syserv_query_var(const char *fname, struct fnbounds_file_header *fh)
ssize_t MONITOR_EXT_WRAP_NAME() read(int fd, void *buf, size_t count)
static int write_mesg(int32_t type, int64_t len)
static int hpcrun_sigpipe_handler(int sig, siginfo_t *info, void *context)
uint64_t reference_offset