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