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
;
24 bt_query_executor_query_status
cli_query(const bt_component_class
*comp_cls
,
25 const char *obj
, const bt_value
*params
,
26 bt_logging_level log_level
, const bt_interrupter
*interrupter
,
27 const bt_value
**user_result
, const char **fail_reason
)
29 const bt_value
*result
= NULL
;
30 bt_query_executor_query_status status
;
31 bt_query_executor
*query_exec
;
33 set_fail_reason(fail_reason
, "unknown error");
34 BT_ASSERT(user_result
);
35 query_exec
= bt_query_executor_create(comp_cls
, obj
, params
);
37 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
41 if (bt_query_executor_set_logging_level(query_exec
, log_level
) !=
42 BT_QUERY_EXECUTOR_SET_LOGGING_LEVEL_STATUS_OK
) {
43 BT_CLI_LOGE_APPEND_CAUSE(
44 "Cannot set query executor's logging level: "
46 bt_common_logging_level_string(log_level
));
51 if (bt_query_executor_add_interrupter(query_exec
,
53 BT_QUERY_EXECUTOR_ADD_INTERRUPTER_STATUS_OK
) {
54 BT_CLI_LOGE_APPEND_CAUSE(
55 "Cannot add interrupter to query executor.");
61 status
= bt_query_executor_query(query_exec
, &result
);
63 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
65 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
67 const uint64_t sleep_time_us
= 100000;
69 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
70 set_fail_reason(fail_reason
, "interrupted by user");
74 /* Wait 100 ms and retry */
75 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
76 "time-us=%" PRIu64
, sleep_time_us
);
78 if (usleep(sleep_time_us
)) {
79 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
80 BT_CLI_LOGW_APPEND_CAUSE(
81 "Query was interrupted by user: "
82 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
83 "query-obj=\"%s\"", comp_cls
,
84 bt_component_class_get_name(comp_cls
),
86 set_fail_reason(fail_reason
,
87 "interrupted by user");
94 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
95 if (interrupter
&& bt_interrupter_is_set(interrupter
)) {
96 set_fail_reason(fail_reason
, "interrupted by user");
101 case BT_QUERY_EXECUTOR_QUERY_STATUS_UNKNOWN_OBJECT
:
102 set_fail_reason(fail_reason
, "unknown query object");
104 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
105 set_fail_reason(fail_reason
, "not enough memory");
108 BT_LOGF("Unknown query status: status=%s",
109 bt_common_func_status_string(status
));
115 *user_result
= result
;
120 status
= BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
;
123 bt_query_executor_put_ref(query_exec
);
124 bt_value_put_ref(result
);