summaryrefslogtreecommitdiff
path: root/sapi/fpm/fpm/fpm_trace_pread.c
blob: 2f1fb5618f0dd019a6babb58cf4074ff1fed1beb (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
	/* $Id: fpm_trace_pread.c,v 1.7 2008/08/26 15:09:15 anight Exp $ */
	/* (c) 2007,2008 Andrei Nigmatulin */

#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64

#include "fpm_config.h"

#include <unistd.h>

#include <fcntl.h>
#include <stdio.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# include <stdint.h>
#endif

#include "fpm_trace.h"
#include "fpm_process_ctl.h"
#include "zlog.h"

static int mem_file = -1;

int fpm_trace_signal(pid_t pid) /* {{{ */
{
	if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
		zlog(ZLOG_SYSERROR, "failed to send SIGSTOP to %d", pid);
		return -1;
	}
	return 0;
}
/* }}} */

int fpm_trace_ready(pid_t pid) /* {{{ */
{
	char buf[128];

	sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
	mem_file = open(buf, O_RDONLY);
	if (0 > mem_file) {
		zlog(ZLOG_SYSERROR, "failed to open %s", buf);
		return -1;
	}
	return 0;
}
/* }}} */

int fpm_trace_close(pid_t pid) /* {{{ */
{
	close(mem_file);
	mem_file = -1;
	return 0;
}
/* }}} */

int fpm_trace_get_long(long addr, long *data) /* {{{ */
{
	if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
		zlog(ZLOG_SYSERROR, "pread() failed");
		return -1;
	}
	return 0;
}
/* }}} */