config.c 4.05 KB
Newer Older
1 2 3 4 5
#include "config.h"
#include "debug.h"
#include "jvmtiutil.h"

#include <string.h>
6
#include <assert.h>
7

8 9
#include <jvmti.h>

10

11
/*
12 13 14 15
 * Agent configuration.
 */
static struct config agent_config;

16 17 18 19 20 21 22 23 24 25 26
/**
 * Parse the instrumentation jar list in format "jar:jar2:...:jarN" to agent_config struct.
 *
 * @param list The list of jar files separated by :
 * @param config
 */
static void
__parse_instrumentations( char * list, struct config * config ){

	size_t jar_count;
	char ** jar_paths = split_string(list, LIST_SEPARATOR, &jar_count);
27

28 29 30
	config->jar_count = jar_count;
	config->instrumentation_jars = jar_paths;
}
31 32 33 34 35 36 37 38

static void
__configure_from_properties (jvmtiEnv * jvmti, struct config * config) {
	//
	// Get boolean values from system properties
	//
	config->debug = jvmti_get_system_property_bool (
		jvmti, PROP_DEBUG, CONF_DEBUG_DEFAULT
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
		);

	config->disl_functionality = jvmti_get_system_property_bool(
		jvmti, DISL_FUNCTIONALITY, DISL_FUNCTIONALITY_DEFAULT
		);

	config->disl_host = jvmti_get_system_property_string(
		jvmti, DISL_SERVER_HOST, DISL_SERVER_HOST_DEFAULT
		);

	config->disl_port = jvmti_get_system_property_string(
		jvmti, DISL_SERVER_PORT, DISL_SERVER_PORT_DEFAULT
		);

	config->split_methods = jvmti_get_system_property_bool (
		jvmti, DISL_SPLIT_METHODS, DISL_SPLIT_METHODS_DEFAULT
		);

	config->catch_exceptions = jvmti_get_system_property_bool (
		jvmti, DISL_CATCH_EXCEPTIONS, DISL_CATCH_EXCEPTIONS_DEFAULT
		);

	config->force_superclass = jvmti_get_system_property_bool (
		jvmti, DISL_FORCE_SUPERCLASS, DISL_FORCE_SUPERCLASS_DEFAULT
		);

	config->force_interfaces = jvmti_get_system_property_bool (
		jvmti, DISL_FORCE_INTERFACES, DISL_FORCE_INTERFACES_DEFAULT
		);

	config->disl_debug = jvmti_get_system_property_bool (
		jvmti, DISL_DEBUG, DISL_DEBUG_DEFAULT
		);

	//
	// Get bypass mode configuration
	//
	char * bypass = jvmti_get_system_property_string (
		jvmti, DISL_BYPASS, DISL_BYPASS_DEFAULT
78
	);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	static const char * values [] = { "disabled", "bootstrap", "dynamic" };
	int bypass_index = find_value_index (bypass, values, sizeof_array (values));
	check_error (bypass_index < 0, "invalid bypass mode, check " DISL_BYPASS);

	config->bypass_mode = bypass_index;
	free (bypass);

	//
	// Get instrumentation list
	//
	if (config->disl_functionality) {
		char * instrumentation = jvmti_get_system_property_string(
			jvmti, DISL_INSTRUMENTATION, NULL
		);
		assert(instrumentation != NULL);
		__parse_instrumentations(instrumentation, config);
	}


99 100 101 102
}


static void
103 104
__configure_from_options(const char * options, struct config * config)
{
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	//
	// Assign default Shadow VM host name and port and bail out
	// if there are no agent options.
	//
	if (options == NULL) {
		config->shadow_host = strdup (CONF_SHADOW_HOST_DEFAULT);
		config->shadow_port = strdup (CONF_SHADOW_PORT_DEFAULT);
		return;
	}

	//
	// Parse the host name and port of the Shadow VM server.
	// Look for port specification first, then take the prefix
	// before ':' as the host name.
	//
	char * host_start = strdup (options);
	char * port_start = strchr (host_start, ':');
	if (port_start != NULL) {
		//
		// Split the option string at the port delimiter (':')
		// using an end-of-string character ('\0') and copy
		// the port.
		//
		port_start [0] = '\0';
		port_start++;

		config->shadow_port = strdup (port_start);
	}

	config->shadow_host = strdup (host_start);
}


138 139 140
const struct config *
config_init (jvmtiEnv * jvmti, const char * options)
{
141 142 143 144 145 146 147 148 149 150 151
	__configure_from_options (options, &agent_config);
	__configure_from_properties (jvmti, &agent_config);

	//
	// Configuration summary.
	//
	rdexec {
		rdaprefix ("host: %s\n", agent_config.shadow_host);
		rdaprefix ("port: %s\n", agent_config.shadow_port);
		rdaprefix ("debug: %d\n", agent_config.debug);
	}
152 153

	return (&agent_config);
154 155 156 157
}


bool
Vít Kabele's avatar
Vít Kabele committed
158
config_shvm_debug()
159
{
160 161 162 163 164
	return agent_config.debug;
}


const char *
165 166
config_shadow_host()
{
167 168 169 170
	return agent_config.shadow_host;
}

const char *
171 172
config_shadow_port()
{
173 174
	return agent_config.shadow_port;
}
175 176 177 178 179 180 181 182 183 184 185 186

bool
config_do_disl()
{
	return (agent_config.disl_functionality);
}

bool
config_disl_debug()
{
	return (agent_config.disl_debug);
}