From patchwork Mon Feb 7 16:38:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12737681 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 AA595C433FE for ; Mon, 7 Feb 2022 16:55:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235333AbiBGQy4 (ORCPT ); Mon, 7 Feb 2022 11:54:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357064AbiBGQjO (ORCPT ); Mon, 7 Feb 2022 11:39:14 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C439C0401D2 for ; Mon, 7 Feb 2022 08:39:13 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id l25so10944833eda.12 for ; Mon, 07 Feb 2022 08:39:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LBorQjXs70DN6wgM58LhoarVcXmwRYF2XLFhlXFRxDo=; b=iIgSeJUWXgEE5tcJ7dZxHfNs2LPyNdyTWM1hCyGN58DhcZv2ae4VNvRW2eJQOER6z4 r392OBvwXC5Ta0qNmLbSKMGGgfMiH6hIKGP+b6waVAbL5Kd0nTDcksPXWXNvRItr9zod vBLlb+bkDOUJ74tu7I8AVoLep3OzHiDyW2OIV+kbKtiU2VHuTrImTtu7R8dX8ZpOFmSH m8s8mfBkXXmZLSmgLf+8+xwz6wMLN7r1irHsSetb8CigH8UO+Lk0ddEv+E+yI+JgWIgG hJZOi9A5YxIFBDptMQiAVNempkArVyyb89OdV+a3wrAfs+5M4+Hoq0HNA4F3lDu+hTxY bwuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LBorQjXs70DN6wgM58LhoarVcXmwRYF2XLFhlXFRxDo=; b=K8BnF8i5fsk0b84OC5vF4gwD69/CLqKpiKjPNPBIO7PcSfMTp0r14E+iJxl4olEizI /YWNe8WGrfrPlawRWlB7Z6dA2r5PsYJE7cIc9vdA4M933/SxPebfN0Gr15ww2yei/QBm FCXRSiJLdhA+bsmhhBHEvj+f5DCh8uPDr3URehiJFRIrPIj/b4VHc6i0QByZbDjTBuWN siJE7FBUtxO8fRf6bFePvynqDOFvIx5rScaLSyYXsxJXPrEWYrIo2VyAYSjQ64HIcV4V JAtqS9slv7BicoyDX5p1HPIbIqnL0JgyB/gNwOOHHBv/J/pWeA2xjDXOu6QRUxHiX009 vPvA== X-Gm-Message-State: AOAM5337CbghdPMDHh5IsX31UCyQS371CtQU6Nn3OaeDMNA5cjrNU5/l vm5LP1rDLx/1jOmd3no1KPuwzLAqYUo= X-Google-Smtp-Source: ABdhPJxfGbn9AXMR9OWeZ+wpRFHVeE0TKBBEagdtu3KUjUbViy5n69EqadfrMrai5ryZCEBKu/uX0g== X-Received: by 2002:a05:6402:35ca:: with SMTP id z10mr314441edc.43.1644251951693; Mon, 07 Feb 2022 08:39:11 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id b7sm5415814edv.58.2022.02.07.08.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 08:39:10 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 1/4] trace-cruncher: Fix coments & style in ft_utils.py Date: Mon, 7 Feb 2022 18:38:57 +0200 Message-Id: <20220207163900.178524-1-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org No functional changes are introduced by this patch. Signed-off-by: Yordan Karadzhov (VMware) --- tracecruncher/ft_utils.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py index 26978f3..26a7306 100644 --- a/tracecruncher/ft_utils.py +++ b/tracecruncher/ft_utils.py @@ -14,8 +14,8 @@ from . import ftracepy as ft def local_tep(): """ Get the "tep" event of the current system (local). """ - tep = ft.tep_handle(); - tep.init_local(dir=ft.dir()); + tep = ft.tep_handle() + tep.init_local(dir=ft.dir()) return tep @@ -23,8 +23,8 @@ def local_tep(): def find_event_id(system, event): """ Get the unique identifier of a trace event. """ - tep = ft.tep_handle(); - tep.init_local(dir=ft.dir(), systems=[system]); + tep = ft.tep_handle() + tep.init_local(dir=ft.dir(), systems=[system]) return tep.get_event(system=system, name=event).id() @@ -51,7 +51,7 @@ class event: self.evt_id = -1 def id(self): - """ Retrieve the unique ID of the kprobe event. + """ Retrieve the unique ID of the event. """ return int(self.evt_id) @@ -74,7 +74,7 @@ class event: filter=filter) def clear_filter(self, instance=ft.no_arg()): - """ Define the filter for this event. + """ Clear the filter for this event. """ ft.clear_event_filter(instance=instance, system=self.system, @@ -120,7 +120,7 @@ class kprobe(kprobe_base): self.add_raw_field(name, probe) def add_array_arg(self, name, param_id, param_type, offset=0, size=-1): - """ Add a array parameter data field to this probe. + """ Add an array function parameter data field to this probe. """ if size < 0: size = 10 @@ -133,7 +133,7 @@ class kprobe(kprobe_base): self.add_raw_field(field_name, probe) def add_string_arg(self, name, param_id, offset=0, usr_space=False): - """ Add a pointer function parameter data field to this probe. + """ Add a string function parameter data field to this probe. """ p_type = 'ustring' if usr_space else 'string' self.add_ptr_arg(name=name, @@ -142,7 +142,7 @@ class kprobe(kprobe_base): offset=offset) def add_string_array_arg(self, name, param_id, offset=0, usr_space=False, size=-1): - """ Add a string array parameter data field to this probe. + """ Add a string array function parameter data field to this probe. """ p_type = 'ustring' if usr_space else 'string' self.add_array_arg(name=name, @@ -156,13 +156,13 @@ class kprobe(kprobe_base): """ probe = ' '.join('{!s}={!s}'.format(key,val) for (key, val) in self.fields.items()) - self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe); - self.kp.register(); + self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe) + self.kp.register() self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) def parse_record_array_field(event, record, field, size=-1): - """ Register this probe to Ftrace. + """ Parse the content of an array function parameter data field. """ if size < 0: size = 10 @@ -187,8 +187,8 @@ class kretval_probe(kprobe_base): def register(self): """ Register this probe to Ftrace. """ - self.kp = ft.kprobe(event=self.name, function=self.func); - self.kp.register(); + self.kp = ft.kprobe(event=self.name, function=self.func) + self.kp.register() self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) @@ -200,7 +200,7 @@ class khist: self.name = name self.inst = None - inst_name = name+'_inst' + inst_name = name + '_inst' if find: self.inst = ft.find_instance(name=inst_name) self.attached = False From patchwork Mon Feb 7 16:38:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12737680 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 9B2E8C4332F for ; Mon, 7 Feb 2022 16:55:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235998AbiBGQy6 (ORCPT ); Mon, 7 Feb 2022 11:54:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357080AbiBGQjQ (ORCPT ); Mon, 7 Feb 2022 11:39:16 -0500 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB6FDC0401D1 for ; Mon, 7 Feb 2022 08:39:14 -0800 (PST) Received: by mail-ed1-x531.google.com with SMTP id w14so31359597edd.10 for ; Mon, 07 Feb 2022 08:39:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IB+d1Y5BRgLdCqTRs25S/swtJs2CbCN0kOoAxnHvQA8=; b=fI9Xk0GLwfgMyDCzQmDqjJpv/RsFbFz9KFZgsEC6l6jXG54rVluZyO+1dErub652Gj zasqAw7ZmUUf2v9qy3B5g2ymUFHVKeewXg50Av9ZjGby0OJMlbaXp/5pRDWhME7o29Np Ry0OHmeOvU5oi57dDqRyaXzuhi2wfqa4up+uK6wXZ8F1qOoRIl/XmcBPAKE2VPOSwZxz 0R/T+yMlIVyhl8rgtZfq4fH7nAD0/ul8vpXcva/RGPRfQ6vMCi5/VuEIySjHJx4oIxVD QkwrwqYpJnuymPUgSV5bJntqanCX13JuhVh+RhV2xVWAZhyaGWHfUuWG9ArGPkPEwP32 qhdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IB+d1Y5BRgLdCqTRs25S/swtJs2CbCN0kOoAxnHvQA8=; b=X0TJOwK83J0fI5dSKxwhSaqQLrXyzjgK96yCuo0CBdkrzs7kpUlBZJIVIz0BVyVSkv Uw2RcW9KYdaPt9gss17k/VWYiv4WFEedJOtJKVarW6+nnAwd4yszXu+/O8wSmC30vJAT RlW9IIf1/U5gwuY2tkyj0EqfWj1sEl7ASa3Kn7ABed422EGPqStOi9tWhkreEsz6+pjU +pC/3f8OPLCjZMIoxj6ZdYB0k+rWUG3Porx72ohEkJlqbTPdXG9kkitn4F0ho0xRUfTY DYc9XqBZFV4Pzq1KXcqbTK10GyVDZ7x7lEtrTOrqbvmVivRlg5kNPKgB6KVSj/wt2fiE jKKQ== X-Gm-Message-State: AOAM5321ihHDlE/Kv+VhHEntgq5SDWvgD275I3vTjUsgoUlgpctESxxJ /H/s6WmtFxIgWxz/vU9KCY+c3wSosoc= X-Google-Smtp-Source: ABdhPJz9eCGsABoqJyUFspi4QKjb9JXwApc9H9RaLZBt77wSrFaLKLVdwXnE5aoWXbJ9fEy/zXI18A== X-Received: by 2002:a05:6402:1757:: with SMTP id v23mr308836edx.105.1644251952777; Mon, 07 Feb 2022 08:39:12 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id b7sm5415814edv.58.2022.02.07.08.39.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 08:39:12 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 2/4] trace-cruncher: Fix misleading comment in start_tracing.py Date: Mon, 7 Feb 2022 18:38:58 +0200 Message-Id: <20220207163900.178524-2-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207163900.178524-1-y.karadz@gmail.com> References: <20220207163900.178524-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The new comment in the code describes correctly the example usage of the 'enable_events' API. Signed-off-by: Yordan Karadzhov (VMware) --- examples/start_tracing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/start_tracing.py b/examples/start_tracing.py index aaeb0cf..367c626 100755 --- a/examples/start_tracing.py +++ b/examples/start_tracing.py @@ -11,7 +11,8 @@ import tracecruncher.ftracepy as ft # Create new Ftrace instance to work in. inst = ft.create_instance() -# Enable all static events from systems "sched" and "irq". +# Enable several static events, including "sched_switch" and "sched_waking" +# from systems "sched" and all events from system "irq". ft.enable_events(instance=inst, events={'sched': ['sched_switch', 'sched_waking'], 'irq': ['all']}) From patchwork Mon Feb 7 16:38:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12737678 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 E5519C433EF for ; Mon, 7 Feb 2022 16:54:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231215AbiBGQy0 (ORCPT ); Mon, 7 Feb 2022 11:54:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357089AbiBGQjQ (ORCPT ); Mon, 7 Feb 2022 11:39:16 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6808EC0401D9 for ; Mon, 7 Feb 2022 08:39:15 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id s7so4690504edd.3 for ; Mon, 07 Feb 2022 08:39:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7caFXxpaFqz4EtyhSF51ykUPHja0w8urgfzVsqd0oFY=; b=JHQoOaBtw4Vhuz6l97ySnIk6bScJIg+5y8E560cO10afA9Z7Gn3gQCZPqN+bXEjar0 PPuXGZAZBP6fxb7HqcfUMWBf67KbHBvUxb5gWC9qWkZtV1sGJEcUTIoPrDPurI6U/cDk nJdc6yrufqwy6O85/ZucikLcsc9lVBozVFZY5/YDoYPW4tE17eYjxU6Y/yZVdoSHsk8H Wj0ExcGD7iT8E0zAykPFK1V/lSu0f7WeifrufiKA0WmB84vbnDFYpZJ5BUaRvcP89JUw QQxvzLPPQ3o+X9S3rQqxiEYLoYIEX18yt4vtMbLrd2kzCkMCMg+YqooJf3aZOf1ThAvz +M/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7caFXxpaFqz4EtyhSF51ykUPHja0w8urgfzVsqd0oFY=; b=Yq1zJIU5eO9spZiC7QAHu2+Ykoi/4APn042qdaoMMPtBer656M6/oD5gWwBV3cn13u m6w8E+1MDi//NU335iN95jc8HJRaQkGTQ2IeMPje46qAwlbOHoNDITloVKQWHmPf995w 030VTn354FeFQ+YeiavApTqOcZkTR0JE9eZ8RMfIPcJp+VdtCMNW36yZj1215f2NF9dT o4F44Ld/U/4BeUuviDdzoYSfxbYy+ybMCzYyEDxVHer2B0dEcIqPk2pwN4Nxabc/HdWi K+4AbQh2y/NvM8Sii3eB2f850E94AfK/a9WrFACm5uWmlr6Td8RixT+8nQXMcmAqxO+S 5jgA== X-Gm-Message-State: AOAM530sYIydN4P6nxG2lxuBofhfL5p5QMlVd/vtyG2pav6xR5TEB6Qs ZpcjF/qIIQ8wyA605mQMy/4Bc16Z5g0= X-Google-Smtp-Source: ABdhPJxupLWcz8RmRkCF7tjLklPidJ7JsBe7ml9VMCrgUoa7WYcHjgonf2Ayq6Zko6nh0FxhwgBqKg== X-Received: by 2002:aa7:d898:: with SMTP id u24mr339909edq.60.1644251953641; Mon, 07 Feb 2022 08:39:13 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id b7sm5415814edv.58.2022.02.07.08.39.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 08:39:13 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 3/4] trace-cruncher: Refactor the python classes for kprobes Date: Mon, 7 Feb 2022 18:38:59 +0200 Message-Id: <20220207163900.178524-3-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207163900.178524-1-y.karadz@gmail.com> References: <20220207163900.178524-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Currently the usage of the python classes for kprobes exhibits a pattern that is inconsistent with the usage of the classes for histograms and synthetic events. The constructors of the histograms and synth. event classes will directly register the new events on the system, so once the corresponding object is created it is ready to be used. However, this is not the case for the constructors of the classes 'kprobe' and 'kretval_probe'. For these two classes, the user is expected to explicitly call the 'register' method before using the new dynamic event. Since such inconsistency can be confusing, here we refactor the 'kprobe' and 'kretval_probe' classes, eliminating the 'register' method and moving the registration inside the constructor. Signed-off-by: Yordan Karadzhov (VMware) --- tracecruncher/ft_utils.py | 136 ++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 71 deletions(-) diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py index 26a7306..741887b 100644 --- a/tracecruncher/ft_utils.py +++ b/tracecruncher/ft_utils.py @@ -81,87 +81,86 @@ class event: event=self.name) -class kprobe_base(event): - def __init__(self, name, func=''): +class _kprobe_base(event): + def __init__(self, name, func): """ Constructor. """ super().__init__(system=ft.tc_event_system(), name=name, static=False) self.func = func self.kp = None + self.evt_id = -1 - def set_function(self, name): - """ Set the name of the function to be traced. + def register(self): + """ Register this probe to Ftrace. """ - self.func = name - + self.kp.register() + self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) -class kprobe(kprobe_base): - def __init__(self, name, func=''): +class kprobe(_kprobe_base): + def __init__(self, name, func, fields): """ Constructor. """ super().__init__(name, func) - self.fields = {} - - def add_raw_field(self, name, probe): - """ Add a raw definition of a data field to this probe. - """ - self.fields[str(name)] = str(probe) - - def add_arg(self, name, param_id, param_type): - """ Add a function parameter data field to this probe. - """ - probe = '$arg{0}:{1}'.format(param_id, param_type) - self.add_raw_field(name, probe) + self.fields = fields + probe = ' '.join('{!s}={!s}'.format(key,val) for (key, val) in self.fields.items()) + self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe) + self.register() - def add_ptr_arg(self, name, param_id, param_type, offset=0): - """ Add a pointer function parameter data field to this probe. - """ - probe = '+{0}($arg{1}):{2}'.format(offset, param_id, param_type) - self.add_raw_field(name, probe) +def kprobe_add_raw_field(name, probe, fields={}): + """ Add a raw definition of a data field to the probe descriptor. + """ + fields[str(name)] = str(probe) + return fields - def add_array_arg(self, name, param_id, param_type, offset=0, size=-1): - """ Add an array function parameter data field to this probe. - """ - if size < 0: - size = 10 - - ptr_size = ctypes.sizeof(ctypes.c_voidp) - for i in range(size): - field_name = name + str(i) - probe = '+{0}(+{1}'.format(offset, i * ptr_size) - probe += '($arg{0})):{1}'.format(param_id, param_type) - self.add_raw_field(field_name, probe) - - def add_string_arg(self, name, param_id, offset=0, usr_space=False): - """ Add a string function parameter data field to this probe. - """ - p_type = 'ustring' if usr_space else 'string' - self.add_ptr_arg(name=name, - param_id=param_id, - param_type=p_type, - offset=offset) - - def add_string_array_arg(self, name, param_id, offset=0, usr_space=False, size=-1): - """ Add a string array function parameter data field to this probe. - """ - p_type = 'ustring' if usr_space else 'string' - self.add_array_arg(name=name, - param_id=param_id, - param_type=p_type, - offset=offset, - size=size) +def kprobe_add_arg(name, param_id, param_type, fields={}): + """ Add a function parameter data field to the probe descriptor. + """ + probe = '$arg{0}:{1}'.format(param_id, param_type) + return kprobe_add_raw_field(name=name, probe=probe, fields=fields) - def register(self): - """ Register this probe to Ftrace. - """ - probe = ' '.join('{!s}={!s}'.format(key,val) for (key, val) in self.fields.items()) +def kprobe_add_ptr_arg(name, param_id, param_type, offset=0, fields={}): + """ Add a pointer function parameter data field to the probe descriptor. + """ + probe = '+{0}($arg{1}):{2}'.format(offset, param_id, param_type) + return kprobe_add_raw_field(name=name, probe=probe, fields=fields) - self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe) - self.kp.register() - self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) +def kprobe_add_array_arg(name, param_id, param_type, offset=0, + size=-1, fields={}): + """ Add an array function parameter data field to the probe descriptor. + """ + if size < 0: + size = 10 + ptr_size = ctypes.sizeof(ctypes.c_voidp) + for i in range(size): + field_name = name + str(i) + probe = '+{0}(+{1}'.format(offset, i * ptr_size) + probe += '($arg{0})):{1}'.format(param_id, param_type) + return kprobe_add_raw_field(name=field_name, probe=probe, fields=fields) -def parse_record_array_field(event, record, field, size=-1): +def kprobe_add_string_arg(name, param_id, offset=0, usr_space=False, fields={}): + """ Add a string function parameter data field to the probe descriptor. + """ + p_type = 'ustring' if usr_space else 'string' + return kprobe_add_ptr_arg(name=name, + param_id=param_id, + param_type=p_type, + offset=offset, + fields=fields) + +def kprobe_add_string_array_arg(name, param_id, offset=0, usr_space=False, + size=-1, fields={}): + """ Add a string array function parameter data field to the probe descriptor. + """ + p_type = 'ustring' if usr_space else 'string' + return kprobe_add_array_arg(name=name, + param_id=param_id, + param_type=p_type, + offset=offset, + size=size, + fields=fields) + +def kprobe_parse_record_array_field(event, record, field, size=-1): """ Parse the content of an array function parameter data field. """ if size < 0: @@ -178,18 +177,13 @@ def parse_record_array_field(event, record, field, size=-1): return arr -class kretval_probe(kprobe_base): - def __init__(self, name, func=''): +class kretval_probe(_kprobe_base): + def __init__(self, name, func): """ Constructor. """ super().__init__(name, func) - - def register(self): - """ Register this probe to Ftrace. - """ self.kp = ft.kprobe(event=self.name, function=self.func) self.kp.register() - self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) class khist: From patchwork Mon Feb 7 16:39:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12737679 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 78D66C433EF for ; Mon, 7 Feb 2022 16:55:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230314AbiBGQyz (ORCPT ); Mon, 7 Feb 2022 11:54:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357091AbiBGQjR (ORCPT ); Mon, 7 Feb 2022 11:39:17 -0500 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 1D569C0401DB for ; Mon, 7 Feb 2022 08:39:16 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id p24so6114797ejo.1 for ; Mon, 07 Feb 2022 08:39:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rpK1JshPl5/VxUVTUP9cBns8MPGKlzp5cv4pZGfXtOM=; b=IKHStMUPNsUG1PnWQM0stsTZo1DPtZPaTrDc5SqHeXwj7RhgCkWNExc3/hfIs4NYyR qMH/tza6r0/yNmRAuWAv01Kj5L7usxVFFNPldtq+XojpyfUJrIWr5E1nukSH6k1kYQDN iBWyW+xI5EW7Wbztas/QnlNJHJtzeSGMVkWt1WNKP5qnSb0C+4AhUAEUC+cjkcuDvPmh h75yIS1ujqVS3qRIjXa8EBaCgkVwpWoAeftrQnLgfUXWLE112850A4BX0cSViOPkK9kL /TEmvcdyo9qO4fzZqXViHSaMYiJqLed3aPwvQtCTdfJn1CaIk7b9tXwlRU/yNGj29Hmm uVoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rpK1JshPl5/VxUVTUP9cBns8MPGKlzp5cv4pZGfXtOM=; b=pTvKx+13he1LFzPbZ/ZIKXk05SqpVEiL7ybMknNo/XRbjaP5EDOUWh8X35uoRsBjJ3 iLNhAHfW/gmlRgyGVSRoYDMkw9I8Rw9DVW2/Tk2V3SnPGJ3ma4ZeqlTtdXwNhvBvk3tX YsDvRxD9WhcAldGeBAE0/4uWoJ5xDCgkaK49RVD7TMXkVQN4wL6J/XQe0gUJV31HH2WJ lottNe69OPkDm2SB6YyGsxMiVEeMdBxOHif8Tyi+LQHwU6iJPqjaFxjv9mb+p1xaZH+c d0PHY+hka1u0SQzwf1E5/wTaE36WwrKf7AanTS/SL+OrzyXtXGK7xBkmAMqNHcrBYWUJ JoVA== X-Gm-Message-State: AOAM531WNNyqtCOCg38a6H7lulH9G8qujaAFCMI7ZCa+vo6I+o6MH2RH DqO5uMgZzi2NKnC85waPLhF0vwa5r9w= X-Google-Smtp-Source: ABdhPJwCdynaeA3YHkwbNfUiKA0cr1I+1tKZeT/5s1bEpBdzXp2M4lR/PqXRvTgCL+xcqLgLS6DCpA== X-Received: by 2002:a17:906:af63:: with SMTP id os3mr458198ejb.204.1644251954397; Mon, 07 Feb 2022 08:39:14 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id b7sm5415814edv.58.2022.02.07.08.39.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 08:39:14 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 4/4] trace-cruncher: Update 'kprobe_open.py' example Date: Mon, 7 Feb 2022 18:39:00 +0200 Message-Id: <20220207163900.178524-4-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207163900.178524-1-y.karadz@gmail.com> References: <20220207163900.178524-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The class kprobe has been refactored. Here we update the corresponding example accordingly. We also add more comments, explaining the usage of the APIs. Signed-off-by: Yordan Karadzhov (VMware) --- examples/kprobe_open.py | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/examples/kprobe_open.py b/examples/kprobe_open.py index dff4861..141df66 100755 --- a/examples/kprobe_open.py +++ b/examples/kprobe_open.py @@ -11,24 +11,42 @@ import sys import tracecruncher.ftracepy as ft import tracecruncher.ft_utils as tc -open_probe = tc.kprobe(name='open', func='do_sys_openat2') +# We will define a kprobe event, to be recorded every time the function +# 'do_sys_openat2' in the kernrl is called. -open_probe.add_string_arg(name='file', param_id=2) +# Add to the kprobe event a string field 'file', that will record the second +# argument of the function. +fields = tc.kprobe_add_string_arg(name='file', param_id=2) -open_probe.add_ptr_arg(name='flags', - param_id=3, - param_type='x64') +# Add to the kprobe event a field 'flags'. This field will take the third +# argument of the function, which is a pointer to an object and will convert +# its starting address into hexadecimal integer. This will decode the first +# data field of the object. +fields = tc.kprobe_add_ptr_arg(name='flags', + param_id=3, + param_type='x64', + fields=fields) -open_probe.add_ptr_arg(name='mode', - param_id=3, - param_type='x64', - offset=8) +# Add to the kprobe event a field 'flags'. This field will take the third +# argument of the function, which is a pointer to an object and will convert +# its starting address plus offset of 8 into hexadecimal integer. This will +# decode the second data field of the object. +fields = tc.kprobe_add_ptr_arg(name='mode', + param_id=3, + param_type='x64', + offset=8, + fields=fields) -open_probe.register() +# Create the kprobe event. +open_probe = tc.kprobe(name='open', + func='do_sys_openat2', + fields=fields) tep = tc.local_tep() tc.short_kprobe_print(tep, [open_probe]) +# Define a callback function that will print +# a short human-readable version. def callback(event, record): print(tep.info(event, record)) @@ -38,6 +56,13 @@ if __name__ == "__main__": print('Usage: ', sys.argv[0], ' [PROCESS]') sys.exit(1) + # Create new Ftrace instance to work in. The tracing in this new instance + # is not going to be enabled yet. inst = ft.create_instance(tracing_on=False) + + # Enable the probe. open_probe.enable(instance=inst) + + # Subscribe for the kprobe event (using the default function name 'callback') + # and trace the user process. ft.trace_process(instance=inst, argv=sys.argv[1:])