2 * etwdump is an extcap tool used to dump etw to pcapng
4 * Copyright 2020, Odysseus Yang
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
14 #define WS_LOG_DOMAIN "etwdump"
16 #include "extcap-base.h"
18 #include <wsutil/strtoi.h>
19 #include <wsutil/filesystem.h>
20 #include <wsutil/privileges.h>
21 #include <wsutil/please_report_bug.h>
22 #include <wsutil/wslog.h>
25 #include <wsutil/cmdarg_err.h>
30 /* extcap-interface has to be unique, or it may use wrong option output by a different extcapbin */
31 #define ETW_EXTCAP_INTERFACE "etwdump"
32 #define ETWDUMP_VERSION_MAJOR "1"
33 #define ETWDUMP_VERSION_MINOR "0"
34 #define ETWDUMP_VERSION_RELEASE "0"
37 EXTCAP_BASE_OPTIONS_ENUM
,
40 OPT_INCLUDE_UNDECIDABLE_EVENT
,
45 static struct ws_option longopts
[] = {
47 { "help", ws_no_argument
, NULL
, OPT_HELP
},
48 { "version", ws_no_argument
, NULL
, OPT_VERSION
},
49 { "iue", ws_optional_argument
, NULL
, OPT_INCLUDE_UNDECIDABLE_EVENT
},
50 { "etlfile", ws_required_argument
, NULL
, OPT_ETLFILE
},
51 { "params", ws_required_argument
, NULL
, OPT_PARAMS
},
55 int g_include_undecidable_event
= false;
57 void SignalHandler(_U_
int signal
)
59 SUPER_EVENT_TRACE_PROPERTIES super_trace_properties
= { 0 };
60 super_trace_properties
.prop
.Wnode
.BufferSize
= sizeof(SUPER_EVENT_TRACE_PROPERTIES
);
61 super_trace_properties
.prop
.Wnode
.ClientContext
= 2;
62 super_trace_properties
.prop
.Wnode
.Flags
= WNODE_FLAG_TRACED_GUID
;
63 super_trace_properties
.prop
.LoggerNameOffset
= sizeof(EVENT_TRACE_PROPERTIES
);
64 super_trace_properties
.prop
.LogFileMode
= EVENT_TRACE_REAL_TIME_MODE
;
65 /* Close trace when press CONTROL+C when running this console alone */
66 ControlTrace((TRACEHANDLE
)NULL
, LOGGER_NAME
, &super_trace_properties
.prop
, EVENT_TRACE_CONTROL_STOP
);
69 static void help(extcap_parameters
* extcap_conf
)
71 extcap_help_print(extcap_conf
);
74 static int list_config(char* interface
)
79 ws_warning("No interface specified.");
83 if (g_strcmp0(interface
, ETW_EXTCAP_INTERFACE
)) {
84 ws_warning("Interface must be %s", ETW_EXTCAP_INTERFACE
);
88 * required=true agu will be displayed before required=false on UI
90 * Empty etlfile and unempty params, read etw events from a live session with the params as the filter
91 * Unempty etlfile and empty params, read etw events from the etl file without filter
92 * Unempty etlfile and unemtpy params, read etw events from the etl file with the params as the filter
93 * Empty eltfile and empty params, invalid
95 printf("arg {number=%u}{call=--etlfile}{display=etl file}"
96 "{type=fileselect}{tooltip=Select etl file to display in Wireshark}{required=false}{group=Capture}\n",
98 printf("arg {number=%u}{call=--params}{display=filter parameters}"
99 "{type=string}{tooltip=Input providers, keyword and level filters for the etl file and live session}{group=Capture}\n",
102 * The undecidable events are those that either don't have sub-dissector or don't have anthing meaningful to display except for the EVENT_HEADER.
104 printf("arg {number=%u}{call=--iue}{display=Should undecidable events be included}"
105 "{type=boolflag}{default=false}{tooltip=Choose if the undecidable event is included}{group=Capture}\n",
108 extcap_config_debug(&inc
);
112 int main(int argc
, char* argv
[])
117 int ret
= EXIT_FAILURE
;
119 char* etlfile
= NULL
;
122 extcap_parameters
* extcap_conf
= g_new0(extcap_parameters
, 1);
124 char* help_header
= NULL
;
126 /* Initialize log handler early so we can have proper logging during startup. */
127 extcap_log_init("etwdump");
130 * Get credential information for later use.
132 init_process_policies();
135 * Attempt to get the pathname of the directory containing the
138 err_msg
= configuration_init(argv
[0], NULL
);
139 if (err_msg
!= NULL
) {
140 ws_warning("Can't get pathname of directory containing the extcap program: %s.",
145 help_url
= data_file_url("etwdump.html");
146 extcap_base_set_util_info(extcap_conf
, argv
[0], ETWDUMP_VERSION_MAJOR
, ETWDUMP_VERSION_MINOR
,
147 ETWDUMP_VERSION_RELEASE
, help_url
);
149 extcap_base_register_interface(extcap_conf
, ETW_EXTCAP_INTERFACE
, "Event Tracing for Windows (ETW) reader", 290, "DLT_ETW");
151 help_header
= ws_strdup_printf(
152 " %s --extcap-interfaces\n"
153 " %s --extcap-interface=%s --extcap-dlts\n"
154 " %s --extcap-interface=%s --extcap-config\n"
155 " %s --extcap-interface=%s --etlfile c:\\wwansvc.etl \n"
156 "--fifo=FILENAME --capture\n", argv
[0], argv
[0], ETW_EXTCAP_INTERFACE
, argv
[0], ETW_EXTCAP_INTERFACE
,
157 argv
[0], ETW_EXTCAP_INTERFACE
);
158 extcap_help_add_header(extcap_conf
, help_header
);
161 extcap_help_add_option(extcap_conf
, "--help", "print this help");
162 extcap_help_add_option(extcap_conf
, "--version", "print the version");
163 extcap_help_add_option(extcap_conf
, "--etlfile <filename>", "A etl filename");
164 extcap_help_add_option(extcap_conf
, "--iue", "Choose if undecidable event is included");
171 while ((result
= ws_getopt_long(argc
, argv
, ":", longopts
, &option_idx
)) != -1) {
174 extcap_version_print(extcap_conf
);
184 etlfile
= g_strdup(ws_optarg
);
188 /* Add params as the prefix since getopt_long will ignore the first argument always */
189 params
= ws_strdup_printf("params %s", ws_optarg
);
192 case OPT_INCLUDE_UNDECIDABLE_EVENT
:
193 g_include_undecidable_event
= true;
197 /* missing option argument */
198 ws_warning("Option '%s' requires an argument", argv
[ws_optind
- 1]);
202 /* Handle extcap specific options */
203 if (!extcap_base_parse_options(extcap_conf
, result
- EXTCAP_OPT_LIST_INTERFACES
, ws_optarg
))
205 ws_warning("Invalid option: %s", argv
[ws_optind
- 1]);
211 extcap_cmdline_debug(argv
, argc
);
213 if (extcap_base_handle_interface(extcap_conf
)) {
218 if (extcap_conf
->show_config
) {
219 ret
= list_config(extcap_conf
->interface
);
223 if (extcap_conf
->capture
) {
225 if (g_strcmp0(extcap_conf
->interface
, ETW_EXTCAP_INTERFACE
)) {
226 ws_warning("ERROR: invalid interface");
230 if (etlfile
== NULL
&& params
== NULL
)
232 ws_warning("ERROR: Both --etlfile and --params arguments are empty");
238 signal(SIGINT
, SignalHandler
);
240 switch(etw_dump(etlfile
, extcap_conf
->fifo
, params
, &ret
, &err_msg
))
242 case WTAP_OPEN_ERROR
:
243 if (err_msg
!= NULL
) {
244 ws_warning("etw_dump failed: %s.",
250 ws_warning("etw_dump failed");
253 case WTAP_OPEN_NOT_MINE
:
256 if (err_msg
!= NULL
) {
257 ws_warning("The live session didn't capture any event. Error message: %s.",
263 ws_warning("The live session didn't capture any event");
268 if (err_msg
!= NULL
) {
269 ws_warning("The file %s is not etl format. Error message: %s.",
275 ws_warning("The file %s is not etl format", etlfile
);
287 extcap_base_cleanup(&extcap_conf
);
302 * Editor modelines - https://www.wireshark.org/tools/modelines.html
307 * indent-tabs-mode: nil
310 * vi: set shiftwidth=4 tabstop=8 expandtab:
311 * :indentSize=4:tabSize=8:noTabs=true: