当前位置: 首页 > 工具软件 > LTTng > 使用案例 >

[LTTng实操]------编写Linux应用程序控制Trace过程

晏兴发
2023-12-01

前略。

简单Makefile编写

因为编译要依赖LTTng的库,所以写几行makefile 避免重复敲命令行。

LTTNG_Lib = $(shell pkg-config --cflags --libs lttng-ctl lttng-ust)

all:
	g++ my_lttng_ctl.cpp  $(LTTNG_Lib) 

clean:
	rm a.out

启动 session daemon

lttng-sessiond --daemonize

这个参考我之前发文章,有很多中方法添加在开机启动里。

目前方便起见,直接手敲了。

Note that some Linux distributions could manage the LTTng session daemon as a service. In this case, we suggest that you use the service manager to start, restart, and stop session daemons.

还有一问题是启动用户。每个用户都可以有一个daemon。

在嵌入式设备中一般只用root账号即可。在开发host中监控用户程序的话,当前用户权限即可。

Trace 过程控制API

#include <lttng/lttng.h>

判断当前是否有session daemon

/*
 * Check if a session daemon is alive.
 *
 * Return 1 if alive or 0 if not. On error, returns a negative negative LTTng
 * error code.
 */
LTTNG_EXPORT extern int lttng_session_daemon_alive(void);

start/stop trace record

/*
 * Start tracing for *all* domain(s) in the session.
 *
 * Return 0 on success else a negative LTTng error code.
 */
LTTNG_EXPORT extern int lttng_start_tracing(const char *session_name);

/*
 * Stop tracing for *all* domain(s) in the session.
 *
 * This call will wait for data availability for each domain of the session so
 * this can take an abritrary amount of time. However, when returning you have
 * the guarantee that the data is ready to be read and analyze. Use the
 * _no_wait call below to avoid this behavior.
 *
 * The session_name can't be NULL.
 *
 * Return 0 on success else a negative LTTng error code.
 */
LTTNG_EXPORT extern int lttng_stop_tracing(const char *session_name);

/*
 * Behave exactly like lttng_stop_tracing but does not wait for data
 * availability.
 */
LTTNG_EXPORT extern int lttng_stop_tracing_no_wait(const char *session_name);

检查Trace data数据是否准备好

/*
 * For a given session name, this call checks if the data is ready to be read
 * or is still being extracted by the consumer(s) (pending) hence not ready to
 * be used by any readers.
 *
 * Return 0 if there is _no_ data pending in the buffers thus having a
 * guarantee that the data can be read safely. Else, return 1 if there is still
 * traced data is pending. On error, a negative value is returned and readable
 * by lttng_strerror().
 */
LTTNG_EXPORT extern int lttng_data_pending(const char *session_name);

<lttng/session.h>

已被包含在lttng.h中。

create

/*
 * Create a tracing session using a name and an optional URL.
 *
 * If _url_ is NULL, no consumer is created for the session. The name can't be
 * NULL here.
 *
 * Return 0 on success else a negative LTTng error code.
 */
LTTNG_EXPORT extern int lttng_create_session(const char *name, const char *url);

/*
 * Create a session exclusively used for live reading.
 *
 * In this mode, the switch-timer parameter is forced for each UST channel, a
 * live-switch-timer is enabled for kernel channels, manually setting
 * switch-timer is forbidden. Synchronization beacons are sent to the relayd,
 * indexes are sent and metadata is checked for each packet.
 *
 * Name can't be NULL. If no URL is given, the default is to send the data to
 * net://127.0.0.1. The timer_interval is in usec.
 *
 * Return 0 on success else a negative LTTng error code.
 */
LTTNG_EXPORT extern int lttng_create_session_live(const char *name, const char *url,
		unsigned int timer_interval);

destory

/*
 * Destroy a tracing session.
 *
 * The session will not be usable, tracing will be stopped thus buffers will be
 * flushed.
 *
 * This call will wait for data availability for each domain of the session,
 * which can take an arbitrary amount of time. However, when returning the
 * tracing data is guaranteed to be ready to be read and analyzed.
 *
 * lttng_destroy_session_no_wait() may be used if such a guarantee is not
 * needed.
 *
 * The name can't be NULL here.
 *
 * Return 0 on success else a negative LTTng error code.
 */
LTTNG_EXPORT extern int lttng_destroy_session(const char *name);

list

/*
 * List all the tracing sessions.
 *
 * Return the number of entries of the "lttng_session" array. The caller
 * must free the returned sessions array directly using free().
 *
 * On error, a negative LTTng error code is returned.
 */
LTTNG_EXPORT extern int lttng_list_sessions(struct lttng_session **sessions);

struct lttng_session

/*
 * Basic session information.
 *
 * The "enabled" field is only used when listing the sessions which indicate if
 * it's started or not.
 *
 * The structures should be initialized to zero before use.
 */
#define LTTNG_SESSION_PADDING1             8
struct lttng_session {
	char name[LTTNG_NAME_MAX];
	/*
	 * Human-readable representation of the trace's destination.
	 * In the case of a local tracing session, a path is provided:
	 *     /path/to/the/output
	 *
	 * In the case of a remote (network) tracing session, the string has
	 * the following format:
	 *     net://hostname/path:ctrl_port [data: data_port]
	 */
	char path[PATH_MAX];
	uint32_t enabled;	/* enabled/started: 1, disabled/stopped: 0 */
	uint32_t snapshot_mode;
	unsigned int live_timer_interval;	/* usec */

	/*
	 * End of public attributes.
	 * The remaining fields are used to deal with ABI management concerns.
	 */

	/*
	 * 32-bit architectures are already naturally aligned on 4 bytes after
	 * 'live_timer_interval'. However, the offset does not result in a
	 * natural alignment on 64-bit architectures. Adding 4 bytes of
	 * padding here results in an aligned offset after 'alignement_padding'
	 * for both bitnesses.
	 *
	 * This was added since not all compilers appear to align unions in the
	 * same way. Some (e.g. MSVC) do not seem to impose an alignement
	 * constraint while others (e.g. gcc, clang, icc) seem to align it to
	 * ensure 'ptr' is naturally aligned.
	 */
	char alignment_padding[4];
	union {
		/*
		 * Ensure the 'extended' union has the same size for both
		 * 32-bit and 64-bit builds.
		 */
		char padding[LTTNG_SESSION_PADDING1];
		void *ptr;
	} extended;
};

<lttng/event.h>

enable & disable event

/*
 * Create or enable an event (or events) for a channel.
 *
 * If the event you are trying to enable does not exist, it will be created,
 * else it is enabled. If channel_name is NULL, the default channel is used
 * (channel0).
 *
 * The handle and ev params can not be NULL.
 *
 * Return 0 on success else a negative LTTng error code.
 */
extern int lttng_enable_event(struct lttng_handle *handle,
		struct lttng_event *ev, const char *channel_name);

/*
 * Create or enable an event with a specific filter.
 *
 * If the event you are trying to enable does not exist, it will be created,
 * else it is enabled.
 * If ev is NULL, all events are enabled with that filter.
 * If channel_name is NULL, the default channel is used (channel0) and created
 * if not found.
 * If filter_expression is NULL, an event without associated filter is
 * created.
 *
 * Return 0 on success else a negative LTTng error code.
 */
extern int lttng_enable_event_with_filter(struct lttng_handle *handle,
		struct lttng_event *event, const char *channel_name,
		const char *filter_expression);

/*
 * Create or enable an event with a filter and/or exclusions.
 *
 * If the event you are trying to enable does not exist, it will be created,
 * else it is enabled.
 * If ev is NULL, all events are enabled with the filter and exclusion options.
 * If channel_name is NULL, the default channel is used (channel0) and created
 * if not found.
 * If filter_expression is NULL, an event without associated filter is
 * created.
 * If exclusion count is zero, the event will be created without exclusions.
 *
 * Return 0 on success else a negative LTTng error code.
 */
extern int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
		struct lttng_event *event, const char *channel_name,
		const char *filter_expression,
		int exclusion_count, char **exclusion_names);

/*
 * Disable event(s) of a channel and domain.
 *
 * If name is NULL, all events are disabled.
 * If channel_name is NULL, the default channel is used (channel0).
 *
 * Return 0 on success else a negative LTTng error code.
 */
extern int lttng_disable_event(struct lttng_handle *handle,
		const char *name, const char *channel_name);

/*
 * Disable event(s) of a channel and domain.
 *
 * Takes a struct lttng_event as parameter.
 * If channel_name is NULL, the default channel is used (channel0).
 *
 * Currently, @filter_expression must be NULL. (disabling specific
 * filter expressions not implemented)
 * Currently, only LTTNG_EVENT_ALL and LTTNG_EVENT_SYSCALL event types
 * are implemented for field @ev.
 *
 * Return 0 on success else a negative LTTng error code.
 */
extern int lttng_disable_event_ext(struct lttng_handle *handle,
		struct lttng_event *ev, const char *channel_name,
		const char *filter_expression);

Trace 过程控制必须要有的步骤

1、开启 session daemon

指令:

lttng-sessiond --daemonize

2、create a recording session

指令:

lttng create my-session --output=/tmp/some-directory

API:

LTTNG_EXPORT extern int lttng_create_session(const char *name, const char *url);

参数可以传自定义的名字和绝对地址(相对我没试过)两个字符串常量。

3、使能 event rules

指令:

lttng enable-event --kernel --syscall open,write,read,close

lttng enable-event --userspace my_app:'*' \
                   --exclude=my_app:set_user,my_app:handle_sig

API:

extern int lttng_enable_event(struct lttng_handle *handle,
		struct lttng_event *ev, const char *channel_name);

extern int lttng_enable_event_with_filter(struct lttng_handle *handle,
		struct lttng_event *event, const char *channel_name,
		const char *filter_expression);

extern int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
		struct lttng_event *event, const char *channel_name,
		const char *filter_expression,
		int exclusion_count, char **exclusion_names);

在代码实现中,前 两个接口是后面那个(除了…使能所有)接口 的套壳。

参数需要自己定义,下面是原型以供参考:

/*
 * Handle used as a context for commands.
 *
 * The structures should be initialized to zero before use.
 */
#define LTTNG_HANDLE_PADDING1              16
struct lttng_handle {
	char session_name[LTTNG_NAME_MAX];
	struct lttng_domain domain;

	char padding[LTTNG_HANDLE_PADDING1];
};

/*
 * Generic lttng event
 *
 * The structures should be initialized to zero before use.
 */
#define LTTNG_EVENT_PADDING1               12
#define LTTNG_EVENT_PADDING2               LTTNG_SYMBOL_NAME_LEN + 32
struct lttng_event {
	/* Offset 0 */
	enum lttng_event_type type;

	/* Offset 4 */
	char name[LTTNG_SYMBOL_NAME_LEN];

	/* Offset 260 */
	enum lttng_loglevel_type loglevel_type;

	/* Offset 264 */
	int loglevel;

	/* Offset 268 */
	int32_t enabled;	/* Does not apply: -1 */

	/* Offset 272 */
	pid_t pid;

	/* Offset 276 */
	unsigned char filter;	/* filter enabled ? */

	/* Offset 277 */
	unsigned char exclusion; /* exclusions added ? */

	/* Offset 278 */
	char padding2[2];

	/* Offset 280 */
	/* Event flag, from 2.6 and above. */
	enum lttng_event_flag flags;

	/* Offset 284 */
	char padding[4];

	/* Offset 288 */
	union {
		uint64_t padding;
		void *ptr;
	} extended;

	/* Offset 296 */
	/* Per event type configuration */
	union {
		struct lttng_event_probe_attr probe;
		struct lttng_event_function_attr ftrace;

		char padding[LTTNG_EVENT_PADDING2];
	} attr;
};

我爬了一下源码,似乎lttng_event这个结构体 ,name的部分。是一次一个。也就是说名字是单个event的名字,以"\0"结束符结尾了。对于命令行中多个参数以逗号隔开的输入,是使用了字符串拆分和循环。我看到不是很仔细,先暂且记录一下。

写代码我无所谓,一行写一个好了。

对于ev这个参数能不能是NULL,API的注释居然出现了冲突。

有待实验。保险起见不要设NULL。显示化设置一个。

4、Start and stop a recording session

指令:

lttng start

lttng stop

API:

LTTNG_EXPORT extern int lttng_start_tracing(const char *session_name);


LTTNG_EXPORT extern int lttng_stop_tracing(const char *session_name);

5、destroy record session

指令:

lttng destroy

API:

LTTNG_EXPORT extern int lttng_destroy_session(const char *name);

session daemon 不归 liblttng-ctl管,如果要杀,直接Kill。。。实际上可能也没必要。因为已经后台服务化了。

 类似资料: