#!/bin/bash
#
# Copyright (C) - 2013 Christian Babeux <christian.babeux@efficios.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License, version 2 only, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

TEST_DESC="UST tracer - Basic events"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
LTTNG_BIN="lttng"
BIN_NAME="gen-ust-events"
SESSION_NAME="valid_filter"
EVENT_NAME="tp:tptest"
NUM_TESTS=25

source $TESTDIR/utils/utils.sh

function enable_ust_lttng_event_per_chan()
{
	sess_name="$1"
	event_name="$2"
	chan_name="$3"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -c $chan_name -u >/dev/null 2>&1
	ok $? "Enable event $event_name for session $sess_name in channel $chan_name"
}

function disable_ust_lttng_event_per_chan()
{
	sess_name="$1"
	event_name="$2"
	chan_name="$3"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -c $chan_name -u >/dev/null 2>&1
	ok $? "Disable event $event_name for session $sess_name in channel $chan_name"
}

function add_ust_lttng_context()
{
	sess_name="$1"
	chan_name="$2"
	type="$3"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN add-context -s $sess_name -c $chan_name -t $type -u >/dev/null 2>&1
	ok $? "Add context $type for session $sess_name in channel $chan_name"
}

function test_event_basic()
{
	TRACE_PATH=$(mktemp -d)
	SESSION_NAME="ust_event_basic"
	SESSION_NAME2="ust_event_basic2"
	CHAN_NAME="mychan"
	CHAN_NAME2="mychan2"
	EVENT_NAME="tp1"
	EVENT_NAME2="ev2"
	EVENT_NAME3="ev3"

	create_lttng_session $SESSION_NAME $TRACE_PATH
	create_lttng_session $SESSION_NAME2 $TRACE_PATH

	enable_ust_lttng_channel $SESSION_NAME $CHAN_NAME
	enable_ust_lttng_channel $SESSION_NAME2 $CHAN_NAME2

	enable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME $CHAN_NAME
	enable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME2 $CHAN_NAME
	enable_ust_lttng_event_per_chan $SESSION_NAME2 $EVENT_NAME3 $CHAN_NAME2

	add_ust_lttng_context $SESSION_NAME $CHAN_NAME "vpid"
	add_ust_lttng_context $SESSION_NAME $CHAN_NAME "vtid"
	add_ust_lttng_context $SESSION_NAME $CHAN_NAME "pthread_id"
	add_ust_lttng_context $SESSION_NAME $CHAN_NAME "procname"
	add_ust_lttng_context $SESSION_NAME2 $CHAN_NAME2 "procname"

	disable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME $CHAN_NAME
	disable_ust_lttng_event_per_chan $SESSION_NAME2 $EVENT_NAME3 $CHAN_NAME2

	enable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME $CHAN_NAME
	enable_ust_lttng_event_per_chan $SESSION_NAME2 $EVENT_NAME3 $CHAN_NAME2

	start_lttng_tracing
	stop_lttng_tracing

	start_lttng_tracing
	stop_lttng_tracing

	TODO="Validate trace output is coherent"
	ok 0 "Validate trace"
	unset TODO

	destroy_lttng_session $SESSION_NAME2
	destroy_lttng_session $SESSION_NAME

	rm -rf $TRACE_PATH
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

start_lttng_sessiond

test_event_basic

stop_lttng_sessiond
