oprofiled.c revision 10e23eebca4175a8dfe3a788b2bebacb1fcfce54
110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/**
210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * @file daemon/oprofiled.c
310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * Initialisation and setup
410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project *
510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * @remark Copyright 2002, 2003 OProfile authors
610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * @remark Read the file COPYING
710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project *
810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * @author John Levon
910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * @author Philippe Elie
1010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * Modified by Aravind Menon for Xen
1110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * These modifications are:
1210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * Copyright (C) 2005 Hewlett-Packard Co.
1310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project */
1410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
1510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "config.h"
1610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
1710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "oprofiled.h"
1810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "opd_printf.h"
1910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "opd_events.h"
2010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
2110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_config.h"
2210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_version.h"
2310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_hw_config.h"
2410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_libiberty.h"
2510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_file.h"
2610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_abi.h"
2710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_string.h"
2810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_cpu_type.h"
2910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_popt.h"
3010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_lockfile.h"
3110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_list.h"
3210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include "op_fileio.h"
3310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
3410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <sys/types.h>
3510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <sys/resource.h>
3610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <stdlib.h>
3710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <fcntl.h>
3810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <stdio.h>
3910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <string.h>
4010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <unistd.h>
4110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <errno.h>
4210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <assert.h>
4310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <dirent.h>
4410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#include <limits.h>
4510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
4610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_alarm;
4710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_hup;
4810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_term;
4910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_child;
5010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_usr1;
5110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectsig_atomic_t signal_usr2;
5210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
5310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectuint op_nr_counters;
5410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectop_cpu cpu_type;
5510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint vsfile;
5610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint vsamples;
5710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint varcs;
5810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint vmodule;
5910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint vmisc;
6010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint separate_lib;
6110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint separate_kernel;
6210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint separate_thread;
6310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint separate_cpu;
6410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint no_vmlinux;
6510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectchar * vmlinux;
6610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectchar * kernel_range;
6710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectchar * session_dir;
6810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint no_xen;
6910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectchar * xenimage;
7010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectchar * xen_range;
7110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic char * verbose;
7210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic char * binary_name_filter;
7310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic char * events;
7410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic int showvers;
7510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic struct oprofiled_ops * opd_ops;
7610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectextern struct oprofiled_ops opd_24_ops;
7710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectextern struct oprofiled_ops opd_26_ops;
7810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
7910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#define OPD_IMAGE_FILTER_HASH_SIZE 32
8010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic struct list_head images_filter[OPD_IMAGE_FILTER_HASH_SIZE];
8110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
8210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic struct poptOption options[] = {
8310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "session-dir", 0, POPT_ARG_STRING, &session_dir, 0, "place sample database in dir instead of default location", "/var/lib/oprofile", },
8410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "kernel-range", 'r', POPT_ARG_STRING, &kernel_range, 0, "Kernel VMA range", "start-end", },
8510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "vmlinux", 'k', POPT_ARG_STRING, &vmlinux, 0, "vmlinux kernel image", "file", },
8610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "no-vmlinux", 0, POPT_ARG_NONE, &no_vmlinux, 0, "vmlinux kernel image file not available", NULL, },
8710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "xen-range", 0, POPT_ARG_STRING, &xen_range, 0, "Xen VMA range", "start-end", },
8810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "xen-image", 0, POPT_ARG_STRING, &xenimage, 0, "Xen image", "file", },
8910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "image", 0, POPT_ARG_STRING, &binary_name_filter, 0, "image name filter", "profile these comma separated image" },
9010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "separate-lib", 0, POPT_ARG_INT, &separate_lib, 0, "separate library samples for each distinct application", "[0|1]", },
9110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "separate-kernel", 0, POPT_ARG_INT, &separate_kernel, 0, "separate kernel samples for each distinct application", "[0|1]", },
9210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "separate-thread", 0, POPT_ARG_INT, &separate_thread, 0, "thread-profiling mode", "[0|1]" },
9310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "separate-cpu", 0, POPT_ARG_INT, &separate_cpu, 0, "separate samples for each CPU", "[0|1]" },
9410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "events", 'e', POPT_ARG_STRING, &events, 0, "events list", "[events]" },
9510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
9610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ "verbose", 'V', POPT_ARG_STRING, &verbose, 0, "be verbose in log file", "all,sfile,arcs,samples,module,misc", },
9710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	POPT_AUTOHELP
9810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	{ NULL, 0, 0, NULL, 0, NULL, NULL, },
9910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project};
10010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
10110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
10210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectvoid opd_open_logfile(void)
10310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
10410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (open(op_log_file, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND, 0644) == -1) {
10510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: couldn't re-open stdout: ");
10610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
10710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
10810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
10910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (dup2(1, 2) == -1) {
11010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: couldn't dup stdout to stderr: ");
11110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
11210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
11310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
11410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
11510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
11610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/**
11710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * opd_fork - fork and return as child
11810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project *
11910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * fork() and exit the parent with _exit().
12010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * Failure is fatal.
12110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project */
12210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_fork(void)
12310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
12410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	switch (fork()) {
12510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		case -1:
12610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			perror("oprofiled: fork() failed: ");
12710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			exit(EXIT_FAILURE);
12810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			break;
12910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		case 0:
13010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			break;
13110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		default:
13210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			/* parent */
13310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			_exit(EXIT_SUCCESS);
13410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			break;
13510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
13610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
13710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
13810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
13910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_go_daemon(void)
14010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
14110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_fork();
14210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
14310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (chdir(op_session_dir)) {
14410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		fprintf(stderr, "oprofiled: opd_go_daemon: couldn't chdir to %s: %s",
14510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			op_session_dir, strerror(errno));
14610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
14710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
14810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
14910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (setsid() < 0) {
15010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: opd_go_daemon: couldn't setsid: ");
15110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
15210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
15310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
15410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_fork();
15510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
15610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
15710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
15810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_write_abi(void)
15910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
16010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char * cbuf;
16110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
16210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	cbuf = xmalloc(strlen(op_session_dir) + 5);
16310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	strcpy(cbuf, op_session_dir);
16410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	strcat(cbuf, "/abi");
16510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	op_write_abi_to_file(cbuf);
16610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	free(cbuf);
16710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
16810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
16910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
17010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/**
17110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * opd_alarm - sync files and report stats
17210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project */
17310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_alarm(int val __attribute__((unused)))
17410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
17510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_alarm = 1;
17610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
17710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
17810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
17910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/* re-open logfile for logrotate */
18010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_sighup(int val __attribute__((unused)))
18110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
18210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_hup = 1;
18310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
18410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
18510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
18610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_sigterm(int val __attribute__((unused)))
18710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
18810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_term = 1;
18910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
19010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
19110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_sigchild(int val __attribute__((unused)))
19210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
19310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_child = 1;
19410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
19510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
19610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
19710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_sigusr1(int val __attribute__((unused)))
19810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
19910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_usr1 = 1;
20010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
20110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
20210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
20310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_sigusr2(int val __attribute__((unused)))
20410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
20510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	signal_usr2 = 1;
20610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
20710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
20810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
20910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_setup_signals(void)
21010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
21110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	struct sigaction act;
21210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
21310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_alarm;
21410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
21510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
21610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
21710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGALRM, &act, NULL)) {
21810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGALRM handler failed: ");
21910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
22010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
22110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
22210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_sighup;
22310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
22410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
22510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigaddset(&act.sa_mask, SIGALRM);
22610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
22710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGHUP, &act, NULL)) {
22810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGHUP handler failed: ");
22910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
23010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
23110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
23210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_sigterm;
23310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
23410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
23510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigaddset(&act.sa_mask, SIGTERM);
23610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
23710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGTERM, &act, NULL)) {
23810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGTERM handler failed: ");
23910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
24010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
24110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
24210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_sigchild;
24310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
24410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
24510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigaddset(&act.sa_mask, SIGCHLD);
24610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
24710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGCHLD, &act, NULL)) {
24810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGCHLD handler failed: ");
24910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
25010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
25110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
25210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_sigusr1;
25310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
25410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
25510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigaddset(&act.sa_mask, SIGTERM);
25610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
25710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGUSR1, &act, NULL)) {
25810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGUSR1 handler failed: ");
25910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
26010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
26110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
26210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_handler = opd_sigusr2;
26310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	act.sa_flags = 0;
26410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigemptyset(&act.sa_mask);
26510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	sigaddset(&act.sa_mask, SIGTERM);
26610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
26710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (sigaction(SIGUSR2, &act, NULL)) {
26810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("oprofiled: install of SIGUSR2 handler failed: ");
26910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
27010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
27110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
27210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
27310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
27410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstruct opd_hashed_name {
27510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char * name;
27610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	struct list_head next;
27710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project};
27810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
27910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
28010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void add_image_filter(char const * name)
28110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
28210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	size_t hash;
28310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	struct opd_hashed_name * elt = xmalloc(sizeof(struct opd_hashed_name));
28410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	elt->name = xmalloc(PATH_MAX);
28510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!realpath(name, elt->name)) {
28610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		free(elt->name);
28710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		free(elt);
28810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		return;
28910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
29010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	hash = op_hash_string(elt->name);
29110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	verbprintf(vmisc, "Adding to image filter: \"%s\"\n", elt->name);
29210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	list_add(&elt->next, &images_filter[hash % OPD_IMAGE_FILTER_HASH_SIZE]);
29310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
29410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
29510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
29610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_parse_image_filter(void)
29710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
29810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	size_t i;
29910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char const * last = binary_name_filter;
30010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char const * cur = binary_name_filter;
30110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
30210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!binary_name_filter)
30310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		return;
30410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
30510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	for (i = 0; i < OPD_IMAGE_FILTER_HASH_SIZE; ++i)
30610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		list_init(&images_filter[i]);
30710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
30810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	while ((cur = strchr(last, ',')) != NULL) {
30910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		char * tmp = op_xstrndup(last, cur - last);
31010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		add_image_filter(tmp);
31110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		free(tmp);
31210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		last = cur + 1;
31310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
31410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	add_image_filter(last);
31510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
31610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
31710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
31810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint is_image_ignored(char const * name)
31910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
32010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	size_t hash;
32110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	struct list_head * pos;
32210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
32310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!binary_name_filter)
32410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		return 0;
32510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
32610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	hash = op_hash_string(name);
32710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
32810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	list_for_each(pos, &images_filter[hash % OPD_IMAGE_FILTER_HASH_SIZE]) {
32910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		struct opd_hashed_name * hashed_name =
33010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			list_entry(pos, struct opd_hashed_name, next);
33110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (!strcmp(hashed_name->name, name))
33210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			return 0;
33310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
33410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
33510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	return 1;
33610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
33710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
33810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
33910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/** return the int in the given oprofilefs file */
34010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint opd_read_fs_int(char const * path, char const * name, int fatal)
34110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
34210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char filename[PATH_MAX + 1];
34310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	snprintf(filename, PATH_MAX, "%s/%s", path, name);
34410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	return op_read_int_from_file(filename, fatal);
34510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
34610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
34710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
34810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_handle_verbose_option(char const * name)
34910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
35010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!strcmp(name, "all")) {
35110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vsfile = 1;
35210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vsamples = 1;
35310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		varcs = 1;
35410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vmodule = 1;
35510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vmisc = 1;
35610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else if (!strcmp(name, "sfile")) {
35710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vsfile = 1;
35810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else if (!strcmp(name, "arcs")) {
35910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		varcs = 1;
36010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else if (!strcmp(name, "samples")) {
36110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vsamples = 1;
36210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else if (!strcmp(name, "module")) {
36310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vmodule = 1;
36410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else if (!strcmp(name, "misc")) {
36510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		vmisc = 1;
36610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else {
36710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		fprintf(stderr, "unknown verbose options\n");
36810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
36910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
37010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
37110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
37210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_parse_verbose(void)
37310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
37410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char const * last = verbose;
37510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char const * cur = verbose;
37610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
37710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!verbose)
37810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		return;
37910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
38010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	while ((cur = strchr(last, ',')) != NULL) {
38110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		char * tmp = op_xstrndup(last, cur - last);
38210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		opd_handle_verbose_option(tmp);
38310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		free(tmp);
38410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		last = cur + 1;
38510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
38610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_handle_verbose_option(last);
38710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
38810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
38910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
39010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic void opd_options(int argc, char const * argv[])
39110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
39210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	poptContext optcon;
39310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	char * tmp;
39410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
39510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	optcon = op_poptGetContext(NULL, argc, argv, options, 0);
39610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
39710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (showvers)
39810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		show_version(argv[0]);
39910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
40010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_parse_verbose();
40110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
40210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (separate_kernel)
40310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		separate_lib = 1;
40410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
40510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	cpu_type = op_get_cpu_type();
40610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	op_nr_counters = op_get_nr_counters(cpu_type);
40710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
40810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!no_vmlinux) {
40910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (!vmlinux || !strcmp("", vmlinux)) {
41010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			fprintf(stderr, "oprofiled: no vmlinux specified.\n");
41110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			poptPrintHelp(optcon, stderr, 0);
41210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			exit(EXIT_FAILURE);
41310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		}
41410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
41510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		/* canonicalise vmlinux filename. fix #637805 */
41610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		tmp = xmalloc(PATH_MAX);
41710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (realpath(vmlinux, tmp))
41810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			vmlinux = tmp;
41910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		else
42010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			free(tmp);
42110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
42210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (!kernel_range || !strcmp("", kernel_range)) {
42310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			fprintf(stderr, "oprofiled: no kernel VMA range specified.\n");
42410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			poptPrintHelp(optcon, stderr, 0);
42510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			exit(EXIT_FAILURE);
42610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		}
42710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
42810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
42910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (events == NULL) {
43010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		fprintf(stderr, "oprofiled: no events specified.\n");
43110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		poptPrintHelp(optcon, stderr, 0);
43210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
43310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
43410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
43510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (!xenimage || !strcmp("", xenimage)) {
43610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		no_xen = 1;
43710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	} else {
43810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		no_xen = 0;
43910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
44010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		/* canonicalise xen image filename. */
44110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		tmp = xmalloc(PATH_MAX);
44210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (realpath(xenimage, tmp))
44310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			xenimage = tmp;
44410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		else
44510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			free(tmp);
44610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
44710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		if (!xen_range || !strcmp("", xen_range)) {
44810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			fprintf(stderr, "oprofiled: no Xen VMA range specified.\n");
44910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			poptPrintHelp(optcon, stderr, 0);
45010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			exit(EXIT_FAILURE);
45110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		}
45210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
45310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
45410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_parse_events(events);
45510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
45610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_parse_image_filter();
45710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
45810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	poptFreeContext(optcon);
45910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
46010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
46110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
46210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project/* determine what kernel we're running and which daemon
46310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project * to use
46410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project */
46510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectstatic struct oprofiled_ops * get_ops(void)
46610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
46710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	switch (op_get_interface()) {
46810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#ifndef ANDROID
46910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		case OP_INTERFACE_24:
47010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			printf("Using 2.4 OProfile kernel interface.\n");
47110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			return &opd_24_ops;
47210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project#endif
47310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		case OP_INTERFACE_26:
47410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			printf("Using 2.6+ OProfile kernel interface.\n");
47510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			return &opd_26_ops;
47610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		default:
47710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			break;
47810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
47910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
48010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	fprintf(stderr, "Couldn't determine kernel version.\n");
48110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	exit(EXIT_FAILURE);
48210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	return NULL;
48310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
48410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
48510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
48610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Projectint main(int argc, char const * argv[])
48710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project{
48810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	int err;
48910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	struct rlimit rlim = { 2048, 2048 };
49010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
49110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_options(argc, argv);
49210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	init_op_config_dirs(session_dir);
49310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
49410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_setup_signals();
49510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
49610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	err = setrlimit(RLIMIT_NOFILE, &rlim);
49710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (err)
49810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		perror("warning: could not set RLIMIT_NOFILE to 2048: ");
49910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
50010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_write_abi();
50110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
50210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_ops = get_ops();
50310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
50410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_ops->init();
50510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
50610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_go_daemon();
50710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
50810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	/* clean up every 10 minutes */
50910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	alarm(60 * 10);
51010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
51110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	if (op_write_lock_file(op_lock_file)) {
51210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		fprintf(stderr, "oprofiled: could not create lock file %s\n",
51310e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project			op_lock_file);
51410e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project		exit(EXIT_FAILURE);
51510e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	}
51610e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
51710e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_ops->start();
51810e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
51910e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	opd_ops->exit();
52010e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project
52110e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project	return 0;
52210e23eebca4175a8dfe3a788b2bebacb1fcfce54The Android Open Source Project}
523