process_name.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <stdlib.h>
  17. #include <string.h>
  18. #include <cutils/process_name.h>
  19. #include <cutils/properties.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #if defined(HAVE_PRCTL)
  25. #include <sys/prctl.h>
  26. #endif
  27. #define PROCESS_NAME_DEVICE "/sys/qemu_trace/process_name"
  28. static const char* process_name = "unknown";
  29. static int running_in_emulator = -1;
  30. void set_process_name(const char* new_name) {
  31. char propBuf[PROPERTY_VALUE_MAX];
  32. if (new_name == NULL) {
  33. return;
  34. }
  35. // We never free the old name. Someone else could be using it.
  36. int len = strlen(new_name);
  37. char* copy = (char*) malloc(len + 1);
  38. strcpy(copy, new_name);
  39. process_name = (const char*) copy;
  40. #if defined(HAVE_PRCTL)
  41. if (len < 16) {
  42. prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0);
  43. } else {
  44. prctl(PR_SET_NAME, (unsigned long) new_name + len - 15, 0, 0, 0);
  45. }
  46. #endif
  47. // If we know we are not running in the emulator, then return.
  48. if (running_in_emulator == 0) {
  49. return;
  50. }
  51. // If the "running_in_emulator" variable has not been initialized,
  52. // then do it now.
  53. if (running_in_emulator == -1) {
  54. property_get("ro.kernel.qemu", propBuf, "");
  55. if (propBuf[0] == '1') {
  56. running_in_emulator = 1;
  57. } else {
  58. running_in_emulator = 0;
  59. return;
  60. }
  61. }
  62. // If the emulator was started with the "-trace file" command line option
  63. // then we want to record the process name in the trace even if we are
  64. // not currently tracing instructions (so that we will know the process
  65. // name when we do start tracing instructions). We do not need to execute
  66. // this code if we are just running in the emulator without the "-trace"
  67. // command line option, but we don't know that here and this function
  68. // isn't called frequently enough to bother optimizing that case.
  69. int fd = open(PROCESS_NAME_DEVICE, O_RDWR);
  70. if (fd < 0)
  71. return;
  72. write(fd, process_name, strlen(process_name) + 1);
  73. close(fd);
  74. }
  75. const char* get_process_name(void) {
  76. return process_name;
  77. }