* Initialize the counters and the trampoline. */
| 60 | * Initialize the counters and the trampoline. |
| 61 | */ |
| 62 | extern void *e9_plugin_init(const Context *cxt) |
| 63 | { |
| 64 | // Check the version: |
| 65 | if (API_VERSION != cxt->api) |
| 66 | error("bad API version; expected %u, found %u", API_VERSION, |
| 67 | cxt->api); |
| 68 | |
| 69 | // The e9_plugin_init() is called once per plugin by E9Tool. This can |
| 70 | // be used to emit additional E9Patch messages, such as address space |
| 71 | // reservations and trampoline templates. |
| 72 | |
| 73 | static const struct option long_options[] = |
| 74 | { |
| 75 | {"address", required_argument, nullptr, OPTION_ADDRESS}, |
| 76 | {"limit", required_argument, nullptr, OPTION_LIMIT}, |
| 77 | {nullptr, no_argument , nullptr, 0} |
| 78 | }; |
| 79 | char * const *argv = cxt->argv->data(); |
| 80 | int argc = (int)cxt->argv->size(); |
| 81 | ssize_t limit = UINT16_MAX; |
| 82 | optind = 1; |
| 83 | while (true) |
| 84 | { |
| 85 | int idx; |
| 86 | int opt = getopt_long_only(argc, argv, "a:l:", long_options, &idx); |
| 87 | if (opt < 0) |
| 88 | break; |
| 89 | switch (opt) |
| 90 | { |
| 91 | case OPTION_ADDRESS: case 'a': |
| 92 | address = (intptr_t)strtoull(optarg, nullptr, 0); |
| 93 | break; |
| 94 | case OPTION_LIMIT: case 'l': |
| 95 | limit = (ssize_t)strtoull(optarg, nullptr, 0); |
| 96 | break; |
| 97 | default: |
| 98 | fprintf(stderr, "usage:\n\n"); |
| 99 | fprintf(stderr, "\t-a ADDR, --address ADDR\n"); |
| 100 | fprintf(stderr, "\t\tPut the counters at ADDR.\n"); |
| 101 | fprintf(stderr, "\t-l NUM, --limit NUM\n"); |
| 102 | fprintf(stderr, "\t\tUse NUM as the limit.\n\n"); |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * This example uses 3 counters (one for calls/jumps/returns). |
| 109 | * We allocate and initialize the counters. For this, we use a |
| 110 | * "reserve" E9Patch API message. |
| 111 | */ |
| 112 | const ssize_t counters[3] = {limit, limit, limit}; |
| 113 | sendReserveMessage(cxt->out, |
| 114 | address, // Memory virtual address |
| 115 | (const uint8_t *)counters, // Memory contents |
| 116 | sizeof(counters), // Memory size |
| 117 | (PROT_READ | PROT_WRITE)); // Memory protections |
| 118 | |
| 119 | /* |