/*
 * SPDX-FileCopyrightText: 2011 EfficiOS Inc.
 * SPDX-FileCopyrightText: 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
 * SPDX-FileCopyrightText: 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 */

#include "lttng/tracker.h"

#define _LGPL_SOURCE
#define __USE_LINUX_IOCTL_DEFS
#include "kernel-ctl.hpp"
#include "kernel-ioctl.hpp"

#include <common/align.hpp>
#include <common/compat/errno.hpp>
#include <common/format.hpp>
#include <common/macros.hpp>
#include <common/time.hpp>

#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdarg.h>
#include <string.h>
#include <sys/ioctl.h>

#define LTTNG_IOCTL_CHECK(fildes, request, ...)                               \
	({                                                                    \
		const int _ioctl_ret = ioctl(fildes, request, ##__VA_ARGS__); \
		LTTNG_ASSERT(_ioctl_ret <= 0);                                \
		!_ioctl_ret ? 0 : -errno;                                     \
	})

#define LTTNG_IOCTL_NO_CHECK(fildes, request, ...)                            \
	({                                                                    \
		const int _ioctl_ret = ioctl(fildes, request, ##__VA_ARGS__); \
		_ioctl_ret >= 0 ? _ioctl_ret : -errno;                        \
	})

/*
 * This flag indicates which version of the kernel ABI to use. The old
 * ABI (namespace _old) does not support a 32-bit user-space when the
 * kernel is 64-bit. The old ABI is kept here for compatibility but is
 * deprecated and will be removed eventually.
 */
namespace {
int lttng_kernel_abi_use_old_abi = -1;

/*
 * Execute the new or old ioctl depending on the ABI version.
 * If the ABI version is not determined yet (lttng_kernel_abi_use_old_abi = -1),
 * this function tests if the new ABI is available and otherwise fallbacks
 * on the old one.
 * This function takes the fd on which the ioctl must be executed and the old
 * and new request codes.
 * It returns the return value of the ioctl executed.
 */
inline int compat_ioctl_no_arg(int fd, unsigned long oldname, unsigned long newname)
{
	int ret;

	if (lttng_kernel_abi_use_old_abi == -1) {
		ret = LTTNG_IOCTL_NO_CHECK(fd, newname);
		if (!ret) {
			lttng_kernel_abi_use_old_abi = 0;
			goto end;
		}
		lttng_kernel_abi_use_old_abi = 1;
	}
	if (lttng_kernel_abi_use_old_abi) {
		ret = LTTNG_IOCTL_NO_CHECK(fd, oldname);
	} else {
		ret = LTTNG_IOCTL_NO_CHECK(fd, newname);
	}

end:
	return ret;
}
} /* namespace */

int kernctl_create_session(int fd)
{
	return compat_ioctl_no_arg(fd, LTTNG_KERNEL_ABI_OLD_SESSION, LTTNG_KERNEL_ABI_SESSION);
}

namespace {
void populate_old_channel_from_abi_channel(struct lttng_kernel_abi_old_channel& old_channel,
					   const struct lttng_kernel_abi_channel& channel_config)
{
	memset(&old_channel, 0, sizeof(old_channel));
	old_channel.overwrite = channel_config.overwrite;
	old_channel.subbuf_size = channel_config.subbuf_size;
	old_channel.num_subbuf = channel_config.num_subbuf;
	old_channel.switch_timer_interval = channel_config.switch_timer_interval;
	old_channel.read_timer_interval = channel_config.read_timer_interval;
	old_channel.output = channel_config.output;
}
} /* namespace */

/* Open the metadata global channel. */
int kernctl_open_metadata(int fd, const struct lttng_kernel_abi_channel& channel_config)
{
	if (lttng_kernel_abi_use_old_abi) {
		struct lttng_kernel_abi_old_channel old_channel;

		populate_old_channel_from_abi_channel(old_channel, channel_config);
		return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_OLD_METADATA, &old_channel);
	}

	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_METADATA, &channel_config);
}

int kernctl_create_channel(int fd, const struct lttng_kernel_abi_channel& channel_config)
{
	if (lttng_kernel_abi_use_old_abi) {
		struct lttng_kernel_abi_old_channel old_channel;

		populate_old_channel_from_abi_channel(old_channel, channel_config);
		return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_OLD_CHANNEL, &old_channel);
	}

	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_CHANNEL, &channel_config);
}

int kernctl_syscall_mask(int fd, char **syscall_mask, uint32_t *nr_bits)
{
	struct lttng_kernel_abi_syscall_mask kmask_len, *kmask = nullptr;
	size_t array_alloc_len;
	char *new_mask;
	int ret = 0;

	if (!syscall_mask) {
		ret = -1;
		goto end;
	}

	if (!nr_bits) {
		ret = -1;
		goto end;
	}

	kmask_len.len = 0;
	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SYSCALL_MASK, &kmask_len);
	if (ret) {
		goto end;
	}

	array_alloc_len = lttng_align_ceil(kmask_len.len, 8) >> 3;

	kmask = zmalloc<lttng_kernel_abi_syscall_mask>(sizeof(*kmask) + array_alloc_len);
	if (!kmask) {
		ret = -1;
		goto end;
	}

	kmask->len = kmask_len.len;
	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SYSCALL_MASK, kmask);
	if (ret) {
		goto end;
	}

	new_mask = (char *) realloc(*syscall_mask, array_alloc_len);
	if (!new_mask) {
		ret = -1;
		goto end;
	}
	memcpy(new_mask, kmask->mask, array_alloc_len);
	*syscall_mask = new_mask;
	*nr_bits = kmask->len;

end:
	free(kmask);
	return ret;
}

int kernctl_track_pid(int fd, int pid)
{
	int ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_TRACK_PID, pid);

	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_PID, pid);
	}

	return ret;
}

int kernctl_untrack_pid(int fd, int pid)
{
	int ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID, pid);

	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_PID, pid);
	}

	return ret;
}

int kernctl_list_tracker_pids(int fd)
{
	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS);
}

int kernctl_track_id(int fd, lttng_kernel_abi_tracker_type tracker_type, int id)
{
	struct lttng_kernel_abi_tracker_args args;
	int ret;

	args.id = id;
	args.type = tracker_type;

	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_TRACK_ID, &args);
	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_ID, &args);
	}

	return ret;
}

int kernctl_untrack_id(int fd, lttng_kernel_abi_tracker_type tracker_type, int id)
{
	struct lttng_kernel_abi_tracker_args args;
	int ret;

	args.id = id;
	args.type = tracker_type;

	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID, &args);
	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_ID, &args);
	}

	return ret;
}

int kernctl_list_tracker_ids(int fd, lttng_kernel_abi_tracker_type tracker_type)
{
	struct lttng_kernel_abi_tracker_args args;
	int ret;

	args.id = -1;
	args.type = tracker_type;

	ret = LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS, &args);
	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_NO_CHECK(
			fd, LTTNG_KERNEL_ABI_OLD_SESSION_LIST_TRACKER_IDS, &args);
	}

	return ret;
}

int kernctl_session_regenerate_metadata(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN);
}

int kernctl_session_regenerate_statedump(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_STATEDUMP);
}

int kernctl_session_set_name(int fd, const char *name)
{
	int ret;
	struct lttng_kernel_abi_session_name session_name;

	ret = lttng_strncpy(session_name.name, name, sizeof(session_name.name));
	if (ret) {
		goto end;
	}

	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_SET_NAME, &session_name);
	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_SESSION_SET_NAME, &session_name);
	}

end:
	return ret;
}

int kernctl_session_set_creation_time(int fd, time_t time)
{
	int ret;
	struct lttng_kernel_abi_session_creation_time creation_time;

	ret = time_to_iso8601_str(time, creation_time.iso8601, sizeof(creation_time.iso8601));
	if (ret) {
		goto end;
	}

	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME, &creation_time);
	if (ret == -ENOSYS) {
		ret = LTTNG_IOCTL_CHECK(
			fd, LTTNG_KERNEL_ABI_OLD_SESSION_SET_CREATION_TIME, &creation_time);
	}

end:
	return ret;
}

int kernctl_session_set_output_format(int fd, enum lttng_kernel_abi_output_format format)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_SESSION_SET_OUTPUT_FORMAT, format);
}

int kernctl_create_stream(int fd)
{
	return compat_ioctl_no_arg(fd, LTTNG_KERNEL_ABI_OLD_STREAM, LTTNG_KERNEL_ABI_STREAM);
}

int kernctl_create_event(int fd, const struct lttng_kernel_abi_event& ev)
{
	if (lttng_kernel_abi_use_old_abi) {
		struct lttng_kernel_abi_old_event old_event;

		memset(&old_event, 0, sizeof(old_event));
		memcpy(old_event.name, ev.name, sizeof(old_event.name));
		old_event.instrumentation = ev.instrumentation;
		switch (ev.instrumentation) {
		case LTTNG_KERNEL_ABI_KPROBE:
			old_event.u.kprobe.addr = ev.u.kprobe.addr;
			old_event.u.kprobe.offset = ev.u.kprobe.offset;
			memcpy(old_event.u.kprobe.symbol_name,
			       ev.u.kprobe.symbol_name,
			       sizeof(old_event.u.kprobe.symbol_name));
			break;
		case LTTNG_KERNEL_ABI_KRETPROBE:
			old_event.u.kretprobe.addr = ev.u.kretprobe.addr;
			old_event.u.kretprobe.offset = ev.u.kretprobe.offset;
			memcpy(old_event.u.kretprobe.symbol_name,
			       ev.u.kretprobe.symbol_name,
			       sizeof(old_event.u.kretprobe.symbol_name));
			break;
		case LTTNG_KERNEL_ABI_FUNCTION:
			memcpy(old_event.u.ftrace.symbol_name,
			       ev.u.ftrace.symbol_name,
			       sizeof(old_event.u.ftrace.symbol_name));
			break;
		default:
			break;
		}

		return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_OLD_EVENT, &old_event);
	}
	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_EVENT, &ev);
}

int kernctl_add_context(int fd, const struct lttng_kernel_abi_context& ctx)
{
	if (lttng_kernel_abi_use_old_abi) {
		struct lttng_kernel_abi_old_context old_ctx;

		memset(&old_ctx, 0, sizeof(old_ctx));
		old_ctx.ctx = ctx.ctx;
		/* only type that uses the union */
		if (ctx.ctx == LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER) {
			old_ctx.u.perf_counter.type = ctx.u.perf_counter.type;
			old_ctx.u.perf_counter.config = ctx.u.perf_counter.config;
			memcpy(old_ctx.u.perf_counter.name,
			       ctx.u.perf_counter.name,
			       sizeof(old_ctx.u.perf_counter.name));
		}
		return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_CONTEXT, &old_ctx);
	}
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_CONTEXT, &ctx);
}

/* Enable event, channel and session LTTNG_IOCTL_CHECK */
int kernctl_enable(int fd)
{
	return compat_ioctl_no_arg(fd, LTTNG_KERNEL_ABI_OLD_ENABLE, LTTNG_KERNEL_ABI_ENABLE);
}

/* Disable event, channel and session LTTNG_IOCTL_CHECK */
int kernctl_disable(int fd)
{
	return compat_ioctl_no_arg(fd, LTTNG_KERNEL_ABI_OLD_DISABLE, LTTNG_KERNEL_ABI_DISABLE);
}

int kernctl_start_session(int fd)
{
	return compat_ioctl_no_arg(
		fd, LTTNG_KERNEL_ABI_OLD_SESSION_START, LTTNG_KERNEL_ABI_SESSION_START);
}

int kernctl_stop_session(int fd)
{
	return compat_ioctl_no_arg(
		fd, LTTNG_KERNEL_ABI_OLD_SESSION_STOP, LTTNG_KERNEL_ABI_SESSION_STOP);
}

int kernctl_create_event_notifier_group(int fd)
{
	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE);
}

int kernctl_create_event_notifier_group_notification_fd(int group_fd)
{
	return LTTNG_IOCTL_NO_CHECK(group_fd,
				    LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD);
}

namespace {
/*
 * The extensible counter commands are only guaranteed to be available
 * against a tracer advertising ABI 2.8 or later; older commands are used
 * otherwise. See kernctl_set_tracer_abi_version()'s documentation.
 */
std::atomic<std::uint32_t> registered_tracer_abi_version{ 0 };

constexpr std::uint32_t encoded_tracer_abi_version(std::uint16_t major, std::uint16_t minor)
{
	return (static_cast<std::uint32_t>(major) << 16) | minor;
}

lttng_kernel_abi_tracer_abi_version decoded_tracer_abi_version(std::uint32_t encoded_version)
{
	lttng_kernel_abi_tracer_abi_version version = {};

	version.major = encoded_version >> 16;
	version.minor = encoded_version & 0xffff;

	return version;
}

int old_counter_conf_from_counter_conf(const struct lttng_kernel_abi_counter_conf& conf,
				       struct lttng_kernel_abi_old_counter_conf& old_conf)
{
	if (conf.dimension_array.number_dimensions > LTTNG_KERNEL_ABI_OLD_COUNTER_DIMENSION_MAX) {
		return -EINVAL;
	}

	old_conf.arithmetic = conf.arithmetic;
	old_conf.bitness = conf.bitness;
	old_conf.number_dimensions = conf.dimension_array.number_dimensions;
	old_conf.global_sum_step = conf.global_sum_step;
	old_conf.coalesce_hits = !!(conf.flags & LTTNG_KERNEL_ABI_COUNTER_CONF_FLAG_COALESCE_HITS);

	const auto *dimensions_begin =
		reinterpret_cast<const char *>(static_cast<uintptr_t>(conf.dimension_array.ptr));

	for (uint32_t dimension_index = 0; dimension_index < conf.dimension_array.number_dimensions;
	     dimension_index++) {
		const auto& dimension =
			*reinterpret_cast<const struct lttng_kernel_abi_counter_dimension *>(
				dimensions_begin +
				(dimension_index * conf.dimension_array.elem_len));

		/*
		 * The old ABI has no notion of a dimension key type: its
		 * dimensions are plain index-keyed arrays. The key_type field
		 * is simply dropped, which is safe since this fallback only
		 * serves error counters (a single index-keyed dimension).
		 */
		auto& old_dimension = old_conf.dimensions[dimension_index];

		old_dimension.size = dimension.size;
		old_dimension.underflow_index = dimension.underflow_index;
		old_dimension.overflow_index = dimension.overflow_index;
		old_dimension.has_underflow =
			!!(dimension.flags & LTTNG_KERNEL_ABI_COUNTER_DIMENSION_FLAG_UNDERFLOW);
		old_dimension.has_overflow =
			!!(dimension.flags & LTTNG_KERNEL_ABI_COUNTER_DIMENSION_FLAG_OVERFLOW);
	}

	return 0;
}

int old_counter_index_from_counter_index(const struct lttng_kernel_abi_counter_index& index,
					 struct lttng_kernel_abi_old_counter_index& old_index)
{
	if (index.number_dimensions > LTTNG_KERNEL_ABI_OLD_COUNTER_DIMENSION_MAX) {
		return -EINVAL;
	}

	const auto *dimension_indexes =
		reinterpret_cast<const uint64_t *>(static_cast<uintptr_t>(index.ptr));

	old_index.number_dimensions = index.number_dimensions;
	for (uint32_t dimension_index = 0; dimension_index < index.number_dimensions;
	     dimension_index++) {
		old_index.dimension_indexes[dimension_index] = dimension_indexes[dimension_index];
	}

	return 0;
}

void counter_value_from_old_counter_value(const struct lttng_kernel_abi_old_counter_value& old_value,
					  struct lttng_kernel_abi_counter_value& value)
{
	value.value = old_value.value;
	value.flags = (old_value.underflow ? LTTNG_KERNEL_ABI_COUNTER_VALUE_FLAG_UNDERFLOW : 0) |
		(old_value.overflow ? LTTNG_KERNEL_ABI_COUNTER_VALUE_FLAG_OVERFLOW : 0);
}
} /* namespace */

bool kernctl_is_extensible_counter_abi_supported()
{
	const auto version = registered_tracer_abi_version.load();

	/* The tracer ABI version must be registered before issuing counter commands. */
	LTTNG_ASSERT(version != 0);
	return version >= encoded_tracer_abi_version(2, 8);
}

int kernctl_create_event_notifier_group_error_counter(
	int group_fd, const struct lttng_kernel_abi_counter_conf *error_counter_conf)
{
	if (kernctl_is_extensible_counter_abi_supported()) {
		return LTTNG_IOCTL_NO_CHECK(group_fd, LTTNG_KERNEL_ABI_COUNTER, error_counter_conf);
	}

	struct lttng_kernel_abi_old_counter_conf old_conf = {};
	const auto ret = old_counter_conf_from_counter_conf(*error_counter_conf, old_conf);
	if (ret) {
		return ret;
	}

	return LTTNG_IOCTL_NO_CHECK(group_fd, LTTNG_KERNEL_ABI_OLD_COUNTER, &old_conf);
}

int kernctl_create_session_counter(int session_fd,
				   const struct lttng_kernel_abi_counter_conf *counter_conf)
{
	return LTTNG_IOCTL_NO_CHECK(session_fd, LTTNG_KERNEL_ABI_COUNTER, counter_conf);
}

int kernctl_create_counter_event(int counter_fd, const void *counter_event)
{
	return LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_COUNTER_EVENT, counter_event);
}

int kernctl_counter_map_nr_descriptors(int counter_fd, uint64_t *nr)
{
	return LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_COUNTER_MAP_NR_DESCRIPTORS, nr);
}

int kernctl_counter_map_descriptor(int counter_fd,
				   uint64_t descriptor_index,
				   uint32_t *out_dimension,
				   uint64_t *out_user_token,
				   std::string *out_key_string,
				   std::vector<uint64_t> *out_array_indexes)
{
	/*
	 * Initial guesses. The kernel writes back the required size on
	 * -ENOSPC, so a single retry should be sufficient.
	 */
	uint32_t array_indexes_capacity_bytes =
		sizeof(uint64_t) * LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX;
	uint32_t key_string_capacity = 64;

	std::vector<char> key_string_buffer;
	std::vector<uint64_t> array_indexes_buffer;

	for (unsigned int attempt = 0; attempt < 2; attempt++) {
		try {
			key_string_buffer.assign(key_string_capacity, '\0');
		} catch (std::bad_alloc& ex) {
			ERR_FMT("Failed to allocate key string buffer of modules map counter descriptor: counter_fd={}, descriptor_index={}, size={}",
				counter_fd,
				descriptor_index,
				key_string_capacity);
			return -ENOMEM;
		}

		try {
			array_indexes_buffer.assign(array_indexes_capacity_bytes / sizeof(uint64_t),
						    0);
		} catch (std::bad_alloc& ex) {
			ERR_FMT("Failed to allocate array indexes buffer of modules map counter descriptor: counter_fd={}, descriptor_index={}, size_bytes={}",
				counter_fd,
				descriptor_index,
				array_indexes_capacity_bytes);
			return -ENOMEM;
		}

		struct lttng_kernel_abi_counter_map_descriptor descriptor {};

		descriptor.len = sizeof(descriptor);
		descriptor.descriptor_index = descriptor_index;
		descriptor.key_string = reinterpret_cast<std::uintptr_t>(key_string_buffer.data());
		descriptor.key_string_len = key_string_capacity;
		descriptor.array_indexes =
			reinterpret_cast<std::uintptr_t>(array_indexes_buffer.data());
		descriptor.array_indexes_len = array_indexes_capacity_bytes;

		const auto ret = LTTNG_IOCTL_NO_CHECK(
			counter_fd, LTTNG_KERNEL_ABI_COUNTER_MAP_DESCRIPTOR, &descriptor);
		if (ret == 0) {
			/* Populate output params. */
			if (out_dimension) {
				*out_dimension = descriptor.dimension;
			}

			if (out_user_token) {
				*out_user_token = descriptor.user_token;
			}

			if (out_key_string) {
				/*
				 * The kernel guarantees a NUL-terminated
				 * string within key_string_len bytes.
				 */
				*out_key_string = std::string(key_string_buffer.data());
			}

			if (out_array_indexes) {
				/*
				 * Copy only the indexes the kernel reported in
				 * array_indexes_len, clamped to the scratchpad.
				 */
				const auto count = std::min<std::size_t>(
					descriptor.array_indexes_len, array_indexes_buffer.size());

				out_array_indexes->assign(array_indexes_buffer.begin(),
							  array_indexes_buffer.begin() + count);
			}

			return 0;
		}

		if (ret != -ENOSPC) {
			ERR_FMT("Failed to get modules map counter descriptor: counter_fd={}, descriptor_index={}, ret={}",
				counter_fd,
				descriptor_index,
				ret);
			return ret;
		} else {
			DBG_FMT("Resizing scratchpad to receive modules map counter descriptor details: counter_fd={}, descriptor_index={}",
				counter_fd,
				descriptor_index);
		}

		/*
		 * Grow each buffer if the kernel reported a larger required
		 * size. Make sure forward progress is made on every retry.
		 */
		bool grew = false;

		if (descriptor.key_string_len > key_string_capacity) {
			key_string_capacity = descriptor.key_string_len;
			grew = true;
		}

		if (descriptor.array_indexes_len > array_indexes_capacity_bytes) {
			array_indexes_capacity_bytes = descriptor.array_indexes_len;
			grew = true;
		}

		if (!grew) {
			/*
			 * The kernel returned -ENOSPC but did not request
			 * more capacity. Treat as protocol error.
			 */
			return -EBADMSG;
		}
	}

	return -EBADMSG;
}

int kernctl_counter_read(int counter_fd, struct lttng_kernel_abi_counter_read *counter_read)
{
	if (kernctl_is_extensible_counter_abi_supported()) {
		return LTTNG_IOCTL_NO_CHECK(
			counter_fd, LTTNG_KERNEL_ABI_COUNTER_READ, counter_read);
	}

	struct lttng_kernel_abi_old_counter_read old_read = {};
	auto ret = old_counter_index_from_counter_index(counter_read->index, old_read.index);
	if (ret) {
		return ret;
	}

	old_read.cpu = counter_read->cpu;
	ret = LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_OLD_COUNTER_READ, &old_read);
	if (ret == 0) {
		counter_value_from_old_counter_value(old_read.value, counter_read->value);
	}

	return ret;
}

int kernctl_counter_get_aggregate_value(int counter_fd,
					struct lttng_kernel_abi_counter_aggregate *value)
{
	if (kernctl_is_extensible_counter_abi_supported()) {
		return LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_COUNTER_AGGREGATE, value);
	}

	struct lttng_kernel_abi_old_counter_aggregate old_aggregate = {};
	auto ret = old_counter_index_from_counter_index(value->index, old_aggregate.index);
	if (ret) {
		return ret;
	}

	ret = LTTNG_IOCTL_NO_CHECK(
		counter_fd, LTTNG_KERNEL_ABI_OLD_COUNTER_AGGREGATE, &old_aggregate);
	if (ret == 0) {
		counter_value_from_old_counter_value(old_aggregate.value, value->value);
	}

	return ret;
}

int kernctl_counter_clear(int counter_fd, struct lttng_kernel_abi_counter_clear *clear)
{
	if (kernctl_is_extensible_counter_abi_supported()) {
		return LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_COUNTER_CLEAR, clear);
	}

	struct lttng_kernel_abi_old_counter_clear old_clear = {};
	const auto ret = old_counter_index_from_counter_index(clear->index, old_clear.index);
	if (ret) {
		return ret;
	}

	return LTTNG_IOCTL_NO_CHECK(counter_fd, LTTNG_KERNEL_ABI_OLD_COUNTER_CLEAR, &old_clear);
}

int kernctl_create_event_notifier(int group_fd,
				  const struct lttng_kernel_abi_event_notifier *event_notifier)
{
	return LTTNG_IOCTL_NO_CHECK(
		group_fd, LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE, event_notifier);
}

int kernctl_capture(int fd, const struct lttng_bytecode *capture)
{
	int ret;
	struct lttng_kernel_abi_capture_bytecode *kb;

	/* Translate bytecode to kernel bytecode. */
	kb = zmalloc<lttng_kernel_abi_capture_bytecode>(sizeof(*kb) + capture->len);
	if (!kb) {
		ret = -ENOMEM;
		goto end;
	}

	kb->len = capture->len;
	kb->reloc_offset = capture->reloc_table_offset;
	kb->seqnum = capture->seqnum;
	memcpy(kb->data, capture->data, capture->len);
	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_CAPTURE, kb);
	free(kb);
end:
	return ret;
}

int kernctl_filter(int fd, const struct lttng_bytecode *filter)
{
	struct lttng_kernel_abi_filter_bytecode *kb;
	uint32_t len;
	int ret;

	/* Translate bytecode to kernel bytecode */
	kb = zmalloc<lttng_kernel_abi_filter_bytecode>(sizeof(*kb) + filter->len);
	if (!kb)
		return -ENOMEM;
	kb->len = len = filter->len;
	kb->reloc_offset = filter->reloc_table_offset;
	kb->seqnum = filter->seqnum;
	memcpy(kb->data, filter->data, len);
	ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_FILTER, kb);
	free(kb);
	return ret;
}

int kernctl_add_callsite(int fd, struct lttng_kernel_abi_event_callsite *callsite)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_ADD_CALLSITE, callsite);
}

int kernctl_tracepoint_list(int fd)
{
	return compat_ioctl_no_arg(
		fd, LTTNG_KERNEL_ABI_OLD_TRACEPOINT_LIST, LTTNG_KERNEL_ABI_TRACEPOINT_LIST);
}

int kernctl_syscall_list(int fd)
{
	return LTTNG_IOCTL_NO_CHECK(fd, LTTNG_KERNEL_ABI_SYSCALL_LIST);
}

int kernctl_tracer_version(int fd, struct lttng_kernel_abi_tracer_version *v)
{
	int ret;

	if (lttng_kernel_abi_use_old_abi == -1) {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_TRACER_VERSION, v);
		if (!ret) {
			lttng_kernel_abi_use_old_abi = 0;
			goto end;
		}
		lttng_kernel_abi_use_old_abi = 1;
	}
	if (lttng_kernel_abi_use_old_abi) {
		struct lttng_kernel_abi_old_tracer_version old_v;

		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_OLD_TRACER_VERSION, &old_v);
		if (ret) {
			goto end;
		}
		v->major = old_v.major;
		v->minor = old_v.minor;
		v->patchlevel = old_v.patchlevel;
	} else {
		ret = LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_TRACER_VERSION, v);
	}

end:
	return ret;
}

int kernctl_tracer_abi_version(int fd, struct lttng_kernel_abi_tracer_abi_version *v)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_TRACER_ABI_VERSION, v);
}

void kernctl_set_tracer_abi_version(const struct lttng_kernel_abi_tracer_abi_version& version)
{
	DBG_FMT("Registering kernel tracer ABI version: version={}.{}",
		version.major,
		version.minor);

	LTTNG_ASSERT(version.major <= std::numeric_limits<std::uint16_t>::max() &&
		     version.minor <= std::numeric_limits<std::uint16_t>::max());
	registered_tracer_abi_version.store(
		encoded_tracer_abi_version(static_cast<std::uint16_t>(version.major),
					   static_cast<std::uint16_t>(version.minor)));
}

lttng_kernel_abi_tracer_abi_version kernctl_registered_tracer_abi_version()
{
	return decoded_tracer_abi_version(registered_tracer_abi_version.load());
}

int kernctl_wait_quiescent(int fd)
{
	return compat_ioctl_no_arg(
		fd, LTTNG_KERNEL_ABI_OLD_WAIT_QUIESCENT, LTTNG_KERNEL_ABI_WAIT_QUIESCENT);
}

int kernctl_buffer_flush(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH);
}

int kernctl_buffer_flush_empty(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY);
}

int kernctl_buffer_flush_or_populate_packet(
	int fd, struct lttng_kernel_abi_ring_buffer_packet_flush_or_populate_packet_args *args)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_OR_POPULATE_PACKET, args);
}

int kernctl_buffer_clear(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_CLEAR);
}

int kernctl_get_next_subbuf_metadata_check(int fd, bool *consistent)
{
	return LTTNG_IOCTL_NO_CHECK(
		fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK, consistent);
}

/* returns the version of the metadata. */
int kernctl_get_metadata_version(int fd, uint64_t *version)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION, version);
}

int kernctl_metadata_cache_dump(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP);
}

/* Buffer operations */

/* For mmap mode, readable without "get" operation */

/* returns the length to mmap. */
int kernctl_get_mmap_len(int fd, unsigned long *len)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_MMAP_LEN, len);
}

/* returns the maximum size for sub-buffers. */
int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
}

/*
 * For mmap mode, operate on the current packet (between get/put or
 * get_next/put_next).
 */

/* returns the offset of the subbuffer belonging to the mmap reader. */
int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_MMAP_READ_OFFSET, off);
}

/* returns the size of the current sub-buffer, without padding (for mmap). */
int kernctl_get_subbuf_size(int fd, unsigned long *len)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF_SIZE, len);
}

/* returns the size of the current sub-buffer, without padding (for mmap). */
int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
}

/* Get exclusive read access to the next sub-buffer that can be read. */
int kernctl_get_next_subbuf(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF);
}

/* Release exclusive sub-buffer access, move consumer forward. */
int kernctl_put_next_subbuf(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF);
}

/* snapshot */

/* Get a snapshot of the current ring buffer producer and consumer positions */
int kernctl_snapshot(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_SNAPSHOT);
}

/*
 * Get a snapshot of the current ring buffer producer and consumer positions,
 * regardless of whether or not the two positions are contained within the
 * same sub-buffer.
 */
int kernctl_snapshot_sample_positions(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS);
}

/* Get the consumer position (iteration start) */
int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
}

/* Get the producer position (iteration end) */
int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
}

/* Get exclusive read access to the specified sub-buffer position */
int kernctl_get_subbuf(int fd, unsigned long *len)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF, len);
}

/* Release exclusive sub-buffer access */
int kernctl_put_subbuf(int fd)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_PUT_SUBBUF);
}

/* Returns the timestamp begin of the current sub-buffer. */
int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
{
	return LTTNG_IOCTL_CHECK(
		fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN, timestamp_begin);
}

/* Returns the timestamp end of the current sub-buffer. */
int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END, timestamp_end);
}

/* Returns the number of discarded events in the current sub-buffer. */
int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
{
	return LTTNG_IOCTL_CHECK(
		fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED, events_discarded);
}

/* Returns the content size in the current sub-buffer. */
int kernctl_get_content_size(int fd, uint64_t *content_size)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE, content_size);
}

/* Returns the packet size in the current sub-buffer. */
int kernctl_get_packet_size(int fd, uint64_t *packet_size)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE, packet_size);
}

/* Returns the stream id of the current sub-buffer. */
int kernctl_get_stream_id(int fd, uint64_t *stream_id)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID, stream_id);
}

/* Returns the current timestamp. */
int kernctl_get_current_timestamp(int fd, uint64_t *ts)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
}

/* Returns the packet sequence number of the current sub-buffer. */
int kernctl_get_sequence_number(int fd, uint64_t *seq)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM, seq);
}

/* Returns the stream instance id. */
int kernctl_get_instance_id(int fd, uint64_t *id)
{
	return LTTNG_IOCTL_CHECK(fd, LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID, id);
}
