| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 17 | #include <fcntl.h> | 
| Jeff Brown | 14d0c6c | 2012-03-19 14:06:50 -0700 | [diff] [blame] | 18 | #include <stdlib.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <string.h> | 
| Elliott Hughes | 292ccd3 | 2014-12-15 12:52:53 -0800 | [diff] [blame] | 20 | #if defined(__linux__) | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 21 | #include <sys/prctl.h> | 
|  | 22 | #endif | 
|  | 23 | #include <sys/stat.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 | #include <unistd.h> | 
|  | 26 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <cutils/process_name.h> | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 28 | #if defined(__ANDROID__) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> | 
| Nick Kralevich | b39e3a8 | 2013-05-23 09:54:47 -0700 | [diff] [blame] | 30 | #endif | 
| Erik Gilling | f6eba8f | 2009-10-27 21:27:33 -0700 | [diff] [blame] | 31 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #define PROCESS_NAME_DEVICE "/sys/qemu_trace/process_name" | 
|  | 33 |  | 
|  | 34 | static const char* process_name = "unknown"; | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 35 | #if defined(__ANDROID__) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | static int running_in_emulator = -1; | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 37 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | void set_process_name(const char* new_name) { | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 40 | #if defined(__ANDROID__) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | char  propBuf[PROPERTY_VALUE_MAX]; | 
| Nick Kralevich | b39e3a8 | 2013-05-23 09:54:47 -0700 | [diff] [blame] | 42 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | if (new_name == NULL) { | 
|  | 45 | return; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | // We never free the old name. Someone else could be using it. | 
| Erik Gilling | f6eba8f | 2009-10-27 21:27:33 -0700 | [diff] [blame] | 49 | int len = strlen(new_name); | 
|  | 50 | char* copy = (char*) malloc(len + 1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | strcpy(copy, new_name); | 
|  | 52 | process_name = (const char*) copy; | 
|  | 53 |  | 
| Elliott Hughes | 292ccd3 | 2014-12-15 12:52:53 -0800 | [diff] [blame] | 54 | #if defined(__linux__) | 
| Erik Gilling | f6eba8f | 2009-10-27 21:27:33 -0700 | [diff] [blame] | 55 | if (len < 16) { | 
|  | 56 | prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0); | 
|  | 57 | } else { | 
|  | 58 | prctl(PR_SET_NAME, (unsigned long) new_name + len - 15, 0, 0, 0); | 
|  | 59 | } | 
|  | 60 | #endif | 
|  | 61 |  | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 62 | #if defined(__ANDROID__) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | // If we know we are not running in the emulator, then return. | 
|  | 64 | if (running_in_emulator == 0) { | 
|  | 65 | return; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // If the "running_in_emulator" variable has not been initialized, | 
|  | 69 | // then do it now. | 
|  | 70 | if (running_in_emulator == -1) { | 
|  | 71 | property_get("ro.kernel.qemu", propBuf, ""); | 
|  | 72 | if (propBuf[0] == '1') { | 
|  | 73 | running_in_emulator = 1; | 
|  | 74 | } else { | 
|  | 75 | running_in_emulator = 0; | 
|  | 76 | return; | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | // If the emulator was started with the "-trace file" command line option | 
|  | 81 | // then we want to record the process name in the trace even if we are | 
|  | 82 | // not currently tracing instructions (so that we will know the process | 
|  | 83 | // name when we do start tracing instructions).  We do not need to execute | 
|  | 84 | // this code if we are just running in the emulator without the "-trace" | 
|  | 85 | // command line option, but we don't know that here and this function | 
|  | 86 | // isn't called frequently enough to bother optimizing that case. | 
|  | 87 | int fd = open(PROCESS_NAME_DEVICE, O_RDWR); | 
|  | 88 | if (fd < 0) | 
|  | 89 | return; | 
|  | 90 | write(fd, process_name, strlen(process_name) + 1); | 
|  | 91 | close(fd); | 
| Nick Kralevich | b39e3a8 | 2013-05-23 09:54:47 -0700 | [diff] [blame] | 92 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | const char* get_process_name(void) { | 
|  | 96 | return process_name; | 
|  | 97 | } |