profile.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <serenity.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. int main(int argc, char** argv)
  32. {
  33. Core::ArgsParser args_parser;
  34. const char* pid_argument = nullptr;
  35. const char* cmd_argument = nullptr;
  36. bool enable = false;
  37. bool disable = false;
  38. args_parser.add_option(pid_argument, "Target PID", nullptr, 'p', "PID");
  39. args_parser.add_option(enable, "Enable", nullptr, 'e');
  40. args_parser.add_option(disable, "Disable", nullptr, 'd');
  41. args_parser.add_option(cmd_argument, "Command", nullptr, 'c', "command");
  42. args_parser.parse(argc, argv);
  43. if (!pid_argument && !cmd_argument) {
  44. args_parser.print_usage(stdout, argv[0]);
  45. return 0;
  46. }
  47. if (pid_argument) {
  48. if (!(enable ^ disable)) {
  49. fprintf(stderr, "-p <PID> requires -e xor -d.\n");
  50. return 1;
  51. }
  52. pid_t pid = atoi(pid_argument);
  53. if (enable) {
  54. if (profiling_enable(pid) < 0) {
  55. perror("profiling_enable");
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. if (profiling_disable(pid) < 0) {
  61. perror("profiling_disable");
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. auto cmd_parts = String(cmd_argument).split(' ');
  67. Vector<const char*> cmd_argv;
  68. for (auto& part : cmd_parts)
  69. cmd_argv.append(part.characters());
  70. cmd_argv.append(nullptr);
  71. dbg() << "Enabling profiling for PID " << getpid();
  72. profiling_enable(getpid());
  73. if (execvp(cmd_argv[0], const_cast<char**>(cmd_argv.data())) < 0) {
  74. perror("execv");
  75. return 1;
  76. }
  77. return 0;
  78. }