2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2019 EfficiOS Inc.
7 #define BT_LOG_TAG "CLI/QUERY"
10 #include <babeltrace2/babeltrace.h>
12 #include "common/common.h"
14 #include "babeltrace2-query.h"
17 void set_fail_reason(const char **fail_reason
, const char *reason
)
20 *fail_reason
= reason
;
25 bt_query_executor_query_status
cli_query(const bt_component_class
*comp_cls
,
26 const char *obj
, const bt_value
*params
,
27 bt_logging_level log_level
, const bt_interrupter
*interrupter
,
28 const bt_value
**user_result
, const char **fail_reason
)
30 const bt_value
*result
= NULL
;
31 bt_query_executor_query_status status
;
32 bt_query_executor
*query_exec
;
34 set_fail_reason(fail_reason
, "unknown error");
35 BT_ASSERT(user_result
);
36 query_exec
= bt_query_executor_create(comp_cls
, obj
, params
);
38 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
42 if (bt_query_executor_set_logging_level(query_exec
, log_level
) !=
43 BT_QUERY_EXECUTOR_SET_LOGGING_LEVEL_STATUS_OK
) {
44 BT_CLI_LOGE_APPEND_CAUSE(
45 "Cannot set query executor's logging level: "
47 bt_common_logging_level_string(log_level
));
52 if (bt_query_executor_add_interrupter(query_exec
,
54 BT_QUERY_EXECUTOR_ADD_INTERRUPTER_STATUS_OK
) {
55 BT_CLI_LOGE_APPEND_CAUSE(
56 "Cannot add interrupter to query executor.");
62 status
= bt_query_executor_query(query_exec
, &result
);
64 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
66 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
68 const uint64_t sleep_time_us
= 100000;
70 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
71 set_fail_reason(fail_reason
, "interrupted by user");
75 /* Wait 100 ms and retry */
76 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
77 "time-us=%" PRIu64
, sleep_time_us
);
79 if (usleep(sleep_time_us
)) {
80 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
81 BT_CLI_LOGW_APPEND_CAUSE(
82 "Query was interrupted by user: "
83 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
84 "query-obj=\"%s\"", comp_cls
,
85 bt_component_class_get_name(comp_cls
),
87 set_fail_reason(fail_reason
,
88 "interrupted by user");
95 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
96 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
97 set_fail_reason(fail_reason
, "interrupted by user");
102 case BT_QUERY_EXECUTOR_QUERY_STATUS_UNKNOWN_OBJECT
:
103 set_fail_reason(fail_reason
, "unknown query object");
105 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
106 set_fail_reason(fail_reason
, "not enough memory");
109 BT_LOGF("Unknown query status: status=%s",
110 bt_common_func_status_string(status
));
116 *user_result
= result
;
121 status
= BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
;
124 bt_query_executor_put_ref(query_exec
);
125 bt_value_put_ref(result
);