Skip to contentSkip to conversation

Add -ust to the name of UST threads of the application

RFC PATCH Superseded Raphaël Beamonte 4 participants 22 messages Apr 21, 2016 – Jun 5, 2016 16 messages not in the archive

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

RFC PATCH 1/2 Add -ust to the name of UST threads of the application

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)
liblttng-ust/compat.h +22 -0
index 43b2223..8d5fc77 100644
@@ -34,6 +34,23 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;
+
+	lttng_ust_getprocname(name);
+
+	if (strlen(name) >= limit) {
+		name[limit] = 0;
+	}
+
+	sprintf(name, "%s%s", name, "-ust");
+
+	(void) prctl(PR_SET_NAME, name, 0, 0, 0);
+}
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +76,11 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+static inline
+void lttng_ust_setustprocname()
+{
+}
+
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +2 -0
index e00a22c..a85cb21 100644
@@ -1295,6 +1295,8 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	lttng_ust_setustprocname();
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {
Signature · 6 lines
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#3 lore raw

RFC PATCH 2/2 Add -ust to the name of UST threads of the application

In reply to a message not in the archive

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)
liblttng-ust/Makefile.am +1 -0
index 876e9b5..8d78d63 100644
@@ -13,6 +13,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
 liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing
liblttng-ust/compat.h +35 -0
index 43b2223..161b957 100644
@@ -24,6 +24,7 @@
  */
 #ifdef __linux__
 
+#include <pthread.h>
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +35,23 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;
+
+	lttng_ust_getprocname(name);
+
+	if (strlen(name) >= limit) {
+		name[limit] = 0;
+	}
+
+	sprintf(name, "%s%s", name, "-ust");
+
+	pthread_setname_np(pthread_self(), name);
+}
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +77,23 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;
+
+	lttng_ust_getprocname(name);
+
+	if (strlen(name) >= limit) {
+		name[limit] = 0;
+	}
+
+	sprintf(name, "%s%s", name, "-ust");
+
+	pthread_set_name_np(pthread_self(), name);
+}
+
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +2 -0
index e00a22c..a85cb21 100644
@@ -1295,6 +1295,8 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	lttng_ust_setustprocname();
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {
Signature · 6 lines
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#4 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

In reply to a message not in the archive

Hi,

I did not get any feedback about this, any thoughts ?

Thanks,
Raphaël

2016-04-21 16:50 GMT-04:00 Raphaël Beamonte <[email protected]>:

Quoted 47 lines

Hi,

This is an RFC patch for comments and ideas about how this should be done. In recent work I've been doing, I found out that some UST- instrumented userspace process was preempting itself. Looking further, it was not the same TID, but the same process name. These processes were LTTng-UST listener threads, but were not identified as so. Adding a simple symbol, such as "-ust" or "-lttng" at the end of the process name would help to identify such process directly in the generated trace.

You'll find below two different implementations with their pros and cons, for which the "-ust" symbol was choosen:

1/ An implementation using prctl
It works properly on Linux, but I didn't find a way to make it work under FreeBSD, hence the empty function in FreeBSD case (keeping the situation as it is now).

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)

2/ An implementation using pthread
It works on both Linux and FreeBSD, but requires to add a link to the pthread library in liblttng-ust Makefile.am. Also, this approach relies on the presence of the pthread_setname_np function, which was included in glibc 2.12, meaning it will not work with older versions (but this should not be a problem?)

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)
Quoted 9 lines

I'll wait for your comments and ideas on that subject.

Thanks,
Raphaël

--
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CAE_Gge2jacOMKQaPdp_dXf3NP+yJTS+jWgJQ-SUnv1-B6WgcGw@mail.gmail.com

#6 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

Hi Raphaël,

Adding Mathieu in CC. Please make sure you CC the maintainers when submitting a patch.

Regards,
Jérémie

Quoted 68 lines · Raphaël Beamonte

Hi,

I did not get any feedback about this, any thoughts ?

Thanks,
Raphaël

2016-04-21 16:50 GMT-04:00 Raphaël Beamonte <[email protected]>:

Quoted 47 lines

Hi,

This is an RFC patch for comments and ideas about how this should be done. In recent work I've been doing, I found out that some UST- instrumented userspace process was preempting itself. Looking further, it was not the same TID, but the same process name. These processes were LTTng-UST listener threads, but were not identified as so. Adding a simple symbol, such as "-ust" or "-lttng" at the end of the process name would help to identify such process directly in the generated trace.

You'll find below two different implementations with their pros and cons, for which the "-ust" symbol was choosen:

1/ An implementation using prctl
It works properly on Linux, but I didn't find a way to make it work under FreeBSD, hence the empty function in FreeBSD case (keeping the situation as it is now).

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)

2/ An implementation using pthread
It works on both Linux and FreeBSD, but requires to add a link to the pthread library in liblttng-ust Makefile.am. Also, this approach relies on the presence of the pthread_setname_np function, which was included in glibc 2.12, meaning it will not work with older versions (but this should not be a problem?)

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)
Quoted 9 lines

I'll wait for your comments and ideas on that subject.

Thanks,
Raphaël

--
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 7 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CA+jJMxvmHRYYtHWYjumXrS1BrBCih3WJnRA5yGFBXTw7jL3HiA@mail.gmail.com

#8 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

And, of course, I forget to add Mathieu in CC... Coffee time!

Jérémie

Quoted 85 lines · Jérémie Galarneau

Hi Raphaël,

Adding Mathieu in CC. Please make sure you CC the maintainers when submitting a patch.

Regards,
Jérémie

Quoted 61 lines · Raphaël Beamonte

Hi,

I did not get any feedback about this, any thoughts ?

Thanks,
Raphaël

2016-04-21 16:50 GMT-04:00 Raphaël Beamonte <[email protected]>:

Quoted 53 lines

Hi,

This is an RFC patch for comments and ideas about how this should be done. In recent work I've been doing, I found out that some UST- instrumented userspace process was preempting itself. Looking further, it was not the same TID, but the same process name. These processes were LTTng-UST listener threads, but were not identified as so. Adding a simple symbol, such as "-ust" or "-lttng" at the end of the process name would help to identify such process directly in the generated trace.

You'll find below two different implementations with their pros and cons, for which the "-ust" symbol was choosen:

1/ An implementation using prctl
It works properly on Linux, but I didn't find a way to make it work under FreeBSD, hence the empty function in FreeBSD case (keeping the situation as it is now).

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)

2/ An implementation using pthread
It works on both Linux and FreeBSD, but requires to add a link to the pthread library in liblttng-ust Makefile.am. Also, this approach relies on the presence of the pthread_setname_np function, which was included in glibc 2.12, meaning it will not work with older versions (but this should not be a problem?)

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)

I'll wait for your comments and ideas on that subject.

Thanks,
Raphaël

Quoted 7 lines
Quoted 3 lines

--
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

Signature · 7 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CA+jJMxs9yNOeeqtYBfGTz1ADOqqiVO=XZd-7qm48BS8dzVo-iw@mail.gmail.com

#10 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

Quoted 60 lines · ----- On Jun 3, 2016, at 8:11 AM, Jeremie Galarneau…

And, of course, I forget to add Mathieu in CC... Coffee time!

Jérémie

Quoted 54 lines · Jérémie Galarneau

Hi Raphaël,

Adding Mathieu in CC. Please make sure you CC the maintainers when submitting a patch.

Regards,
Jérémie

Quoted 44 lines · Raphaël Beamonte

Hi,

I did not get any feedback about this, any thoughts ?

Thanks,
Raphaël

2016-04-21 16:50 GMT-04:00 Raphaël Beamonte <[email protected]>:

Quoted 36 lines

Hi,

This is an RFC patch for comments and ideas about how this should be done. In recent work I've been doing, I found out that some UST- instrumented userspace process was preempting itself. Looking further, it was not the same TID, but the same process name. These processes were LTTng-UST listener threads, but were not identified as so. Adding a simple symbol, such as "-ust" or "-lttng" at the end of the process name would help to identify such process directly in the generated trace.

You'll find below two different implementations with their pros and cons, for which the "-ust" symbol was choosen:

1/ An implementation using prctl
It works properly on Linux, but I didn't find a way to make it work under FreeBSD, hence the empty function in FreeBSD case (keeping the situation as it is now).

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)

2/ An implementation using pthread
It works on both Linux and FreeBSD, but requires to add a link to the pthread library in liblttng-ust Makefile.am. Also, this approach relies on the presence of the pthread_setname_np function, which was included in glibc 2.12, meaning it will not work with older versions (but this should not be a problem?)

lttng-ust already uses libc for mutexes and other. So I don't see why you have to _add_ any explicit dependency there.

About the glibc version, we should use autotools to detect the features supported by the system libc, and pass a config define to the source code, so we can use the glibc pthread_setname_np if available, else we keep the current behavior.

Thoughts ?

Thanks,

Mathieu

Quoted 38 lines
Quoted 27 lines
Quoted 17 lines
Quoted 17 lines

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)

I'll wait for your comments and ideas on that subject.

Thanks,
Raphaël

Quoted 7 lines
Quoted 3 lines

--
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Quoted 4 lines

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

Quoted 64 lines · ----- On Jun 3, 2016, at 8:18 AM, Mathieu Desnoyers…

----- On Jun 3, 2016, at 8:11 AM, Jeremie Galarneau

Quoted 60 lines · [email protected] wrote:

And, of course, I forget to add Mathieu in CC... Coffee time!

Jérémie

Quoted 54 lines · Jérémie Galarneau

Hi Raphaël,

Adding Mathieu in CC. Please make sure you CC the maintainers when submitting a patch.

Regards,
Jérémie

Quoted 44 lines · Raphaël Beamonte

Hi,

I did not get any feedback about this, any thoughts ?

Thanks,
Raphaël

2016-04-21 16:50 GMT-04:00 Raphaël Beamonte <[email protected]>:

Quoted 36 lines

Hi,

This is an RFC patch for comments and ideas about how this should be done. In recent work I've been doing, I found out that some UST- instrumented userspace process was preempting itself. Looking further, it was not the same TID, but the same process name. These processes were LTTng-UST listener threads, but were not identified as so. Adding a simple symbol, such as "-ust" or "-lttng" at the end of the process name would help to identify such process directly in the generated trace.

You'll find below two different implementations with their pros and cons, for which the "-ust" symbol was choosen:

1/ An implementation using prctl
It works properly on Linux, but I didn't find a way to make it work under FreeBSD, hence the empty function in FreeBSD case (keeping the situation as it is now).

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/compat.h         | 22 ++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 2 files changed, 24 insertions(+)

2/ An implementation using pthread
It works on both Linux and FreeBSD, but requires to add a link to the pthread library in liblttng-ust Makefile.am. Also, this approach relies on the presence of the pthread_setname_np function, which was included in glibc 2.12, meaning it will not work with older versions (but this should not be a problem?)

Quoted 2 lines

lttng-ust already uses libc for mutexes and other. So I don't see why you have to _add_ any explicit dependency there.

and we already explicitly use pthread (pthread_create).

Thanks,

Mathieu

Quoted 59 lines

About the glibc version, we should use autotools to detect the features supported by the system libc, and pass a config define to the source code, so we can use the glibc pthread_setname_np if available, else we keep the current behavior.

Thoughts ?

Thanks,

Mathieu

Quoted 34 lines
Quoted 27 lines
Quoted 17 lines
Quoted 17 lines

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 35 +++++++++++++++++++++++++++++++++++
 liblttng-ust/lttng-ust-comm.c |  2 ++
 3 files changed, 38 insertions(+)

I'll wait for your comments and ideas on that subject.

Thanks,
Raphaël

Quoted 7 lines
Quoted 3 lines

--
2.1.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Quoted 4 lines

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

Quoted 4 lines

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

Hi!

Thanks for all of your comments, I appreciate it! Thanks Jeremie for adding Mathieu in Cc, forgot to do it... Will remember next time! :)

2016-06-02 9:22 GMT-04:00 Sebastien Boisvert <[email protected]>:

Quoted 2 lines

Do you know if the name of these listener threads can be set in their constructor, directly when they are brought into this world ?

pthread_create does not allow to specify a name when creating the new thread, as far as I know! That's why the 'lttng_ust_setustprocname()' function is called first thing in the ust_listener_thread function.

Quoted 2 lines

Perhaps the availability of pthread_setname_np and/or pthread_set_name_np (you have both names in your patch 2) should be ascertained in another venue, such as the configure.ac -- where it belongs.

True!

2016-06-03 2:24 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 5 lines
Quoted 2 lines · ----- On Jun 3, 2016, at 8:18 AM, Mathieu Desnoyers…

lttng-ust already uses libc for mutexes and other. So I don't see why you have to _add_ any explicit dependency there.

and we already explicitly use pthread (pthread_create).

You're right, pthread is already used. However, when compiling the code without adding the pthread dependency, I get the following warning:

In file included from lttng-tracer.h:30:0,

                 from lttng-ust-abi.c:50:

compat.h: In function ‘lttng_ust_setustprocname’:
compat.h:52:2: warning: implicit declaration of function ‘pthread_setname_np’ [-Wimplicit-function-declaration]
pthread_setname_np(pthread_self(), name);
^
CC lttng-context-procname.lo
In file included from lttng-context-procname.c:28:0:
compat.h: In function ‘lttng_ust_setustprocname’:
compat.h:52:2: warning: implicit declaration of function ‘pthread_setname_np’ [-Wimplicit-function-declaration]
pthread_setname_np(pthread_self(), name);
^

It seems to be because pthread is not specified as dependency for liblttng-ust-tracepoint in liblttng-ust/Makefile.am. However, perhaps that pthread dependency should be added elsewhere?

Quoted 4 lines
Quoted 4 lines

About the glibc version, we should use autotools to detect the features supported by the system libc, and pass a config define to the source code, so we can use the glibc pthread_setname_np if available, else we keep the current behavior.

That's a good idea, will do!

I'll update and rebase the patch following your comments.
If you have any other insight about the pthread dependency, I'm all ears!

Thanks,
Raphaël
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CAE_Gge2032JkDubp=vpaqNzoQre7MfvQC4UGm-wXPmux-Fwh6g@mail.gmail.com

#16 lore raw

Re: [RFC PATCH 0/2] Identify UST threads by changing thread names

Quoted 49 lines · ----- On Jun 3, 2016, at 4:12 PM, Raphaël Beamonte [email protected]…

Hi!

Thanks for all of your comments, I appreciate it! Thanks Jeremie for adding Mathieu in Cc, forgot to do it... Will remember next time! :)

2016-06-02 9:22 GMT-04:00 Sebastien Boisvert <[email protected]>:

Quoted 3 lines

Do you know if the name of these listener threads can be set in their constructor, directly
when they are brought into this world ?

pthread_create does not allow to specify a name when creating the new thread, as far as I know! That's why the 'lttng_ust_setustprocname()' function is called first thing in the ust_listener_thread function.

Quoted 4 lines

Perhaps the availability of pthread_setname_np and/or pthread_set_name_np (you have both names
in your patch 2) should be ascertained in another venue, such as the configure.ac -- where it belongs.

True!

2016-06-03 2:24 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 6 lines

----- On Jun 3, 2016, at 8:18 AM, Mathieu Desnoyers

Quoted 2 lines · [email protected] wrote:

lttng-ust already uses libc for mutexes and other. So I don't see why you have to _add_ any explicit dependency there.

and we already explicitly use pthread (pthread_create).

You're right, pthread is already used. However, when compiling the code without adding the pthread dependency, I get the following warning:

In file included from lttng-tracer.h:30:0,

                from lttng-ust-abi.c:50:

compat.h: In function ‘lttng_ust_setustprocname’:
compat.h:52:2: warning: implicit declaration of function ‘pthread_setname_np’ [-Wimplicit-function-declaration]
pthread_setname_np(pthread_self(), name);
^
CC lttng-context-procname.lo
In file included from lttng-context-procname.c:28:0:
compat.h: In function ‘lttng_ust_setustprocname’:
compat.h:52:2: warning: implicit declaration of function ‘pthread_setname_np’ [-Wimplicit-function-declaration]
pthread_setname_np(pthread_self(), name);
^

Quoted 3 lines

It seems to be because pthread is not specified as dependency for liblttng-ust-tracepoint in liblttng-ust/Makefile.am. However, perhaps that pthread dependency should be added elsewhere?

Seems fair.

Thanks,

Mathieu

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

About the glibc version, we should use autotools to detect the features supported by the system libc, and pass a config define to the source code, so we can use the glibc pthread_setname_np if available, else we keep the current behavior.

That's a good idea, will do!

I'll update and rebase the patch following your comments.
If you have any other insight about the pthread dependency, I'm all ears!

Thanks,
Raphaël

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#17 lore raw

v2 cover Identify UST threads by changing thread names

In reply to a message not in the archive

Hi,

This is the update of the RFC patch following your comments. This patch updates the pthread_setname_np/pthread_set_name_np version of the RFC patch, and includes the following changes:
- Check in configure.ac whether the pthread_setname_np or pthread_set_name_np functions exists. If it's not the case, current behavior is preserved.
- Update compat.h to define a macro for the pthread_set*name_np function to use in order to reduce repeated code.
- Update compat.h and lttng-ust-comm.c to use a macro to call the lttng_ust_setustprocname function in order for that macro to be empty when the required pthread_setn*ame_np is not available.
- Update compat.h to fix sprintf usage with the same variable to read from and write to.
- The patch has been rebased on current lttng-ust's master branch.

This patch still contains the link to the pthread library in the liblttng-ust/Makefile.am file, which still seems required to avoid compilation warnings about implicit function declaration.

Thoughts and comments?

Thanks!
Raphaël

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
Signature · 6 lines
2.8.1

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#18 lore raw

v3 cover Identify UST threads by changing thread names

In reply to a message not in the archive

Hi,

This is the update of the RFC patch following your comments. This patch updates the pthread_setname_np/pthread_set_name_np version of the RFC patch, and includes the following changes:
- Add a LTTNG_UST_PROCNAME_SUFFIX macro in compat.h that contains the string to be added at the end of the process name
- Fix the limit value (was 4 instead of 5, now uses directly strlen on the LTTNG_UST_PROCNAME_SUFFIX macro value)
- Removes the macro used to call the lttng_ust_setustprocname function, an empty lttng_ust_setustprocname is called instead when the set*name pthread functions are not available
- Replaced 'lttng_ust_setustprocname()' per 'lttng_ust_setustprocname(void)' in the function declaration.

This should address all the comments you had!
Any other thoughts or comments?

Thanks a lot!
Raphaël

Raphaël Beamonte (1):
Add -ust to the name of UST threads of the application

 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 51 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 +++++
 4 files changed, 62 insertions(+), 1 deletion(-)
Signature · 6 lines
2.8.1

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#19 lore raw

RFC PATCH v2 1/1 Add -ust to the name of UST threads of the application

In reply to a message not in the archive

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))
 
 # Check for dlfcn.h
 AC_CHECK_HEADER([dlfcn.h])
liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
 liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing
liblttng-ust/compat.h +47 -1

72 lines not on this page · raw message

liblttng-ust/lttng-ust-comm.c +6 -0
index e00a22c..4de68c7 100644
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	LTTNG_UST_SETUSTPROCNAME;
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {
Signature · 6 lines
2.8.1

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): 80ee3399800d43b5d3f51aa095ebca160d4f20b9.1464966729.git.raphael.beamonte@gmail.com

#21 lore raw

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 91 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))
 
 # Check for dlfcn.h
 AC_CHECK_HEADER([dlfcn.h])
liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
 liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing
liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+	if (len >= limit) {
+		len = limit;
+	}
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the current stack frame.

Quoted 13 lines
liblttng-ust/compat.h +5 -0
+
+	PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

lttng_ust_setustprocname()

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1306

Quoted 5 lines
liblttng-ust/lttng-ust-comm.c +1 -0
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw
Quoted 178 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+	if (len >= limit) {
+		len = limit;
+	}
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

Quoted 13 lines
liblttng-ust/compat.h +5 -0
+
+	PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

Quoted 7 lines

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

Thanks,

Mathieu

Quoted 7 lines
Quoted 7 lines
liblttng-ust/lttng-ust-comm.c +1 -0
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {
Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#25 lore raw
Quoted 182 lines · ----- On Jun 3, 2016, at 5:54 PM, Mathieu Desnoyers…
Quoted 180 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+	char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+	if (len >= limit) {
+		len = limit;
+	}
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+	sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

Quoted 13 lines
liblttng-ust/compat.h +5 -0
+
+	PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

Quoted 8 lines
Quoted 5 lines

LTTNG_UST_SETUSTPROCNAME()

or

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

And there is no reason to make it a macro. It should be a static inline whenever possible.

Thanks,

Mathieu

Quoted 16 lines

Thanks,

Mathieu

Quoted 7 lines

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1306

Quoted 7 lines
liblttng-ust/lttng-ust-comm.c +1 -0
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#27 lore raw

2016-06-03 11:55 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 148 lines
Quoted 146 lines · ----- On Jun 3, 2016, at 5:54 PM, Mathieu Desnoyers…
Quoted 144 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+   AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+   AC_CHECK_LIB([pthread], [pthread_set_name_np],
+           AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+           AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
    error.h
 liblttng_ust_tracepoint_la_LIBADD = \
    -lurcu-bp \
+   -lpthread \
    $(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
    (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
            strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+   char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+   if (len >= limit) {
+           len = limit;
+   }
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

You're right, will fix that.
I'm also questionning the interest of limit being a variable here. Should I replace both occurrences directly by "LTTNG_UST_PROCNAME_LEN
- 5" or create a "#define LTTNG_UST_PROCNAME_LIMIT (LTTNG_UST_PROCNAME_LEN - 5)" macro ? Mathieu, what do you think would be the best to do here ?
The condition will also be replaced from ">=" to ">", as the "=" part is not useful anymore in that version of the patch (it was in v1, because of the operations that were done).

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 15 lines
liblttng-ust/compat.h +5 -0
+
+   PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
    int sock, ret, prev_connect_failed = 0, has_waited = 0;
    long timeout;
 
+   /*
+    * If available, add '-ust' to the end of this thread's
+    * process name
+    */
+   LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

And there is no reason to make it a macro. It should be a static inline whenever possible.

I made it a macro as I was not sure about the best path to take, and the macro seemed to be the cleaner approach!
However, would it be better like that:

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>
#endif

static inline
void lttng_ust_setustprocname()
{

#ifdef PTHREAD_SETNAME_NP
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);

#endif
}

Or like that ?

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>

static inline
void lttng_ust_setustprocname()

{
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);
}

#else
static inline
void lttng_ust_setustprocname()
{}
#endif

In both those cases, the macro disappears, and is replaced by a call to lttng_ust_setustprocname().
I'm not sure however which is preferable according to the coding style (In one case, two #ifdef are required, in the other, we declare the function in each branch of the ifdef/else).

Thoughts ? I'll send a v3 of the patch according to your preference in both matters.

Thanks a lot!
Raphaël
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CAE_Gge2Av=iucY8abMCHpU8PjgptdCLhvyoWk7nwfazt6ei3VQ@mail.gmail.com

#29 lore raw
Quoted 158 lines · ----- On Jun 3, 2016, at 6:12 PM, Raphaël Beamonte [email protected]…

2016-06-03 11:55 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 156 lines

----- On Jun 3, 2016, at 5:54 PM, Mathieu Desnoyers

Quoted 153 lines · [email protected] wrote:
Quoted 151 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+   AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+   AC_CHECK_LIB([pthread], [pthread_set_name_np],
+           AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+           AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
    error.h
 liblttng_ust_tracepoint_la_LIBADD = \
    -lurcu-bp \
+   -lpthread \
    $(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
    (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
            strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+   char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+   if (len >= limit) {
+           len = limit;
+   }
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

Quoted 6 lines

You're right, will fix that.
I'm also questionning the interest of limit being a variable here. Should I replace both occurrences directly by "LTTNG_UST_PROCNAME_LEN
- 5" or create a "#define LTTNG_UST_PROCNAME_LIMIT (LTTNG_UST_PROCNAME_LEN - 5)" macro ? Mathieu, what do you think would be the best to do here ?

Hardcoded constants are always a bad idea.

#define MYSTRING "blah"

LTTNG_UST_PROCNAME_LEN - strlen(MYSTRING)

would be ok though.

Quoted 75 lines

The condition will also be replaced from ">=" to ">", as the "=" part is not useful anymore in that version of the patch (it was in v1, because of the operations that were done).

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 15 lines
liblttng-ust/compat.h +5 -0
+
+   PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
    int sock, ret, prev_connect_failed = 0, has_waited = 0;
    long timeout;
 
+   /*
+    * If available, add '-ust' to the end of this thread's
+    * process name
+    */
+   LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

And there is no reason to make it a macro. It should be a static inline whenever possible.

I made it a macro as I was not sure about the best path to take, and the macro seemed to be the cleaner approach!
However, would it be better like that:

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>
#endif

static inline
void lttng_ust_setustprocname()
{

#ifdef PTHREAD_SETNAME_NP
   ...

   PTHREAD_SETNAME_NP(pthread_self(), name);

#endif
}

Or like that ?

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>

static inline
void lttng_ust_setustprocname()

{
   ...

   PTHREAD_SETNAME_NP(pthread_self(), name);
}
Quoted 4 lines

#else
static inline
void lttng_ust_setustprocname()
{}

Change for

static inline
void lttng_ust_setustprocname(void)
{
}

Quoted 2 lines

#endif

Don't forget that (void) in C differs from (). () is like a variadic argument function.

The latter form is prefered by our coding style. No #ifdef within functions unless it's really impossible to do otherwise.

Thanks,

Mathieu

Quoted 12 lines

In both those cases, the macro disappears, and is replaced by a call to lttng_ust_setustprocname().
I'm not sure however which is preferable according to the coding style (In one case, two #ifdef are required, in the other, we declare the function in each branch of the ifdef/else).

Thoughts ? I'll send a v3 of the patch according to your preference in both matters.

Thanks a lot!
Raphaël

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#30 lore raw
In reply to a message not in the archive

Hi Raphaël,

I had another thought.

If the process name is "my-super-process", I think it only makes sense to append the "-ust" suffix if there is sufficient space. "my-super-process-ust" is fine, but I feel that "my-super-pro-ust" is meaningless.

Quoted 251 lines · Raphaël Beamonte

2016-06-03 11:55 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 148 lines
Quoted 146 lines · ----- On Jun 3, 2016, at 5:54 PM, Mathieu Desnoyers…
Quoted 144 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+   AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+   AC_CHECK_LIB([pthread], [pthread_set_name_np],
+           AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+           AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
    error.h
 liblttng_ust_tracepoint_la_LIBADD = \
    -lurcu-bp \
+   -lpthread \
    $(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
    (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
            strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+   char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+   if (len >= limit) {
+           len = limit;
+   }
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

You're right, will fix that.
I'm also questionning the interest of limit being a variable here. Should I replace both occurrences directly by "LTTNG_UST_PROCNAME_LEN
- 5" or create a "#define LTTNG_UST_PROCNAME_LIMIT (LTTNG_UST_PROCNAME_LEN - 5)" macro ? Mathieu, what do you think would be the best to do here ?
The condition will also be replaced from ">=" to ">", as the "=" part is not useful anymore in that version of the patch (it was in v1, because of the operations that were done).

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 15 lines
liblttng-ust/compat.h +5 -0
+
+   PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
    int sock, ret, prev_connect_failed = 0, has_waited = 0;
    long timeout;
 
+   /*
+    * If available, add '-ust' to the end of this thread's
+    * process name
+    */
+   LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

And there is no reason to make it a macro. It should be a static inline whenever possible.

I made it a macro as I was not sure about the best path to take, and the macro seemed to be the cleaner approach!
However, would it be better like that:

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>
#endif

static inline
void lttng_ust_setustprocname()
{

#ifdef PTHREAD_SETNAME_NP
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);

#endif
}

Or like that ?

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>

static inline
void lttng_ust_setustprocname()

{
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);
}

#else
static inline
void lttng_ust_setustprocname()
{}
#endif

In both those cases, the macro disappears, and is replaced by a call to lttng_ust_setustprocname().
I'm not sure however which is preferable according to the coding style (In one case, two #ifdef are required, in the other, we declare the function in each branch of the ifdef/else).

Quoted 6 lines

Thoughts ? I'll send a v3 of the patch according to your preference in both matters.

Thanks a lot!
Raphaël

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#32 lore raw
Quoted 9 lines · ----- On Jun 3, 2016, at 6:24 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I had another thought.

If the process name is "my-super-process", I think it only makes sense to append the
"-ust" suffix if there is sufficient space. "my-super-process-ust" is fine, but I feel
that "my-super-pro-ust" is meaningless.

I see having the -ust overwriting the end of the process name as a feature that would be useful for users to distinguish between ust threads and non-ust threads in cases where the binary name is long.

Thanks,

Mathieu

Quoted 260 lines · Raphaël Beamonte
Quoted 257 lines

2016-06-03 11:55 GMT-04:00 Mathieu Desnoyers <[email protected]>:

Quoted 156 lines

----- On Jun 3, 2016, at 5:54 PM, Mathieu Desnoyers

Quoted 153 lines · [email protected] wrote:
Quoted 151 lines · ----- On Jun 3, 2016, at 5:47 PM, Sebastien Boisvert [email protected] wrote:

Hi Raphaël,

I think you have a buffer overflow, see below.

Quoted 96 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 48 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 ++++++
 4 files changed, 59 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+   AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+   AC_CHECK_LIB([pthread], [pthread_set_name_np],
+           AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+           AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
    error.h
 liblttng_ust_tracepoint_la_LIBADD = \
    -lurcu-bp \
+   -lpthread \
    $(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info
 $(LTTNG_UST_LIBRARY_VERSION)

liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing

liblttng-ust/compat.h +21 -1
index 43b2223..4fee919 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
    (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,45 @@ void lttng_ust_getprocname(char *name)
            strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 91

Quoted 10 lines
liblttng-ust/compat.h +10 -0
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_SETUSTPROCNAME lttng_ust_setustprocname()
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname()
+{
+   char name[LTTNG_UST_PROCNAME_LEN];

Let's say that LTTNG_UST_PROCNAME_LEN is 10.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 92

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int limit = LTTNG_UST_PROCNAME_LEN - 4;

Then, limit is 10 - 4 = 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 93

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   int len = 0;

This can be moved on the strlen(name) line.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 95

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   lttng_ust_getprocname(name);

Let's suppose that after the call to lttng_ust_getprocname(), name contains "012345678\0",

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 97

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+   len = strlen(name);

The variable "len" will contain the integer 9.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 101

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+   if (len >= limit) {
+           len = limit;
+   }
+

After that, len is >= limit (9 >= 6), so len becomes 6.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 1 line
liblttng-ust/compat.h +1 -0
+   sprintf(name + len, "%s", "-ust");

name is a pointer to "012345678\0" (9 bytes + '\0')

name + len is name + 6, so it points to "678\0"

sprintf will essentially copy "-ust\0" to "678\0".

So, if my calculations are not wrong, you have a buffer overflow which will corrupt the stack memory of the
current stack frame.

You're right, will fix that.
I'm also questionning the interest of limit being a variable here. Should I replace both occurrences directly by "LTTNG_UST_PROCNAME_LEN
- 5" or create a "#define LTTNG_UST_PROCNAME_LIMIT (LTTNG_UST_PROCNAME_LEN - 5)" macro ? Mathieu, what do you think would be the best to do here ?
The condition will also be replaced from ">=" to ">", as the "=" part is not useful anymore in that version of the patch (it was in v1, because of the operations that were done).

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 15 lines
liblttng-ust/compat.h +5 -0
+
+   PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+#define LTTNG_UST_SETUSTPROCNAME
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..4de68c7 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
    int sock, ret, prev_connect_failed = 0, has_waited = 0;
    long timeout;
 
+   /*
+    * If available, add '-ust' to the end of this thread's
+    * process name
+    */
+   LTTNG_UST_SETUSTPROCNAME;

Is this common practice ? Just by looking at this line, it is unclear that it is a function call.

I would prefer

LTTNG_UST_SETUSTPROCNAME()

or

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

lttng_ust_setustprocname()

The latter is preferred by our coding style, indeed.

And there is no reason to make it a macro. It should be a static inline whenever possible.

I made it a macro as I was not sure about the best path to take, and the macro seemed to be the cleaner approach!
However, would it be better like that:

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>
#endif

static inline
void lttng_ust_setustprocname()
{

#ifdef PTHREAD_SETNAME_NP
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);

#endif
}

Or like that ?

#ifdef PTHREAD_SETNAME_NP
#include <pthread.h>

static inline
void lttng_ust_setustprocname()

{
    ...

    PTHREAD_SETNAME_NP(pthread_self(), name);
}

#else
static inline
void lttng_ust_setustprocname()
{}
#endif

In both those cases, the macro disappears, and is replaced by a call to lttng_ust_setustprocname().
I'm not sure however which is preferable according to the coding style (In one case, two #ifdef are required, in the other, we declare the function in each branch of the ifdef/else).

Thoughts ? I'll send a v3 of the patch according to your preference in both matters.

Thanks a lot!
Raphaël

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#34 lore raw

PATCH v3 1/1 Add -ust to the name of UST threads of the application

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 51 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 +++++
 4 files changed, 62 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))
 
 # Check for dlfcn.h
 AC_CHECK_HEADER([dlfcn.h])
liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
 liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing
liblttng-ust/compat.h +50 -1

75 lines not on this page · raw message

liblttng-ust/lttng-ust-comm.c +6 -0
index e00a22c..c32e8ae 100644
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	lttng_ust_setustprocname();
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {
Signature · 6 lines
2.8.1

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): 1d3230f091658e56c8ccf12279c3afd89b4062c7.1464971957.git.raphael.beamonte@gmail.com

#36 lore raw

+1

Quoted 131 lines · Raphaël Beamonte

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---
 configure.ac                  |  5 +++++
 liblttng-ust/Makefile.am      |  1 +
 liblttng-ust/compat.h         | 51 ++++++++++++++++++++++++++++++++++++++++++-
 liblttng-ust/lttng-ust-comm.c |  6 +++++
 4 files changed, 62 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))
 
 # Check for dlfcn.h
 AC_CHECK_HEADER([dlfcn.h])
liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la
 liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
 liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint" -fno-strict-aliasing
liblttng-ust/compat.h +50 -1
index 43b2223..68d4d50 100644
@@ -3,6 +3,7 @@
 
 /*
  * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
  * lttng_ust_getprocname.
  */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +66,48 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.
+ */
+#ifdef PTHREAD_SETNAME_NP
+#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname(void)
+{
+	char name[LTTNG_UST_PROCNAME_LEN];
+	int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX);
+	int len;
+
+	lttng_ust_getprocname(name);
+
+	len = strlen(name);
+	if (len > limit) {
+		len = limit;
+	}
+
+	sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
+
+	PTHREAD_SETNAME_NP(pthread_self(), name);
+}
+#else
+static inline
+void lttng_ust_setustprocname(void)
+{
+}
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..c32e8ae 100644
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1306

Quoted 10 lines
liblttng-ust/lttng-ust-comm.c +6 -0
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	lttng_ust_setustprocname();
+
 	/* Restart trying to connect to the session daemon */
 restart:
 	if (prev_connect_failed) {

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

#37 lore raw
In reply to a message not in the archive
Quoted 66 lines · ----- On Jun 3, 2016, at 12:45 PM, Raphaël Beamonte [email protected]…

Add the required functions to change the thread name of the UST threads and add the -ust string at its end. This will help to identify LTTng-UST processes when analyzing the trace of a process.

---

configure.ac                  |  5 +++++
liblttng-ust/Makefile.am      |  1 +
liblttng-ust/compat.h         | 51 ++++++++++++++++++++++++++++++++++++++++++-
liblttng-ust/lttng-ust-comm.c |  6 +++++
4 files changed, 62 insertions(+), 1 deletion(-)
configure.ac +5 -0
index 5692553..de462ff 100644
@@ -135,6 +135,11 @@ AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBDL], [test
 "x$have_libdl" = "xyes"])
 AM_CONDITIONAL([LTTNG_UST_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"])
 
 AC_CHECK_LIB([pthread], [pthread_create])
+AC_CHECK_LIB([pthread], [pthread_setname_np],
+	AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], [Define to 1 if pthread_setname_np
 is available.]),
+	AC_CHECK_LIB([pthread], [pthread_set_name_np],
+		AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], [Define to 1 if
 pthread_set_name_np is available.]),
+		AC_MSG_RESULT([pthread setname/set_name not found.])))

# Check for dlfcn.h
AC_CHECK_HEADER([dlfcn.h])

liblttng-ust/Makefile.am +1 -0
index f1801cf..05929be 100644
@@ -14,6 +14,7 @@ liblttng_ust_tracepoint_la_SOURCES = \
 	error.h
 liblttng_ust_tracepoint_la_LIBADD = \
 	-lurcu-bp \
+	-lpthread \
 	$(top_builddir)/snprintf/libustsnprintf.la

liblttng_ust_tracepoint_la_LDFLAGS = -no-undefined -version-info $(LTTNG_UST_LIBRARY_VERSION)
liblttng_ust_tracepoint_la_CFLAGS = -DUST_COMPONENT="liblttng_ust_tracepoint"

liblttng-ust/Makefile.am +0 -1
-fno-strict-aliasing
liblttng-ust/compat.h +1 -1
index 43b2223..68d4d50 100644
@@ -3,6 +3,7 @@
 
 /*
 * Copyright (C) 2011 Mathieu Desnoyers <[email protected]>
+ * Copyright (C) 2016 Raphaël Beamonte <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
@@ -23,7 +24,6 @@
 * lttng_ust_getprocname.
 */
 #ifdef __linux__
-
 #include <sys/prctl.h>
 
 #define LTTNG_UST_PROCNAME_LEN 17

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 41

Quoted 8 lines
liblttng-ust/compat.h +5 -0
@@ -34,6 +34,13 @@ void lttng_ust_getprocname(char *name)
 	(void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
 }
 
+/*
+ * if pthread_setname_np is available.
+ */
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define PTHREAD_SETNAME_NP pthread_setname_np

replace by:

static inline int lttng_pthread_setname_np(pthread_t thread, const char *name)

{
    return pthread_setname_np(thread, name);
}
Quoted 6 lines
liblttng-ust/compat.h +2 -0
+#endif
+
 #elif defined(__FreeBSD__)
 #include <stdlib.h>
 #include <string.h>

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 73

Quoted 8 lines
liblttng-ust/compat.h +5 -0
@@ -59,6 +66,48 @@ void lttng_ust_getprocname(char *name)
 		strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
 }
 
+/*
+ * if pthread_set_name_np is available.
+ */
+#ifdef HAVE_PTHREAD_SET_NAME_NP
+#define PTHREAD_SETNAME_NP pthread_set_name_np

replace by:

static inline int lttng_pthread_setname_np(pthread_t thread, const char *name)

{
    return pthread_set_name_np(thread, name);
}

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 79

Quoted 6 lines
liblttng-ust/compat.h +6 -0
+#endif
+
+#endif
+
+/*
+ * if a pthread setname/set_name function is available, declare

if -> If

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 83

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+ * the setustprocname() function that will add '-ust' to the end
+ * of the current process name, while truncating it if needed.
+ */
+#ifdef PTHREAD_SETNAME_NP

#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP)

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 89

Quoted 6 lines
liblttng-ust/compat.h +6 -0
+#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
+
+#include <pthread.h>
+
+static inline
+void lttng_ust_setustprocname(void)

should probably return int (error).

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 102

Quoted 13 lines
liblttng-ust/compat.h +13 -0
+{
+	char name[LTTNG_UST_PROCNAME_LEN];
+	int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX);
+	int len;
+
+	lttng_ust_getprocname(name);
+
+	len = strlen(name);
+	if (len > limit) {
+		len = limit;
+	}
+
+	sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);

missing sprintf error handling.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 104

Quoted 2 lines
liblttng-ust/compat.h +2 -0
+
+	PTHREAD_SETNAME_NP(pthread_self(), name);

replace by lttng_pthread_setname_np(), and add error handling.

liblttng-ust/compat.h void lttng_ust_getprocname(char *name) · line 108

Quoted 4 lines
liblttng-ust/compat.h +4 -0
+}
+#else
+static inline
+void lttng_ust_setustprocname(void)

should return int.

Quoted 10 lines
liblttng-ust/compat.h +2 -0
+{
+}
 #endif
 
 #include <errno.h>
liblttng-ust/lttng-ust-comm.c +0 -0
index e00a22c..c32e8ae 100644

liblttng-ust/lttng-ust-comm.c void *ust_listener_thread(void *arg) · line 1302

Quoted 8 lines
liblttng-ust/lttng-ust-comm.c +5 -0
@@ -1295,6 +1295,12 @@ void *ust_listener_thread(void *arg)
 	int sock, ret, prev_connect_failed = 0, has_waited = 0;
 	long timeout;
 
+	/*
+	 * If available, add '-ust' to the end of this thread's
+	 * process name
+	 */
+	lttng_ust_setustprocname();

Add error handling.

Thanks,

Mathieu

Quoted 11 lines
liblttng-ust/lttng-ust-comm.c +1 -0
+
 	/* Restart trying to connect to the session daemon */

restart:

	if (prev_connect_failed) {

--
2.8.1

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 7 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev