Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "LinuxUtils.h"
8 :
9 : #if defined(XP_LINUX)
10 :
11 : #include <ctype.h>
12 : #include <stdio.h>
13 :
14 : #include "nsPrintfCString.h"
15 :
16 : namespace mozilla {
17 :
18 : void
19 0 : LinuxUtils::GetThreadName(pid_t aTid, nsACString& aName)
20 : {
21 0 : aName.Truncate();
22 0 : if (aTid <= 0) {
23 0 : return;
24 : }
25 :
26 0 : const size_t kBuffSize = 16; // 15 chars max + '\n'
27 : char buf[kBuffSize];
28 0 : nsPrintfCString path("/proc/%d/comm", aTid);
29 0 : FILE* fp = fopen(path.get(), "r");
30 0 : if (!fp) {
31 : // The fopen could also fail if the thread exited before we got here.
32 0 : return;
33 : }
34 :
35 0 : size_t len = fread(buf, 1, kBuffSize, fp);
36 0 : fclose(fp);
37 :
38 : // No need to strip the '\n', since isspace() includes it.
39 0 : while (len > 0 &&
40 0 : (isspace(buf[len - 1]) || isdigit(buf[len - 1]) ||
41 0 : buf[len - 1] == '#' || buf[len - 1] == '_')) {
42 0 : --len;
43 : }
44 :
45 0 : aName.Assign(buf, len);
46 : }
47 :
48 : }
49 :
50 : #endif // XP_LINUX
|