From patchwork Mon Aug 23 12:53:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12452789 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.7 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 07A2CC4338F for ; Mon, 23 Aug 2021 12:54:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DD74261368 for ; Mon, 23 Aug 2021 12:54:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235775AbhHWMyn (ORCPT ); Mon, 23 Aug 2021 08:54:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235738AbhHWMyn (ORCPT ); Mon, 23 Aug 2021 08:54:43 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 991AAC061575 for ; Mon, 23 Aug 2021 05:54:00 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id u15so10466905wmj.1 for ; Mon, 23 Aug 2021 05:54:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=STWeokfcx6qA/UgUiqfu23UhAh6YEHrTUltNx3cIG2U=; b=SxNuPdjKTkVyEb6Pv+zRCvPJzNNvXvly46VK9BIo3e0OhbmDJCYTHcx3Hh22/O6u1N bSTHM/i0Ik3AYpyeZmIHUfPmgnG+jjs1og6Vj0YJAXHbOJigGPTbLf9z1y471KXCTtf+ GuOLy1QUuDSjTKROZ2qn6pB84P5BDd7Hb9mpbjiukBDYKNPC+h7lv0t9U2DVgWvbiS2N 6PC2C0Df7RigI7Fvu+MDMMeKWZXiXem+aN252zBJAhrlLByiRUVLN4WtPALQ11fofSx/ /SjGcBDWbmKQwpT2yYqZ+zMmOGXfiJM3ApfpIuYnDABMXJiG8c/HPFAtUr/AMVvIj5nP pq2Q== 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:mime-version :content-transfer-encoding; bh=STWeokfcx6qA/UgUiqfu23UhAh6YEHrTUltNx3cIG2U=; b=gLhXlwWpssUlSTCfw81sLT7ggiyyhti/sC6uo0IBkKtDyQPXOTah8f3Z+CgIuMosNr WMiBD5Q3mf8gYeDIXQxF11YmSlGLgbMHq9zoLWbxnernhKd+WjTFCQa74FIR0u8hCoz1 BWnzgBGvYkokBr2mSGC1PWFp15R0E/1SpcSXQHmMesS/LdPSFtNCmdSU8kKAnDVGZ4WL NYoTYeW2FUFutBQUuLQUjWZgU9qT5vjqeyexjEsY989AYdyzGZhX2s3SZE6c9j7zJh3+ FZMRxmqB2g6VI6A/PRzWjooortFMRl87F+ORCVtFPRbRZVZ02zEnxfxUvkplZVzPxkdX TFSw== X-Gm-Message-State: AOAM531xeSZ5lhtfjJ4SQtNU7isLf0qmu3fip3xGd/IgqMfwC5v3fTrH 3gAUi6trxOd05yPwoyy7Rh+5eCoY6ec= X-Google-Smtp-Source: ABdhPJxBlZFVKzP73oIl9txQLF9WWZ+yE81gKRsnlCZN6ZtIRgp0JyKsGVvHN7pSat4dfVH12p++SQ== X-Received: by 2002:a1c:3b05:: with SMTP id i5mr2555523wma.136.1629723238933; Mon, 23 Aug 2021 05:53:58 -0700 (PDT) Received: from crow.. ([95.87.199.109]) by smtp.gmail.com with ESMTPSA id b10sm11469454wrt.43.2021.08.23.05.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 05:53:58 -0700 (PDT) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 1/3] trace-cruncher: Add methods for accessing tracefs errorlog Date: Mon, 23 Aug 2021 15:53:46 +0300 Message-Id: <20210823125348.145615-1-y.karadz@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Just wrapping the corresponding APIs of libtracefs. The functionalities are also available as static methods and will be used by the module itself when printing the error messages. Signed-off-by: Yordan Karadzhov (VMware) --- src/ftracepy-utils.c | 67 ++++++++++++++++++++++++++++++++++++++++++++ src/ftracepy-utils.h | 6 ++++ src/ftracepy.c | 10 +++++++ 3 files changed, 83 insertions(+) diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c index 581432d..a3ae03a 100644 --- a/src/ftracepy-utils.c +++ b/src/ftracepy-utils.c @@ -23,6 +23,37 @@ PyObject *TFS_ERROR; PyObject *TEP_ERROR; PyObject *TRACECRUNCHER_ERROR; +static const char *get_instance_name(struct tracefs_instance *instance); + +static char *tfs_error_log(struct tracefs_instance *instance, bool *ok) +{; + char *err_log = tracefs_error_all(instance); + + errno = 0; + if (errno && !err_log) { + PyErr_Format(TFS_ERROR, + "Unable to get error log for instance \'%s\'.", + get_instance_name(instance)); + } + + if (ok) + *ok = errno ? false : true; + + return err_log; +} + +static bool tfs_clear_error_log(struct tracefs_instance *instance) +{ + if (tracefs_error_clear(instance) < 0) { + PyErr_Format(TFS_ERROR, + "Unable to clear error log for instance \'%s\'.", + get_instance_name(instance)); + return false; + } + + return true; +} + PyObject *PyTepRecord_time(PyTepRecord* self) { unsigned long ts = self->ptrObj ? self->ptrObj->ts : 0; @@ -2051,6 +2082,42 @@ PyObject *PyFtrace_hook2pid(PyObject *self, PyObject *args, PyObject *kwargs) Py_RETURN_NONE; } +PyObject *PyFtrace_error_log(PyObject *self, PyObject *args, + PyObject *kwargs) +{ + struct tracefs_instance *instance; + PyObject *ret = NULL; + char *err_log; + bool ok; + + if (!get_instance_from_arg(args, kwargs, &instance)) + return NULL; + + err_log = tfs_error_log(instance, &ok); + if (err_log) { + ret = PyUnicode_FromString(err_log); + free(err_log); + } else if (ok) { + ret = PyUnicode_FromString("(nil)"); + } + + return ret; +} + +PyObject *PyFtrace_clear_error_log(PyObject *self, PyObject *args, + PyObject *kwargs) +{ + struct tracefs_instance *instance; + + if (!get_instance_from_arg(args, kwargs, &instance)) + return NULL; + + if (!tfs_clear_error_log(instance)) + return NULL; + + Py_RETURN_NONE; +} + void PyFtrace_at_exit(void) { } diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h index 4e04fb0..26a2f14 100644 --- a/src/ftracepy-utils.h +++ b/src/ftracepy-utils.h @@ -177,6 +177,12 @@ PyObject *PyFtrace_iterate_trace(PyObject *self, PyObject *args, PyObject *PyFtrace_hook2pid(PyObject *self, PyObject *args, PyObject *kwargs); +PyObject *PyFtrace_error_log(PyObject *self, PyObject *args, + PyObject *kwargs); + +PyObject *PyFtrace_clear_error_log(PyObject *self, PyObject *args, + PyObject *kwargs); + void PyFtrace_at_exit(void); #endif diff --git a/src/ftracepy.c b/src/ftracepy.c index a4fba01..e5a91fc 100644 --- a/src/ftracepy.c +++ b/src/ftracepy.c @@ -310,6 +310,16 @@ static PyMethodDef ftracepy_methods[] = { METH_VARARGS | METH_KEYWORDS, "Trace only particular process." }, + {"error_log", + (PyCFunction) PyFtrace_error_log, + METH_VARARGS | METH_KEYWORDS, + "Get the content of the error log." + }, + {"clear_error_log", + (PyCFunction) PyFtrace_clear_error_log, + METH_VARARGS | METH_KEYWORDS, + "Clear the content of the error log." + }, {NULL, NULL, 0, NULL} }; From patchwork Mon Aug 23 12:53:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12452791 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.7 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 B761FC4320A for ; Mon, 23 Aug 2021 12:54:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 982C961368 for ; Mon, 23 Aug 2021 12:54:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235738AbhHWMyo (ORCPT ); Mon, 23 Aug 2021 08:54:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235842AbhHWMyn (ORCPT ); Mon, 23 Aug 2021 08:54:43 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 499E5C061575 for ; Mon, 23 Aug 2021 05:54:01 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id d22-20020a1c1d16000000b002e7777970f0so253055wmd.3 for ; Mon, 23 Aug 2021 05:54:01 -0700 (PDT) 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=v5Ih0Qj2bIlVyaxT/bP+O9o75/OXqCHo7ClGoPbWYf8=; b=k2HPWo2XnuIRDUgf34HuaJ8PqadA5ddsIRRCM82pqvWGx7cF0MQ5DW1DoD1DFnbvTC apbBGMWnq6aeCP3Xb9NCnfjg0S3XsODZR8PE8C/cV6t0GKGYzfB6tegGDy74cDC/6QXY S+PlMR0XdKGY4j6Ty2ibktTeil+mGLBiVZDm1juxLela/qWs4+8D+lwBg6X/nGOrmq0/ BTvtJKvU1SnGk+sTnP9cSXMAqGFFlkBdK5Gp/vUwalWPd8qPE2mLxvqcfmhj6B1/LEnq g1SKVX+NBxeXc0e3hSomL1iNF5Axawts8z5Pyf6st5wrvNlLg1ER2wzMk8yeUkLhwuEa BrAg== 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=v5Ih0Qj2bIlVyaxT/bP+O9o75/OXqCHo7ClGoPbWYf8=; b=M/UtqV86Ga3pQAeJbbwxW18hTQEFL1HxbAtKFZm/xYedh3pmVlsu6extmFjUjVakoM SZ6vggH8xMs6bd6KYEvXVkO4VwhGtkuOnPRQK8n+ONEk5d3wGE1/qzlm2fkVZ1yINA7f Lu7KZzC38htU4ff9TcTU2X5SJjlt+isrBix3sjC+DvbT7tRZ1CjDMgLdmZK5qiwsCFHT zg3YSSXSSBw7XcOBWKMOvn0qmo4RPNegjP1VN1+E1HrYmJv96r3Iu4MYEYjn7cjACBcO 9O/4d2WXZJq3g/rWQa0p/XfnlBnjJxA5ia6b2LsXnIgDP8YIC9fRbtPzfZMpOwZ6sO/h FZaQ== X-Gm-Message-State: AOAM530jgOwwAjIOSfajjkP+qwWO66Atq0/gOpYHu6vvF7pMLQZctR08 Aht21h/Nih1eBjOCPDCyPAODVoDGbTM= X-Google-Smtp-Source: ABdhPJyeZiTlwBj2Opd3yOEsawB/Xvd2RV2bbzEm730EXd3BCTO4jfuKDJm7oNDGaLtyiBK6A0r/Tw== X-Received: by 2002:a1c:443:: with SMTP id 64mr10024462wme.180.1629723239743; Mon, 23 Aug 2021 05:53:59 -0700 (PDT) Received: from crow.. ([95.87.199.109]) by smtp.gmail.com with ESMTPSA id b10sm11469454wrt.43.2021.08.23.05.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 05:53:59 -0700 (PDT) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 2/3] trace-cruncher: Have standard 'not existing' message Date: Mon, 23 Aug 2021 15:53:47 +0300 Message-Id: <20210823125348.145615-2-y.karadz@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210823125348.145615-1-y.karadz@gmail.com> References: <20210823125348.145615-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The string "(nil)" is used to state that an object do not exists / is empty. Signed-off-by: Yordan Karadzhov (VMware) --- src/common.h | 2 ++ src/ftracepy-utils.c | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/common.h b/src/common.h index b7f6a87..476e4f1 100644 --- a/src/common.h +++ b/src/common.h @@ -24,6 +24,8 @@ static const char *NO_ARG = "/NONE/"; +#define TC_NIL_MSG "(nil)" + static inline bool is_all(const char *arg) { const char all[] = "all"; diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c index a3ae03a..4561a4d 100644 --- a/src/ftracepy-utils.c +++ b/src/ftracepy-utils.c @@ -68,7 +68,7 @@ PyObject *PyTepRecord_cpu(PyTepRecord* self) PyObject *PyTepEvent_name(PyTepEvent* self) { - const char * name = self->ptrObj ? self->ptrObj->name : "nil"; + const char *name = self->ptrObj ? self->ptrObj->name : TC_NIL_MSG; return PyUnicode_FromString(name); } @@ -169,7 +169,7 @@ PyObject *PyTepEvent_parse_record_field(PyTepEvent* self, PyObject *args, } if (!field_size) - return PyUnicode_FromString("(nil)"); + return PyUnicode_FromString(TC_NIL_MSG); if (field->flags & TEP_FIELD_IS_STRING) { char *val_str = record->ptrObj->data + field_offset; @@ -2098,7 +2098,7 @@ PyObject *PyFtrace_error_log(PyObject *self, PyObject *args, ret = PyUnicode_FromString(err_log); free(err_log); } else if (ok) { - ret = PyUnicode_FromString("(nil)"); + ret = PyUnicode_FromString(TC_NIL_MSG); } return ret; From patchwork Mon Aug 23 12:53:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12452793 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.7 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 754C0C432BE for ; Mon, 23 Aug 2021 12:54:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 59C4661368 for ; Mon, 23 Aug 2021 12:54:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235954AbhHWMyp (ORCPT ); Mon, 23 Aug 2021 08:54:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235842AbhHWMyo (ORCPT ); Mon, 23 Aug 2021 08:54:44 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CA01C061575 for ; Mon, 23 Aug 2021 05:54:02 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id u1so10496485wmm.0 for ; Mon, 23 Aug 2021 05:54:02 -0700 (PDT) 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=lcRAT9uatMWuoPKhISK7u8+gO7QvrVE+pXNHjzxluc0=; b=VIg03fO7qIoyXqLEtGV7jboy3Tlj9F7IqzG/BBTii1+ZV/Xs9eEtE1yrf+aQPTtqzU 0klFDNVog1Lh22NNA7y8x2hxYF11Yq5xr+VcGLcUuJ820GSaKHYVv0dEF2jPaqI1lJSr yoXhgiv2WYrUj7JsBc18DonFROwYZ4+aWI4wIxQLygKdNoVByMvZHUIJf+45v+objOw3 +cXxs9L94MIvumF3hpCG0fQ00qEIhQ41tfTq38WlhaOc66cMdHf0zwNjBn0FMR7berTt GeJ5ZzWwCkovW23XovJgVBwhO1O+fg1x0c8vpeO0HN0SmEuAn9C8dfGKxuQbZmdE1iG8 xtlg== 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=lcRAT9uatMWuoPKhISK7u8+gO7QvrVE+pXNHjzxluc0=; b=Se3fP07daPMpPv3SsliKZFR2cNUfIayEKBmqKT/mL5rxHoC9X9GgbV95s0rRzuUIkK fXsxvn9tdjcOEwuZ0vTxhF99MA0cPPCS7PsMRQ/6/C573i8A8VVNlD0c23n/9t+BGQzF kSFMLkbX5Nez7C7IeRtkRZyl4VKJxph8bKoxM/bgXPIT2Pux/Y7BrW2wF4/4WYzPrOXI 0bLR/YuFg0IJzANEsNJ+yjlY1mZAUVABaKGIRFVmhd5BNh2VDBs0amdDFfvp9VZiIydO UogNUsSy6DU09owRlpqiAcHUx00LIvdQ1OjmPOPkn1/aK4n7ULOVxwjRJtoPik5xAaeS tnlA== X-Gm-Message-State: AOAM532AXQMFL7Zf3F8rUzJ3J9N1G3SeSBPdSogRQm4IoLDBfdbrX2mr zuPHI6iD1O1huwMhlzByjaca0i9GWfk= X-Google-Smtp-Source: ABdhPJyEMHMMFuiLYaRwBaGTBPblGVzAD2rX5r1nSYqC1/NEPPwb+5aE1rv0e//a2sEOgVrGi8Iawg== X-Received: by 2002:a1c:6a04:: with SMTP id f4mr16080094wmc.54.1629723240482; Mon, 23 Aug 2021 05:54:00 -0700 (PDT) Received: from crow.. ([95.87.199.109]) by smtp.gmail.com with ESMTPSA id b10sm11469454wrt.43.2021.08.23.05.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 05:54:00 -0700 (PDT) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 3/3] trace-cruncher: Print the tracefs log on an error Date: Mon, 23 Aug 2021 15:53:48 +0300 Message-Id: <20210823125348.145615-3-y.karadz@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210823125348.145615-1-y.karadz@gmail.com> References: <20210823125348.145615-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Every time when an error is related to the usage of 'libtracefs' we check the tracefs error log and if it contains a message, this message is added to the stack of the Python error messages. Signed-off-by: Yordan Karadzhov (VMware) --- src/ftracepy-utils.c | 114 ++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 38 deletions(-) diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c index 4561a4d..208e21d 100644 --- a/src/ftracepy-utils.c +++ b/src/ftracepy-utils.c @@ -54,6 +54,45 @@ static bool tfs_clear_error_log(struct tracefs_instance *instance) return true; } +static void TfsError_fmt(struct tracefs_instance *instance, + const char *fmt, ...) +{ + char *tfs_err_log = tfs_error_log(instance, NULL); + va_list args; + + va_start(args, fmt); + if (tfs_err_log) { + char *tc_err_log; + + vasprintf(&tc_err_log, fmt, args); + va_end(args); + + PyErr_Format(TEP_ERROR, "%s\ntfs_error: %s", + tc_err_log, tfs_err_log); + + tfs_clear_error_log(instance); + free(tfs_err_log); + free(tc_err_log); + } else { + PyErr_FormatV(TEP_ERROR, fmt, args); + va_end(args); + } +} + +static void TfsError_setstr(struct tracefs_instance *instance, + const char *msg) +{ + char *tfs_err_log = tfs_error_log(instance, NULL); + + if (tfs_err_log) { + PyErr_Format(TEP_ERROR, "%s\ntfs_error: %s", msg, tfs_err_log); + tfs_clear_error_log(instance); + free(tfs_err_log); + } else { + PyErr_SetString(TEP_ERROR, msg); + } +} + PyObject *PyTepRecord_time(PyTepRecord* self) { unsigned long ts = self->ptrObj ? self->ptrObj->ts : 0; @@ -283,7 +322,7 @@ PyObject *PyTep_init_local(PyTep *self, PyObject *args, const char **sys_names = get_arg_list(system_list); if (!sys_names) { - PyErr_SetString(TFS_ERROR, + TfsError_setstr(NULL, "Inconsistent \"systems\" argument."); return NULL; } @@ -295,8 +334,7 @@ PyObject *PyTep_init_local(PyTep *self, PyObject *args, } if (!tep) { - PyErr_Format(TFS_ERROR, - "Failed to get local events from \'%s\'.", + TfsError_fmt(NULL, "Failed to get local events from \'%s\'.", dir_str); return NULL; } @@ -331,7 +369,7 @@ PyObject *PyTep_get_event(PyTep *self, PyObject *args, static bool check_file(struct tracefs_instance *instance, const char *file) { if (!tracefs_file_exists(instance, file)) { - PyErr_Format(TFS_ERROR, "File %s does not exist.", file); + TfsError_fmt(instance, "File %s does not exist.", file); return false; } @@ -341,7 +379,7 @@ static bool check_file(struct tracefs_instance *instance, const char *file) static bool check_dir(struct tracefs_instance *instance, const char *dir) { if (!tracefs_dir_exists(instance, dir)) { - PyErr_Format(TFS_ERROR, "Directory %s does not exist.", dir); + TfsError_fmt(instance, "Directory %s does not exist.", dir); return false; } @@ -366,7 +404,7 @@ static int write_to_file(struct tracefs_instance *instance, size = tracefs_instance_file_write(instance, file, val); if (size <= 0) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Can not write \'%s\' to file \'%s\' (inst: \'%s\').", val, file, get_instance_name(instance)); PyErr_Print(); @@ -386,7 +424,7 @@ static int append_to_file(struct tracefs_instance *instance, size = tracefs_instance_file_append(instance, file, val); if (size <= 0) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Can not append \'%s\' to file \'%s\' (inst: \'%s\').", val, file, get_instance_name(instance)); PyErr_Print(); @@ -406,7 +444,7 @@ static int read_from_file(struct tracefs_instance *instance, *val = tracefs_instance_file_read(instance, file, &size); if (size < 0) - PyErr_Format(TFS_ERROR, "Can not read from file %s", file); + TfsError_fmt(instance, "Can not read from file %s", file); return size; } @@ -529,7 +567,7 @@ PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args, if (!instance || !tracefs_instance_exists(name) || !tracefs_instance_is_new(instance)) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to create new trace instance \'%s\'.", name); return NULL; @@ -633,7 +671,7 @@ PyObject *PyFtrace_set_current_tracer(PyObject *self, PyObject *args, } if (!all_tracers || !all_tracers[i]) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Tracer \'%s\' is not available.", tracer); return NULL; @@ -643,7 +681,7 @@ PyObject *PyFtrace_set_current_tracer(PyObject *self, PyObject *args, } if (!write_to_file_and_check(instance, file, tracer)) { - PyErr_Format(TFS_ERROR, "Failed to enable tracer \'%s\'", + TfsError_fmt(instance, "Failed to enable tracer \'%s\'", tracer); return NULL; } @@ -762,7 +800,7 @@ bool get_event_enable_file(struct tracefs_instance *instance, fail: instance_name = instance ? tracefs_instance_get_name(instance) : "top"; - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to locate event:\n Instance: %s System: %s Event: %s", instance_name, system, event); free(buff); @@ -788,7 +826,7 @@ static bool event_enable_disable(struct tracefs_instance *instance, ret = tracefs_event_disable(instance, system, event); if (ret != 0) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to enable/disable event:\n System: %s Event: %s", system ? system : "NULL", event ? event : "NULL"); @@ -878,7 +916,7 @@ static bool set_enable_events(PyObject *self, PyObject *args, PyObject *kwargs, is_all(PyUnicode_DATA(event_list))) { return event_enable_disable(instance, NULL, NULL, enable); } else { - PyErr_SetString(TFS_ERROR, + TfsError_setstr(instance, "Failed to enable events for unspecified system"); return false; } @@ -886,7 +924,7 @@ static bool set_enable_events(PyObject *self, PyObject *args, PyObject *kwargs, systems = get_arg_list(system_list); if (!systems) { - PyErr_SetString(TFS_ERROR, "Inconsistent \"systems\" argument."); + TfsError_setstr(instance, "Inconsistent \"systems\" argument."); return false; } @@ -926,7 +964,7 @@ static bool set_enable_events(PyObject *self, PyObject *args, PyObject *kwargs, return true; fail_with_err: - PyErr_SetString(TFS_ERROR, "Inconsistent \"events\" argument."); + TfsError_setstr(instance, "Inconsistent \"events\" argument."); fail: free(systems); @@ -1025,7 +1063,7 @@ PyObject *PyFtrace_set_event_filter(PyObject *self, PyObject *args, sprintf(path, "events/%s/%s/filter", system, event); if (!write_to_file_and_check(instance, path, filter)) { - PyErr_SetString(TFS_ERROR, "Failed to set event filter"); + TfsError_setstr(instance, "Failed to set event filter"); return NULL; } @@ -1056,7 +1094,7 @@ PyObject *PyFtrace_clear_event_filter(PyObject *self, PyObject *args, sprintf(path, "events/%s/%s/filter", system, event); if (!write_to_file(instance, path, OFF)) { - PyErr_SetString(TFS_ERROR, "Failed to clear event filter"); + TfsError_setstr(instance, "Failed to clear event filter"); return NULL; } @@ -1072,7 +1110,7 @@ static bool tracing_ON(struct tracefs_instance *instance) const char *instance_name = instance ? tracefs_instance_get_name(instance) : "top"; - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to start tracing (Instance: %s)", instance_name); return false; @@ -1104,7 +1142,7 @@ static bool tracing_OFF(struct tracefs_instance *instance) const char *instance_name = instance ? tracefs_instance_get_name(instance) : "top"; - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to stop tracing (Instance: %s)", instance_name); return false; @@ -1141,7 +1179,7 @@ PyObject *PyFtrace_is_tracing_ON(PyObject *self, PyObject *args, const char *instance_name = instance ? tracefs_instance_get_name(instance) : "top"; - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Failed to check if tracing is ON (Instance: %s)", instance_name); return NULL; @@ -1202,7 +1240,7 @@ static bool set_pid(struct tracefs_instance *instance, return true; fail: - PyErr_Format(TFS_ERROR, "Failed to set PIDs for \"%s\"", + TfsError_fmt(instance, "Failed to set PIDs for \"%s\"", file); return false; } @@ -1266,7 +1304,7 @@ static bool set_opt(struct tracefs_instance *instance, if (sprintf(file, "options/%s", opt) <= 0 || !write_to_file_and_check(instance, file, val)) { - PyErr_Format(TFS_ERROR, "Failed to set option \"%s\"", opt); + TfsError_fmt(instance, "Failed to set option \"%s\"", opt); return false; } @@ -1398,7 +1436,7 @@ static bool register_kprobe(const char *event, const char *probe) { if (tracefs_kprobe_raw(TC_SYS, event, function, probe) < 0) { - PyErr_Format(TFS_ERROR, "Failed to register kprobe \'%s\'.", + TfsError_fmt(NULL, "Failed to register kprobe \'%s\'.", event); return false; } @@ -1411,7 +1449,7 @@ static bool register_kretprobe(const char *event, const char *probe) { if (tracefs_kretprobe_raw(TC_SYS, event, function, probe) < 0) { - PyErr_Format(TFS_ERROR, "Failed to register kretprobe \'%s\'.", + TfsError_fmt(NULL, "Failed to register kretprobe \'%s\'.", event); return false; } @@ -1422,7 +1460,7 @@ static bool register_kretprobe(const char *event, static bool unregister_kprobe(const char *event) { if (tracefs_kprobe_clear_probe(TC_SYS, event, true) < 0) { - PyErr_Format(TFS_ERROR, "Failed to unregister kprobe \'%s\'.", + TfsError_fmt(NULL, "Failed to unregister kprobe \'%s\'.", event); return false; } @@ -1571,7 +1609,7 @@ PyObject *PyKprobe_set_filter(PyKprobe *self, PyObject *args, sprintf(path, "events/%s/%s/filter", TC_SYS, self->ptrObj->event); if (!write_to_file_and_check(instance, path, filter)) { - PyErr_SetString(TFS_ERROR, "Failed to set kprobe filter."); + TfsError_setstr(instance, "Failed to set kprobe filter."); return NULL; } @@ -1599,7 +1637,7 @@ PyObject *PyKprobe_clear_filter(PyKprobe *self, PyObject *args, sprintf(path, "events/%s/%s/filter", TC_SYS, self->ptrObj->event); if (!write_to_file(instance, path, OFF)) { - PyErr_SetString(TFS_ERROR, "Failed to clear kprobe filter."); + TfsError_setstr(instance, "Failed to clear kprobe filter."); return NULL; } @@ -1723,7 +1761,7 @@ static bool hook2pid(struct tracefs_instance *instance, PyObject *pid_val, int f return true; fail: - PyErr_SetString(TFS_ERROR, "Failed to hook to PID"); + TfsError_setstr(instance, "Failed to hook to PID"); PyErr_Print(); return false; } @@ -1740,7 +1778,7 @@ static void start_tracing_procces(struct tracefs_instance *instance, tracing_ON(instance); if (execvpe(argv[0], argv, envp) < 0) { - PyErr_Format(TFS_ERROR, "Failed to exec \'%s\'", + TfsError_fmt(instance, "Failed to exec \'%s\'", argv[0]); } @@ -1754,14 +1792,14 @@ static PyObject *get_callback_func(const char *plugin_name, const char * py_call py_name = PyUnicode_FromString(plugin_name); py_module = PyImport_Import(py_name); if (!py_module) { - PyErr_Format(TFS_ERROR, "Failed to import plugin \'%s\'", + TfsError_fmt(NULL, "Failed to import plugin \'%s\'", plugin_name); return NULL; } py_func = PyObject_GetAttrString(py_module, py_callback); if (!py_func || !PyCallable_Check(py_func)) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(NULL, "Failed to import callback from plugin \'%s\'", plugin_name); return NULL; @@ -1817,7 +1855,7 @@ static bool notrace_this_pid(struct tracefs_instance *instance) if (!pid2file(instance, "set_ftrace_notrace_pid", pid, true) || !pid2file(instance, "set_event_notrace_pid", pid, true)) { - PyErr_SetString(TFS_ERROR, + TfsError_setstr(instance, "Failed to desable tracing for \'this\' process."); return false; } @@ -1849,7 +1887,7 @@ static bool init_callback_tep(struct tracefs_instance *instance, *tep = tracefs_local_events(tracefs_instance_get_dir(instance)); if (!*tep) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(instance, "Unable to get 'tep' event from instance \'%s\'.", get_instance_name(instance)); return false; @@ -1892,7 +1930,7 @@ PyObject *PyFtrace_trace_shell_process(PyObject *self, PyObject *args, pid = fork(); if (pid < 0) { - PyErr_SetString(TFS_ERROR, "Failed to fork"); + TfsError_setstr(instance, "Failed to fork"); return NULL; } @@ -1938,7 +1976,7 @@ PyObject *PyFtrace_trace_process(PyObject *self, PyObject *args, return NULL; if (!PyList_CheckExact(py_argv)) { - PyErr_SetString(TFS_ERROR, "Failed to parse \'argv\' list"); + TfsError_setstr(instance, "Failed to parse \'argv\' list"); return NULL; } @@ -1946,7 +1984,7 @@ PyObject *PyFtrace_trace_process(PyObject *self, PyObject *args, pid = fork(); if (pid < 0) { - PyErr_SetString(TFS_ERROR, "Failed to fork"); + TfsError_setstr(instance, "Failed to fork"); return NULL; } @@ -1988,7 +2026,7 @@ PyObject *PyFtrace_read_trace(PyObject *self, PyObject *args, tracing_ON(pipe_instance); if (tracefs_trace_pipe_print(pipe_instance, 0) < 0) { - PyErr_Format(TFS_ERROR, + TfsError_fmt(pipe_instance, "Unable to read trace data from instance \'%s\'.", get_instance_name(pipe_instance)); return NULL;