From patchwork Mon Jun 26 09:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13292563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA12FEB64DA for ; Mon, 26 Jun 2023 09:20:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjFZJUR (ORCPT ); Mon, 26 Jun 2023 05:20:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjFZJTd (ORCPT ); Mon, 26 Jun 2023 05:19:33 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 247A42D76 for ; Mon, 26 Jun 2023 02:16:57 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-98bcc533490so366585966b.0 for ; Mon, 26 Jun 2023 02:16:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687771015; x=1690363015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yEPcTxjaEjVnJ9EdnwR9/7qwFtqWTq8VdOb5lliXdnI=; b=R0QV53+GmTrXlVzpVPtBo1DEGsQ7a1NeusS66sHQqRPibFC41gkNNVR1CQCpgKCjYy +TJ7oABkRNEAarcw87j4SoY1zFMU+0AaW1GUG0gqQZNK7ap+IyJhJoWLrGXTGNfGqDx0 K2N3GTKp+W8I7L5w8St3L9+vTHPxwy4px+CUaHT0a1IDGFiJNyCR3nP1WbV95/cNv6d+ uG4FxO9N/dEA0sUKe+UZpr8inAHRRZns5Jm+8n3eZqPg85n9h9jGyFmafIWXUvG12+uF +8WXMxYknyp+2dlmhvrrMc0dwO3OMFfSMAmZOuk7g9wqiiA5JGL/lKmycgHj4c4Gd/Qw 1jIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687771015; x=1690363015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yEPcTxjaEjVnJ9EdnwR9/7qwFtqWTq8VdOb5lliXdnI=; b=MJukQGNh4qx92Uv6lWhjiD17bKLMkvAsL6gpTXgQglE7Vdvs+St05G6M7Q5Taa54nh a0C0q3x0zXTyDKRmfgTMJpE7vUtcsYbqMDGwPsHhzenZZtxYYRU+Uxf0g6QueESeJBe4 /1bz/WAPVVxkVLF7FntkqCNSdZ2o69so893ivJOYcy9VsGmuk59cn9QDEdQZx7kwrcVP 2RDxdiSkHfzPW5zV4O6qG5ur/RpbmnikoC/BoxAgqIuepZsgfyXBvCxfEGCC5uREbyee VYhzh5H2A6mgTi12vkwITrd0j0JoxCIQ5NZS87Nf4mcBjpgUFtyST4Xs+epTOCe6/7hB S1Ug== X-Gm-Message-State: AC+VfDzYGhNfwt/j3vKa/Oc9BOAyPj/jpBzxvR7c+GZLJMijr0fE589u 8A9VZY4mEfRpv7/vjoPJ4WgTPHLG1WMXcA== X-Google-Smtp-Source: ACHHUZ58i/BSZbf8uQyUPOS4H+ahkKf4A5l9l94a9CUXDq6KJf8p90qfCBUGJBHv9A7pbXz2uAQDlA== X-Received: by 2002:a17:907:a03:b0:982:2278:bcef with SMTP id bb3-20020a1709070a0300b009822278bcefmr27242635ejc.60.1687771015161; Mon, 26 Jun 2023 02:16:55 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id lc1-20020a170906f90100b00988e953a586sm3061414ejb.61.2023.06.26.02.16.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 02:16:54 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: rostedt@goodmis.org, avidanborisov@gmail.com Subject: [PATCH v2 1/4] trace-cmd record: Add --daemonize Date: Mon, 26 Jun 2023 12:16:32 +0300 Message-Id: <20230626091635.3002827-2-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230626091635.3002827-1-avidanborisov@gmail.com> References: <20230530045119.30c8d973@rorschach.local.home> <20230626091635.3002827-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov Add a --daemonize flag to trace-cmd record which allows the user to record in the background, after tracing has completely initialized. Signed-off-by: Avidan Borisov --- .../trace-cmd/trace-cmd-record.1.txt | 3 + tracecmd/trace-record.c | 129 +++++++++++++++++- tracecmd/trace-usage.c | 1 + 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/Documentation/trace-cmd/trace-cmd-record.1.txt b/Documentation/trace-cmd/trace-cmd-record.1.txt index b709e48..366ab46 100644 --- a/Documentation/trace-cmd/trace-cmd-record.1.txt +++ b/Documentation/trace-cmd/trace-cmd-record.1.txt @@ -418,6 +418,9 @@ OPTIONS run on a privileged guest that the host is aware of (as denoted by the 'cid' in the *-P* option for the agent). +*--daemonize* + Run trace-cmd in the background as a daemon after recording has started. + EXAMPLES -------- diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index bced804..0049768 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -91,6 +91,8 @@ static bool quiet; static bool fork_process; +static bool do_daemonize; + /* Max size to let a per cpu file get */ static int max_kb; @@ -1634,6 +1636,109 @@ static inline void ptrace_attach(struct buffer_instance *instance, int pid) { } #endif /* NO_PTRACE */ +static bool child_detached; + +static void daemonize_set_child_detached(int s) +{ + child_detached = true; +} + +static void daemonize_start(void) +{ + int devnull; + int status; + int pid; + int rc; + + pid = fork(); + if (pid == -1) + die("daemonize: fork failed"); + + if (pid == 0) { /* child */ + /* + * We keep stdout and stderr open to allow the user to + * see output and errors after the daemonization (the user can + * choose to supress it with >/dev/null if the user wants). + * + * No reason to keep stdin open (it might interfere with the + * shell), we redirect it to /dev/null. + */ + devnull = open("/dev/null", O_RDONLY); + if (devnull == -1) + die("daemonize: open /dev/null failed"); + + if (devnull > 0) { + if (dup2(devnull, 0) == -1) + die("daemonize: dup2"); + close(0); + } + + return; + + /* + * The child returns to back to the caller, but the parent waits until + * SIGRTMIN is received from the child (by calling daemonize_finish()), + * or the child exits for some reason (usually an indication of + * an error), which ever comes first. + * + * Then the parent exits (with the status code of the child, + * if it finished early, or with 0 if SIGRTMIN was received), + * which causes the child (and its entire process tree) to be + * inherited by init. + * + * Note that until the child calls daemonize_finish(), it still has + * the same session id as the parent, so it can die together with + * the parent before daemonization finished (purposefully, since the + * user might send a quick Ctrl^C to cancel the command, and we don't + * want background processes staying alive in that case) + */ + } else { /* parent */ + struct sigaction sa = { + /* disable SA_RESTART, to allow waitpid() to be interrupted by SIGRTMIN */ + .sa_flags = 0, + .sa_handler = daemonize_set_child_detached + }; + + if (sigemptyset(&sa.sa_mask) == -1) + die("daemonize: sigemptyset failed"); + + if (sigaddset(&sa.sa_mask, SIGRTMIN) == -1) + die("daemonize: sigaddset failed"); + + if (sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL) == -1) + die("daemonize: sigprocmask failed"); + + if (sigaction(SIGRTMIN, &sa, NULL) == -1) + die("daemonize: sigaction failed"); + + do { + rc = waitpid(pid, &status, 0); + } while (!child_detached && ((rc == -1) && (errno == EINTR))); + + if (child_detached) + exit(0); + else if (rc == pid) + exit(WIFEXITED(status)); + else + die("daemonize: waitpid failed"); + + __builtin_unreachable(); + } +} + +static void daemonize_finish(void) +{ + /* + * setsid() will also set the sid to be the pgid to all currently + * running threads in the process group (such as the tsync thread). + */ + if (setsid() == -1) + die("daemonize: setsid"); + + if (kill(getppid(), SIGRTMIN) == -1) + die("daemonize: kill"); +} + static void trace_or_sleep(enum trace_type type, bool pwait) { int i; @@ -1748,6 +1853,9 @@ static void run_cmd(enum trace_type type, const char *user, int argc, char **arg execute_program(argc, argv); } + + if (do_daemonize) + daemonize_finish(); if (fork_process) exit(0); if (do_ptrace) { @@ -5790,6 +5898,7 @@ enum { OPT_proxy = 261, OPT_temp = 262, OPT_notimeout = 264, + OPT_daemonize = 265, }; void trace_stop(int argc, char **argv) @@ -6217,6 +6326,7 @@ static void parse_record_options(int argc, {"file-version", required_argument, NULL, OPT_file_ver}, {"proxy", required_argument, NULL, OPT_proxy}, {"temp", required_argument, NULL, OPT_temp}, + {"daemonize", no_argument, NULL, OPT_daemonize}, {NULL, 0, NULL, 0} }; @@ -6690,6 +6800,11 @@ static void parse_record_options(int argc, die("--fork option used for 'start' command only"); fork_process = true; break; + case OPT_daemonize: + if (!IS_RECORD(ctx)) + die("--daemonize option used for 'record' command only"); + do_daemonize = true; + break; case OPT_tsc2nsec: ret = get_tsc_nsec(&ctx->tsc2nsec.shift, &ctx->tsc2nsec.mult); @@ -6933,6 +7048,9 @@ static void record_trace(int argc, char **argv, struct buffer_instance *instance; struct filter_pids *pid; + if (do_daemonize) + daemonize_start(); + /* * If top_instance doesn't have any plugins or events, then * remove it from being processed. @@ -7051,8 +7169,15 @@ static void record_trace(int argc, char **argv, } } } - /* sleep till we are woken with Ctrl^C */ - printf("Hit Ctrl^C to stop recording\n"); + + if (do_daemonize) { + daemonize_finish(); + printf("Send SIGINT to pid %d to stop recording\n", getpid()); + } else { + /* sleep till we are woken with Ctrl^C */ + printf("Hit Ctrl^C to stop recording\n"); + } + for_all_instances(instance) { /* If an instance is not tracing individual processes * or there is an error while waiting for a process to diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c index ecb7cad..45cb4f0 100644 --- a/tracecmd/trace-usage.c +++ b/tracecmd/trace-usage.c @@ -81,6 +81,7 @@ static struct usage_help usage_help[] = { " available algorithms can be listed with trace-cmd list -c\n" " --proxy vsocket to reach the agent. Acts the same as -A (for an agent)\n" " but will send the proxy connection to the agent.\n" + " --daemonize run trace-cmd in the background as a daemon after recording has started\n" }, { "set", From patchwork Mon Jun 26 09:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13292562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40D80EB64D7 for ; Mon, 26 Jun 2023 09:20:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229684AbjFZJUQ (ORCPT ); Mon, 26 Jun 2023 05:20:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjFZJTe (ORCPT ); Mon, 26 Jun 2023 05:19:34 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56F8A2D77 for ; Mon, 26 Jun 2023 02:16:58 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-987341238aeso364183866b.3 for ; Mon, 26 Jun 2023 02:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687771016; x=1690363016; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=43aJdWgXMNMLJqR8pernzt03otstqW01zHTf041szd8=; b=ZWtQjA+XiVaiw8LwY5XLxpTUgjl9mJtZ4Epwdvke02ZqQcMU+RhqfS5Xv9r7UplPyV P9fWn9iCXcTT+r3YMo8R8Ea7GTYRjC5ARBWI3kfCWZBDeyKr7FxSuDm87nftcLcXZepY i13A5icUe7Id/bjr452WMcbZt5h6Q9JGbxvQfwCL9tV5Xt/QMdPNh/8acUy5cr6jtKkh 7LyNImIpUmLR3qbPAMMqQsuRk15n5ynamv7gOG7ZzTkC2eqUvL4EnVs4NqN41IKwzWn5 q09bcPYwzF4h19cgDooG1EcrpV/OcRoiOZiIWffGZEiaXcegsDue7IggTzC6UlxLSf0B X2Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687771016; x=1690363016; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=43aJdWgXMNMLJqR8pernzt03otstqW01zHTf041szd8=; b=FhOXaIyJo0JYl3ol6nn4E0vhQHs5ZLDXYCLQJstMWDlkUt0RIjyJ9prLiy/XAytXeo aaxGS2iBI9hrti67QexALM6awc2tIwPsnYZv9j7UA6ASMiJThVD6Dlso9TWsXtvKn393 +OkrHhckX09i68iyZtsewMmTOTRe9G72zzm8Jl+XOz+OBA1U4ONOVLDI5K2MsqEIOY5J B7JsRs8/mAVyL5Y+Qxe308VhQTZftZsSiYK3jUHG3ZR+SmgHq8k5/tOEuCvjyt0Fb0v8 ucwbPsFYjTpCPbX/3sjQy/RYOhkybmMZhiqB5QgL94d6mNNqmaD5wUiJ1xZVsqrRzGdP Z1aw== X-Gm-Message-State: AC+VfDxzWgrQa3f2w7UX9rDPTwKG+Cj6XBT+VUTufWZ7AAMCDnIph1sK VcRvoUELVa+VPg3tr6qXazTX6JXmyEi7vQ== X-Google-Smtp-Source: ACHHUZ4NXaiCwerbuwywLhtpc3CzIMRtlHZ79UvjZkJJ4bIReo+Tk6dZQnY3UasK63Y+Al9dptNQuQ== X-Received: by 2002:a17:907:72c3:b0:988:cb57:f741 with SMTP id du3-20020a17090772c300b00988cb57f741mr16765134ejc.54.1687771016464; Mon, 26 Jun 2023 02:16:56 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id lc1-20020a170906f90100b00988e953a586sm3061414ejb.61.2023.06.26.02.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 02:16:56 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: rostedt@goodmis.org, avidanborisov@gmail.com Subject: [PATCH v2 2/4] trace-cmd: export pidfile functions from trace-listen.c Date: Mon, 26 Jun 2023 12:16:33 +0300 Message-Id: <20230626091635.3002827-3-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230626091635.3002827-1-avidanborisov@gmail.com> References: <20230530045119.30c8d973@rorschach.local.home> <20230626091635.3002827-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov trace-listen.c has some utility functions for creating and removing pidfiles, to avoid code duplication we make those functions generic and export them to the rest of the codebase. Signed-off-by: Avidan Borisov --- tracecmd/include/trace-local.h | 4 ++++ tracecmd/trace-listen.c | 32 ++++++++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index 8f18f6d..33397b1 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -461,4 +461,8 @@ static inline bool is_digits(const char *s) bool trace_tsc2nsec_is_supported(void); +void make_pid_name(char *buf, const char *pidfile_basename); +void remove_pid_file(const char *pidfile_basename); +void make_pid_file(const char *pidfile_basename); + #endif /* __TRACE_LOCAL_H */ diff --git a/tracecmd/trace-listen.c b/tracecmd/trace-listen.c index e95c571..5894a92 100644 --- a/tracecmd/trace-listen.c +++ b/tracecmd/trace-listen.c @@ -34,6 +34,8 @@ #define VAR_RUN_DIR VAR_DIR_Q(VAR_DIR) "/run" +#define LISTEN_PIDFILE "trace-cmd-net.pid" + static char *default_output_dir = "."; static char *output_dir; static char *default_output_file = "trace"; @@ -52,7 +54,8 @@ static bool done; #define pdie(fmt, ...) \ do { \ tracecmd_plog_error(fmt, ##__VA_ARGS__);\ - remove_pid_file(); \ + if (do_daemon) \ + remove_pid_file(LISTEN_PIDFILE);\ exit(-1); \ } while (0) @@ -126,21 +129,16 @@ static void finish(int sig) done = true; } -static void make_pid_name(int mode, char *buf) +void make_pid_name(char *buf, const char *pidfile_basename) { - snprintf(buf, PATH_MAX, VAR_RUN_DIR "/trace-cmd-net.pid"); + snprintf(buf, PATH_MAX, VAR_RUN_DIR "/%s", pidfile_basename); } -static void remove_pid_file(void) +void remove_pid_file(const char *pidfile_basename) { char buf[PATH_MAX]; - int mode = do_daemon; - - if (!do_daemon) - return; - - make_pid_name(mode, buf); + make_pid_name(buf, pidfile_basename); unlink(buf); } @@ -991,16 +989,12 @@ static void do_accept_loop(int sfd) clean_up(); } -static void make_pid_file(void) +void make_pid_file(const char *pidfile_basename) { char buf[PATH_MAX]; - int mode = do_daemon; int fd; - if (!do_daemon) - return; - - make_pid_name(mode, buf); + make_pid_name(buf, pidfile_basename); fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { @@ -1075,7 +1069,8 @@ static void do_listen(char *port) if (!tracecmd_get_debug()) signal_setup(SIGCHLD, sigstub); - make_pid_file(); + if (do_daemon) + make_pid_file(LISTEN_PIDFILE); if (use_vsock) sfd = get_vsock(port); @@ -1090,7 +1085,8 @@ static void do_listen(char *port) kill_clients(); - remove_pid_file(); + if (do_daemon) + remove_pid_file(LISTEN_PIDFILE); } static void start_daemon(void) From patchwork Mon Jun 26 09:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13292560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35710EB64DA for ; Mon, 26 Jun 2023 09:20:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229574AbjFZJUO (ORCPT ); Mon, 26 Jun 2023 05:20:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229579AbjFZJTh (ORCPT ); Mon, 26 Jun 2023 05:19:37 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB9A12D7C for ; Mon, 26 Jun 2023 02:16:59 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-98d34f1e54fso286828366b.2 for ; Mon, 26 Jun 2023 02:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687771018; x=1690363018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ssvwn1tdcdG6G287S2LoeqKIHY9bjql6tD9NTTbWJyE=; b=LO8jO+mdJzNhfLZo7y7HRoK4yBuixH9A4QOnagXLtlO6flL2kmSItbhJzt8QHvQSm+ 28MNzzbY6wBbLHM/8yPQAB87eD0YXxqj3NEv71NFJeGLkcJyfiMkPPc+PgFX0y1Z79qw 6Zy2Vy1MsamdJS8JTmIPh7M7xI1l3wcqAhnxUttZUthCqgN8wHQn8VWiasx5219pKujU x4HxcW3//LQQCom2TGW510h9JQntvLRelBql+5dMhOEdeoW71IsjpcqRj1xDTam1I50e Jv0J9TW7liYlHTKx++BQKCIakLfMqHziPrrtyozK7/P682f3GiASOE/9lw7Naamg8Yvz VNVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687771018; x=1690363018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ssvwn1tdcdG6G287S2LoeqKIHY9bjql6tD9NTTbWJyE=; b=BZ25yPM8xIuqeA+4TiKL895un6cy/nbJDKb4HxNVBkoOmpKTO8FWbvl2SDNDfX/TbZ ccLiSHo6HtBKfeWYxu7GkgCSGMl9hO7gMGR+GwEr6QOIla7FPlX2n4p3ErbDnDT5VceV opGf3pPP9FIFF1Db+8FcrJqEC0CO2R0LQZKXeX0sZY6WGcu1QxmIcywoX8Okm/LOctld mi40RgKMxnbdF3wRyzGyQLtESum0tb6EMSzPay4D5sMpvfeCWRGbSUP94WUcp4vTe5ez 5kbc4JIT1s9fK8BzhBPHRnexEeYCAG+WIPLue29dpnliyzJR1H1YlrXD0UQFrvI/GuV7 4XGQ== X-Gm-Message-State: AC+VfDyqiVQQR3Z01OO6lk+86abaREQ7Mqqv9xvoYV95R6yD20PFEiad GrGzokbNFTp9JPRduhsZCziisTrTcRVOAQ== X-Google-Smtp-Source: ACHHUZ5flIxiNKJB9oqO43ijy/K4zhWn20QkmKHCo7ZC8D7W8dAZspJiYDoydjOR76/EJOHZ37vHWA== X-Received: by 2002:a17:907:1628:b0:991:eb77:74e with SMTP id hb40-20020a170907162800b00991eb77074emr451567ejc.76.1687771017929; Mon, 26 Jun 2023 02:16:57 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id lc1-20020a170906f90100b00988e953a586sm3061414ejb.61.2023.06.26.02.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 02:16:57 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: rostedt@goodmis.org, avidanborisov@gmail.com Subject: [PATCH v2 3/4] trace-cmd record: Create a pidfile when using --daemonize Date: Mon, 26 Jun 2023 12:16:34 +0300 Message-Id: <20230626091635.3002827-4-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230626091635.3002827-1-avidanborisov@gmail.com> References: <20230530045119.30c8d973@rorschach.local.home> <20230626091635.3002827-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov When used with --daemonize, create a pidfile at /var/run/trace-cmd-record.pid during the duration of the recording, and delete it after we're done. Signed-off-by: Avidan Borisov --- Documentation/trace-cmd/trace-cmd-record.1.txt | 1 + tracecmd/trace-record.c | 13 +++++++++++++ tracecmd/trace-usage.c | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Documentation/trace-cmd/trace-cmd-record.1.txt b/Documentation/trace-cmd/trace-cmd-record.1.txt index 366ab46..0c9a914 100644 --- a/Documentation/trace-cmd/trace-cmd-record.1.txt +++ b/Documentation/trace-cmd/trace-cmd-record.1.txt @@ -420,6 +420,7 @@ OPTIONS *--daemonize* Run trace-cmd in the background as a daemon after recording has started. + Creates a pidfile at /var/run/trace-cmd-record.pid with the pid of trace-cmd during the recording. EXAMPLES -------- diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 0049768..69a10bf 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -46,6 +46,8 @@ #define _STR(x) #x #define STR(x) _STR(x) +#define RECORD_PIDFILE "trace-cmd-record.pid" + #define TRACE_CTRL "tracing_on" #define TRACE "trace" #define AVAILABLE "available_tracers" @@ -93,6 +95,8 @@ static bool fork_process; static bool do_daemonize; +static bool created_pidfile; + /* Max size to let a per cpu file get */ static int max_kb; @@ -646,6 +650,9 @@ void die(const char *fmt, ...) else ret = -1; + if (created_pidfile) + remove_pid_file(RECORD_PIDFILE); + kill_threads(); va_start(ap, fmt); fprintf(stderr, " "); @@ -1737,6 +1744,9 @@ static void daemonize_finish(void) if (kill(getppid(), SIGRTMIN) == -1) die("daemonize: kill"); + + make_pid_file(RECORD_PIDFILE); + created_pidfile = true; } static void trace_or_sleep(enum trace_type type, bool pwait) @@ -7225,6 +7235,9 @@ static void record_trace(int argc, char **argv, destroy_stats(); finalize_record_trace(ctx); + + if (created_pidfile) + remove_pid_file(RECORD_PIDFILE); } /* diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c index 45cb4f0..37d576b 100644 --- a/tracecmd/trace-usage.c +++ b/tracecmd/trace-usage.c @@ -81,7 +81,9 @@ static struct usage_help usage_help[] = { " available algorithms can be listed with trace-cmd list -c\n" " --proxy vsocket to reach the agent. Acts the same as -A (for an agent)\n" " but will send the proxy connection to the agent.\n" - " --daemonize run trace-cmd in the background as a daemon after recording has started\n" + " --daemonize run trace-cmd in the background as a daemon after recording has started.\n" + " creates a pidfile at /var/run/trace-cmd-record.pid with the pid of trace-cmd\n" + " during the recording.\n" }, { "set", From patchwork Mon Jun 26 09:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13292564 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2F8DEB64D7 for ; Mon, 26 Jun 2023 09:20:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229554AbjFZJUS (ORCPT ); Mon, 26 Jun 2023 05:20:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbjFZJTi (ORCPT ); Mon, 26 Jun 2023 05:19:38 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25CC410DE for ; Mon, 26 Jun 2023 02:17:01 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-98e109525d6so290648266b.0 for ; Mon, 26 Jun 2023 02:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687771019; x=1690363019; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3jajLnawW651XBwfEFnMq3+Y5XmyzQflq7MAKne2Mc0=; b=cTZiPmukD0o4ks0Y2Y5d9us+W8G4cTt3uiJomhTWjv5zZg9LZE+gk9fyUO6oqTgyOB xqQOUKFSSeAZoKpLKNOJilfam2C8NynTPWzKPTU5JYO9zmonjT9awTlp2rAVCXjBfFus rReCBBQUMrnA1g+exwBBJhFiQRO1LC1b708QPgzwqVdPXfy5SwolaLLUlmTabMzMkAEl VYj8rbzeKmqLjaM2DigmIbtjIbNv+OkXkQLjs47+xD8IGFluZpsWhPaiOHeKBm4xGwRo KtZTpMMNakA54+wOGURv5f7V4cp7qPkNGkLtt8Bt+ZtZI0fkiUPkhKMHD6f8dA4xfbAV aGIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687771019; x=1690363019; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3jajLnawW651XBwfEFnMq3+Y5XmyzQflq7MAKne2Mc0=; b=bw5w00IjxjQ4ssj89A5W9/uyN7M0OEzLqsKa9F5wG//MVPwzR6ZV2L+ffsI0xGZxud h3aFJA8Ct0NBKsuPIjB+zCxTn3UpJJqQkBBmpCxQeaWCNI2P3BoisHIwiDcuTLf2z8kC 94w2tUaZLZBlB9MtlomNpVorLhZOwwjNk9PdcyH9aF53ea2aiIpQKqD2LkcBKodwrNW8 wA/aMIGfX6uXGbmceuOMFXbYNwwJL/1JuoC3SIlg2i2MNsDaMsaK3pur1VSWTLWJIAg4 4eC2wwiQvFduYJ1YvvnPeFqGfKJuu1aR81l7Qz/fzX29rVpyx/OqcLBNXV9aeclq+pFD SOJQ== X-Gm-Message-State: AC+VfDwvOCMLMVmq7XvzsaWZnyKAii5onV/ZOmtUjoc0oMCQCbna4bZR +jr8Pl6QtP0vLcLQ9klRxDLQXJ1AH788hA== X-Google-Smtp-Source: ACHHUZ7fo9BzTnXjr8aYuqaM53bAq/yHdsQ9mmtUZWp+ZWZtWCDUU9MfGZ3Og2ehBVeSOhDWPfidrQ== X-Received: by 2002:a17:907:9483:b0:989:4952:bfa5 with SMTP id dm3-20020a170907948300b009894952bfa5mr14269499ejc.30.1687771019290; Mon, 26 Jun 2023 02:16:59 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id lc1-20020a170906f90100b00988e953a586sm3061414ejb.61.2023.06.26.02.16.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 02:16:59 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: rostedt@goodmis.org, avidanborisov@gmail.com Subject: [PATCH v2 4/4] trace-cmd record: Add --daemonize example to man page Date: Mon, 26 Jun 2023 12:16:35 +0300 Message-Id: <20230626091635.3002827-5-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230626091635.3002827-1-avidanborisov@gmail.com> References: <20230530045119.30c8d973@rorschach.local.home> <20230626091635.3002827-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov Add a small usage example of how --daemonize can be used with guest/host tracing for analyzing network activity. Signed-off-by: Avidan Borisov --- .../trace-cmd/trace-cmd-record.1.txt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Documentation/trace-cmd/trace-cmd-record.1.txt b/Documentation/trace-cmd/trace-cmd-record.1.txt index 0c9a914..79ab3d0 100644 --- a/Documentation/trace-cmd/trace-cmd-record.1.txt +++ b/Documentation/trace-cmd/trace-cmd-record.1.txt @@ -515,6 +515,35 @@ task: sleep-21611 => try_to_wake_up (0xffffffff8106340a) ---- +An example of using --daemonize together with guest/host tracing: +[source,shell] +---- +$ sudo trace-cmd record --daemonize -p nop -e 'sched:sched_process_exec' -A guest -p nop -e net && +> ping -c 1 10.20.1.2 && +> sudo start-stop-daemon --stop --signal INT --retry 20 --pidfile /var/run/trace-cmd-record.pid && +> sudo trace-cmd report -i trace.dat -i trace-guest.dat | head +Negotiated kvm time sync protocol with guest guest +Send SIGINT to pid 3071371 to stop recording +PING 10.20.1.2 (10.20.1.2) 56(84) bytes of data. +64 bytes from 10.20.1.2: icmp_seq=1 ttl=64 time=0.134 ms +--- 10.20.1.2 ping statistics --- +1 packets transmitted, 1 received, 0% packet loss, time 0ms +rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms +CPU0 data recorded at offset=0x14f000 + 229 bytes in size (4096 uncompressed) +.... + trace.dat: cpus=28 +trace-guest.dat: cpus=1 + trace.dat: ping-3071450 [013] 1196830.834258: sched_process_exec: filename=/bin/ping pid=3071450 old_pid=3071450 +trace-guest.dat: -0 [000] 1196830.835990: napi_gro_receive_entry: dev=eth1 napi_id=0x2002 queue_mapping=1 skbaddr=0xffff95d051a5c400 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 ip_summed=0 hash=0x00000000 l4_hash=0 len=84 data_len=0 truesize=768 mac_header_valid=1 mac_header=-14 nr_frags=0 gso_size=0 gso_type=0 +trace-guest.dat: -0 [000] 1196830.835997: napi_gro_receive_exit: ret=3 +trace-guest.dat: -0 [000] 1196830.835998: netif_receive_skb: dev=eth1 skbaddr=0xffff95d051a5c400x len=84 +trace-guest.dat: -0 [000] 1196830.836021: net_dev_queue: dev=eth1 skbaddr=0xffff95d051a5c700x len=98 +trace-guest.dat: -0 [000] 1196830.836024: net_dev_start_xmit: dev=eth1 queue_mapping=0 skbaddr=0xffff95d051a5c700 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 ip_summed=0 len=98 data_len=0 network_offset=14 transport_offset_valid=1 transport_offset=34 tx_flags=0 gso_size=0 gso_segs=0 gso_type=0 +trace-guest.dat: -0 [000] 1196830.836069: net_dev_xmit: dev=eth1 skbaddr=0xffff95d051a5c700 len=98 rc=0 + trace.dat: sudo-3071451 [015] 1196830.838262: sched_process_exec: filename=/usr/bin/sudo pid=3071451 old_pid=3071451 +---- + SEE ALSO -------- trace-cmd(1), trace-cmd-report(1), trace-cmd-start(1), trace-cmd-stop(1),