Skip to contentSkip to conversation

libtracefs: Have string lists contain their size

PATCH New Steven Rostedt 1 participant 3 messages Jul 2, 2021

lore
Share & tools
lore
#0 lore raw

cover libtracefs: Have string lists contain their size

Have the string lists contain the size inside them.

The way this is done is by allocating one pointer than the user needs:

/* just pseudo code of the idea, no error checking */

 if (!list) {
	list = malloc(sizeof(*list) * 3);
	list[0] = (char *)1;
	list[1] = strdup(users_string);
	list[2] = NULL;
	return &list[1];

}
list--;
size = *(unsigned long *)list;
tmp = realloc(list, sizeof(*list) * (size + 3));
list = tmp;
list[0] = (char *)(size + 1);
list++;
list[size++] = strdup(users_string);
list[size] = NULL;
return list;

Implementing this has fixed a few bugs and memory leaks.

Steven Rostedt (VMware) (2):
libtracefs: Move tracefs_list_free() to tracefs-utils.c
libtracefs: Restructure how string lists work

 include/tracefs-local.h |  1 +
 include/tracefs.h       |  4 +-
 src/tracefs-events.c    | 61 ++++++++++--------------------
 src/tracefs-instance.c  |  9 +----
 src/tracefs-kprobes.c   | 17 ++-------
 src/tracefs-tools.c     |  6 +--
 src/tracefs-utils.c     | 83 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 112 insertions(+), 69 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 libtracefs: Move tracefs_list_free() to tracefs-utils.c

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

The tracefs_list_free() function is more a helper utility, and it is better placed in tracefs-utils.c than tracefs-events.c

---
 src/tracefs-events.c | 20 --------------------
 src/tracefs-utils.c  | 20 ++++++++++++++++++++
 2 files changed, 20 insertions(+), 20 deletions(-)
src/tracefs-events.c +0 -20
index 568bfe60be3b..77f4624776e2 100644
@@ -284,26 +284,6 @@ __hidden char *trace_append_file(const char *dir, const char *name)
 	return ret < 0 ? NULL : file;
 }
 
-/**
- * tracefs_list_free - free list if strings, returned by APIs
- *			tracefs_event_systems()
- *			tracefs_system_events()
- *
- *@list pointer to a list of strings, the last one must be NULL
- */
-void tracefs_list_free(char **list)
-{
-	int i;
-
-	if (!list)
-		return;
-
-	for (i = 0; list[i]; i++)
-		free(list[i]);
-
-	free(list);
-}
-
 /**
  * tracefs_event_systems - return list of systems for tracing
  * @tracing_dir: directory holding the "events" directory
src/tracefs-utils.c +20 -0
index a8dd7fc35a00..b29131ecf0c5 100644
@@ -364,3 +364,23 @@ int tracefs_error_clear(struct tracefs_instance *instance)
 {
 	return tracefs_instance_file_clear(instance, ERROR_LOG);
 }
+
+/**
+ * tracefs_list_free - free list if strings, returned by APIs
+ *			tracefs_event_systems()
+ *			tracefs_system_events()
+ *
+ *@list pointer to a list of strings, the last one must be NULL
+ */
+void tracefs_list_free(char **list)
+{
+	int i;
+
+	if (!list)
+		return;
+
+	for (i = 0; list[i]; i++)
+		free(list[i]);
+
+	free(list);
+}
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 libtracefs: Restructure how string lists work

In reply to Steven Rostedt

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

Add the size of the string list to the allocated list, and return to the user the address of the second element. That is:

char **list;

  list[0] = (char *)size;
  list[1] = string1;
  list[2] = string2;
  [..]
  list[n] = NULL

The size will contain the number of strings in the list.

The users of this list will not have access to the size (they should not know about it). This is why all tracefs strings must be freed with tracefs_list_free() as this will handle the allocations correctly.

---
 include/tracefs-local.h |  1 +
 include/tracefs.h       |  4 ++-
 src/tracefs-events.c    | 41 +++++++++++++--------------
 src/tracefs-instance.c  |  9 +-----
 src/tracefs-kprobes.c   | 17 +++--------
 src/tracefs-tools.c     |  6 +---
 src/tracefs-utils.c     | 63 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 92 insertions(+), 49 deletions(-)
include/tracefs-local.h +1 -0
index 73698e8014bd..92ad7e6d58e9 100644
@@ -65,4 +65,5 @@ supported_opts_mask(struct tracefs_instance *instance);
 struct tracefs_options_mask *
 enabled_opts_mask(struct tracefs_instance *instance);
 
+char **trace_list_create_empty(void);
 #endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +3 -1
index 1c8703ae7e26..04e2aec431ee 100644
@@ -63,6 +63,9 @@ char *tracefs_error_last(struct tracefs_instance *instance);
 char *tracefs_error_all(struct tracefs_instance *instance);
 int tracefs_error_clear(struct tracefs_instance *instance);
 
+void tracefs_list_free(char **list);
+char **tracefs_list_add(char **list, const char *string);
+
 /**
  * tracefs_trace_on_get_fd - Get a file descriptor of "tracing_on" in given instance
  * @instance: ftrace instance, can be NULL for the top instance
@@ -87,7 +90,6 @@ int tracefs_binary_write(struct tracefs_instance *instance, void *data, int len)
 void tracefs_binary_close(struct tracefs_instance *instance);
 
 /* events */
-void tracefs_list_free(char **list);
 char **tracefs_event_systems(const char *tracing_dir);
 char **tracefs_system_events(const char *tracing_dir, const char *system);
 int tracefs_iterate_raw_events(struct tep_handle *tep,
src/tracefs-events.c +19 -22

92 lines not on this page · raw message

src/tracefs-instance.c +1 -8
index ba7667349be2..11fb580456ff 100644
@@ -641,7 +641,6 @@ static inline bool match(const char *str, regex_t *re)
 struct instance_list {
 	regex_t		*re;
 	char		**list;
-	int		size;
 	int		failed;
 };
 
@@ -654,17 +653,11 @@ static int build_list(const char *name, void *data)
 	if (!match(name, list->re))
 		return 0;
 
-	instances = realloc(list->list, sizeof(*instances) * (list->size + 2));
+	instances = tracefs_list_add(list->list, name);
 	if (!instances)
 		goto out;
 
 	list->list = instances;
-	instances[list->size] = strdup(name);
-	if (!instances[list->size])
-		goto out;
-
-	list->size++;
-	instances[list->size] = NULL;
 	ret = 0;
 
  out:
src/tracefs-kprobes.c +4 -13
index 2bc589a82627..6fdd8f90a6ca 100644
@@ -181,7 +181,6 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 	char *saveptr;
 	char *event;
 	char *ktype;
-	int cnt = 0;
 	int ret;
 
 	errno = 0;
@@ -190,8 +189,7 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 		if (errno)
 			return NULL;
 		/* content is NULL on empty file, return an empty list */
-		list = calloc(1, sizeof(*list));
-		return list;
+		return trace_list_create_empty();
 	}
 
 	ret = parse_kprobe(content, &saveptr, &ktype, NULL, &event, NULL, NULL);
@@ -214,28 +212,21 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 			}
 		}
 
-		event = strdup(event);
-		if (!event)
-			goto fail;
-
-		tmp = realloc(list, sizeof(*list) * (cnt + 2));
+		tmp = tracefs_list_add(list, event);
 		if (!tmp)
 			goto fail;
-
 		list = tmp;
-		list[cnt++] = event;
-		list[cnt] = NULL;
  next:
 		ret = parse_kprobe(NULL, &saveptr, &ktype, NULL, &event, NULL, NULL);
 	}
 
 	if (!list)
-		list = calloc(1, sizeof(*list));
+		list = trace_list_create_empty();
  out:
 	free(content);
 	return list;
  fail:
-	free(list);
+	tracefs_list_free(list);
 	list = NULL;
 	goto out;
 }
src/tracefs-tools.c +1 -5
index 42dc68cf164b..ca0ed58ab8cd 100644
@@ -1238,7 +1238,6 @@ int tracefs_filter_functions(const char *filter, const char *module, char ***lis
 	struct func_filter func_filter;
 	struct func_list *func_list, *f;
 	char **funcs = NULL;
-	int cnt = 0;
 	int ret;
 
 	if (!filter)
@@ -1256,14 +1255,11 @@ int tracefs_filter_functions(const char *filter, const char *module, char ***lis
 	for (f = func_list; f; f = f->next) {
 		char **tmp;
 
-		tmp = realloc(funcs, sizeof(*funcs) * (cnt + 2));
+		tmp = tracefs_list_add(funcs, f->func);
 		if (!tmp) {
 			tracefs_list_free(funcs);
 			goto out;
 		}
-		tmp[cnt++] = f->func;
-		tmp[cnt] = NULL;
-		f->func = NULL;
 		funcs = tmp;
 	}
 
src/tracefs-utils.c +63 -0

68 lines not on this page · raw message

Signature · 1 line
2.30.2