Skip to contentSkip to conversation

trace-cmd: Add gcc printf format specifier

PATCH New Steven Rostedt 1 participant 3 messages Apr 22, 2021

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Add gcc printf format specifier

From: "Steven Rostedt (VMware)" <[email protected]>

To keep from having bad warning() or die() formats with repect to their args, add the gcc printf format specifier that will have gcc check the formatting of the strings used.

Steven Rostedt (VMware) (2):
trace-cmd: Add printf format attribute to die() and warning()
trace-cmd: Fix up the die() formats

 tracecmd/include/trace-local.h |  5 +++++
 tracecmd/trace-dump.c          | 14 +++++++-------
 tracecmd/trace-record.c        |  2 +-
 3 files changed, 13 insertions(+), 8 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/2 trace-cmd: Add printf format attribute to die() and warning()

From: "Steven Rostedt (VMware)" <[email protected]>

Add the gcc printf format attribute to die(), __die() and warning() such that any mistake in the formats of the output will be caught by the compiler.

---
 tracecmd/include/trace-local.h | 5 +++++
 1 file changed, 5 insertions(+)
tracecmd/include/trace-local.h +5 -0
index 4235ae21..eadf103e 100644
@@ -27,6 +27,9 @@ typedef unsigned long long u64;
 
 struct buffer_instance;
 
+#define __printf(a, b) __attribute__((format(printf,a,b)))
+
+__printf(1,2)
 void warning(const char *fmt, ...);
 
 /* for local shared information with trace-cmd executable */
@@ -331,8 +334,10 @@ int trace_open_vsock(unsigned int cid, unsigned int port);
 char *trace_get_guest_file(const char *file, const char *guest);
 
 /* No longer in event-utils.h */
+__printf(1,2)
 void __noreturn die(const char *fmt, ...); /* Can be overriden */
 void *malloc_or_die(unsigned int size); /* Can be overridden */
+__printf(1,2)
 void __noreturn __die(const char *fmt, ...);
 void __noreturn _vdie(const char *fmt, va_list ap);
 
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/2 trace-cmd: Fix up the die() formats

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

After adding the printf format attribute to die(), gcc found several places that the wrong format specifier (or no specifier) was used.

Fix them all up.

---
 tracecmd/trace-dump.c   | 14 +++++++-------
 tracecmd/trace-record.c |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)
tracecmd/trace-dump.c +7 -7
index 3f56f65a..98425b98 100644
@@ -150,13 +150,13 @@ static void dump_initial_format(int fd)
 
 	/* check initial bytes */
 	if (read_file_bytes(fd, buf, sizeof(magic)))
-		die("cannot read %d bytes magic", sizeof(magic));
+		die("cannot read %ld bytes magic", sizeof(magic));
 	if (memcmp(buf, magic, sizeof(magic)) != 0)
 		die("wrong file magic");
 
 	/* check initial tracing string */
 	if (read_file_bytes(fd, buf, strlen(TRACING_STR)))
-		die("cannot read %d bytes tracing string", strlen(TRACING_STR));
+		die("cannot read %ld bytes tracing string", strlen(TRACING_STR));
 	buf[strlen(TRACING_STR)] = 0;
 	if (strncmp(buf, TRACING_STR, strlen(TRACING_STR)) != 0)
 		die("wrong tracing string: %s", buf);
@@ -194,7 +194,7 @@ static void dump_header_page(int fd)
 
 	/* check header string */
 	if (read_file_bytes(fd, buf, strlen(HEAD_PAGE_STR) + 1))
-		die("cannot read %d bytes header string", strlen(HEAD_PAGE_STR));
+		die("cannot read %ld bytes header string", strlen(HEAD_PAGE_STR));
 	if (strncmp(buf, HEAD_PAGE_STR, strlen(HEAD_PAGE_STR)) != 0)
 		die("wrong header string: %s", buf);
 
@@ -215,7 +215,7 @@ static void dump_header_event(int fd)
 
 	/* check header string */
 	if (read_file_bytes(fd, buf, strlen(HEAD_PAGE_EVENT) + 1))
-		die("cannot read %d bytes header string", strlen(HEAD_PAGE_EVENT));
+		die("cannot read %ld bytes header string", strlen(HEAD_PAGE_EVENT));
 	if (strncmp(buf, HEAD_PAGE_EVENT, strlen(HEAD_PAGE_EVENT)) != 0)
 		die("wrong header string: %s", buf);
 
@@ -263,7 +263,7 @@ static void dump_events_format(int fd)
 	while (systems) {
 
 		if (read_file_string(fd, buf, DUMP_SIZE))
-			die("cannot read the name of the $dth system", systems);
+			die("cannot read the name of the %dth system", systems);
 		if (read_file_number(fd, &events, 4))
 			die("cannot read the count of the events in system %s",
 			     buf);
@@ -598,10 +598,10 @@ static void dump_clock(int fd)
 		die("cannot read clock size");
 	clock = calloc(1, size);
 	if (!clock)
-		die("cannot allocate clock %d bytes", size);
+		die("cannot allocate clock %lld bytes", size);
 
 	if (read_file_bytes(fd, clock, size))
-		die("cannot read clock %d bytes", size);
+		die("cannot read clock %lld bytes", size);
 	clock[size] = 0;
 	do_print((SUMMARY | CLOCK), "\t\t%s\n", clock);
 	free(clock);
tracecmd/trace-record.c +1 -1
index fd03a605..db23c001 100644
@@ -3838,7 +3838,7 @@ static void setup_guest(struct buffer_instance *instance)
 
 	fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 	if (fd < 0)
-		die("Failed to open", file);
+		die("Failed to open %s", file);
 
 	/* Start reading tracing metadata */
 	if (tracecmd_msg_read_data(msg_handle, fd))
Signature · 1 line
2.29.2