2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <fcntl.h>
|
2012-03-19 22:06:50 +01:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2014-12-15 21:52:53 +01:00
|
|
|
#if defined(__linux__)
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <sys/prctl.h>
|
2013-05-23 18:54:47 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <cutils/process_name.h>
|
|
|
|
#ifdef HAVE_ANDROID_OS
|
|
|
|
#include <cutils/properties.h>
|
2009-10-28 05:27:33 +01:00
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define PROCESS_NAME_DEVICE "/sys/qemu_trace/process_name"
|
|
|
|
|
|
|
|
static const char* process_name = "unknown";
|
2014-04-30 00:49:14 +02:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
2009-03-04 04:32:55 +01:00
|
|
|
static int running_in_emulator = -1;
|
2014-04-30 00:49:14 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
void set_process_name(const char* new_name) {
|
2013-05-23 18:54:47 +02:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
2009-03-04 04:32:55 +01:00
|
|
|
char propBuf[PROPERTY_VALUE_MAX];
|
2013-05-23 18:54:47 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (new_name == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We never free the old name. Someone else could be using it.
|
2009-10-28 05:27:33 +01:00
|
|
|
int len = strlen(new_name);
|
|
|
|
char* copy = (char*) malloc(len + 1);
|
2009-03-04 04:32:55 +01:00
|
|
|
strcpy(copy, new_name);
|
|
|
|
process_name = (const char*) copy;
|
|
|
|
|
2014-12-15 21:52:53 +01:00
|
|
|
#if defined(__linux__)
|
2009-10-28 05:27:33 +01:00
|
|
|
if (len < 16) {
|
|
|
|
prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
prctl(PR_SET_NAME, (unsigned long) new_name + len - 15, 0, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-23 18:54:47 +02:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
2009-03-04 04:32:55 +01:00
|
|
|
// If we know we are not running in the emulator, then return.
|
|
|
|
if (running_in_emulator == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the "running_in_emulator" variable has not been initialized,
|
|
|
|
// then do it now.
|
|
|
|
if (running_in_emulator == -1) {
|
|
|
|
property_get("ro.kernel.qemu", propBuf, "");
|
|
|
|
if (propBuf[0] == '1') {
|
|
|
|
running_in_emulator = 1;
|
|
|
|
} else {
|
|
|
|
running_in_emulator = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the emulator was started with the "-trace file" command line option
|
|
|
|
// then we want to record the process name in the trace even if we are
|
|
|
|
// not currently tracing instructions (so that we will know the process
|
|
|
|
// name when we do start tracing instructions). We do not need to execute
|
|
|
|
// this code if we are just running in the emulator without the "-trace"
|
|
|
|
// command line option, but we don't know that here and this function
|
|
|
|
// isn't called frequently enough to bother optimizing that case.
|
|
|
|
int fd = open(PROCESS_NAME_DEVICE, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
write(fd, process_name, strlen(process_name) + 1);
|
|
|
|
close(fd);
|
2013-05-23 18:54:47 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* get_process_name(void) {
|
|
|
|
return process_name;
|
|
|
|
}
|