LinuxUtils.cpp 1.1 KB

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