From patchwork Thu Jan 7 08:32:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003157 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 461F4C433E6 for ; Thu, 7 Jan 2021 08:33:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 13C2D20738 for ; Thu, 7 Jan 2021 08:33:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726970AbhAGIdf (ORCPT ); Thu, 7 Jan 2021 03:33:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdf (ORCPT ); Thu, 7 Jan 2021 03:33:35 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07FF3C0612F5 for ; Thu, 7 Jan 2021 00:32:55 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id t30so4784930wrb.0 for ; Thu, 07 Jan 2021 00:32:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vcKhDeYU3eG+D7PcdFnAB8JZNBcJUbcNVUYI5AulMkQ=; b=c1i5ohjYDGcjHL6EcMXuocVrPIFny/UQbrDo+YeGQyj9ZfZiM5QxKrnIQFn7v+PV3s X2jyJgkdz06h/UTyjekpUfjpmXDJV6PmPcdftzma1CV41HdwmCr5K51hN5HoiBkftyUU x7lIkMQ8LkWqWgvT17v5xnqyKreDPY6dwBuYsBaANOpQVUnnSONOGDRgMRDyGa6g3N/H s4DtpMEo0Xdl4qs+z51L+eYPEV9q5rdqwKi8fsXFLoUKdOi8NE0LOZhF9jAql+69BFD2 B2Piufp5iVQ0vUlm8og3/nltMEHpcE3b0PL7sIOqxhJuseZN3gSd7n7LRUumiDHWP2h8 qs6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vcKhDeYU3eG+D7PcdFnAB8JZNBcJUbcNVUYI5AulMkQ=; b=fMQE8Cb1/Sm/KKqQafV/leiat7lQi/NuRgMm53X15EZ0iz87LAQWYuJd0T7gx0kolP gyxebpmnQuneSfwM02+0E9DrfrgPm3G9TBEWNNINBr48YRIZKHTDo7brpkvbf5tX8e7N t4R2KIYi+Ht10ic1DPHNBYTdPviwMvwlZiADrHoeq250YFPL5B2F1zoikZOiZ8diYfZp 3ygM6GZKDp2u8FDA5LwhksZA/saffYAcx+cNUamKvkgXPyBUGx5pRrWU4PK0WoGyYJYO pxvkKl4N3yxWgumiy5BQDRiPR/iq0yhNAkaWhp+DSPwcxDN3P+hkJrUSbDK+5nSpfGxS eGQg== X-Gm-Message-State: AOAM531Kcrd3RUfM/O3N/xZ20+tsqazYhRM2XFueqm2i/QSIhSPgYSvi CpdHb0fss+VyHgcQBqLg1ALXpZA9a6dafOeq X-Google-Smtp-Source: ABdhPJwHkfg5bM4a7bNRtn/83qaUeIsSals6y1PmWTGei/Q9/CWcihs+Vs290h/nttykWo8jDHnLbg== X-Received: by 2002:a5d:4b4c:: with SMTP id w12mr7692569wrs.402.1610008373771; Thu, 07 Jan 2021 00:32:53 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:53 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 1/6] libtracefs: New APIs for opening and reading ftrace files Date: Thu, 7 Jan 2021 10:32:45 +0200 Message-Id: <20210107083250.16295-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org These new APIs can be used to read integer from frtace file and to open ftrace file: tracefs_instance_file_read_int(); tracefs_instance_file_open(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/tracefs.h | 6 ++++- src/tracefs-instance.c | 59 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/include/tracefs.h b/include/tracefs.h index 3d70aca..460fa4c 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -29,7 +29,11 @@ char *tracefs_instance_get_dir(struct tracefs_instance *instance); int tracefs_instance_file_write(struct tracefs_instance *instance, const char *file, const char *str); char *tracefs_instance_file_read(struct tracefs_instance *instance, - char *file, int *psize); + const char *file, int *psize); +int tracefs_instance_file_read_int(struct tracefs_instance *instance, + const char *file, long long *res); +int tracefs_instance_file_open(struct tracefs_instance *instance, + const char *file, int mode); int tracefs_instances_walk(int (*callback)(const char *, void *), void *context); bool tracefs_instance_exists(const char *name); diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c index bf3de7c..27b9e99 100644 --- a/src/tracefs-instance.c +++ b/src/tracefs-instance.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include "tracefs.h" #include "tracefs-local.h" @@ -282,7 +282,7 @@ int tracefs_instance_file_write(struct tracefs_instance *instance, * The return string must be freed by free() */ char *tracefs_instance_file_read(struct tracefs_instance *instance, - char *file, int *psize) + const char *file, int *psize) { char *buf = NULL; int size = 0; @@ -301,6 +301,61 @@ char *tracefs_instance_file_read(struct tracefs_instance *instance, return buf; } +/** + * tracefs_instance_file_read_int - Read an integer from a trace file. + * @instance: ftrace instance, can be NULL for the top instance + * @file: name of the file + * @res: The integer from the file. + * + * Returns 0 if the reading is successful and the result is stored in res, -1 + * in case of an error. + */ +int tracefs_instance_file_read_int(struct tracefs_instance *instance, + const char *file, long long *res) +{ + long long ret = LLONG_MAX; + int size = 0; + char *str; + + str = tracefs_instance_file_read(instance, file, &size); + if (size && str) + ret = strtoll(str, NULL, 0); + free(str); + + if (LLONG_MIN == ret || LLONG_MAX == ret) + return -1; + *res = ret; + return 0; +} + +/** + * tracefs_instance_file_open - Open a trace file for reading and writing + * @instance: ftrace instance, can be NULL for the top instance + * @file: name of the file + * @mode: file open flags, -1 for default O_RDWR + * + * Returns -1 in case of an error, or a valid file descriptor otherwise. + * The returned FD must be closed with close() + */ +int tracefs_instance_file_open(struct tracefs_instance *instance, + const char *file, int mode) +{ + int flags = O_RDWR; + int fd = -1; + char *path; + + path = tracefs_instance_get_file(instance, file); + if (!path) + return -1; + + if (mode >= 0) + flags = mode; + fd = open(path, flags); + tracefs_put_tracing_file(path); + + return fd; +} + static bool check_file_exists(struct tracefs_instance *instance, char *name, bool dir) { From patchwork Thu Jan 7 08:32:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003159 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1322C433E0 for ; Thu, 7 Jan 2021 08:33:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 640C120738 for ; Thu, 7 Jan 2021 08:33:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726977AbhAGIdh (ORCPT ); Thu, 7 Jan 2021 03:33:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdg (ORCPT ); Thu, 7 Jan 2021 03:33:36 -0500 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 386ECC0612F8 for ; Thu, 7 Jan 2021 00:32:56 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id g185so4777492wmf.3 for ; Thu, 07 Jan 2021 00:32:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TWpP65YDCc1mQUdLFjkYo9gs89cZKkr/iPV7lbXlSyE=; b=ZjJ8+wE7ADzPRYor1BkGq1Wq/ELn+SU88CG4Bt1DvasW0pdCs8P6VlS1MyAlGEyr7k L9daOfOdG9HqHTzD2zgeRf9QlhPRhyMtzkO8XxsrmGC+PBuD9zmSpk+KEpfttIlO5IRg IrJauSmSnorUCgZxnV1ePs0hfWJkvhbGYQqMHQhYoQFM3AsyP7U+2JRmRrxHcC/FjO1t R4c06sO8nKBHE7ONsiGjDJynkJ8gsTN85y2cQAQDRFRu51uqCwGsLU7qokynhwfaqP0P /ORLAn4/VKBNbI0b7Y62ardAIfDdCmAKKhnicOcEykV4tbqkDK0v2fL21N5xTc5t1Fjh 8mIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TWpP65YDCc1mQUdLFjkYo9gs89cZKkr/iPV7lbXlSyE=; b=m8FVL0yHVuD1839uv5NcsT05Yzn1VGn7EijZ2tc1QXOnhNsUN/pOHDfMHMd22yN82f CHRFx08Xhbp60jYa1L9pVG4vZ1rnF074QxEC2gKGGRBYu5uqVYo2IiNO3egkqgU3eMtv FBko12in+oMjs0iP8T5100MC959LDH7V9W8mNSTAIMEM/XGJTUROZg21Dsxh2kWVo4W/ cnMSjHy1EB2Qn6F9g6UNygjx+d5iM+g8FSH1hi6UMMQkk2rJxWOn0vSW2qV1JcdNvXUS e/DGFKlLuv/SqiX0eeZ9qf73+BQwLmOM8O1Y3nwBlBNEO8MQ0BiXuHCgLf6cxMHmz+eV SkSA== X-Gm-Message-State: AOAM533jqkfauGKCBZz9R9Rg3XC2Ba1aAsuINww5RcrZrWkq/xpl2jX2 Ayu9v9cvO/047+Cmpva16Mv1NoZEOkHw96rK X-Google-Smtp-Source: ABdhPJzZ+VK3dXb5+9fu612ksIlbXVz9azaxgbLHKzgoyF9341g6Xp4y/ku8Coz2Z2adsCaCg8iKfg== X-Received: by 2002:a1c:e142:: with SMTP id y63mr7228593wmg.28.1610008375023; Thu, 07 Jan 2021 00:32:55 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:54 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 2/6] libtracefs: New APIs for enable / disable tracing Date: Thu, 7 Jan 2021 10:32:46 +0200 Message-Id: <20210107083250.16295-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org These new APIs can be used to enable / disable tracing in given ftrace instance: tracefs_trace_is_on(); tracefs_trace_on(); tracefs_trace_off(); tracefs_trace_on_fd(); tracefs_trace_off_fd(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/tracefs.h | 6 +++ src/Makefile | 1 + src/tracefs-tools.c | 123 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 src/tracefs-tools.c diff --git a/include/tracefs.h b/include/tracefs.h index 460fa4c..bc799c7 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -40,6 +40,12 @@ bool tracefs_instance_exists(const char *name); bool tracefs_file_exists(struct tracefs_instance *instance, char *name); bool tracefs_dir_exists(struct tracefs_instance *instance, char *name); +int tracefs_trace_is_on(struct tracefs_instance *instance); +int tracefs_trace_on(struct tracefs_instance *instance); +int tracefs_trace_off(struct tracefs_instance *instance); +int tracefs_trace_on_fd(int fd); +int tracefs_trace_off_fd(int fd); + /* events */ void tracefs_list_free(char **list); char **tracefs_event_systems(const char *tracing_dir); diff --git a/src/Makefile b/src/Makefile index 3f64905..dabdbb4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,6 +6,7 @@ OBJS = OBJS += tracefs-utils.o OBJS += tracefs-instance.o OBJS += tracefs-events.o +OBJS += tracefs-tools.o OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d) diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c new file mode 100644 index 0000000..89e2d6d --- /dev/null +++ b/src/tracefs-tools.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: LGPL-2.1 +/* + * Copyright (C) 2008, 2009, 2010 Red Hat Inc, Steven Rostedt + * + * Updates: + * Copyright (C) 2021, VMware, Tzvetomir Stoyanov + * + */ +#include +#include +#include + +#include "tracefs.h" +#include "tracefs-local.h" + +#define TRACE_CTRL "tracing_on" + +static int trace_on_off(int fd, bool on) +{ + int ret; + + if (on) + ret = write(fd, "1", 1); + else + ret = write(fd, "0", 1); + + if (ret == 1) + return 0; + + return -1; +} + +static int trace_on_off_file(struct tracefs_instance *instance, bool on) +{ + int ret; + int fd; + + fd = tracefs_instance_file_open(instance, TRACE_CTRL, O_WRONLY); + if (fd < 0) + return -1; + ret = trace_on_off(fd, on); + close(fd); + + return ret; +} + +/** + * tracefs_trace_is_on - Check if writing traces to the ring buffer is enabled + * @instance: ftrace instance, can be NULL for the top instance + * + * Returns -1 in case of an error, 0 if tracing is disable or 1 if tracing + * is enabled. + */ +int tracefs_trace_is_on(struct tracefs_instance *instance) +{ + char buf[10]; + int ret; + int fd; + + fd = tracefs_instance_file_open(instance, TRACE_CTRL, O_RDONLY); + if (fd < 0) + return -1; + ret = read(fd, buf, 10); + if (ret > 0) { + buf[9] = 0; + ret = atoi(buf); + } else { + ret = -1; + } + close(fd); + + return ret; +} + +/** + * tracefs_trace_on - Enable writing traces to the ring buffer of the given instance + * @instance: ftrace instance, can be NULL for the top instance + * + * Returns -1 in case of an error or 0 otherwise + */ +int tracefs_trace_on(struct tracefs_instance *instance) +{ + return trace_on_off_file(instance, true); +} + +/** + * tracefs_trace_off - Disable writing traces to the ring buffer of the given instance + * @instance: ftrace instance, can be NULL for the top instance + * + * Returns -1 in case of an error or 0 otherwise + */ +int tracefs_trace_off(struct tracefs_instance *instance) +{ + return trace_on_off_file(instance, false); +} + +/** + * tracefs_trace_on_fd - Enable writing traces to the ring buffer + * @fd: File descriptor to ftrace tracing_on file, previously opened for reading + * with tracefs_instance_file_open() + * + * Returns -1 in case of an error or 0 otherwise + */ +int tracefs_trace_on_fd(int fd) +{ + if (fd < 0) + return -1; + return trace_on_off(fd, true); +} + +/** + * tracefs_trace_off_fd - Disable writing traces to the ring buffer + * @fd: File descriptor to ftrace tracing_on file, previously opened for reading + * with tracefs_instance_file_open() + * + * Returns -1 in case of an error or 0 otherwise + */ +int tracefs_trace_off_fd(int fd) +{ + if (fd < 0) + return -1; + return trace_on_off(fd, false); +} From patchwork Thu Jan 7 08:32:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003161 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BAE2C433E6 for ; Thu, 7 Jan 2021 08:33:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5683F20738 for ; Thu, 7 Jan 2021 08:33:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726338AbhAGIdi (ORCPT ); Thu, 7 Jan 2021 03:33:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdh (ORCPT ); Thu, 7 Jan 2021 03:33:37 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59A6BC0612F9 for ; Thu, 7 Jan 2021 00:32:57 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id y17so4735439wrr.10 for ; Thu, 07 Jan 2021 00:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=haKTlhRBvQnAoCt7ye6uAWGXu2XyjhNIGokztycdGfI=; b=DUPLG9dVeHK/15o/vAmHV6fmDYF7oYh6cxxGpTbh3BNLLzUFIqAjEv8TER+vaZUeaO k2x4jRQ60LKShJK2RsMjHQJ+EMMDMPMRxCDORMGakkWNd+XZ9Uyz14Ee0rqTqeE4WvYK /7awd09IRqIi7ugzZBJNcdj1qtlg/TbcdbftcIiXRcTjDBx2xX3bjqZL0nM4P6dKBoJB O0sy8X14HU9eJPReR3drxCRBaFQ0kPanuiJRS2jTMCYkp2AojULpRcuwiBWhiHbVLt+z SAPX/UsWtZsux3UafR+nIxLYzzTe63rKmfSkzzYf5cy+8J6TLxe5A2b0UhSQELqRSuH7 88nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=haKTlhRBvQnAoCt7ye6uAWGXu2XyjhNIGokztycdGfI=; b=ebNY5UMw2UKTfme8lHk21UK4vH5fGOe7Ya2k6AHaj2zfs2uVfjWCsjn5bW6TgPUVxr AW1FZhCQFWvwX/QyuyTfQ+Egz8nW8v+YAjbeCVz3MG98dzNst6R6D500x3XhINkhL3r3 KCd1e8aea61HPD5ZDH1TDkJQ/BoENfvJu8BxQs2bP6Zb/If/sfB3q9jw7v2VJdEsKvhF W3W4hJwJa2mnNzTXbMm9Brai7gC1XI/t0kLKploaJ9C66bOIOAzpO6DpnosIj+ozQ8ss FYzW3Hf9/oK99PFypa9Inn+ZfOvVdlhI+mgb7gMbdxZgpmXeSqnAMDdNjZ54Hn/mcgVw 3B1A== X-Gm-Message-State: AOAM531z3qbAfxRkHGYy9sk346+DS4mtqHvqv2ow4h3zcK/OFae2Ky2+ do5hmNtOWM2oOuk4yKk988p11tbvg8ppHbi6 X-Google-Smtp-Source: ABdhPJzltPSPyJPu2RrFhJvEJOh7PdQhu6+/fPbkFhMVJwUYuu83OhWVp2SriSuHMORrRO6lUf0ETQ== X-Received: by 2002:adf:eec6:: with SMTP id a6mr7750334wrp.239.1610008376150; Thu, 07 Jan 2021 00:32:56 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:55 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 3/6] libtracefs: Documentation for the new APIs for opening and reading ftrace files Date: Thu, 7 Jan 2021 10:32:47 +0200 Message-Id: <20210107083250.16295-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Man pages updated with documentation about: tracefs_instance_file_open(); tracefs_instance_file_read_int(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- Documentation/libtracefs-instances-files.txt | 47 ++++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/Documentation/libtracefs-instances-files.txt b/Documentation/libtracefs-instances-files.txt index 9b18b73..d3aaef2 100644 --- a/Documentation/libtracefs-instances-files.txt +++ b/Documentation/libtracefs-instances-files.txt @@ -4,8 +4,8 @@ libtracefs(3) NAME ---- tracefs_file_exists, tracefs_dir_exists, tracefs_instance_get_file, -tracefs_instance_get_dir, tracefs_instance_file_write, -tracefs_instance_file_read - Work with files in tracing instances. +tracefs_instance_get_dir, tracefs_instance_file_open, tracefs_instance_file_write, +tracefs_instance_file_read, tracefs_instance_file_read_int - Work with files in tracing instances. SYNOPSIS -------- @@ -17,8 +17,10 @@ bool *tracefs_file_exists*(struct tracefs_instance pass:[*]_instance_, char pass bool *tracefs_dir_exists*(struct tracefs_instance pass:[*]_instance_, char pass:[*]_name_); char pass:[*]*tracefs_instance_get_file*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_); char pass:[*]*tracefs_instance_get_dir*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_instance_file_open*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int _mode_); int *tracefs_instance_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_); -char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, char pass:[*]_file_, int pass:[*]_psize_); +char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int pass:[*]_psize_); +int *tracefs_instance_file_read_int*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, long long int pass:[*]_res_); -- @@ -41,12 +43,19 @@ The _tracefs_instance_get_dir()_ function returns the full path of the director with given _name_ in _instance_. Note, it does not check if the directory exists in the instance. +The _tracefs_instance_file_open()_ function opens trace _file_ from given _instance_ and returns +a file descriptor to it. The file access _mode_ can be specified, see *open*(3) for more details. +If -1 is passed as _mode_, default O_RDWR is used. + The _tracefs_instance_file_write()_ function writes a string _str_ in a _file_ from the given _instance_, without the terminating NULL character. -The _tracefs_instance_file_read()_ function reads the content of a _file_ from +The _tracefs_instance_file_read()_ function reads the content of a _file_ from the given _instance_. +The _tracefs_instance_file_read_int()_ function reads the content of a _file_ from +the given _instance_ and converts it to a long long integer, which is stored in _res_. + RETURN VALUE ------------ The _tracefs_file_exists()_ and _tracefs_dir_exists()_ functions return true if the @@ -56,6 +65,9 @@ The _tracefs_instance_get_file()_ and _tracefs_instance_get_dir()_ functions ret a string or NULL in case of an error. The returned string must be freed with _tracefs_put_tracing_file()_. +The _tracefs_instance_file_open()_ function returns a file descriptor to the opened file. It must be +closed with *close*(3). In case of an error, -1 is returned. + The _tracefs_instance_file_write()_ function returns the number of written bytes, or -1 in case of an error. @@ -63,6 +75,9 @@ The _tracefs_instance_file_read()_ function returns a pointer to a NULL terminat string, read from the file, or NULL in case of an error. The returned string must be freed with free(). +The _tracefs_instance_file_read_int()_ function returns 0 if a valid integer is read from the file +and stored in _res_ or -1 in case of an error. + EXAMPLE ------- [source,c] @@ -123,6 +138,30 @@ struct tracefs_instance *inst = tracefs_instance_create("foo"); tracefs_instance_destroy(inst); else tracefs_instance_free(inst); + ... + + long long int res; + if (tracefs_instance_file_read_int(NULL, "tracing_on", &res) == 0) { + if (res == 0) { + /* tracing is disabled in the top instance */ + } else if (res == 1) { + /* tracing is enabled in the top instance */ + } else { + /* Unknown tracing state of the top instance */ + } + } else { + /* Failed to read integer from tracing_on file */ + } + + ... + + int fd; + fd = tracefs_instance_file_open(NULL, "tracing_on", O_WRONLY); + if (fd >= 0) { + /* Got file descriptor to the tracing_on file from the top instance for writing */ + ... + close(fd); + } -- FILES ----- From patchwork Thu Jan 7 08:32:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003163 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C489C433E0 for ; Thu, 7 Jan 2021 08:33:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E8C920738 for ; Thu, 7 Jan 2021 08:33:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726996AbhAGIdj (ORCPT ); Thu, 7 Jan 2021 03:33:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdi (ORCPT ); Thu, 7 Jan 2021 03:33:38 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65E4DC0612FA for ; Thu, 7 Jan 2021 00:32:58 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id y23so4782944wmi.1 for ; Thu, 07 Jan 2021 00:32:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uS7DCua/vhhQcEfCge+9yCG9OTf1xjHKM2C8mH7dCeM=; b=hgobh96fvuCid+1BohoBoTmzo9YzhvGkB9TjlionEg2xG7lQ6G6lHvtPw4DojitmaG 3JFAPaRUlDBtD4yYWyYlCJll8WoXXzoC8Ic02sqOo8jGrDaw7Py+ELRSl8bJlD6QfMvL TCb5h+hAs9lcQjF7djKXcrN6C1i1ZjEyuzZQ8uz9Pjw7T4gCk3w+z2GGN4f+GFEqRoIj 4IcKWT4vqcJ5GBiSelvBRF9RdoV2W85uT0odoAUPsuLXBmtSAK1KZN0liKE06y/XmfDy v8NcFgaAXGqmqIuPMpHi5iQ2zajiYTeLdPpyUScraqQhkP7mDXE1a2qS7FQHvop3j0kl HMcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uS7DCua/vhhQcEfCge+9yCG9OTf1xjHKM2C8mH7dCeM=; b=Ru+qYtEUlU8Od7/tEVfG8VgJWuq/50X8kygrzb0teX8/CGZsyTVMh9PHpAYVtpj+xB cnLE44HPGKu3V7NHix/CDgu2g7oVjm8b7v8o8hZ23mw+xJuV65OMOpFT+l763F+Bf19n P3v3oDr8DbycTj2ZIrElQrrHZ+o33mLCb0YPMLi//Y2+x9uN2vowTkBXRBQ6XkRV1pX4 Z372dGBfhyUQW1l/RHQJfFEMEuPjvsHYZ/1Tge2YfbLrWeW8yb324GDLviVZwlMsmOIJ r/CfvUAJq9Y7Q8nULx2DI0EHHLxzyQFC+RS32KkPxXFU4P3L2Tu/7BjKraO0E7u1ja8l Oi3A== X-Gm-Message-State: AOAM533T5RzSfQnwVwKVERFhWWlj1uzfvlJSqxPke9oBTzInwiwE+g/K Ix9b41JgBAgwHXDr3wqlQ2KM3k2i1Yfh+a8X X-Google-Smtp-Source: ABdhPJxflF4kuJGWR0G6MrKlmKbZv+yqEalx0oaAobEQF0aD+f5r/DjcCtwAQ9tJNP0QGX9dQN5s4w== X-Received: by 2002:a1c:9a4d:: with SMTP id c74mr7232830wme.5.1610008377209; Thu, 07 Jan 2021 00:32:57 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:56 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 4/6] libtracefs: Documentation for enable / disable tracing APIs Date: Thu, 7 Jan 2021 10:32:48 +0200 Message-Id: <20210107083250.16295-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org New man pages with documentation about: tracefs_trace_is_on(); tracefs_trace_on(); tracefs_trace_off(); tracefs_trace_on_fd(); tracefs_trace_off_fd(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- Documentation/libtracefs-traceon.txt | 143 +++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Documentation/libtracefs-traceon.txt diff --git a/Documentation/libtracefs-traceon.txt b/Documentation/libtracefs-traceon.txt new file mode 100644 index 0000000..af4b7f7 --- /dev/null +++ b/Documentation/libtracefs-traceon.txt @@ -0,0 +1,143 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_trace_is_on, tracefs_trace_on, tracefs_trace_off, tracefs_trace_on_fd, +tracefs_trace_off_fd - Functions to enable or disable tracing. + +SYNOPSIS +-------- +[verse] +-- +*#include * + +int *tracefs_trace_is_on*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_trace_on*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_trace_off*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_trace_on_fd*(int _fd_); +int *tracefs_trace_off_fd*(int _fd_); +-- + +DESCRIPTION +----------- +This set of functions can be used to check, enable or disable writing to the ring buffer in +the given trace instance. The tracing is enabled when writing to the ring buffer is enabled. + +The _tracefs_trace_is_on()_ function checks if tracing is enabled for the given _instance_. If +_instance_ is NULL, the top instance is used. + +The _tracefs_trace_on()_ and _tracefs_trace_off()_ functions set the tracing in the _instance_ +to enable or disable state. If _instance_ is NULL, the top instance is used. + +The _tracefs_trace_on_fd()_ and _tracefs_trace_off_fd()_ functions set the tracing state to enable +or disable using the given _fd_. This file descriptor must be opened for writing with +*tracefs_instance_file_open*(3) for *tracing_on* file from desired trace instance. These functions +are faster than *tracefs_trace_on* and *tracefs_trace_off*. + +RETURN VALUE +------------ +The _tracefs_trace_is_on()_ function returns 0 if tracing is disable, 1 if it is enabled or +-1 in case of an error. + +The _tracefs_trace_on()_, _tracefs_trace_off()_, _tracefs_trace_on_fd()_ and +_tracefs_trace_off_fd()_ functions return -1 in case of an error or 0 otherwise. + +EXAMPLE +------- +[source,c] +-- +#include + + int ret; + + ret = tracefs_trace_is_on(NULL); + if (ret == 0) { + /* Tracing is disabled in the top instance */ + } else if (ret == 1) {" + /* Tracing is enabled in the top instance */ + } else { + /* Error getting tracing state of the top instance */ + } + + ... + + if (!tracefs_trace_on(NULL)) { + /* Enabled tracing in the top instance */ + + ... + + if (!tracefs_trace_off(NULL)) { + /* Disabled tracing in the top instance */ + } else { + /* Error disabling tracing in the top instance */ + } + } else { + /* Error enabling tracing in the top instance */ + } + + ... + + int fd = tracefs_instance_file_open(NULL, "tracing_on", O_WRONLY); + + if (fd < 0) { + /* Error opening tracing_on file */ + } + ... + if (!tracefs_trace_on_fd(fd)) { + /* Enabled tracing in the top instance */ + + ... + + if (!tracefs_trace_off_fd(fd)) { + /* Disabled tracing in the top instance */ + } else { + /* Error disabling tracing in the top instance */ + } + } else { + /* Error enabling tracing in the top instance */ + } + + ... + + close(fd); +-- +FILES +----- +[verse] +-- +*tracefs.h* + Header file to include in order to have access to the library APIs. +*-ltracefs* + Linker switch to add when building a program that uses the library. +-- + +SEE ALSO +-------- +_libtracefs(3)_, +_libtraceevent(3)_, +_trace-cmd(1)_ + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* +*Tzvetomir Stoyanov* +-- +REPORTING BUGS +-------------- +Report bugs to + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2021 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL). From patchwork Thu Jan 7 08:32:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1033AC433DB for ; Thu, 7 Jan 2021 08:33:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CDF5E20738 for ; Thu, 7 Jan 2021 08:33:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726999AbhAGIdk (ORCPT ); Thu, 7 Jan 2021 03:33:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdk (ORCPT ); Thu, 7 Jan 2021 03:33:40 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6A25C0612FB for ; Thu, 7 Jan 2021 00:32:59 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id y23so4782992wmi.1 for ; Thu, 07 Jan 2021 00:32:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WcrN2QECMeUQHo6SiX87vG7Px54DeR0GyapfjL1vkLY=; b=n1Gz6rCUJYY+EkjPAd/Zxmmlvdde9yBnaoLb0q9kJE8HdM7PCBRrGle5TlBwzMKDdZ y5WKSwzWwlh0Os8uCOjJq8bDBw/wLS0QWazL9rJubGAYrz5FXdR1yxsHE2IrTZqFeijz lpVNLGxLDCgkqzEMABTMZjGBinO+eK4GdQE3DJ66bZip513Fw2y3G3RBLJT+Pjmafp6C 0BMiSijcC+MBcRyI9Ebn15EbQheosf1lLU9mk8E1+aXFyjPSHe4Wq/esC0X360v4kT+U nDwUhzEVSOK2PG4ajKyWhQ14Z06fcylB3z44PSRydG3UIubPclMcMqxWZHLlWqN3+/Hj oxuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WcrN2QECMeUQHo6SiX87vG7Px54DeR0GyapfjL1vkLY=; b=MpbhgdxM4Ostb418km921WDOGTMWMr2QgEIWIdD3y2bQ46q6dU5Sq9pIhxOr47dm3x ScUX3Z+0mQ2waz0fhIu213jsgnDxnZCL00ceU5wWB0LqPmudUUi5OAa5OS5llWqzvcZV 9wZfI4nhT2yJe0yXFkccnxAIwcMScMlv04p3GAXJpy6gTe/WgylWlW2K6eq2syTWYHsW enNamXDQK3qMk+wF+75o894Wml7mhEZAefzq1gAh95YJN23zfffsiG2HUm1xiQHl1sVc RVbWzybj4+Jco4PrMkV8VYsdUfT+nS1JoQTO0/YYV/smYXSoYWsoRWEJYhSdMNj/n5YG dbCg== X-Gm-Message-State: AOAM533nhn3uUmkufUoGk6minW7oMdGBj0sFYXPtD4AcDfiMI6rMvtNt 0Wh6rti6pos3uiQfzQVdbhU= X-Google-Smtp-Source: ABdhPJzbimwnKHEaVwK8bcDJa/ow3XHjzxox2Gk4Y30XyZUfXJLdGT5KtNdgBHW/MKuVO4pZpMgwug== X-Received: by 2002:a1c:2c83:: with SMTP id s125mr6943809wms.161.1610008378448; Thu, 07 Jan 2021 00:32:58 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:57 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 5/6] libtracefs: Unit tests for the new APIs for opening and reading ftrace files Date: Thu, 7 Jan 2021 10:32:49 +0200 Message-Id: <20210107083250.16295-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Added unit tests for: tracefs_instance_file_open(); tracefs_instance_file_read_int(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- utest/tracefs-utest.c | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c index b45a3c6..a51d6fc 100644 --- a/utest/tracefs-utest.c +++ b/utest/tracefs-utest.c @@ -218,6 +218,7 @@ static void test_instance_file_read(struct tracefs_instance *inst, char *fname) #define ALL_TRACERS "available_tracers" #define CUR_TRACER "current_tracer" #define PER_CPU "per_cpu" +#define TRACE_ON "tracing_on" static void test_instance_file(void) { struct tracefs_instance *instance = NULL; @@ -322,6 +323,95 @@ static void test_instance_file(void) free(inst_dir); } +static bool check_fd_name(int fd, char *name) +{ + char link[PATH_MAX + 1]; + char path[PATH_MAX + 1]; + struct stat st; + char *file; + int ret; + + snprintf(link, PATH_MAX, "/proc/self/fd/%d", fd); + ret = lstat(link, &st); + CU_TEST(ret == 0); + if (ret < 0) + return false; + CU_TEST(S_ISLNK(st.st_mode)); + if (!S_ISLNK(st.st_mode)) + return false; + ret = readlink(link, path, PATH_MAX); + CU_TEST(ret > 0); + if (ret > PATH_MAX || ret < 0) + return false; + path[ret] = 0; + file = basename(path); + CU_TEST(file != NULL); + if (!file) + return false; + ret = strcmp(file, name); + CU_TEST(ret == 0); + if (ret) + return false; + return true; +} + +#define FLAGS_STR "flags:" +static bool check_fd_mode(int fd, int mode) +{ + char path[PATH_MAX + 1]; + long fmode = -1; + char *line = NULL; + struct stat st; + size_t len = 0; + ssize_t size; + FILE *file; + int ret; + + snprintf(path, PATH_MAX, "/proc/self/fdinfo/%d", fd); + ret = stat(path, &st); + CU_TEST(ret == 0); + if (ret < 0) + return false; + file = fopen(path, "r"); + if (!file) + return false; + while ((size = getline(&line, &len, file)) > 0) { + if (strncmp(line, FLAGS_STR, strlen(FLAGS_STR))) + continue; + fmode = strtol(line + strlen(FLAGS_STR), NULL, 8); + break; + } + free(line); + fclose(file); + if (fmode < 0 || + (O_ACCMODE & fmode) != (O_ACCMODE & mode)) + return false; + return true; +} + +static void test_instance_file_fd(void) +{ + const char *name = get_rand_str(); + long long res = -1; + char rd[2]; + int fd; + + fd = tracefs_instance_file_open(test_instance, name, -1); + CU_TEST(fd == -1); + fd = tracefs_instance_file_open(test_instance, TRACE_ON, O_RDONLY); + CU_TEST(fd >= 0); + CU_TEST(check_fd_name(fd, TRACE_ON)); + CU_TEST(check_fd_mode(fd, O_RDONLY)); + + CU_TEST(tracefs_instance_file_read_int(test_instance, TRACE_ON, &res) == 0); + CU_TEST((res == 0 || res == 1)); + CU_TEST(read(fd, &rd, 1) == 1); + rd[1] = 0; + CU_TEST(res == atoi(rd)); + + close(fd); +} + static void exclude_string(char **strings, char *name) { int i; @@ -614,6 +704,8 @@ void test_tracefs_lib(void) CU_add_test(suite, "tracing file / directory APIs", test_trace_file); CU_add_test(suite, "instance file / directory APIs", + test_instance_file_fd); + CU_add_test(suite, "instance file descriptor", test_instance_file); CU_add_test(suite, "systems and events APIs", test_system_event); From patchwork Thu Jan 7 08:32:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12003167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55160C433DB for ; Thu, 7 Jan 2021 08:33:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 21A7E20738 for ; Thu, 7 Jan 2021 08:33:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727003AbhAGIdq (ORCPT ); Thu, 7 Jan 2021 03:33:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbhAGIdq (ORCPT ); Thu, 7 Jan 2021 03:33:46 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C08D2C0612FC for ; Thu, 7 Jan 2021 00:33:00 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id d13so4705257wrc.13 for ; Thu, 07 Jan 2021 00:33:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s2I3KtOFcWjYCRGWlxpzPO3r0lLeLwX7VasqrIJSlw0=; b=tkquyvNtlRGKpP5lnJc73TK7XjIT3FQVyXiQmxYS05dwsUtxtfjPoBaIG61t04caGY abEpq7EvPm636hPB4q8HVEVKI5BVCmqcrb6Eny80OJ5xyEA7ZVz8Ilq0d53o1RTJdSvL WcjjJtfWZrb6u/9+LmXvtZNBdJ9vva7NsFOMou+Dz+A53Ld/rGPFlTz1IlvAQuNzYz41 vI5ihEbeZAqdE6t/9pTAkqsMSOslQO2Bm2bSQlw6Kum59nQXJVhRo/0FsbSXyS0N/PEp kpz1OQmNkEiQMcTNAfLnRBTaYPrvB/Rp1f4WH6aYi/ZzRIxaS+Wzt4zbsxnto0hqhvLt IBuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s2I3KtOFcWjYCRGWlxpzPO3r0lLeLwX7VasqrIJSlw0=; b=FH9o91V++yBg8pglccXIkM5d/9UJmhGoyccrzkvfNdPAbXj8yfDUVlqD+FIWe65o0j owDWxgLQ0EzDfIEYH4C9PyAWxg94A/waUiz+8DbYtqzF73zNfpbmZGiC992Q90VnUZp6 doP0Nkbj+0v6yjkD5Zb+yxkMI1YEKSZ0slQfviL/b2YQDgwItRFxgKPNn6ISbKYuiu71 d39JjII/DqLRJ06JdGuZHJ1H/9pkEt0IlqaZhLAPXoCI8+IAvfyruAnIH2pQ5nnS65gd v8RcRbxphjgmvhDJkR3cA7ohOX6HoCph7DtiKzgB79mb10wDxnazyaTjNeIUq9VJQsfa fYjQ== X-Gm-Message-State: AOAM530sMPJEIrpeYPgEl7HNsXHCaKP7qdS1EH7dYGCa3KTq171/FJDa jgTdZ6dPxQc8kdk/aTazn0w= X-Google-Smtp-Source: ABdhPJwVyuziMB8R1Qr7yLEDDXxB6QSxu4T9ToZAlHFN7mR5CA3nN5vrtj55YRXG/9E+onLQYCpuqA== X-Received: by 2002:a5d:5005:: with SMTP id e5mr7588952wrt.279.1610008379632; Thu, 07 Jan 2021 00:32:59 -0800 (PST) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id v20sm7293867wra.19.2021.01.07.00.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 00:32:58 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 6/6] libtracefs: Unit tests for enable / disable tracing APIs Date: Thu, 7 Jan 2021 10:32:50 +0200 Message-Id: <20210107083250.16295-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210107083250.16295-1-tz.stoyanov@gmail.com> References: <20210107083250.16295-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org New unit tests for these APIs: tracefs_trace_is_on(); tracefs_trace_on(); tracefs_trace_off(); tracefs_trace_on_fd(); tracefs_trace_off_fd(); Signed-off-by: Tzvetomir Stoyanov (VMware) --- utest/tracefs-utest.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c index a51d6fc..2a80cf7 100644 --- a/utest/tracefs-utest.c +++ b/utest/tracefs-utest.c @@ -412,6 +412,42 @@ static void test_instance_file_fd(void) close(fd); } +static void test_tracing_onoff(void) +{ + long long res = -1; + int fd; + + fd = tracefs_instance_file_open(test_instance, TRACE_ON, O_WRONLY); + CU_TEST(fd >= 0); + CU_TEST(tracefs_instance_file_read_int(test_instance, TRACE_ON, &res) == 0); + if (res == 1) { + CU_TEST(tracefs_trace_is_on(test_instance) == 1); + CU_TEST(tracefs_trace_off(test_instance) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 0); + CU_TEST(tracefs_trace_on(test_instance) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 1); + + CU_TEST(tracefs_trace_off_fd(fd) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 0); + CU_TEST(tracefs_trace_on_fd(fd) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 1); + } else { + CU_TEST(tracefs_trace_is_on(test_instance) == 0); + CU_TEST(tracefs_trace_on(test_instance) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 1); + CU_TEST(tracefs_trace_off(test_instance) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 0); + + CU_TEST(tracefs_trace_on_fd(fd) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 1); + CU_TEST(tracefs_trace_off_fd(fd) == 0); + CU_TEST(tracefs_trace_is_on(test_instance) == 0); + } + + if (fd >= 0) + close(fd); +} + static void exclude_string(char **strings, char *name) { int i; @@ -719,4 +755,7 @@ void test_tracefs_lib(void) test_instances_walk); CU_add_test(suite, "tracefs_get_clock API", test_get_clock); + CU_add_test(suite, "tracing on / off", + test_tracing_onoff); + }