Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Test program for unwinding of frames. |
| 2 | Copyright (C) 2013, 2014, 2016 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | |
| 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | elfutils is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <config.h> |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdio_ext.h> |
| 23 | #include <locale.h> |
| 24 | #include <dirent.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <errno.h> |
| 27 | #include <error.h> |
| 28 | #include <unistd.h> |
| 29 | #include <dwarf.h> |
| 30 | #ifdef __linux__ |
| 31 | #include <sys/resource.h> |
| 32 | #include <sys/ptrace.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <sys/user.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <string.h> |
| 39 | #include <argp.h> |
| 40 | #include ELFUTILS_HEADER(dwfl) |
| 41 | #endif |
| 42 | |
| 43 | #ifndef __linux__ |
| 44 | |
| 45 | int |
| 46 | main (int argc __attribute__ ((unused)), char **argv) |
| 47 | { |
| 48 | fprintf (stderr, "%s: Unwinding not supported for this architecture\n", |
| 49 | argv[0]); |
| 50 | return 77; |
| 51 | } |
| 52 | |
| 53 | #else /* __linux__ */ |
| 54 | |
| 55 | static int |
| 56 | dump_modules (Dwfl_Module *mod, void **userdata __attribute__ ((unused)), |
| 57 | const char *name, Dwarf_Addr start, |
| 58 | void *arg __attribute__ ((unused))) |
| 59 | { |
| 60 | Dwarf_Addr end; |
| 61 | dwfl_module_info (mod, NULL, NULL, &end, NULL, NULL, NULL, NULL); |
| 62 | printf ("%#" PRIx64 "\t%#" PRIx64 "\t%s\n", (uint64_t) start, (uint64_t) end, |
| 63 | name); |
| 64 | return DWARF_CB_OK; |
| 65 | } |
| 66 | |
| 67 | static bool use_raise_jmp_patching; |
| 68 | static pid_t check_tid; |
| 69 | |
| 70 | static void |
| 71 | callback_verify (pid_t tid, unsigned frameno, Dwarf_Addr pc, |
| 72 | const char *symname, Dwfl *dwfl) |
| 73 | { |
| 74 | static bool seen_main = false; |
| 75 | if (symname && *symname == '.') |
| 76 | symname++; |
| 77 | if (symname && strcmp (symname, "main") == 0) |
| 78 | seen_main = true; |
| 79 | if (pc == 0) |
| 80 | { |
| 81 | assert (seen_main); |
| 82 | return; |
| 83 | } |
| 84 | if (check_tid == 0) |
| 85 | check_tid = tid; |
| 86 | if (tid != check_tid) |
| 87 | { |
| 88 | // For the main thread we are only interested if we can unwind till |
| 89 | // we see the "main" symbol. |
| 90 | return; |
| 91 | } |
| 92 | Dwfl_Module *mod; |
| 93 | /* See case 4. Special case to help out simple frame pointer unwinders. */ |
| 94 | static bool duplicate_sigusr2 = false; |
| 95 | if (duplicate_sigusr2) |
| 96 | frameno--; |
| 97 | static bool reduce_frameno = false; |
| 98 | if (reduce_frameno) |
| 99 | frameno--; |
| 100 | if (! use_raise_jmp_patching && frameno >= 2) |
| 101 | frameno += 2; |
| 102 | const char *symname2 = NULL; |
| 103 | switch (frameno) |
| 104 | { |
| 105 | case 0: |
| 106 | if (! reduce_frameno && symname |
| 107 | && (strcmp (symname, "__kernel_vsyscall") == 0 |
| 108 | || strcmp (symname, "__libc_do_syscall") == 0)) |
| 109 | reduce_frameno = true; |
| 110 | else |
| 111 | assert (symname && strcmp (symname, "raise") == 0); |
| 112 | break; |
| 113 | case 1: |
| 114 | assert (symname != NULL && strcmp (symname, "sigusr2") == 0); |
| 115 | break; |
| 116 | case 2: // x86_64 only |
| 117 | /* __restore_rt - glibc maybe does not have to have this symbol. */ |
| 118 | break; |
| 119 | case 3: // use_raise_jmp_patching |
| 120 | if (use_raise_jmp_patching) |
| 121 | { |
| 122 | /* Verify we trapped on the very first instruction of jmp. */ |
| 123 | assert (symname != NULL && strcmp (symname, "jmp") == 0); |
| 124 | mod = dwfl_addrmodule (dwfl, pc - 1); |
| 125 | if (mod) |
| 126 | symname2 = dwfl_module_addrname (mod, pc - 1); |
| 127 | assert (symname2 == NULL || strcmp (symname2, "jmp") != 0); |
| 128 | break; |
| 129 | } |
| 130 | FALLTHROUGH; |
| 131 | case 4: |
| 132 | /* Some simple frame unwinders get this wrong and think sigusr2 |
| 133 | is calling itself again. Allow it and just pretend there is |
| 134 | an extra sigusr2 frame. */ |
| 135 | if (symname != NULL && strcmp (symname, "sigusr2") == 0) |
| 136 | { |
| 137 | duplicate_sigusr2 = true; |
| 138 | break; |
| 139 | } |
| 140 | assert (symname != NULL && strcmp (symname, "stdarg") == 0); |
| 141 | break; |
| 142 | case 5: |
| 143 | /* Verify we trapped on the very last instruction of child. */ |
| 144 | assert (symname != NULL && strcmp (symname, "backtracegen") == 0); |
| 145 | mod = dwfl_addrmodule (dwfl, pc); |
| 146 | if (mod) |
| 147 | symname2 = dwfl_module_addrname (mod, pc); |
| 148 | |
| 149 | // Note that the following assert might in theory even fail on x86_64, |
| 150 | // there is no guarantee that the compiler doesn't reorder the |
| 151 | // instructions or even inserts some padding instructions at the end |
| 152 | // (which apparently happens on ppc64). |
| 153 | if (use_raise_jmp_patching) |
| 154 | assert (symname2 == NULL || strcmp (symname2, "backtracegen") != 0); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | frame_callback (Dwfl_Frame *state, void *frame_arg) |
| 161 | { |
| 162 | int *framenop = frame_arg; |
| 163 | Dwarf_Addr pc; |
| 164 | bool isactivation; |
| 165 | |
| 166 | if (*framenop > 16) |
| 167 | { |
| 168 | error (0, 0, "Too many frames: %d\n", *framenop); |
| 169 | return DWARF_CB_ABORT; |
| 170 | } |
| 171 | |
| 172 | if (! dwfl_frame_pc (state, &pc, &isactivation)) |
| 173 | { |
| 174 | error (0, 0, "%s", dwfl_errmsg (-1)); |
| 175 | return DWARF_CB_ABORT; |
| 176 | } |
| 177 | Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1); |
| 178 | |
| 179 | /* Get PC->SYMNAME. */ |
| 180 | Dwfl_Thread *thread = dwfl_frame_thread (state); |
| 181 | Dwfl *dwfl = dwfl_thread_dwfl (thread); |
| 182 | Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted); |
| 183 | const char *symname = NULL; |
| 184 | if (mod) |
| 185 | symname = dwfl_module_addrname (mod, pc_adjusted); |
| 186 | |
| 187 | printf ("#%2d %#" PRIx64 "%4s\t%s\n", *framenop, (uint64_t) pc, |
| 188 | ! isactivation ? "- 1" : "", symname); |
| 189 | pid_t tid = dwfl_thread_tid (thread); |
| 190 | callback_verify (tid, *framenop, pc, symname, dwfl); |
| 191 | (*framenop)++; |
| 192 | |
| 193 | return DWARF_CB_OK; |
| 194 | } |
| 195 | |
| 196 | static int |
| 197 | thread_callback (Dwfl_Thread *thread, void *thread_arg __attribute__((unused))) |
| 198 | { |
| 199 | printf ("TID %ld:\n", (long) dwfl_thread_tid (thread)); |
| 200 | int frameno = 0; |
| 201 | switch (dwfl_thread_getframes (thread, frame_callback, &frameno)) |
| 202 | { |
| 203 | case 0: |
| 204 | break; |
| 205 | case DWARF_CB_ABORT: |
| 206 | return DWARF_CB_ABORT; |
| 207 | case -1: |
| 208 | error (0, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1)); |
| 209 | /* All platforms do not have yet proper unwind termination. */ |
| 210 | break; |
| 211 | default: |
| 212 | abort (); |
| 213 | } |
| 214 | return DWARF_CB_OK; |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | dump (Dwfl *dwfl) |
| 219 | { |
| 220 | ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, dump_modules, NULL, 0); |
| 221 | assert (ptrdiff == 0); |
| 222 | bool err = false; |
| 223 | switch (dwfl_getthreads (dwfl, thread_callback, NULL)) |
| 224 | { |
| 225 | case 0: |
| 226 | break; |
| 227 | case DWARF_CB_ABORT: |
| 228 | err = true; |
| 229 | break; |
| 230 | case -1: |
| 231 | error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1)); |
| 232 | err = true; |
| 233 | break; |
| 234 | default: |
| 235 | abort (); |
| 236 | } |
| 237 | callback_verify (0, 0, 0, NULL, dwfl); |
| 238 | if (err) |
| 239 | exit (EXIT_FAILURE); |
| 240 | } |
| 241 | |
| 242 | struct see_exec_module |
| 243 | { |
| 244 | Dwfl_Module *mod; |
| 245 | char selfpath[PATH_MAX + 1]; |
| 246 | }; |
| 247 | |
| 248 | static int |
| 249 | see_exec_module (Dwfl_Module *mod, void **userdata __attribute__ ((unused)), |
| 250 | const char *name __attribute__ ((unused)), |
| 251 | Dwarf_Addr start __attribute__ ((unused)), void *arg) |
| 252 | { |
| 253 | struct see_exec_module *data = arg; |
| 254 | if (strcmp (name, data->selfpath) != 0) |
| 255 | return DWARF_CB_OK; |
| 256 | assert (data->mod == NULL); |
| 257 | data->mod = mod; |
| 258 | return DWARF_CB_ABORT; |
| 259 | } |
| 260 | |
| 261 | /* We used to do this on x86_64 only (see backtrace-child why we now don't): |
| 262 | PC will get changed to function 'jmp' by backtrace.c function |
| 263 | prepare_thread. Then SIGUSR2 will be signalled to backtrace-child |
| 264 | which will invoke function sigusr2. |
| 265 | This is all done so that signal interrupts execution of the very first |
| 266 | instruction of a function. Properly handled unwind should not slip into |
| 267 | the previous unrelated function. */ |
| 268 | |
| 269 | #ifdef __x86_64__ |
| 270 | /* #define RAISE_JMP_PATCHING 1 */ |
| 271 | #endif |
| 272 | |
| 273 | static void |
| 274 | prepare_thread (pid_t pid2 __attribute__ ((unused)), |
| 275 | void (*jmp) (void) __attribute__ ((unused))) |
| 276 | { |
| 277 | #ifndef RAISE_JMP_PATCHING |
| 278 | abort (); |
| 279 | #else /* RAISE_JMP_PATCHING */ |
| 280 | long l; |
| 281 | struct user_regs_struct user_regs; |
| 282 | errno = 0; |
| 283 | l = ptrace (PTRACE_GETREGS, pid2, 0, (intptr_t) &user_regs); |
| 284 | assert (errno == 0); |
| 285 | assert (l == 0); |
| 286 | user_regs.rip = (intptr_t) jmp; |
| 287 | l = ptrace (PTRACE_SETREGS, pid2, 0, (intptr_t) &user_regs); |
| 288 | assert (errno == 0); |
| 289 | assert (l == 0); |
| 290 | l = ptrace (PTRACE_CONT, pid2, NULL, (void *) (intptr_t) SIGUSR2); |
| 291 | int status; |
| 292 | pid_t got = waitpid (pid2, &status, __WALL); |
| 293 | assert (errno == 0); |
| 294 | assert (got == pid2); |
| 295 | assert (WIFSTOPPED (status)); |
| 296 | assert (WSTOPSIG (status) == SIGUSR1); |
| 297 | #endif /* RAISE_JMP_PATCHING */ |
| 298 | } |
| 299 | |
| 300 | #include <asm/unistd.h> |
| 301 | #include <unistd.h> |
| 302 | #define tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig)) |
| 303 | |
| 304 | static void |
| 305 | report_pid (Dwfl *dwfl, pid_t pid) |
| 306 | { |
| 307 | int result = dwfl_linux_proc_report (dwfl, pid); |
| 308 | if (result < 0) |
| 309 | error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1)); |
| 310 | else if (result > 0) |
| 311 | error (2, result, "dwfl_linux_proc_report"); |
| 312 | |
| 313 | if (dwfl_report_end (dwfl, NULL, NULL) != 0) |
| 314 | error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1)); |
| 315 | |
| 316 | result = dwfl_linux_proc_attach (dwfl, pid, true); |
| 317 | if (result < 0) |
| 318 | error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1)); |
| 319 | else if (result > 0) |
| 320 | error (2, result, "dwfl_linux_proc_attach"); |
| 321 | } |
| 322 | |
| 323 | static Dwfl * |
| 324 | pid_to_dwfl (pid_t pid) |
| 325 | { |
| 326 | static char *debuginfo_path; |
| 327 | static const Dwfl_Callbacks proc_callbacks = |
| 328 | { |
| 329 | .find_debuginfo = dwfl_standard_find_debuginfo, |
| 330 | .debuginfo_path = &debuginfo_path, |
| 331 | |
| 332 | .find_elf = dwfl_linux_proc_find_elf, |
| 333 | }; |
| 334 | Dwfl *dwfl = dwfl_begin (&proc_callbacks); |
| 335 | if (dwfl == NULL) |
| 336 | error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1)); |
| 337 | report_pid (dwfl, pid); |
| 338 | return dwfl; |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | exec_dump (const char *exec) |
| 343 | { |
| 344 | pid_t pid = fork (); |
| 345 | switch (pid) |
| 346 | { |
| 347 | case -1: |
| 348 | abort (); |
| 349 | case 0: |
| 350 | execl (exec, exec, "--ptraceme", NULL); |
| 351 | abort (); |
| 352 | default: |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* Catch the main thread. Catch it first otherwise the /proc evaluation of |
| 357 | PID may have caught still ourselves before executing execl above. */ |
| 358 | errno = 0; |
| 359 | int status; |
| 360 | pid_t got = waitpid (pid, &status, 0); |
| 361 | assert (errno == 0); |
| 362 | assert (got == pid); |
| 363 | assert (WIFSTOPPED (status)); |
| 364 | // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1. |
| 365 | assert (WSTOPSIG (status) == SIGUSR2); |
| 366 | |
| 367 | /* Catch the spawned thread. Do not use __WCLONE as we could get racy |
| 368 | __WCLONE, probably despite pthread_create already had to be called the new |
| 369 | task is not yet alive enough for waitpid. */ |
| 370 | pid_t pid2 = waitpid (-1, &status, __WALL); |
| 371 | assert (errno == 0); |
| 372 | assert (pid2 > 0); |
| 373 | assert (pid2 != pid); |
| 374 | assert (WIFSTOPPED (status)); |
| 375 | // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1. |
| 376 | assert (WSTOPSIG (status) == SIGUSR1); |
| 377 | |
| 378 | Dwfl *dwfl = pid_to_dwfl (pid); |
| 379 | char *selfpathname; |
| 380 | int i = asprintf (&selfpathname, "/proc/%ld/exe", (long) pid); |
| 381 | assert (i > 0); |
| 382 | struct see_exec_module data; |
| 383 | ssize_t ssize = readlink (selfpathname, data.selfpath, |
| 384 | sizeof (data.selfpath)); |
| 385 | free (selfpathname); |
| 386 | assert (ssize > 0 && ssize < (ssize_t) sizeof (data.selfpath)); |
| 387 | data.selfpath[ssize] = '\0'; |
| 388 | data.mod = NULL; |
| 389 | dwfl_getmodules (dwfl, see_exec_module, &data, 0); |
| 390 | assert (data.mod != NULL); |
| 391 | GElf_Addr loadbase; |
| 392 | Elf *elf = dwfl_module_getelf (data.mod, &loadbase); |
| 393 | GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem); |
| 394 | assert (ehdr != NULL); |
| 395 | /* It is false also on x86_64 with i386 inferior. */ |
| 396 | #ifndef RAISE_JMP_PATCHING |
| 397 | use_raise_jmp_patching = false; |
| 398 | #else /* RAISE_JMP_PATCHING_ */ |
| 399 | use_raise_jmp_patching = ehdr->e_machine == EM_X86_64; |
| 400 | #endif /* __x86_64__ */ |
| 401 | void (*jmp) (void) = 0; |
| 402 | if (use_raise_jmp_patching) |
| 403 | { |
| 404 | // Find inferior symbol named "jmp". |
| 405 | int nsym = dwfl_module_getsymtab (data.mod); |
| 406 | int symi; |
| 407 | for (symi = 1; symi < nsym; ++symi) |
| 408 | { |
| 409 | GElf_Sym symbol; |
| 410 | const char *symbol_name = dwfl_module_getsym (data.mod, symi, &symbol, NULL); |
| 411 | if (symbol_name == NULL) |
| 412 | continue; |
| 413 | switch (GELF_ST_TYPE (symbol.st_info)) |
| 414 | { |
| 415 | case STT_SECTION: |
| 416 | case STT_FILE: |
| 417 | case STT_TLS: |
| 418 | continue; |
| 419 | default: |
| 420 | if (strcmp (symbol_name, "jmp") != 0) |
| 421 | continue; |
| 422 | break; |
| 423 | } |
| 424 | /* LOADBASE is already applied here. */ |
| 425 | jmp = (void (*) (void)) (uintptr_t) symbol.st_value; |
| 426 | break; |
| 427 | } |
| 428 | assert (symi < nsym); |
| 429 | prepare_thread (pid2, jmp); |
| 430 | } |
| 431 | dwfl_end (dwfl); |
| 432 | check_tid = pid2; |
| 433 | dwfl = pid_to_dwfl (pid); |
| 434 | dump (dwfl); |
| 435 | dwfl_end (dwfl); |
| 436 | } |
| 437 | |
| 438 | #define OPT_BACKTRACE_EXEC 0x100 |
| 439 | |
| 440 | static const struct argp_option options[] = |
| 441 | { |
| 442 | { "backtrace-exec", OPT_BACKTRACE_EXEC, "EXEC", 0, N_("Run executable"), 0 }, |
| 443 | { NULL, 0, NULL, 0, NULL, 0 } |
| 444 | }; |
| 445 | |
| 446 | |
| 447 | static error_t |
| 448 | parse_opt (int key, char *arg, struct argp_state *state) |
| 449 | { |
| 450 | switch (key) |
| 451 | { |
| 452 | case ARGP_KEY_INIT: |
| 453 | state->child_inputs[0] = state->input; |
| 454 | break; |
| 455 | |
| 456 | case OPT_BACKTRACE_EXEC: |
| 457 | exec_dump (arg); |
| 458 | exit (0); |
| 459 | |
| 460 | default: |
| 461 | return ARGP_ERR_UNKNOWN; |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int |
| 467 | main (int argc __attribute__ ((unused)), char **argv) |
| 468 | { |
| 469 | /* We use no threads here which can interfere with handling a stream. */ |
| 470 | __fsetlocking (stdin, FSETLOCKING_BYCALLER); |
| 471 | __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 472 | __fsetlocking (stderr, FSETLOCKING_BYCALLER); |
| 473 | |
| 474 | /* Set locale. */ |
| 475 | (void) setlocale (LC_ALL, ""); |
| 476 | |
| 477 | elf_version (EV_CURRENT); |
| 478 | |
| 479 | Dwfl *dwfl = NULL; |
| 480 | const struct argp_child argp_children[] = |
| 481 | { |
| 482 | { .argp = dwfl_standard_argp () }, |
| 483 | { .argp = NULL } |
| 484 | }; |
| 485 | const struct argp argp = |
| 486 | { |
| 487 | options, parse_opt, NULL, NULL, argp_children, NULL, NULL |
| 488 | }; |
| 489 | (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl); |
| 490 | assert (dwfl != NULL); |
| 491 | /* We want to make sure the dwfl was properly attached. */ |
| 492 | if (dwfl_pid (dwfl) < 0) |
| 493 | error (2, 0, "dwfl_pid: %s", dwfl_errmsg (-1)); |
| 494 | dump (dwfl); |
| 495 | dwfl_end (dwfl); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | #endif /* ! __linux__ */ |
| 500 | |