ramosian-glider / sanitizers

0 stars 0 forks source link

ASan should print thread names. #80

Open ramosian-glider opened 9 years ago

ramosian-glider commented 9 years ago

Originally reported on Google Code with ID 79

Calling prctl(PR_SET_NAME) to name the threads is a nice feature that may be used by
many clients (Chromium does it, so does PulseAudio: http://0pointer.de/blog/projects/name-your-threads.html)
It will be nice to print the thread names in ASan reports.

Reported by ramosian.glider on 2012-06-14 16:03:44

ramosian-glider commented 9 years ago
I'd like to try to implement this when I get time.  Any hints, e.g., where to start
looking.

Reported by mohamed.omar.bana on 2012-07-06 10:02:17

ramosian-glider commented 9 years ago
First, you need a small program that spawns a thread, sets its name (GDB can help to
check whether it actually happens) and crashes because of an ASan error. We'll need
to put it into the test suite later.
Next, you'll need to wrap prctl() on Linux and pthread_setname_np() on Mac and store
the name somewhere. Mac-specific wrappers reside in asan_mac.cc, you'll need to introduce
something similar for Linux as well (maybe rename them to InitializePlatformInterceptors?)
The thread name should be stored in AsanThreadSummary, as we may want to print them
after the thread has been deleted.
After all, you'll need to replace all the code printing "T%d" with calls to an AsanThreadSummary
method that returns the thread number together with thread name (if it's available).
Keep in mind this may affect the output tests.

For the record, name-your-threads.html is down, but is available from Google cache:
http://webcache.googleusercontent.com/search?sugexp=chrome,mod=10&sourceid=chrome&ie=UTF-8&q=cache%3A0pointer.de%2Fblog%2Fprojects%2Fname-your-threads.html

Reported by ramosian.glider on 2012-07-06 10:41:28

ramosian-glider commented 9 years ago
http://llvm.org/viewvc/llvm-project?rev=169598&view=rev implements part of what's needed.
Alex, Tiumur, what's the analog to prctl(PR_SET_NAME) on Mac/Windows. 

Also, how do we couple this with tsan's AnnotateThreadName? 
Should we route AnnotateThreadName to prctl? Unfortunately, names in prctl are limited
with 15 bytes, so that may degrade our functionality... 

Perhaps like this? 
  SanitizerSetThreadName calls prctl and also remembers the full name in a thread-local
variable. (not all platforms have TLS)

  SanitizerGetThreadName checks if we have our own thread-local variable set. 
  If yes, it returns it, otherwise it returns what's in prctl

Thoughts? 

Reported by konstantin.s.serebryany on 2012-12-07 11:35:26

ramosian-glider commented 9 years ago

Reported by konstantin.s.serebryany on 2012-12-07 11:41:59

ramosian-glider commented 9 years ago
I can't help with the other parts, but prctl(PR_SET_NAME,X,0,0,0) is also the right
way to set thread names on Mac.

Reported by lee@leecbaker.com on 2012-12-07 13:53:17

ramosian-glider commented 9 years ago
http://llvm.org/viewvc/llvm-project?rev=169601&view=rev  implements 
reporting thread names in the actual reports: 

% AsanTest --gtest_filter=*DISABLED_DemoThreadedTest --gtest_also_run_disabled_tests
 2>&1 | grep hread 
...
WRITE of size 4 at 0x7fbb5ab95040 thread T3 (UseThr)
freed by thread T2 (FreeThr) here:
previously allocated by thread T1 (AllocThr) here:
Thread T3 (UseThr) created by T0 here:
Thread T2 (FreeThr) created by T0 here:
Thread T1 (AllocThr) created by T0 here:

Reported by konstantin.s.serebryany on 2012-12-07 15:18:50

ramosian-glider commented 9 years ago
> what's the analog to prctl(PR_SET_NAME) on Windows
http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

You could have looked it up in the Chromium sources, base/threading/platform_thread_win.cc

Reported by timurrrr@google.com on 2012-12-08 09:26:23

ramosian-glider commented 9 years ago
FTR:
automagically, the current asan implementation on linux works for pthread_setname_np,
because this function calls prctl(PR_SET_NAME)

Reported by konstantin.s.serebryany on 2013-01-17 11:50:33

ramosian-glider commented 9 years ago
FTR, this is only true if pthread_setname_np() is being called for the current thread.
Not sure if the other case is used wide enough.

Reported by ramosian.glider on 2013-01-17 12:02:25

ramosian-glider commented 9 years ago

Reported by konstantin.s.serebryany on 2013-02-15 14:31:32

ramosian-glider commented 9 years ago

Reported by ramosian.glider on 2015-07-30 09:05:30

ramosian-glider commented 9 years ago
Adding Project:AddressSanitizer as part of GitHub migration.

Reported by ramosian.glider on 2015-07-30 09:06:54