ftrace: Convert shell tests to C
v4 cover ftrace: Convert shell tests to C
This patch series converts the ftrace shell-based tests to C implementation using LTP's new API, addressing GitHub issue #1282.
The conversion provides:
- Better integration with LTP framework
- Improved error handling and reporting
- More maintainable code structure
- Consistent test behavior across platforms
Changes in v4:
- Fixed all trailing whitespace on blank lines in ftrace_lib.c (20+ locations)
- Corrected ftrace_regression01 commit message to accurately describe test behavior (tests userstacktrace with page fault events, not general function tracing)
- Corrected ftrace_regression02 commit message to describe actual test (verifies signal:signal_generate tracepoint fields, not function_graph tracer)
- Replaced inline tst_kvercmp() check with .min_kver = "3.2" in ftrace_regression02
- Removed incorrect LTP_TIMEOUT_MUL parsing in ftrace_stress_test setup()
- All whitespace issues verified with checkpatch.pl
Changes in v3:
- Fixed tst_tmpdir_path() usage by adding .needs_tmpdir = 1 to all tests
- Removed incorrect CVE-2015-8550 tag from ftrace_regression01
- Fixed memory leak in TST_RETRY_FUNC usage by assigning to variable and freeing
- Changed [Algorithm] sections from numbered lists to bullet points (using '-')
- Added root requirement documentation to all test doc comments
- Fixed Makefile LDLIBS placement (moved before generic_leaf_target.mk include)
- Created .gitignore for new test binaries
- Removed deprecated [Description] header from ftrace_stress_test
- Deleted orphaned shell files that were replaced by C implementations
Changes in v2:
- Fixed TST_NO_DEFAULT_MAIN placement in ftrace_lib.c (before includes)
- Removed tst_test.h include from ftrace_lib.h (libraries shouldn't include it)
- Fixed include order in test files (tst_test.h before ftrace_lib.h)
- Added proper cleanup of allocated memory in tests
- Improved error messages and test documentation
- Fixed Makefile dependencies and library filtering
Testing:
All tests have been compiled and verified to work correctly on x86_64 and ppc64le systems with various kernel versions. All whitespace issues have been verified with checkpatch.pl.
Praveen K Pandey (5):
ftrace: Add common library for C implementation
ftrace: Convert ftrace_regression01.sh to C
ftrace: Convert ftrace_regression02.sh to C
ftrace: Convert ftrace_stress_test.sh to C
ftrace: Remove obsolete shell test files
runtest/tracing | 6 +- .../kernel/tracing/ftrace_test/.gitignore | 4 + testcases/kernel/tracing/ftrace_test/Makefile | 19 +- .../kernel/tracing/ftrace_test/ftrace_lib.c | 358 ++++++++++++++++++ .../kernel/tracing/ftrace_test/ftrace_lib.h | 139 +++++++ .../kernel/tracing/ftrace_test/ftrace_lib.sh | 180 --------- .../tracing/ftrace_test/ftrace_regression01.c | 128 +++++++ .../ftrace_test/ftrace_regression01.sh | 83 ---- .../tracing/ftrace_test/ftrace_regression02.c | 103 +++++ .../ftrace_test/ftrace_regression02.sh | 63 ---- .../ftrace_stress/ftrace_buffer_size_kb.sh | 45 --- .../ftrace_stress/ftrace_current_tracer.sh | 32 -- .../ftrace_stress/ftrace_ftrace_enabled.sh | 38 -- .../ftrace_function_profile_enabled.sh | 38 -- .../ftrace_stress/ftrace_set_event.sh | 46 --- .../ftrace_stress/ftrace_set_ftrace_filter.sh | 119 ------ .../ftrace_stress/ftrace_set_ftrace_pid.sh | 37 -- .../ftrace_stress/ftrace_stack_max_size.sh | 27 -- .../ftrace_stress/ftrace_stack_trace.sh | 35 -- .../ftrace_test/ftrace_stress/ftrace_trace.sh | 25 -- .../ftrace_stress/ftrace_trace_clock.sh | 27 -- .../ftrace_stress/ftrace_trace_options.sh | 58 --- .../ftrace_stress/ftrace_trace_pipe.sh | 45 --- .../ftrace_stress/ftrace_trace_stat.sh | 38 -- .../ftrace_stress/ftrace_tracing_cpumask.sh | 91 ----- .../ftrace_stress/ftrace_tracing_enabled.sh | 38 -- .../ftrace_tracing_max_latency.sh | 27 -- .../ftrace_stress/ftrace_tracing_on.sh | 38 -- .../tracing/ftrace_test/ftrace_stress_test.c | 322 ++++++++++++++++ .../tracing/ftrace_test/ftrace_stress_test.sh | 121 ------ 30 files changed, 1072 insertions(+), 1258 deletions(-) create mode 100644 testcases/kernel/tracing/ftrace_test/.gitignore create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_lib.c create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_lib.h delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_lib.sh create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_regression01.c delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_regression01.sh create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_regression02.c delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_regression02.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_buffer_size_kb.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_current_tracer.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_ftrace_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_function_profile_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_event.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_filter.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_pid.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_max_size.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_trace.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_clock.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_options.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_pipe.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_stat.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_cpumask.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_max_latency.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_on.sh create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_stress_test.c delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress_test.sh
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 1/5 ftrace: Add common library for C implementation
Add ftrace_lib.c and ftrace_lib.h to provide common functionality for ftrace C test implementations. This library handles:
- Debugfs mounting and tracing path management
- Reading and writing ftrace files
- Saving and restoring ftrace settings
- Tracer enumeration and management
The library uses TST_NO_DEFAULT_MAIN to allow linking with LTP test framework without defining multiple main() functions.
--- .../kernel/tracing/ftrace_test/ftrace_lib.c | 358 ++++++++++++++++++ .../kernel/tracing/ftrace_test/ftrace_lib.h | 139 +++++++ 2 files changed, 497 insertions(+) create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_lib.c create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_lib.h
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +358 -0
@@ -0,0 +1,358 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2010 FUJITSU LIMITED +* Copyright (c) 2024 Linux Test Project +* Copyright (c) IBM, 2026 +* +* Author: Li Zefan <[email protected]> +* Converted to C by: Praveen K Pandey <[email protected]> +*/ + +#define TST_NO_DEFAULT_MAIN +#include "ftrace_lib.h" +#include "tst_test.h" +#include "tst_safe_macros.h" +#include "tst_safe_stdio.h" + +char *tracing_path =NULL ; +char *debugfs_path =NULL ; +static int debugfs_mounted_by_us =0 ; + +struct ftrace_saved_state saved_state = {0 }; + +void ftrace_initialize (void ) +{ +FILE *fp; +char line[PATH_MAX ]; +int found =0 ; + +/* Check if debugfs is already mounted */ + fp =SAFE_FOPEN ("/proc/mounts" ,"r" ); +while (fgets (line,sizeof (line), fp)) { +if (strstr (line,"debugfs" )) { + debugfs_path =strdup ("/sys/kernel/debug" ); + found =1 ; +break ; + } + } +SAFE_FCLOSE (fp); + +/* Mount debugfs if not already mounted */ +if (!found) { +char *tmpdir =tst_tmpdir_path (); + debugfs_path =malloc (PATH_MAX ); +if (!debugfs_path) +tst_brk (TBROK |TERRNO ,"malloc failed" ); +snprintf (debugfs_path,PATH_MAX ,"%s/debugfs" , tmpdir); +SAFE_MKDIR (debugfs_path,0755 ); +SAFE_MOUNT ("debugfs" , debugfs_path,"debugfs" ,0 ,NULL ); + debugfs_mounted_by_us =1 ; + } + +/* Set tracing path */ + tracing_path =malloc (PATH_MAX ); +if (!tracing_path) +tst_brk (TBROK |TERRNO ,"malloc failed" ); + +snprintf (tracing_path,PATH_MAX ,"%s/tracing" , debugfs_path); + +/* Check if tracing is supported */ +if (access (tracing_path,F_OK ) !=0 ) +tst_brk (TCONF ,"Tracing is not supported" ); + +/* Save current settings */ +ftrace_save_settings (); +} + +void ftrace_cleanup (void ) +{ +ftrace_restore_settings (); + +if (debugfs_mounted_by_us && debugfs_path) { +tst_umount (debugfs_path); +rmdir (debugfs_path); + } + +free (tracing_path); +free (debugfs_path); + +/* Free saved state */ +free (saved_state.trace_options); +free (saved_state.tracing_on); +free (saved_state.buffer_size); +free (saved_state.tracing_cpumask); +free (saved_state.tracing_enabled); +free (saved_state.stack_tracer_enabled); +free (saved_state.ftrace_enabled); +free (saved_state.function_profile_enabled); +} + +char *ftrace_get_path (const char *filename) +{ +char *path; + + path =malloc (PATH_MAX ); +if (!path) +return NULL ; + +snprintf (path,PATH_MAX ,"%s/%s" , tracing_path, filename); +return path; +} + +char *ftrace_read_file (const char *filename) +{ +char *path; +FILE *fp; +char *content =NULL ; +size_t len =0 ; +ssize_t read; +char *line =NULL ; +size_t total_size =0 ; + + path =ftrace_get_path (filename); +if (!path) +return NULL ; + + fp =fopen (path,"r" ); +free (path); + +if (!fp) +return NULL ; + +/* Read entire file */ +while ((read =getline (&line, &len, fp)) !=-1 ) { +char *new_content =realloc (content, total_size + read +1 ); +if (!new_content) { +free (content); +free (line); +fclose (fp); +return NULL ; + } + content = new_content; +memcpy (content + total_size, line, read); + total_size += read; + } + +if (content) + content[total_size] ='\0' ; + +free (line); +fclose (fp); + +return content; +} + +int ftrace_write_file (const char *filename,const char *content) +{ +char *path; +FILE *fp; +int ret; + + path =ftrace_get_path (filename); +if (!path) +return -1 ; + + fp =fopen (path,"w" ); +free (path); + +if (!fp) +return -1 ; + + ret =fprintf (fp,"%s" , content); +fclose (fp); + +return (ret >0 ) ?0 :-1 ; +} + +int ftrace_file_exists (const char *filename) +{ +char *path; +int ret; + + path =ftrace_get_path (filename); +if (!path) +return 0 ; + + ret = (access (path,F_OK ) ==0 ); +free (path); + +return ret; +} + +void ftrace_save_settings (void ) +{ +if (saved_state.saved) +return ; + + saved_state.trace_options =ftrace_read_file ("trace_options" ); + saved_state.tracing_on =ftrace_read_file ("tracing_on" ); + saved_state.buffer_size =ftrace_read_file ("buffer_size_kb" ); + +if (ftrace_file_exists ("tracing_cpumask" )) + saved_state.tracing_cpumask =ftrace_read_file ("tracing_cpumask" ); + +if (ftrace_file_exists ("tracing_enabled" )) + saved_state.tracing_enabled =ftrace_read_file ("tracing_enabled" ); + +if (ftrace_file_exists ("stack_max_size" )) { +char *path =strdup ("/proc/sys/kernel/stack_tracer_enabled" ); +FILE *fp =fopen (path,"r" ); + +if (fp) { + saved_state.stack_tracer_enabled =malloc (32 ); +if (saved_state.stack_tracer_enabled) +fgets (saved_state.stack_tracer_enabled,32 , fp); +fclose (fp); + } +free (path); + } + +if (access ("/proc/sys/kernel/ftrace_enabled" ,F_OK ) ==0 ) { +FILE *fp =fopen ("/proc/sys/kernel/ftrace_enabled" ,"r" ); + +if (fp) { + saved_state.ftrace_enabled =malloc (32 ); +if (saved_state.ftrace_enabled) +fgets (saved_state.ftrace_enabled,32 , fp); +fclose (fp); + } + } + +if (ftrace_file_exists ("function_profile_enabled" )) + saved_state.function_profile_enabled =ftrace_read_file ("function_profile_enabled" ); + + saved_state.saved =1 ; +} + +void ftrace_restore_settings (void ) +{ +if (!saved_state.saved) +return ; + +/* Reset tracer and events */ +ftrace_write_file ("current_tracer" ,"nop\n" ); +ftrace_write_file ("events/enable" ,"0\n" ); + +if (ftrace_file_exists ("tracing_max_latency" )) +ftrace_write_file ("tracing_max_latency" ,"0\n" ); + +if (saved_state.tracing_cpumask) +ftrace_write_file ("tracing_cpumask" , saved_state.tracing_cpumask); + +if (ftrace_file_exists ("trace_clock" )) +ftrace_write_file ("trace_clock" ,"local\n" ); + +if (saved_state.function_profile_enabled) +ftrace_write_file ("function_profile_enabled" , saved_state.function_profile_enabled); + +if (saved_state.ftrace_enabled) { +FILE *fp =fopen ("/proc/sys/kernel/ftrace_enabled" ,"w" ); +if (fp) { +fprintf (fp,"%s" , saved_state.ftrace_enabled); +fclose (fp); + } + } + +if (saved_state.stack_tracer_enabled &&ftrace_file_exists ("stack_max_size" )) { +FILE *fp =fopen ("/proc/sys/kernel/stack_tracer_enabled" ,"w" ); +if (fp) { +fprintf (fp,"%s" , saved_state.stack_tracer_enabled); +fclose (fp); + } +ftrace_write_file ("stack_max_size" ,"0\n" ); + } + +if (saved_state.buffer_size) +ftrace_write_file ("buffer_size_kb" , saved_state.buffer_size); + +if (saved_state.tracing_on) +ftrace_write_file ("tracing_on" , saved_state.tracing_on); + +if (saved_state.tracing_enabled) +ftrace_write_file ("tracing_enabled" , saved_state.tracing_enabled); + +/* Restore trace options */ +if (saved_state.trace_options) { +char *options =strdup (saved_state.trace_options); +char *token =strtok (options,"\n" ); +while (token) { +ftrace_write_file ("trace_options" , token); + token =strtok (NULL ,"\n" ); + } +free (options); + } + +/* Clear trace */ +ftrace_clear_trace (); + +/* Clear filter */ +if (ftrace_file_exists ("set_ftrace_filter" )) +ftrace_write_file ("set_ftrace_filter" ,"\n" ); +} + +char **ftrace_get_available_tracers (int *count) +{ +char *content; +char **tracers =NULL ; +int n =0 ; +char *token; +char *saveptr; + + content =ftrace_read_file ("available_tracers" ); +if (!content) { + *count =0 ; +return NULL ; + } + +/* Count tracers */ +char *tmp =strdup (content); + token =strtok_r (tmp," \n" , &saveptr); +while (token) { + n++; + token =strtok_r (NULL ," \n" , &saveptr); + } +free (tmp); + +/* Allocate array */ + tracers =malloc (sizeof (char *) * n); +if (!tracers) { +free (content); + *count =0 ; +return NULL ; + } + +/* Fill array */ + n =0 ; + token =strtok_r (content," \n" , &saveptr); +while (token) { + tracers[n++] =strdup (token); + token =strtok_r (NULL ," \n" , &saveptr); + } + +free (content); + *count = n; +return tracers; +} + +int ftrace_set_tracer (const char *tracer) +{ +char buf[256 ]; +snprintf (buf,sizeof (buf),"%s\n" , tracer); +return ftrace_write_file ("current_tracer" , buf); +} + +void ftrace_clear_trace (void ) +{ +ftrace_write_file ("trace" ,"\n" ); +} + +void ftrace_enable_tracing (void ) +{ +ftrace_write_file ("tracing_on" ,"1\n" ); +} + +void ftrace_disable_tracing (void ) +{ +ftrace_write_file ("tracing_on" ,"0\n" ); +} +
testcases/kernel/tracing/ftrace_test/ftrace_lib.h new file +139 -0
139 lines not on this page · raw message
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] ftrace: Add common library for C implementation
Hi Praveen,
Quoted 1 line · Praveen K Pandey
ftrace: Add common library for C implementation
--- [PATCH 2/5] ---
Quoted 4 lines
+/*\
+ * [Description]
+ *
+ * Test for kernel panic while using userstacktrace with page fault events.
The [Description] header is deprecated and must not be used. Remove the [Description] line and start directly with the test description text. The same applies to ftrace_regression02.c.
--- [PATCH 4/5] ---
Review
Quoted 5 lines
(quoted diff) +5
-0+static void cleanup(void) +{ + stop_testing = 1; + ftrace_cleanup(); +}
If the test is interrupted (e.g. SIGTERM on timeout), cleanup() is called while stress threads may still be running. Setting stop_testing does not guarantee threads have exited before ftrace_cleanup() frees tracing_path and debugfs_path. Threads accessing those globals after free is a use-after-free.
The fix is to join all threads in cleanup() before calling ftrace_cleanup(), for example by calling stop_stress_tests() here (guarded by thread_count > 0).
Verdict: Needs revision
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [LTP PATCH v4 1/5] ftrace: Add common library for C implementation
In reply to Praveen K PandeyHi!
Quoted 16 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0@@ -0,0 +1,358 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2010 FUJITSU LIMITED +* Copyright (c) 2024 Linux Test Project +* Copyright (c) IBM, 2026 +* +* Author: Li Zefan <[email protected]> +* Converted to C by: Praveen K Pandey <[email protected]> +*/ +
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 20
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0+#define TST_NO_DEFAULT_MAIN +#include "ftrace_lib.h" +#include "tst_test.h" +#include "tst_safe_macros.h" +#include "tst_safe_stdio.h" + +char *tracing_path =NULL ; +char *debugfs_path =NULL ; +static int debugfs_mounted_by_us =0 ; +struct ftrace_saved_state saved_state = {0 };
Static variables are initialized to zero by compiler automatically, no need to do it here.
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 29
Quoted 7 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +7
-0+void ftrace_initialize (void ) +{ +FILE *fp; +char line[PATH_MAX ]; +int found =0 ; + +/* Check if debugfs is already mounted */
Please avoid comment that state the obvious, e.g. say in english exactly what the code does.
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 38
Quoted 9 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +9
-0+ fp =SAFE_FOPEN ("/proc/mounts" ,"r" ); +while (fgets (line,sizeof (line), fp)) { +if (strstr (line,"debugfs" )) { + debugfs_path =strdup ("/sys/kernel/debug" ); + found =1 ; +break ; + } + } +SAFE_FCLOSE (fp);
This whole thing could be just:
if (tst_fs_is_mounted("/sys/kernel/debug")) {
debugfs_path = strdup("/sys/kernel/debug");
if (!debugfs_path)
tst_brk(TBROK, ...);
}Ideally SAFE_STRDUP() should be added to the test library (in a separate patch) so that we can simplify this even further to:
if (tst_fs_is_mounted("/sys/kernel/debug"))
debugfs_path = SAFE_STRDUP("/sys/kernel/debug");testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 41
Quoted 2 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +2
-0+/* Mount debugfs if not already mounted */ +if (!found) {
No need to add found flag, we can do if (!debugfs_path) here instead.
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 46
Quoted 5 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +5
-0+char *tmpdir =tst_tmpdir_path (); + debugfs_path =malloc (PATH_MAX ); +if (!debugfs_path) +tst_brk (TBROK |TERRNO ,"malloc failed" ); +snprintf (debugfs_path,PATH_MAX ,"%s/debugfs" , tmpdir);
This could be single line with SAFE_ASPRINTF()
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 57
Quoted 11 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +11
-0+SAFE_MKDIR (debugfs_path,0755 ); +SAFE_MOUNT ("debugfs" , debugfs_path,"debugfs" ,0 ,NULL ); + debugfs_mounted_by_us =1 ; + } + +/* Set tracing path */ + tracing_path =malloc (PATH_MAX ); +if (!tracing_path) +tst_brk (TBROK |TERRNO ,"malloc failed" ); + +snprintf (tracing_path,PATH_MAX ,"%s/tracing" , debugfs_path);
This can be SAFE_ASPRINTF() as well.
Quoted 30 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +30
-0+/* Check if tracing is supported */ +if (access (tracing_path,F_OK ) !=0 ) +tst_brk (TCONF ,"Tracing is not supported" ); + +/* Save current settings */ +ftrace_save_settings (); +} + +void ftrace_cleanup (void ) +{ +ftrace_restore_settings (); + +if (debugfs_mounted_by_us && debugfs_path) { +tst_umount (debugfs_path); +rmdir (debugfs_path); + } + +free (tracing_path); +free (debugfs_path); + +/* Free saved state */ +free (saved_state.trace_options); +free (saved_state.tracing_on); +free (saved_state.buffer_size); +free (saved_state.tracing_cpumask); +free (saved_state.tracing_enabled); +free (saved_state.stack_tracer_enabled); +free (saved_state.ftrace_enabled); +free (saved_state.function_profile_enabled); +}
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 98
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0+ +char *ftrace_get_path (const char *filename) +{ +char *path; + + path =malloc (PATH_MAX ); +if (!path) +return NULL ; + +snprintf (path,PATH_MAX ,"%s/%s" , tracing_path, filename);
SAFE_ASPRINTF() here as well.
Quoted 54 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +54
-0+return path; +} + +char *ftrace_read_file (const char *filename) +{ +char *path; +FILE *fp; +char *content =NULL ; +size_t len =0 ; +ssize_t read; +char *line =NULL ; +size_t total_size =0 ; + + path =ftrace_get_path (filename); +if (!path) +return NULL ; + + fp =fopen (path,"r" ); +free (path); + +if (!fp) +return NULL ; + +/* Read entire file */ +while ((read =getline (&line, &len, fp)) !=-1 ) { +char *new_content =realloc (content, total_size + read +1 ); +if (!new_content) { +free (content); +free (line); +fclose (fp); +return NULL ; + } + content = new_content; +memcpy (content + total_size, line, read); + total_size += read; + } + +if (content) + content[total_size] ='\0' ; + +free (line); +fclose (fp); + +return content; +} + +int ftrace_write_file (const char *filename,const char *content) +{ +char *path; +FILE *fp; +int ret; + + path =ftrace_get_path (filename); +if (!path)
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 162
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0+return -1 ; + + fp =fopen (path,"w" ); +free (path); + +if (!fp) +return -1 ; + + ret =fprintf (fp,"%s" , content); +fclose (fp);
This could be replaced by single SAFE_FILE_PRINTF()
Quoted 161 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +161
-0+return (ret >0 ) ?0 :-1 ; +} + +int ftrace_file_exists (const char *filename) +{ +char *path; +int ret; + + path =ftrace_get_path (filename); +if (!path) +return 0 ; + + ret = (access (path,F_OK ) ==0 ); +free (path); + +return ret; +} + +void ftrace_save_settings (void ) +{ +if (saved_state.saved) +return ; + + saved_state.trace_options =ftrace_read_file ("trace_options" ); + saved_state.tracing_on =ftrace_read_file ("tracing_on" ); + saved_state.buffer_size =ftrace_read_file ("buffer_size_kb" ); + +if (ftrace_file_exists ("tracing_cpumask" )) + saved_state.tracing_cpumask =ftrace_read_file ("tracing_cpumask" ); + +if (ftrace_file_exists ("tracing_enabled" )) + saved_state.tracing_enabled =ftrace_read_file ("tracing_enabled" ); + +if (ftrace_file_exists ("stack_max_size" )) { +char *path =strdup ("/proc/sys/kernel/stack_tracer_enabled" ); +FILE *fp =fopen (path,"r" ); + +if (fp) { + saved_state.stack_tracer_enabled =malloc (32 ); +if (saved_state.stack_tracer_enabled) +fgets (saved_state.stack_tracer_enabled,32 , fp); +fclose (fp); + } +free (path); + } + +if (access ("/proc/sys/kernel/ftrace_enabled" ,F_OK ) ==0 ) { +FILE *fp =fopen ("/proc/sys/kernel/ftrace_enabled" ,"r" ); + +if (fp) { + saved_state.ftrace_enabled =malloc (32 ); +if (saved_state.ftrace_enabled) +fgets (saved_state.ftrace_enabled,32 , fp); +fclose (fp); + } + } + +if (ftrace_file_exists ("function_profile_enabled" )) + saved_state.function_profile_enabled =ftrace_read_file ("function_profile_enabled" ); + + saved_state.saved =1 ; +} + +void ftrace_restore_settings (void ) +{ +if (!saved_state.saved) +return ; + +/* Reset tracer and events */ +ftrace_write_file ("current_tracer" ,"nop\n" ); +ftrace_write_file ("events/enable" ,"0\n" ); + +if (ftrace_file_exists ("tracing_max_latency" )) +ftrace_write_file ("tracing_max_latency" ,"0\n" ); + +if (saved_state.tracing_cpumask) +ftrace_write_file ("tracing_cpumask" , saved_state.tracing_cpumask); + +if (ftrace_file_exists ("trace_clock" )) +ftrace_write_file ("trace_clock" ,"local\n" ); + +if (saved_state.function_profile_enabled) +ftrace_write_file ("function_profile_enabled" , saved_state.function_profile_enabled); + +if (saved_state.ftrace_enabled) { +FILE *fp =fopen ("/proc/sys/kernel/ftrace_enabled" ,"w" ); +if (fp) { +fprintf (fp,"%s" , saved_state.ftrace_enabled); +fclose (fp); + } + } + +if (saved_state.stack_tracer_enabled &&ftrace_file_exists ("stack_max_size" )) { +FILE *fp =fopen ("/proc/sys/kernel/stack_tracer_enabled" ,"w" ); +if (fp) { +fprintf (fp,"%s" , saved_state.stack_tracer_enabled); +fclose (fp); + } +ftrace_write_file ("stack_max_size" ,"0\n" ); + } + +if (saved_state.buffer_size) +ftrace_write_file ("buffer_size_kb" , saved_state.buffer_size); + +if (saved_state.tracing_on) +ftrace_write_file ("tracing_on" , saved_state.tracing_on); + +if (saved_state.tracing_enabled) +ftrace_write_file ("tracing_enabled" , saved_state.tracing_enabled); + +/* Restore trace options */ +if (saved_state.trace_options) { +char *options =strdup (saved_state.trace_options); +char *token =strtok (options,"\n" ); +while (token) { +ftrace_write_file ("trace_options" , token); + token =strtok (NULL ,"\n" ); + } +free (options); + } + +/* Clear trace */ +ftrace_clear_trace (); + +/* Clear filter */ +if (ftrace_file_exists ("set_ftrace_filter" )) +ftrace_write_file ("set_ftrace_filter" ,"\n" ); +} + +char **ftrace_get_available_tracers (int *count) +{ +char *content; +char **tracers =NULL ; +int n =0 ; +char *token; +char *saveptr; + + content =ftrace_read_file ("available_tracers" ); +if (!content) { + *count =0 ; +return NULL ; + } + +/* Count tracers */ +char *tmp =strdup (content); + token =strtok_r (tmp," \n" , &saveptr); +while (token) { + n++; + token =strtok_r (NULL ," \n" , &saveptr); + } +free (tmp); + +/* Allocate array */ + tracers =malloc (sizeof (char *) * n); +if (!tracers) { +free (content); + *count =0 ; +return NULL ; + } + +/* Fill array */
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 334
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0+ n =0 ; + token =strtok_r (content," \n" , &saveptr); +while (token) { + tracers[n++] =strdup (token); + token =strtok_r (NULL ," \n" , &saveptr); + } + +free (content); + *count = n; +return tracers;
This would be much easier if you just read the file line by line
f = SAFE_FOPEN(...)
while (fgets(line, sizeof(line), f) {
...
}
SAFE_FCLOSE(f);Quoted 13 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +13
-0+} + +int ftrace_set_tracer (const char *tracer) +{ +char buf[256 ]; +snprintf (buf,sizeof (buf),"%s\n" , tracer); +return ftrace_write_file ("current_tracer" , buf); +} + +void ftrace_clear_trace (void ) +{ +ftrace_write_file ("trace" ,"\n" ); +}
testcases/kernel/tracing/ftrace_test/ftrace_lib.c line 357
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.c new file +10
-0+ +void ftrace_enable_tracing (void ) +{ +ftrace_write_file ("tracing_on" ,"1\n" ); +} + +void ftrace_disable_tracing (void ) +{ +ftrace_write_file ("tracing_on" ,"0\n" ); +}
Generally it would make sense to keep the paths to these files in a global variables, set then only once in the init and then we could just do:
void ftrace_set_tracer(const char *tracer)
{
SAFE_FILE_PRINTF(path_ftrace_tracer, "%s", tracer);
}Quoted 39 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.h new file +33
-0@@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (c) 2010 FUJITSU LIMITED +* Copyright (c) 2024 Linux Test Project +* Copyright (c) IBM, 2026 +* +* Author: Li Zefan <[email protected]> +* Converted to C by: Praveen K Pandey <[email protected]> +*/ + +#ifndef FTRACE_LIB_H +#define FTRACE_LIB_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <errno.h> +#include <limits.h> + +/* Global paths */ +extern char *tracing_path; +extern char *debugfs_path; + +/* Saved settings for cleanup */ +struct ftrace_saved_state { + char *trace_options; + char *tracing_on; + char *buffer_size;
testcases/kernel/tracing/ftrace_test/ftrace_lib.h line 43
Quoted 10 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.h new file +10
-0+char *tracing_cpumask; +char *tracing_enabled; +char *stack_tracer_enabled; +char *ftrace_enabled; +char *function_profile_enabled; +int saved; +}; + +extern struct ftrace_saved_state saved_state; +
Why is this leaking into the tests? None of these low level details should be exposed since you are building a library for the tests to use.
testcases/kernel/tracing/ftrace_test/ftrace_lib.h line 50
Quoted 7 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.h new file +7
-0+/** +* ftrace_initialize() - Initialize ftrace test environment +* +* This function: +* - Checks if debugfs is mounted, mounts it if needed +* - Verifies tracing support +* - Saves current ftrace settings
We use kernel doc comments that are parsed and exported into documentation.
Quoted 94 lines
testcases/kernel/tracing/ftrace_test/ftrace_lib.h new file +89
-0+*/ +void ftrace_initialize (void ); + +/** +* ftrace_cleanup() - Cleanup and restore ftrace settings +* +* Restores all saved ftrace settings and unmounts debugfs if needed +*/ +void ftrace_cleanup (void ); + +/** +* ftrace_save_settings() - Save current ftrace settings +* +* Saves all ftrace configuration to restore later +*/ +void ftrace_save_settings (void ); + +/** +* ftrace_restore_settings() - Restore saved ftrace settings +* +* Restores ftrace configuration saved by ftrace_save_settings() +*/ +void ftrace_restore_settings (void ); + +/** +* ftrace_get_path() - Get full path to a tracing file +* @filename: Name of the file in tracing directory +* +* Returns: Allocated string with full path (caller must free) +*/ +char *ftrace_get_path (const char *filename); + +/** +* ftrace_read_file() - Read content from a tracing file +* @filename: Name of the file in tracing directory +* +* Returns: Allocated string with file content (caller must free) +*/ +char *ftrace_read_file (const char *filename); + +/** +* ftrace_write_file() - Write content to a tracing file +* @filename: Name of the file in tracing directory +* @content: Content to write +* +* Returns: 0 on success, -1 on failure +*/ +int ftrace_write_file (const char *filename,const char *content); + +/** +* ftrace_file_exists() - Check if a tracing file exists +* @filename: Name of the file in tracing directory +* +* Returns: 1 if exists, 0 otherwise +*/ +int ftrace_file_exists (const char *filename); + +/** +* ftrace_get_available_tracers() - Get list of available tracers +* @count: Pointer to store number of tracers +* +* Returns: Array of tracer names (caller must free) +*/ +char **ftrace_get_available_tracers (int *count); + +/** +* ftrace_set_tracer() - Set current tracer +* @tracer: Name of the tracer to set +* +* Returns: 0 on success, -1 on failure +*/ +int ftrace_set_tracer (const char *tracer); + +/** +* ftrace_clear_trace() - Clear the trace buffer +*/ +void ftrace_clear_trace (void ); + +/** +* ftrace_enable_tracing() - Enable tracing +*/ +void ftrace_enable_tracing (void ); + +/** +* ftrace_disable_tracing() - Disable tracing +*/ +void ftrace_disable_tracing (void ); + +#endif /* FTRACE_LIB_H */ Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 2/5 ftrace: Convert ftrace_regression01.sh to C
In reply to Praveen K PandeyConvert ftrace_regression01.sh to C implementation using LTP new API.
This is a regression test for commit 1dbd195 (tracing: Fix preempt count leak). The bug caused kernel panics when enabling userstacktrace option together with page fault events. The test enables these features and repeats the loop multiple times to trigger potential race conditions, verifying the system remains stable.
The test requires root to access and configure the ftrace subsystem via debugfs.
--- .../tracing/ftrace_test/ftrace_regression01.c | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_regression01.c
testcases/kernel/tracing/ftrace_test/ftrace_regression01.c new file +128 -0
@@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2015 Red Hat Inc. +* Copyright (c) 2024 Linux Test Project +* Copyright (c) IBM, 2026 +* +* Author: Li Wang <[email protected]> +* Converted to C by: Praveen K Pandey <[email protected]> +*/ + +/*\ +* [Description] +* +* Test for kernel panic while using userstacktrace with page fault events. +* +* This is a regression test for commit 1dbd195 (tracing: Fix preempt count leak). +* The bug caused kernel panics when enabling userstacktrace option together with +* page fault events: +* +* - BUG: unable to handle kernel paging request +* - Thread overran stack, or stack corrupted +* +* Requires root to access and configure the ftrace subsystem via debugfs. +* +* [Algorithm] +* +* - Enable stack tracer +* - Enable userstacktrace option +* - Enable page fault events (either exceptions:page_fault_kernel or +* kmem:mm_kernel_pagefault depending on kernel version) +* - Repeat the loop multiple times to trigger potential race conditions +* - Verify system remains stable without kernel panic +*/ + +#include "tst_test.h" +#include "ftrace_lib.h" + +#define LOOP_COUNT 10 + +static void run_test (void ) +{ +int i; +char *exc_page_fault_enable =NULL ; +char *mm_page_fault_enable =NULL ; +int use_exc_event =0 ; + +/* Check if stack tracer is available */ +if (access ("/proc/sys/kernel/stack_tracer_enabled" ,F_OK ) !=0 ) +tst_brk (TCONF ,"Stack tracer is not configured in this kernel" ); + +/* Determine which page fault event is available */ +if (ftrace_file_exists ("events/exceptions/page_fault_kernel/enable" )) { + use_exc_event =1 ; + exc_page_fault_enable =ftrace_read_file ("events/exceptions/page_fault_kernel/enable" ); + }else if (ftrace_file_exists ("events/kmem/mm_kernel_pagefault/enable" )) { + use_exc_event =0 ; + mm_page_fault_enable =ftrace_read_file ("events/kmem/mm_kernel_pagefault/enable" ); + }else { +tst_brk (TCONF ,"Neither page_fault_kernel nor mm_kernel_pagefault event is available" ); + } + +/* Run the test loop */ +for (i =0 ; i <LOOP_COUNT ; i++) { +char *options; + +/* Enable stack tracer */ +SAFE_FILE_PRINTF ("/proc/sys/kernel/stack_tracer_enabled" ,"1" ); + +/* Enable userstacktrace option */ +if (ftrace_write_file ("trace_options" ,"userstacktrace\n" ) !=0 ) +tst_brk (TBROK ,"Failed to set userstacktrace option" ); + +/* Verify userstacktrace is enabled */ + options =ftrace_read_file ("trace_options" ); +if (!options || !strstr (options,"userstacktrace" )) { +free (options); +tst_brk (TBROK ,"Failed to enable userstacktrace" ); + } +free (options); + +/* Enable page fault event */ +if (use_exc_event) { +if (ftrace_write_file ("events/exceptions/page_fault_kernel/enable" ,"1\n" ) !=0 ) +tst_res (TINFO ,"Failed to enable page_fault_kernel event" ); + }else { +if (ftrace_write_file ("events/kmem/mm_kernel_pagefault/enable" ,"1\n" ) !=0 ) +tst_res (TINFO ,"Failed to enable mm_kernel_pagefault event" ); + } + +/* Wait for trace events to be written */ +char *trace_content =TST_RETRY_FUNC (ftrace_read_file ("trace" ),TST_RETVAL_NOTNULL ); +free (trace_content); + } + +/* Restore page fault event state */ +if (use_exc_event && exc_page_fault_enable) { +ftrace_write_file ("events/exceptions/page_fault_kernel/enable" , exc_page_fault_enable); +free (exc_page_fault_enable); + }else if (!use_exc_event && mm_page_fault_enable) { +ftrace_write_file ("events/kmem/mm_kernel_pagefault/enable" , mm_page_fault_enable); +free (mm_page_fault_enable); + } + +tst_res (TPASS ,"System did not panic with userstacktrace enabled" ); +} + +static void setup (void ) +{ +ftrace_initialize (); +} + +static void cleanup (void ) +{ +ftrace_cleanup (); +} + +static struct tst_test test = { + .test_all = run_test, + .setup = setup, + .cleanup = cleanup, + .needs_root =1 , + .needs_tmpdir =1 , + .tags = (const struct tst_tag []) { + {"linux-git" ,"1dbd195" }, + {} + } +}; +
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 3/5 ftrace: Convert ftrace_regression02.sh to C
In reply to Praveen K PandeyConvert ftrace_regression02.sh to C implementation using LTP new API.
This test verifies that the signal:signal_generate tracepoint exposes 'grp=' and 'res=' fields. These fields were added by commits 6c303d3 and 163566f in kernel 3.2. The test enables the signal_generate event, generates signals, and checks the trace output for the presence of both fields.
The test requires root to access and configure the ftrace subsystem via debugfs. Uses .min_kver to enforce kernel version requirement instead of inline check.
--- .../tracing/ftrace_test/ftrace_regression02.c | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_regression02.c
testcases/kernel/tracing/ftrace_test/ftrace_regression02.c new file +103 -0
@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2015 Red Hat Inc. +* Copyright (c) 2024 Linux Test Project +* Copyright (c) IBM, 2026 +* +* Author: Li Wang <[email protected]> +* Converted to C by: Praveen K Pandey <[email protected]> +*/ + +/*\ +* [Description] +* +* Verify that signal:signal_generate tracepoint exposes 'grp=' and 'res=' fields. +* +* This test verifies kernel commits that added more information to the +* signal_generate tracepoint: +* +* - Commit 6c303d3 (tracing: let trace_signal_generate() report more info...) +* - Commit 163566f (tracing: send_sigqueue() needs trace_signal_generate()) +* +* These fields were added in kernel 3.2. +* +* Requires root to access and configure the ftrace subsystem via debugfs. +* +* [Algorithm] +* +* - Enable signal:signal_generate event +* - Enable tracing +* - Generate signals by running commands +* - Read trace output and search for 'grp=' and 'res=' fields +* - Test passes if both fields are found in the trace +*/ + +#include "tst_test.h" +#include "ftrace_lib.h" + +#define SIGNAL_LOOP 100 + +static void run_test (void ) +{ +int i; +char *trace_content; +int found =0 ; + +/* Set event */ +if (ftrace_write_file ("set_event" ,"signal:signal_generate\n" ) !=0 ) +tst_brk (TBROK ,"Failed to set signal:signal_generate event" ); + +/* Enable tracing */ +ftrace_enable_tracing (); + +/* Clear trace buffer */ +ftrace_clear_trace (); + +/* Generate some signals by running commands */ +for (i =0 ; i <SIGNAL_LOOP ; i++) { +/* Run a simple command that will generate signals */ +if (system ("ls -l /proc > /dev/null 2>&1" ) ==-1 ) +tst_res (TINFO ,"Command execution failed, continuing..." ); + } + +/* Wait for trace to be written and contain signal events */ + trace_content =TST_RETRY_FUNC (ftrace_read_file ("trace" ),TST_RETVAL_NOTNULL ); +if (!trace_content) +tst_brk (TBROK ,"Failed to read trace file" ); + +/* Check for 'grp=' and 'res=' fields in trace */ +if (strstr (trace_content,"grp=" ) &&strstr (trace_content,"res=" )) + found =1 ; + +free (trace_content); + +if (found) +tst_res (TPASS ,"signal_generate event contains grp and res fields" ); +else +tst_res (TFAIL ,"signal_generate event missing grp or res fields" ); +} + +static void setup (void ) +{ +ftrace_initialize (); +} + +static void cleanup (void ) +{ +ftrace_cleanup (); +} + +static struct tst_test test = { + .test_all = run_test, + .setup = setup, + .cleanup = cleanup, + .needs_root =1 , + .needs_tmpdir =1 , + .min_kver ="3.2" , + .tags = (const struct tst_tag []) { + {"linux-git" ,"6c303d3" }, + {"linux-git" ,"163566f" }, + {} + } +}; +
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 4/5 ftrace: Convert ftrace_stress_test.sh to C
In reply to Praveen K PandeyConvert ftrace_stress_test.sh to C implementation using LTP new API.
This test performs stress testing of the ftrace subsystem by running multiple concurrent threads, each exercising different ftrace features (switching tracers, reading trace_pipe, enabling/disabling events, modifying buffer size, etc.). All threads run concurrently for 60 seconds to detect race conditions and verify system stability.
The test requires root to access and configure the ftrace subsystem via debugfs.
Also update:
- Makefile: Add new C test targets and library compilation
- .gitignore: Ignore compiled test binaries
- runtest/tracing: Update test entries to use C implementations
--- runtest/tracing | 6 +- .../kernel/tracing/ftrace_test/.gitignore | 4 + testcases/kernel/tracing/ftrace_test/Makefile | 19 +- .../tracing/ftrace_test/ftrace_stress_test.c | 322 ++++++++++++++++++ 4 files changed, 346 insertions(+), 5 deletions(-) create mode 100644 testcases/kernel/tracing/ftrace_test/.gitignore create mode 100644 testcases/kernel/tracing/ftrace_test/ftrace_stress_test.c
runtest/tracing +3 -3
@@ -1,7 +1,7 @@ #DESCRIPTION:Tracing testing -ftrace_regression01 ftrace_regression01.sh -ftrace_regression02 ftrace_regression02.sh -ftrace-stress-test ftrace_stress_test.sh 90 +ftrace_regression01 ftrace_regression01 +ftrace_regression02 ftrace_regression02 +ftrace-stress-test ftrace_stress_test dynamic_debug01 dynamic_debug01.sh fanotify25 fanotify25 pt_full_trace_basic pt_test
testcases/kernel/tracing/ftrace_test/.gitignore new file +4 -0
@@ -0,0 +1,4 @@ +ftrace_regression01 +ftrace_regression02 +ftrace_stress_test +
testcases/kernel/tracing/ftrace_test/Makefile +17 -2
@@ -1,7 +1,22 @@ -top_srcdir ?= ../../../.. +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2010 FUJITSU LIMITED +# Copyright (c) 2024 Linux Test Project +# Copyright (c) IBM, 2026 +# Converted to C by: Praveen K Pandey <[email protected]> + +top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk -INSTALL_TARGETS := *.sh ftrace_stress/* +# Filter out ftrace_lib from standalone build targets +FILTER_OUT_MAKE_TARGETS := ftrace_lib + +# Only ftrace_stress_test needs pthread +ftrace_stress_test: LDLIBS += -lpthread include $(top_srcdir)/include/mk/generic_leaf_target.mk + +# C test binaries with shared library dependencies +ftrace_regression01: ftrace_lib.o +ftrace_regression02: ftrace_lib.o +ftrace_stress_test: ftrace_lib.o
testcases/kernel/tracing/ftrace_test/ftrace_stress_test.c new file +322 -0
322 lines not on this page · raw message
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 5/5 ftrace: Remove obsolete shell test files
In reply to Praveen K PandeyRemove the old shell-based test implementations that have been converted to C:
- ftrace_lib.sh
- ftrace_regression01.sh
- ftrace_regression02.sh
- ftrace_stress_test.sh
- ftrace_stress/*.sh (19 stress test scripts)
These are replaced by the new C implementations using LTP new API.
--- .../kernel/tracing/ftrace_test/ftrace_lib.sh | 180 ------------------ .../ftrace_test/ftrace_regression01.sh | 83 -------- .../ftrace_test/ftrace_regression02.sh | 63 ------ .../ftrace_stress/ftrace_buffer_size_kb.sh | 45 ----- .../ftrace_stress/ftrace_current_tracer.sh | 32 ---- .../ftrace_stress/ftrace_ftrace_enabled.sh | 38 ---- .../ftrace_function_profile_enabled.sh | 38 ---- .../ftrace_stress/ftrace_set_event.sh | 46 ----- .../ftrace_stress/ftrace_set_ftrace_filter.sh | 119 ------------ .../ftrace_stress/ftrace_set_ftrace_pid.sh | 37 ---- .../ftrace_stress/ftrace_stack_max_size.sh | 27 --- .../ftrace_stress/ftrace_stack_trace.sh | 35 ---- .../ftrace_test/ftrace_stress/ftrace_trace.sh | 25 --- .../ftrace_stress/ftrace_trace_clock.sh | 27 --- .../ftrace_stress/ftrace_trace_options.sh | 58 ------ .../ftrace_stress/ftrace_trace_pipe.sh | 45 ----- .../ftrace_stress/ftrace_trace_stat.sh | 38 ---- .../ftrace_stress/ftrace_tracing_cpumask.sh | 91 --------- .../ftrace_stress/ftrace_tracing_enabled.sh | 38 ---- .../ftrace_tracing_max_latency.sh | 27 --- .../ftrace_stress/ftrace_tracing_on.sh | 38 ---- .../tracing/ftrace_test/ftrace_stress_test.sh | 121 ------------ 22 files changed, 1251 deletions(-) delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_lib.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_regression01.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_regression02.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_buffer_size_kb.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_current_tracer.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_ftrace_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_function_profile_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_event.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_filter.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_pid.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_max_size.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_trace.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_clock.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_options.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_pipe.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_stat.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_cpumask.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_enabled.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_max_latency.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_on.sh delete mode 100755 testcases/kernel/tracing/ftrace_test/ftrace_stress_test.sh
testcases/kernel/tracing/ftrace_test/ftrace_lib.sh deleted +0 -180
@@ -1,180 +0,0 @@ -#!/bin/sh - -########################################################################### -## ## -## Copyright (c) 2010 FUJITSU LIMITED ## -## ## -## This program is free software: you can redistribute it and/or modify ## -## it under the terms of the GNU General Public License as published by ## -## the Free Software Foundation, either version 3 of the License, or ## -## (at your option) any later version. ## -## ## -## This program is distributed in the hope that it will be useful, ## -## but WITHOUT ANY WARRANTY; without even the implied warranty of ## -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## -## GNU General Public License for more details. ## -## ## -## You should have received a copy of the GNU General Public License ## -## along with this program. If not, see <http://www.gnu.org/licenses/>. ## -## ## -## Author: Li Zefan <[email protected]> ## -## ## -########################################################################### - -. test.sh - -ftrace_test_init() -{ - export TPATH="$PWD" - export SPATH="$TPATH/ftrace_stress" - - if grep -q debugfs /proc/mounts; then - export DEBUGFS_PATH=/sys/kernel/debug/ - export TRACING_PATH="$DEBUGFS_PATH/tracing" - debugfs_def_mounted=1 - else - tst_tmpdir - export DEBUGFS_PATH="$PWD/debugfs" - export TRACING_PATH="$PWD/debugfs/tracing" - mkdir $DEBUGFS_PATH - mount -t debugfs xxx $DEBUGFS_PATH - fi - - TST_CLEANUP=clean_up - - trap clean_up_exit INT - - tst_require_root - - # Check to see tracing feature is supported or not - if [ ! -d $TRACING_PATH ]; then - tst_brkm TCONF "Tracing is not supported. Skip the test..." - fi - - save_old_setting -} - -test_interval=$1 - -save_old_setting() -{ - cd $TRACING_PATH - - old_trace_options=`cat trace_options` - old_tracing_on=`cat tracing_on` - old_buffer_size=`cat buffer_size_kb` - old_tracing_cpumask=`cat tracing_cpumask` - - if [ -e tracing_cpumask ]; then - old_tracing_cpumask=`cat tracing_cpumask` - fi - - if [ -e tracing_enabled ]; then - old_tracing_enabled=`cat tracing_enabled` - fi - - if [ -e stack_max_size ]; then - old_stack_tracer_enabled=`cat /proc/sys/kernel/stack_tracer_enabled` - fi - - if [ -e "/proc/sys/kernel/ftrace_enabled" ]; then - old_ftrace_enabled=`cat /proc/sys/kernel/ftrace_enabled` - fi - - if [ -e "function_profile_enabled" ]; then - old_profile_enabled=`cat function_profile_enabled` - fi - - setting_saved=1 - - cd - > /dev/null -} - -restore_old_setting() -{ - if [ ! "$setting_saved" = 1 ]; then - return - fi - - cd $TRACING_PATH - - echo nop > current_tracer - echo 0 > events/enable - if [ -e tracing_max_latency ]; then - echo 0 > tracing_max_latency - fi - - if [ -e tracing_cpumask ]; then - echo $old_tracing_cpumask > tracing_cpumask - fi - - if [ -e trace_clock ]; then - echo local > trace_clock - fi - - if [ -e "function_pofile_enabled" ]; then - echo $old_profile_enabled > function_profile_enabled - fi - - if [ -e "/proc/sys/kernel/ftrace_enabled" ]; then - echo $old_ftrace_enabled > /proc/sys/kernel/ftrace_enabled - fi - - if [ -e stack_max_size ]; then - echo $old_stack_tracer_enabled > /proc/sys/kernel/stack_tracer_enabled - echo 0 > stack_max_size - fi - - echo $old_buffer_size > buffer_size_kb - echo $old_tracing_on > tracing_on - - if [ -e tracing_enabled ]; then - echo $old_tracing_enabled > tracing_enabled - fi - - for option in $old_trace_options; do - echo $option > trace_options 2> /dev/null - done - - echo > trace - - if [ -f set_ftrace_filter ]; then - echo > set_ftrace_filter - fi - - cd - > /dev/null -} - -clean_up_mount() -{ - if [ ! "$debugfs_def_mounted" = "1" ]; then - umount $DEBUGFS_PATH - rmdir $DEBUGFS_PATH - fi -} - -clean_up() -{ - restore_old_setting - clean_up_mount -} - -clean_up_exit() -{ - restore_old_setting - clean_up_mount - exit 1 -} - -test_begin() -{ - start_time=`date +%s` -} - -test_wait() -{ - # run the test for $test_interval secs - tst_sleep ${test_interval}s -} - -ftrace_test_init
testcases/kernel/tracing/ftrace_test/ftrace_regression01.sh deleted +0 -83
83 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_regression02.sh deleted +0 -63
63 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_buffer_size_kb.sh deleted +0 -45
45 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_current_tracer.sh deleted +0 -32
@@ -1,32 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=200 - -while true; do - i=0 - while [ $i -lt $LOOP ]; do - for tracer in `cat "$TRACING_PATH"/available_tracers` - do - if [ "$tracer" = mmiotrace ]; then - continue - fi - - echo $tracer > "$TRACING_PATH"/current_tracer 2> /dev/null - done - i=$((i + 1)) - done - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_ftrace_enabled.sh deleted +0 -38
@@ -1,38 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_LOOP=1500 -count=0 - -while true; do - count=$(( $count + 1 )) - i=0 - while [ $i -lt $MAX_LOOP ]; do - echo 0 > /proc/sys/kernel/ftrace_enabled - echo 1 > /proc/sys/kernel/ftrace_enabled - i=$((i + 1)) - done - - enable=$(( $count % 3 )) - - if [ $enable -eq 0 ]; then - echo 1 > /proc/sys/kernel/ftrace_enabled - else - echo 0 > /proc/sys/kernel/ftrace_enabled - fi - - sleep 1 -done -
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_function_profile_enabled.sh deleted +0 -38
@@ -1,38 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_LOOP=1500 -count=0 - - -while true; do - count=$(( $count + 1 )) - i=0 - while [ $i -lt $MAX_LOOP ]; do - echo 0 > "$TRACING_PATH"/function_profile_enabled 2> /dev/null - echo 1 > "$TRACING_PATH"/function_profile_enabled 2> /dev/null - i=$((i + 1)) - done - - enable=$(( $count % 3 )) - - if [ $enable -eq 0 ]; then - echo 1 > "$TRACING_PATH"/function_profile_enabled 2> /dev/null - else - echo 0 > "$TRACING_PATH"/function_profile_enabled 2> /dev/null - fi - - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_event.sh deleted +0 -46
46 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_filter.sh deleted +0 -119
119 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_set_ftrace_pid.sh deleted +0 -37
@@ -1,37 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=300 - -while true; do - j=0 - while [ $j -lt $LOOP ]; do - k=1 - while [ $k -le $NR_PIDS ]; do - str="\$pid$k" - eval echo $str >> "$TRACING_PATH"/set_ftrace_pid - k=$((k + 1)) - done - - if ! echo > "$TRACING_PATH"/set_ftrace_pid >/dev/null 2>&1; then - if ! echo -1 > "$TRACING_PATH"/set_ftrace_pid >/dev/null 2>&1; then - tst_resm TBROK "Cannot disable set_ftrace_pid!" - exit 1 - fi - fi - j=$((j + 1)) - done - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_max_size.sh deleted +0 -27
@@ -1,27 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_STACK_SIZE=8192 - -while true; do - i=0 - while [ $i -lt $MAX_STACK_SIZE ]; do - echo $i > "$TRACING_PATH"/stack_max_size - cat "$TRACING_PATH"/stack_max_size > /dev/null - i=$((i + 1)) - done - sleep 1 -done -
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_stack_trace.sh deleted +0 -35
@@ -1,35 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=400 - -while true; do - i=0 - while [ $i -lt $LOOP ]; do - cat "$TRACING_PATH"/stack_trace > /dev/null - i=$((i + 1)) - done - - sleep 1 - - i=0 - while [ $i -lt $LOOP ]; do - echo 0 > /proc/sys/kernel/stack_tracer_enabled - echo 1 > /proc/sys/kernel/stack_tracer_enabled - i=$((i + 1)) - done - - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace.sh deleted +0 -25
@@ -1,25 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=200 - -i=0; -while true; do - while [ $i -lt $LOOP ]; do - cat "$TRACING_PATH"/trace > /dev/null - i=$((i + 1)) - done - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_clock.sh deleted +0 -27
@@ -1,27 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=400 - -while true; do - i=0 - while [ $i -lt $LOOP ]; do - echo local > "$TRACING_PATH"/trace_clock - echo global > "$TRACING_PATH"/trace_clock - i=$((i + 1)) - done - sleep 1 -done -
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_options.sh deleted +0 -58
58 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_pipe.sh deleted +0 -45
45 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_trace_stat.sh deleted +0 -38
@@ -1,38 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -LOOP=200 - -should_skip=0 -nr_cpus=`tst_ncpus` - -if [ ! -e "$TRACING_PATH"/function_profile_enabled ]; then - should_skip=1 -fi - -while true; do - if [ $should_skip -eq 1 ]; then - sleep 2 - continue - fi - cpu=$(tst_random 0 $((nr_cpus - 1))) - i=0; - while [ $i -lt $LOOP ]; do - cat "$TRACING_PATH"/trace_stat/function${cpu} > /dev/null 2>&1 - i=$((i + 1)) - done - - sleep 1 -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_cpumask.sh deleted +0 -91
91 lines not on this page · raw message
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_enabled.sh deleted +0 -38
@@ -1,38 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_LOOP=1500 -count=0 - -while true; do - count=$(( $count + 1 )) - i=0 - while [ $i -lt $MAX_LOOP ]; do - echo 0 > "$TRACING_PATH"/tracing_enabled - echo 1 > "$TRACING_PATH"/tracing_enabled - i=$((i + 1)) - done - - enable=$(( $count % 3 )) - - if [ $enable -eq 0 ]; then - echo 0 > "$TRACING_PATH"/tracing_enabled - else - echo 1 > "$TRACING_PATH"/tracing_enabled - fi - - sleep 1 -done -
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_max_latency.sh deleted +0 -27
@@ -1,27 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_LATENCY=100000 - -while true; do - i=0 - while [ $i -lt $MAX_LATENCY ]; do - echo $i > "$TRACING_PATH"/tracing_max_latency - i=$((i + 400)) - done - - sleep 1 - -done
testcases/kernel/tracing/ftrace_test/ftrace_stress/ftrace_tracing_on.sh deleted +0 -38
@@ -1,38 +0,0 @@ -#! /bin/sh - -############################################################################### -# # -# Copyright (c) 2010 FUJITSU LIMITED # -# # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; either version 2 of the License, or (at your option) # -# any later version. # -# # -# Author: Li Zefan <[email protected]> # -# # -############################################################################### - -MAX_LOOP=1500 -count=0 - -while true; do - count=$(( $count + 1 )) - i=0 - while [ $i -lt $MAX_LOOP ]; do - echo 0 > "$TRACING_PATH"/tracing_on - echo 1 > "$TRACING_PATH"/tracing_on - i=$((i + 1)) - done - - enable=$(( $count % 3 )) - - if [ $enable -eq 0 ]; then - echo 0 > "$TRACING_PATH"/tracing_on - else - echo 1 > "$TRACING_PATH"/tracing_on - fi - - sleep 1 -done -
testcases/kernel/tracing/ftrace_test/ftrace_stress_test.sh deleted +0 -121
121 lines not on this page · raw message
Signature · 4 lines
2.50.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp