Bug 1056962 - Part 1: Move GetThreadName to LinuxUtils. r=jld

This commit is contained in:
Eric Rahm 2014-08-22 16:35:29 -07:00
Родитель a7c14b548e
Коммит 87668897f6
4 изменённых файлов: 89 добавлений и 33 удалений

50
xpcom/base/LinuxUtils.cpp Normal file
Просмотреть файл

@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LinuxUtils.h"
#if defined(XP_LINUX)
#include <ctype.h>
#include <stdio.h>
#include "nsPrintfCString.h"
namespace mozilla {
void
LinuxUtils::GetThreadName(pid_t aTid, nsACString& aName)
{
aName.Truncate();
if (aTid <= 0) {
return;
}
const size_t kBuffSize = 16; // 15 chars max + '\n'
char buf[kBuffSize];
nsPrintfCString path("/proc/%d/comm", aTid);
FILE* fp = fopen(path.get(), "r");
if (!fp) {
// The fopen could also fail if the thread exited before we got here.
return;
}
size_t len = fread(buf, 1, kBuffSize, fp);
fclose(fp);
// No need to strip the '\n', since isspace() includes it.
while (len > 0 &&
(isspace(buf[len - 1]) || isdigit(buf[len - 1]) ||
buf[len - 1] == '#' || buf[len - 1] == '_')) {
--len;
}
aName.Assign(buf, len);
}
}
#endif // XP_LINUX

34
xpcom/base/LinuxUtils.h Normal file
Просмотреть файл

@ -0,0 +1,34 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_LinuxUtils_h
#define mozilla_LinuxUtils_h
#if defined(XP_LINUX)
#include <unistd.h>
#include "nsString.h"
namespace mozilla {
class LinuxUtils
{
public:
// Obtain the name of a thread, omitting any numeric suffix added by a
// thread pool library (as in, e.g., "Binder_2" or "mozStorage #1").
// The empty string is returned on error.
//
// Note: if this is ever needed on kernels older than 2.6.33 (early 2010),
// it will have to parse /proc/<pid>/status instead, because
// /proc/<pid>/comm didn't exist before then.
static void GetThreadName(pid_t aTid, nsACString& aName);
};
}
#endif // XP_LINUX
#endif

Просмотреть файл

@ -7,6 +7,7 @@
#include "mozilla/SystemMemoryReporter.h"
#include "mozilla/Attributes.h"
#include "mozilla/LinuxUtils.h"
#include "mozilla/PodOperations.h"
#include "mozilla/Preferences.h"
#include "mozilla/TaggedAnonymousMemory.h"
@ -349,37 +350,6 @@ private:
return NS_OK;
}
// Obtain the name of a thread, omitting any numeric suffix added by a
// thread pool library (as in, e.g., "Binder_2" or "mozStorage #1").
// The empty string is returned on error.
//
// Note: if this is ever needed on kernels older than 2.6.33 (early 2010),
// it will have to parse /proc/<pid>/status instead, because
// /proc/<pid>/comm didn't exist before then.
void GetThreadName(pid_t aTid, nsACString& aName)
{
aName.Truncate();
if (aTid <= 0) {
return;
}
char buf[16]; // 15 chars max + '\n'
nsPrintfCString path("/proc/%d/comm", aTid);
FILE* fp = fopen(path.get(), "r");
if (!fp) {
// The fopen could also fail if the thread exited before we got here.
return;
}
size_t len = fread(buf, 1, sizeof(buf), fp);
fclose(fp);
// No need to strip the '\n', since isspace() includes it.
while (len > 0 &&
(isspace(buf[len - 1]) || isdigit(buf[len - 1]) ||
buf[len - 1] == '#' || buf[len - 1] == '_')) {
--len;
}
aName.Assign(buf, len);
}
nsresult ParseMappings(FILE* aFile,
const nsACString& aProcessName,
nsIHandleReportCallback* aHandleReport,
@ -504,7 +474,7 @@ private:
// "[stack:" entries from reaching the IsAnonymous case below.)
pid_t tid = atoi(absPath.get() + 7);
nsAutoCString threadName, escapedThreadName;
GetThreadName(tid, threadName);
LinuxUtils::GetThreadName(tid, threadName);
if (threadName.IsEmpty()) {
threadName.AssignLiteral("<unknown>");
}
@ -1037,7 +1007,7 @@ private:
// Attempt to map the pid to a more useful name.
nsAutoCString procName;
GetThreadName(atoi(pid), procName);
LinuxUtils::GetThreadName(atoi(pid), procName);
if (procName.IsEmpty()) {
procName.Append("pid=");

Просмотреть файл

@ -74,6 +74,7 @@ EXPORTS.mozilla += [
'ClearOnShutdown.h',
'CycleCollectedJSRuntime.h',
'Debug.h',
'LinuxUtils.h',
'nsMemoryInfoDumper.h',
'StackWalk.h',
'StaticMutex.h',
@ -128,6 +129,7 @@ if CONFIG['OS_TARGET'] != 'WINNT' or \
if CONFIG['OS_ARCH'] == 'Linux':
SOURCES += [
'LinuxUtils.cpp',
'SystemMemoryReporter.cpp',
]