blob: a6ab951b714f57612c2e9d8505e3f53080889847 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown14d0c6c2012-03-19 14:06:50 -070017#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <string.h>
19#include <cutils/process_name.h>
Nick Kralevichb39e3a82013-05-23 09:54:47 -070020#ifdef HAVE_ANDROID_OS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <cutils/properties.h>
Nick Kralevichb39e3a82013-05-23 09:54:47 -070022#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <unistd.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27
Erik Gillingf6eba8f2009-10-27 21:27:33 -070028#if defined(HAVE_PRCTL)
29#include <sys/prctl.h>
30#endif
31
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#define PROCESS_NAME_DEVICE "/sys/qemu_trace/process_name"
33
34static const char* process_name = "unknown";
35static int running_in_emulator = -1;
36
37void set_process_name(const char* new_name) {
Nick Kralevichb39e3a82013-05-23 09:54:47 -070038#ifdef HAVE_ANDROID_OS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039 char propBuf[PROPERTY_VALUE_MAX];
Nick Kralevichb39e3a82013-05-23 09:54:47 -070040#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42 if (new_name == NULL) {
43 return;
44 }
45
46 // We never free the old name. Someone else could be using it.
Erik Gillingf6eba8f2009-10-27 21:27:33 -070047 int len = strlen(new_name);
48 char* copy = (char*) malloc(len + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 strcpy(copy, new_name);
50 process_name = (const char*) copy;
51
Erik Gillingf6eba8f2009-10-27 21:27:33 -070052#if defined(HAVE_PRCTL)
53 if (len < 16) {
54 prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0);
55 } else {
56 prctl(PR_SET_NAME, (unsigned long) new_name + len - 15, 0, 0, 0);
57 }
58#endif
59
Nick Kralevichb39e3a82013-05-23 09:54:47 -070060#ifdef HAVE_ANDROID_OS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 // If we know we are not running in the emulator, then return.
62 if (running_in_emulator == 0) {
63 return;
64 }
65
66 // If the "running_in_emulator" variable has not been initialized,
67 // then do it now.
68 if (running_in_emulator == -1) {
69 property_get("ro.kernel.qemu", propBuf, "");
70 if (propBuf[0] == '1') {
71 running_in_emulator = 1;
72 } else {
73 running_in_emulator = 0;
74 return;
75 }
76 }
77
78 // If the emulator was started with the "-trace file" command line option
79 // then we want to record the process name in the trace even if we are
80 // not currently tracing instructions (so that we will know the process
81 // name when we do start tracing instructions). We do not need to execute
82 // this code if we are just running in the emulator without the "-trace"
83 // command line option, but we don't know that here and this function
84 // isn't called frequently enough to bother optimizing that case.
85 int fd = open(PROCESS_NAME_DEVICE, O_RDWR);
86 if (fd < 0)
87 return;
88 write(fd, process_name, strlen(process_name) + 1);
89 close(fd);
Nick Kralevichb39e3a82013-05-23 09:54:47 -070090#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091}
92
93const char* get_process_name(void) {
94 return process_name;
95}