From: Francis Deslauriers Date: Thu, 28 Jun 2018 13:47:51 +0000 (-0400) Subject: Add lttng_event copy constructor X-Git-Tag: v2.11.0-rc1~84 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=abcc74facc86124042abdb9fd4391b6eadda2b9e;p=lttng-tools.git Add lttng_event copy constructor Since the lttng_event structure now has an extended_ptr to store additional data, such as a userspace probe location, we can't simply memcpy the struct to duplicate it. This commit introduces a lttng_event_copy function that allocates a new lttng_event struct and copies the content of both the struct and its extended_ptr. Signed-off-by: Francis Deslauriers Signed-off-by: Jérémie Galarneau --- diff --git a/include/lttng/event.h b/include/lttng/event.h index 0131defcc..548ec2a46 100644 --- a/include/lttng/event.h +++ b/include/lttng/event.h @@ -304,6 +304,8 @@ extern int lttng_list_events(struct lttng_handle *handle, extern struct lttng_event *lttng_event_create(void); +extern struct lttng_event *lttng_event_copy(const struct lttng_event *event); + extern void lttng_event_destroy(struct lttng_event *event); /* diff --git a/src/lib/lttng-ctl/event.c b/src/lib/lttng-ctl/event.c index 9413b7052..046028cd2 100644 --- a/src/lib/lttng-ctl/event.c +++ b/src/lib/lttng-ctl/event.c @@ -54,6 +54,38 @@ error: goto end; } +struct lttng_event *lttng_event_copy(const struct lttng_event *event) +{ + struct lttng_event *new_event; + struct lttng_event_extended *new_event_extended; + + new_event = zmalloc(sizeof(*event)); + if (!event) { + PERROR("Error allocating event structure"); + goto end; + } + + /* Copy the content of the old event. */ + memcpy(new_event, event, sizeof(*event)); + + /* + * We need to create a new extended since the previous pointer is now + * invalid. + */ + new_event_extended = zmalloc(sizeof(*new_event_extended)); + if (!new_event_extended) { + PERROR("Error allocating event extended structure"); + goto error; + } + + new_event->extended.ptr = new_event_extended; +end: + return new_event; +error: + free(event); + goto end; +} + void lttng_event_destroy(struct lttng_event *event) { struct lttng_event_extended *event_extended;