1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
|
/* Process a stream of stack samples into stack traces.
Copyright (C) 2023-2025 Red Hat, Inc.
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This file incorporates work covered by the following copyright and
permission notice:
Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Subject to the terms and conditions of this license, each copyright holder
and contributor hereby grants to those receiving rights under this license
a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except for failure to satisfy the conditions of this license)
patent license to make, have made, use, offer to sell, sell, import, and
otherwise transfer this software, where such license applies only to those
patent claims, already acquired or hereafter acquired, licensable by such
copyright holder or contributor that are necessarily infringed by:
(a) their Contribution(s) (the licensed copyrights of copyright holders
and non-copyrightable additions of contributors, in source or binary
form) alone; or
(b) combination of their Contribution(s) with the work of authorship to
which such Contribution(s) was added by such copyright holder or
contributor, if, at the time the Contribution is added, such addition
causes such combination to be necessarily infringed. The patent license
shall not apply to any other combinations which include the
Contribution.
Except as expressly stated above, no rights or licenses from any copyright
holder or contributor is granted under this license, whether expressly, by
implication, estoppel or otherwise.
DISCLAIMER
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include <config.h>
#include <assert.h>
#include <argp.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <locale.h>
#include <system.h>
#include <linux/perf_event.h>
/* TODO: Need to generalize the code beyond x86 architectures. */
#include <asm/perf_regs.h>
#ifndef _ASM_X86_PERF_REGS_H
#error "eu-stacktrace is currently limited to x86 architectures"
#endif
/*************************************
* Includes: libdwfl data structures *
*************************************/
#include ELFUTILS_HEADER(ebl)
/* #include ELFUTILS_HEADER(dwfl) */
#include "../libdwfl/libdwflP.h"
/* XXX: Private header needed for find_procfile. */
#include ELFUTILS_HEADER(dwfl_stacktrace)
/*************************************
* Includes: sysprof data structures *
*************************************/
#if HAVE_SYSPROF_6_HEADERS
#include <sysprof-6/sysprof-capture-types.h>
#define HAVE_SYSPROF_HEADERS 1
#elif HAVE_SYSPROF_4_HEADERS
#include <sysprof-4/sysprof-capture-types.h>
#define HAVE_SYSPROF_HEADERS 1
#else
#define HAVE_SYSPROF_HEADERS 0
#endif
#if HAVE_SYSPROF_HEADERS
/* XXX: To be added to new versions of sysprof. If a
sysprof-capture-types.h with new capture frame is being used, this
#if should guard against duplicate declarations. */
#if SYSPROF_CAPTURE_FRAME_LAST < 19
#undef SYSPROF_CAPTURE_FRAME_LAST
#define SYSPROF_CAPTURE_FRAME_STACK_USER 18
#define SYSPROF_CAPTURE_FRAME_LAST 19
SYSPROF_ALIGNED_BEGIN(1)
typedef struct
{
SysprofCaptureFrame frame;
uint64_t size;
int32_t tid;
uint32_t padding;
uint8_t data[0];
} SysprofCaptureStackUser
SYSPROF_ALIGNED_END(1);
/* Does not appear standalone; instead, appended to the end of a SysprofCaptureStackUser frame. */
SYSPROF_ALIGNED_BEGIN(1)
typedef struct
{
uint32_t n_regs;
uint32_t abi;
uint64_t regs[0];
} SysprofCaptureUserRegs
SYSPROF_ALIGNED_END(1);
#endif /* SYSPROF_CAPTURE_FRAME_STACK_USER */
#endif /* HAVE_SYSPROF_HEADERS */
/**************************
* Global data structures *
**************************/
/* TODO: Reduce repeated error messages in show_failures. */
static int maxframes = 256;
static char *input_path = NULL;
static int input_fd = -1;
static char *output_path = NULL;
static int output_fd = -1;
static int signal_count = 0;
#define MODE_OPTS "none/passthru/naive"
#define MODE_NONE 0x0
#define MODE_PASSTHRU 0x1
#define MODE_NAIVE 0x2
static int processing_mode = MODE_NAIVE;
#define FORMAT_OPTS "sysprof"
#define FORMAT_PERF 0x1
#define FORMAT_SYSPROF 0x2
static int input_format;
static int output_format = FORMAT_SYSPROF;
/* XXX Used to decide regs_mask for dwflst_perf_sample_getframes. */
Ebl *default_ebl = NULL;
/* non-printable argp options. */
#define OPT_DEBUG 0x100
/* Diagnostic options. */
static bool show_frames = false;
static bool show_samples = false;
static bool show_failures = false;
static bool show_summary = true;
/* Environment vars to drive diagnostic options: */
#define ELFUTILS_STACKTRACE_VERBOSE_ENV_VAR "ELFUTILS_STACKTRACE_VERBOSE"
/* Valid values that turn on diagnostics: 'true', 'verbose', 'debug', '1', '2'. */
/* Enables even more diagnostics on modules: */
/* #define DEBUG_MODULES */
/* Enables standard access to DWARF debuginfo, matching stack.c.
This is of dubious benefit -- for profiling, we really should
aim to resolve everything with minimal overhead using eh CFI. */
/* #define FIND_DEBUGINFO */
/* Program exit codes. All samples processed without any errors is
GOOD. Some non-fatal errors during processing is an ERROR. A
fatal error or no samples processed at all is BAD. A command line
USAGE exit is generated by argp_error. */
#define EXIT_OK 0
#define EXIT_ERROR 1
#define EXIT_BAD 2
#define EXIT_USAGE 64
/**************************
* Sysprof format support *
**************************/
/* TODO: elfutils (internal) libraries use libNN_set_errno and _E_WHATEVER;
this code sets errno variable directly and uses standard EWHATEVER. */
/* XXX based on sysprof src/libsysprof-capture/sysprof-capture-reader.c
Note: BSD license attribution at the top of the file applies to this
segment. Could split into a separate file or even a library,
in which case the attribution notice will move along with it. */
#if HAVE_SYSPROF_HEADERS
/* A complete passthrough can be implemented based on the following 7 functions:
- sysprof_reader_begin/sysprof_reader_end :: sysprof_capture_reader_new_from_fd
- sysprof_reader_getheader :: sysprof_capture_reader_read_file_header
- sysprof_reader_getframes :: sysprof_capture_reader_discover_end_time, an example main loop that doesn't handle every type of frame
- sysprof_reader_next_frame :: sysprof_capture_reader_peek_frame + sysprof_capture_reader_skip + sysprof_capture_reader_read_basic
- sysprof_reader_ensure_space_for :: sysprof_capture_reader_ensure_space_for
- sysprof_reader_bswap_frame :: sysprof_capture_reader_bswap_frame
*/
/* Callback results */
enum
{
SYSPROF_CB_OK = 0,
SYSPROF_CB_ABORT
};
typedef struct
{
uint8_t *buf;
size_t bufsz;
size_t len;
size_t pos;
size_t fd_off; /* XXX track offset for debugging only */
int fd;
int endian;
SysprofCaptureFileHeader header;
} SysprofReader;
/* forward decls */
SysprofReader *sysprof_reader_begin (int fd);
void sysprof_reader_end (SysprofReader *reader);
bool sysprof_reader_getheader (SysprofReader *reader,
SysprofCaptureFileHeader *header);
void sysprof_reader_bswap_frame (SysprofReader *reader,
SysprofCaptureFrame *frame);
bool sysprof_reader_ensure_space_for (SysprofReader *reader, size_t len);
SysprofCaptureFrame *sysprof_reader_next_frame (SysprofReader *reader);
ptrdiff_t sysprof_reader_getframes (SysprofReader *reader,
int (*callback) (SysprofCaptureFrame *frame,
void *arg),
void *arg);
SysprofReader *
sysprof_reader_begin (int fd)
{
SysprofReader *reader;
assert (fd > -1);
reader = malloc (sizeof (SysprofReader));
if (reader == NULL)
{
errno = ENOMEM;
return NULL;
}
reader->bufsz = USHRT_MAX * 2;
reader->buf = malloc (reader->bufsz);
if (reader->buf == NULL)
{
free (reader);
errno = ENOMEM;
return NULL;
}
reader->len = 0;
reader->pos = 0;
reader->fd = fd;
reader->fd_off = 0;
if (!sysprof_reader_getheader (reader, &reader->header))
{
int errsv = errno;
sysprof_reader_end (reader);
errno = errsv;
return NULL;
}
if (reader->header.little_endian)
reader->endian = __LITTLE_ENDIAN;
else
reader->endian = __BIG_ENDIAN;
return reader;
}
void
sysprof_reader_end (SysprofReader *reader)
{
if (reader != NULL)
{
free (reader->buf);
free (reader);
}
}
bool
sysprof_reader_getheader (SysprofReader *reader,
SysprofCaptureFileHeader *header)
{
assert (reader != NULL);
assert (header != NULL);
if (sizeof *header != read (reader->fd, header, sizeof *header))
{
/* errno is propagated */
return false;
}
reader->fd_off += sizeof *header;
if (header->magic != SYSPROF_CAPTURE_MAGIC)
{
errno = EBADMSG;
return false;
}
header->capture_time[sizeof header->capture_time - 1] = '\0';
return true;
}
void
sysprof_reader_bswap_frame (SysprofReader *reader, SysprofCaptureFrame *frame)
{
assert (reader != NULL);
assert (frame != NULL);
if (unlikely (reader->endian != __BYTE_ORDER))
{
frame->len = bswap_16 (frame->len);
frame->cpu = bswap_16 (frame->cpu);
frame->pid = bswap_32 (frame->pid);
frame->time = bswap_64 (frame->time);
}
}
bool
sysprof_reader_ensure_space_for (SysprofReader *reader, size_t len)
{
assert (reader != NULL);
assert (reader->pos <= reader->len);
assert (len > 0);
/* Ensure alignment of length to read */
len = (len + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1);
if ((reader->len - reader->pos) < len)
{
ssize_t r;
if (reader->len > reader->pos)
memmove (reader->buf,
&reader->buf[reader->pos],
reader->len - reader->pos);
reader->len -= reader->pos;
reader->pos = 0;
while (reader->len < len)
{
assert ((reader->pos + reader->len) < reader->bufsz);
assert (reader->len < reader->bufsz);
/* Read into our buffer */
r = read (reader->fd,
&reader->buf[reader->len],
reader->bufsz - reader->len);
if (r <= 0)
break;
reader->fd_off += r;
reader->len += r;
}
}
return (reader->len - reader->pos) >= len;
}
/* XXX May want to signal errors in more detail with an rc. */
SysprofCaptureFrame *
sysprof_reader_next_frame (SysprofReader *reader)
{
SysprofCaptureFrame frame_hdr;
SysprofCaptureFrame *frame = NULL;
assert (reader != NULL);
assert ((reader->pos % SYSPROF_CAPTURE_ALIGN) == 0);
assert (reader->pos <= reader->len);
assert (reader->pos <= reader->bufsz);
if (!sysprof_reader_ensure_space_for (reader, sizeof *frame))
return NULL;
assert ((reader->pos % SYSPROF_CAPTURE_ALIGN) == 0);
frame = (SysprofCaptureFrame *)(void *)&reader->buf[reader->pos];
frame_hdr = *frame;
sysprof_reader_bswap_frame (reader, &frame_hdr);
if (frame_hdr.len < sizeof (SysprofCaptureFrame))
return NULL;
if (!sysprof_reader_ensure_space_for (reader, frame_hdr.len))
return NULL;
frame = (SysprofCaptureFrame *)(void *)&reader->buf[reader->pos];
sysprof_reader_bswap_frame (reader, frame);
if (frame->len > (reader->len - reader->pos))
return NULL;
reader->pos += frame->len;
if ((reader->pos % SYSPROF_CAPTURE_ALIGN) != 0)
return NULL;
/* if (frame->type < 0 || frame->type >= SYSPROF_CAPTURE_FRAME_LAST) */
if (frame->type >= SYSPROF_CAPTURE_FRAME_LAST)
return NULL;
return frame;
}
ptrdiff_t
sysprof_reader_getframes (SysprofReader *reader,
int (*callback) (SysprofCaptureFrame *,
void *),
void *arg)
{
SysprofCaptureFrame *frame;
assert (reader != NULL);
while ((frame = sysprof_reader_next_frame (reader)))
{
int ok = callback (frame, arg);
if (ok != SYSPROF_CB_OK)
return -1;
}
return 0;
}
#endif /* HAVE_SYSPROF_HEADERS */
/****************************************************
* Dwfl and statistics table for multiple processes *
****************************************************/
Dwflst_Process_Tracker *tracker = NULL;
/* This echoes lib/dynamicsizehash.* with some necessary modifications. */
typedef struct
{
bool used;
pid_t pid;
Dwfl *dwfl;
char *comm;
int max_frames; /* for diagnostic purposes */
int total_samples; /* for diagnostic purposes */
int lost_samples; /* for diagnostic purposes */
Dwfl_Unwound_Source last_unwound; /* track CFI source, for diagnostic purposes */
Dwfl_Unwound_Source worst_unwound; /* track CFI source, for diagnostic purposes */
} dwfltab_ent;
typedef struct
{
ssize_t size;
ssize_t filled;
dwfltab_ent *table;
} dwfltab;
/* XXX table size must be a prime */
#define DWFLTAB_DEFAULT_SIZE 1021
extern size_t next_prime (size_t); /* XXX from libeu.a lib/next_prime.c */
dwfltab_ent *dwfltab_find(pid_t pid); /* forward decl */
/* TODO: Could turn this into a field of sui instead of a global. */
dwfltab default_table;
/* XXX based on lib/dynamicsizehash.* *_init */
bool dwfltab_init(void)
{
dwfltab *htab = &default_table;
htab->size = DWFLTAB_DEFAULT_SIZE;
htab->filled = 0;
htab->table = calloc ((htab->size + 1), sizeof(htab->table[0]));
return (htab->table != NULL);
}
/* XXX based on lib/dynamicsizehash.* insert_entry_2 */
bool dwfltab_resize(void)
{
/* TODO: Also implement LRU eviction, especially given the number of
extremely-short-lived processes seen on GNOME desktop. */
dwfltab *htab = &default_table;
ssize_t old_size = htab->size;
dwfltab_ent *old_table = htab->table;
htab->size = next_prime (htab->size * 2);
htab->table = calloc ((htab->size + 1), sizeof(htab->table[0]));
if (htab->table == NULL)
{
htab->size = old_size;
htab->table = old_table;
return false;
}
htab->filled = 0;
/* Transfer the old entries to the new table. */
for (ssize_t idx = 1; idx <= old_size; ++idx)
if (old_table[idx].used)
{
dwfltab_ent *ent0 = &old_table[idx];
dwfltab_ent *ent1 = dwfltab_find(ent0->pid);
assert (ent1 != NULL);
memcpy (ent1, ent0, sizeof(dwfltab_ent));
}
free (old_table);
/* TODO: Need to decide log level for this message. For now, it's
not a failure, and printing it by default seems harmless: */
fprintf(stderr, N_("Resized Dwfl table from %ld to %ld, copied %ld entries\n"),
old_size, htab->size, htab->filled);
return true;
}
/* XXX based on lib/dynamicsizehash.* *_find */
dwfltab_ent *dwfltab_find(pid_t pid)
{
dwfltab *htab = &default_table;
ssize_t idx = 1 + (htab->size > (ssize_t)pid ? (ssize_t)pid : (ssize_t)pid % htab->size);
if (!htab->table[idx].used)
goto found;
if (htab->table[idx].pid == pid)
goto found;
int64_t hash = 1 + pid % (htab->size - 2);
do
{
if (idx <= hash)
idx = htab->size + idx - hash;
else
idx -= hash;
if (!htab->table[idx].used)
goto found;
if (htab->table[idx].pid == pid)
goto found;
}
while (true);
found:
if (htab->table[idx].pid == 0)
{
if (100 * htab->filled > 90 * htab->size)
if (!dwfltab_resize())
return NULL;
htab->table[idx].used = true;
htab->table[idx].pid = pid;
htab->filled += 1;
}
return &htab->table[idx];
}
Dwfl *
pid_find_dwfl (pid_t pid)
{
dwfltab_ent *entry = dwfltab_find(pid);
if (entry == NULL)
return NULL;
return entry->dwfl;
}
char *
pid_find_comm (pid_t pid)
{
dwfltab_ent *entry = dwfltab_find(pid);
if (entry == NULL)
return "<unknown>";
if (entry->comm != NULL)
return entry->comm;
char name[64];
int i = snprintf (name, sizeof(name), "/proc/%ld/comm", (long) pid);
FILE *procfile = fopen(name, "r");
if (procfile == NULL)
goto fail;
size_t linelen = 0;
i = getline(&entry->comm, &linelen, procfile);
if (i < 0)
{
free(entry->comm);
goto fail;
}
for (i = linelen - 1; i > 0; i--)
if (entry->comm[i] == '\n')
entry->comm[i] = '\0';
fclose(procfile);
goto done;
fail:
entry->comm = (char *)malloc(16);
snprintf (entry->comm, 16, "<unknown>");
done:
return entry->comm;
}
/* Cache dwfl structs in a basic hash table. */
void
pid_store_dwfl (pid_t pid, Dwfl *dwfl)
{
dwfltab_ent *entry = dwfltab_find(pid);
if (entry == NULL)
return;
entry->pid = pid;
entry->dwfl = dwfl;
pid_find_comm(pid);
return;
}
/*****************************************************
* Sysprof backend: basic none/passthrough callbacks *
*****************************************************/
#if HAVE_SYSPROF_HEADERS
int
sysprof_none_cb (SysprofCaptureFrame *frame __attribute__ ((unused)),
void *arg __attribute__ ((unused)))
{
return SYSPROF_CB_OK;
}
struct sysprof_passthru_info
{
int output_fd;
SysprofReader *reader;
int pos; /* for diagnostic purposes */
};
int
sysprof_passthru_cb (SysprofCaptureFrame *frame, void *arg)
{
struct sysprof_passthru_info *spi = (struct sysprof_passthru_info *)arg;
sysprof_reader_bswap_frame (spi->reader, frame); /* reverse the earlier bswap */
ssize_t n_write = write (spi->output_fd, frame, frame->len);
spi->pos += frame->len;
assert ((spi->pos % SYSPROF_CAPTURE_ALIGN) == 0);
if (n_write < 0)
error (EXIT_BAD, errno, N_("Write error to file or FIFO '%s'"), output_path);
return SYSPROF_CB_OK;
}
#endif /* HAVE_SYSPROF_HEADERS */
/****************************************
* Sysprof backend: unwinding callbacks *
****************************************/
#if HAVE_SYSPROF_HEADERS
#define UNWIND_ADDR_INCREMENT 512
struct sysprof_unwind_info
{
int output_fd;
SysprofReader *reader;
int pos; /* for diagnostic purposes */
int n_addrs;
int max_addrs; /* for diagnostic purposes */
uint64_t last_abi;
Dwarf_Addr last_base; /* for diagnostic purposes */
Dwarf_Addr last_sp; /* for diagnostic purposes */
#ifdef DEBUG_MODULES
Dwfl *last_dwfl; /* for diagnostic purposes */
#endif
pid_t last_pid; /* for diagnostic purposes, to provide access to dwfltab */
Dwarf_Addr *addrs; /* allocate blocks of UNWIND_ADDR_INCREMENT */
void *outbuf;
};
#ifdef FIND_DEBUGINFO
static char *debuginfo_path = NULL;
static const Dwfl_Callbacks sample_callbacks =
{
.find_elf = dwflst_tracker_linux_proc_find_elf,
.find_debuginfo = dwfl_standard_find_debuginfo,
.debuginfo_path = &debuginfo_path,
};
#else
int
nop_find_debuginfo (Dwfl_Module *mod __attribute__((unused)),
void **userdata __attribute__((unused)),
const char *modname __attribute__((unused)),
GElf_Addr base __attribute__((unused)),
const char *file_name __attribute__((unused)),
const char *debuglink_file __attribute__((unused)),
GElf_Word debuglink_crc __attribute__((unused)),
char **debuginfo_file_name __attribute__((unused)))
{
#ifdef DEBUG_MODULES
fprintf(stderr, "nop_find_debuginfo: modname=%s file_name=%s debuglink_file=%s\n",
modname, file_name, debuglink_file);
#endif
return -1;
}
static const Dwfl_Callbacks sample_callbacks =
{
.find_elf = dwflst_tracker_linux_proc_find_elf,
.find_debuginfo = nop_find_debuginfo, /* work with CFI only */
};
#endif /* FIND_DEBUGINFO */
/* TODO: Probably needs to be relocated to libdwfl/linux-pid-attach.c
to remove a dependency on the private dwfl interface. */
int
find_procfile (Dwfl *dwfl, pid_t *pid, Elf **elf, int *elf_fd)
{
char buffer[36];
FILE *procfile;
int err = 0; /* The errno to return and set for dwfl->attacherr. */
/* Make sure to report the actual PID (thread group leader) to
dwfl_attach_state. */
snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) *pid);
procfile = fopen (buffer, "r");
if (procfile == NULL)
{
err = errno;
fail:
if (dwfl->process == NULL && dwfl->attacherr == DWFL_E_NOERROR) /* XXX requires libdwflP.h */
{
errno = err;
/* TODO: __libdwfl_canon_error not exported from libdwfl */
/* dwfl->attacherr = __libdwfl_canon_error (DWFL_E_ERRNO); */
}
return err;
}
char *line = NULL;
size_t linelen = 0;
while (getline (&line, &linelen, procfile) >= 0)
if (startswith (line, "Tgid:"))
{
errno = 0;
char *endptr;
long val = strtol (&line[5], &endptr, 10);
if ((errno == ERANGE && val == LONG_MAX)
|| *endptr != '\n' || val < 0 || val != (pid_t) val)
*pid = 0;
else
*pid = (pid_t) val;
break;
}
free (line);
fclose (procfile);
if (*pid == 0)
{
err = ESRCH;
goto fail;
}
char name[64];
int i = snprintf (name, sizeof (name), "/proc/%ld/task", (long) *pid);
if (i <= 0 || i >= (ssize_t) sizeof (name) - 1)
{
errno = -ENOMEM;
goto fail;
}
DIR *dir = opendir (name);
if (dir == NULL)
{
err = errno;
goto fail;
}
else
closedir(dir);
i = snprintf (name, sizeof (name), "/proc/%ld/exe", (long) *pid);
assert (i > 0 && i < (ssize_t) sizeof (name) - 1);
*elf_fd = open (name, O_RDONLY);
if (*elf_fd >= 0)
{
*elf = elf_begin (*elf_fd, ELF_C_READ_MMAP, NULL);
if (*elf == NULL)
{
/* Just ignore, dwfl_attach_state will fall back to trying
to associate the Dwfl with one of the existing Dwfl_Module
ELF images (to know the machine/class backend to use). */
if (show_failures)
fprintf(stderr, N_("find_procfile pid %lld: elf not found"),
(long long)*pid);
close (*elf_fd);
*elf_fd = -1;
}
}
else
*elf = NULL;
return 0;
}
Dwfl *
sysprof_init_dwfl_cb (Dwflst_Process_Tracker *cb_tracker,
pid_t pid,
void *arg __attribute__ ((unused)))
{
Dwfl *dwfl = dwflst_tracker_dwfl_begin (cb_tracker);
int err = dwfl_linux_proc_report (dwfl, pid);
if (err < 0)
{
if (show_failures)
fprintf(stderr, "dwfl_linux_proc_report pid %lld: %s",
(long long) pid, dwfl_errmsg (-1));
return NULL;
}
err = dwfl_report_end (dwfl, NULL, NULL);
if (err != 0)
{
if (show_failures)
fprintf(stderr, "dwfl_report_end pid %lld: %s",
(long long) pid, dwfl_errmsg (-1));
return NULL;
}
return dwfl;
}
Dwfl *
sysprof_find_dwfl (struct sysprof_unwind_info *sui,
SysprofCaptureStackUser *ev,
SysprofCaptureUserRegs *regs,
Elf **out_elf)
{
pid_t pid = ev->frame.pid;
/* XXX: Note that sysprof requesting the x86_64 register file from
perf_events will result in an array of 17 regs even for 32-bit
applications. */
if (regs->n_regs < ebl_frame_nregs(default_ebl)) /* XXX expecting everything except FLAGS */
{
if (show_failures)
fprintf(stderr, N_("sysprof_find_dwfl: n_regs=%d, expected %ld\n"),
regs->n_regs, ebl_frame_nregs(default_ebl));
return NULL;
}
Elf *elf = NULL;
Dwfl *dwfl = dwflst_tracker_find_pid (tracker, pid, sysprof_init_dwfl_cb, NULL);
bool cached = false;
if (dwfl != NULL && dwfl->process != NULL)
{
cached = true;
goto reuse;
}
int elf_fd = -1;
int err = find_procfile (dwfl, &pid, &elf, &elf_fd);
if (err < 0)
{
if (show_failures)
fprintf(stderr, "find_procfile pid %lld: %s",
(long long) pid, dwfl_errmsg (-1));
return NULL;
}
reuse:
/* TODO: Generalize to other architectures than x86_64. */
sui->last_sp = regs->regs[7];
sui->last_base = sui->last_sp;
if (show_frames) {
bool is_abi32 = (regs->abi == PERF_SAMPLE_REGS_ABI_32);
fprintf(stderr, "sysprof_find_dwfl pid %lld%s: size=%ld%s pc=%lx sp=%lx+(%lx)\n",
(long long) pid, cached ? " (cached)" : "",
ev->size, is_abi32 ? " (32-bit)" : "",
regs->regs[8], sui->last_base, (long)0);
}
if (!cached)
pid_store_dwfl (pid, dwfl);
*out_elf = elf;
return dwfl;
}
int
sysprof_unwind_frame_cb (Dwfl_Frame *state, void *arg)
{
Dwarf_Addr pc;
bool isactivation;
if (! dwfl_frame_pc (state, &pc, &isactivation))
{
if (show_failures)
fprintf(stderr, "dwfl_frame_pc: %s\n",
dwfl_errmsg(-1));
return DWARF_CB_ABORT;
}
Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
Dwarf_Addr sp;
/* TODO: Need to generalize this code beyond x86 architectures. */
struct sysprof_unwind_info *sui = (struct sysprof_unwind_info *)arg;
int is_abi32 = (sui->last_abi == PERF_SAMPLE_REGS_ABI_32);
/* DWARF register order cf. elfutils backends/{x86_64,i386}_initreg.c: */
int user_regs_sp = is_abi32 ? 4 : 7;
int rc = dwfl_frame_reg (state, user_regs_sp, &sp);
if (rc < 0)
{
if (show_failures)
fprintf(stderr, "dwfl_frame_reg: %s\n",
dwfl_errmsg(-1));
return DWARF_CB_ABORT;
}
#ifdef DEBUG_MODULES
Dwfl_Module *mod = dwfl_addrmodule(sui->last_dwfl, pc);
if (mod == NULL)
{
fprintf(stderr, "* pc=%lx -> NO MODULE\n", pc);
}
else
{
const char *mainfile;
const char *debugfile;
const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, NULL,
NULL, &mainfile, &debugfile);
fprintf (stderr, "* module %s -> mainfile=%s debugfile=%s\n", modname, mainfile, debugfile);
Dwarf_Addr bias;
Dwarf_CFI *cfi_eh = dwfl_module_eh_cfi (mod, &bias);
if (cfi_eh == NULL)
fprintf(stderr, "* pc=%lx -> NO EH_CFI\n", pc);
}
#endif
dwfltab_ent *dwfl_ent = dwfltab_find(sui->last_pid);
if (dwfl_ent != NULL)
{
Dwfl_Unwound_Source unwound_source = dwfl_frame_unwound_source(state);
if (unwound_source > dwfl_ent->worst_unwound)
dwfl_ent->worst_unwound = unwound_source;
dwfl_ent->last_unwound = unwound_source;
if (show_frames)
fprintf(stderr, "* frame %d: pc_adjusted=%lx sp=%lx+(%lx) [%s]\n",
sui->n_addrs, pc_adjusted, sui->last_base, sp - sui->last_base,
dwfl_unwound_source_str(unwound_source));
}
else
{
if (show_frames)
fprintf(stderr, N_("* frame %d: pc_adjusted=%lx sp=%lx+(%lx) [dwfl_ent not found]\n"),
sui->n_addrs, pc_adjusted, sui->last_base, sp - sui->last_base);
}
if (sui->n_addrs > maxframes)
{
/* XXX very rarely, the unwinder can loop infinitely; worth investigating? */
if (show_failures)
fprintf(stderr, N_("sysprof_unwind_frame_cb: sample exceeded maxframes %d\n"),
maxframes);
return DWARF_CB_ABORT;
}
sui->last_sp = sp;
if (sui->n_addrs >= sui->max_addrs)
{
sui->addrs = reallocarray (sui->addrs, sui->max_addrs + UNWIND_ADDR_INCREMENT, sizeof(Dwarf_Addr));
sui->max_addrs = sui->max_addrs + UNWIND_ADDR_INCREMENT;
}
sui->addrs[sui->n_addrs] = pc;
sui->n_addrs++;
return DWARF_CB_OK;
}
int
sysprof_unwind_cb (SysprofCaptureFrame *frame, void *arg)
{
struct sysprof_unwind_info *sui = (struct sysprof_unwind_info *)arg;
ssize_t n_write;
/* additional diagnostic to display process name */
char *comm = NULL;
if (frame->type == SYSPROF_CAPTURE_FRAME_SAMPLE || frame->type == SYSPROF_CAPTURE_FRAME_STACK_USER)
comm = pid_find_comm(frame->pid);
if (frame->type == SYSPROF_CAPTURE_FRAME_SAMPLE)
{
/* XXX additional diagnostics for comparing to eu-stacktrace unwind */
SysprofCaptureSample *ev_sample = (SysprofCaptureSample *)frame;
if (show_samples)
fprintf(stderr, N_("sysprof_unwind_cb pid %lld (%s): callchain sample with %d frames\n"),
(long long)frame->pid, comm, ev_sample->n_addrs);
if (show_summary)
{
/* For final diagnostics. */
dwfltab_ent *dwfl_ent = dwfltab_find(frame->pid);
if (dwfl_ent == NULL && show_failures)
fprintf(stderr, N_("sysprof_unwind_cb pid %lld (%s): could not create Dwfl table entry\n"),
(long long)frame->pid, comm);
else if (dwfl_ent != NULL)
{
if (ev_sample->n_addrs > dwfl_ent->max_frames)
dwfl_ent->max_frames = ev_sample->n_addrs;
dwfl_ent->total_samples ++;
if (ev_sample->n_addrs <= 2)
dwfl_ent->lost_samples ++;
}
}
}
if (frame->type != SYSPROF_CAPTURE_FRAME_STACK_USER)
{
sysprof_reader_bswap_frame (sui->reader, frame); /* reverse the earlier bswap */
n_write = write (sui->output_fd, frame, frame->len);
sui->pos += frame->len;
assert ((sui->pos % SYSPROF_CAPTURE_ALIGN) == 0);
if (n_write < 0)
error (EXIT_BAD, errno, N_("Write error to file or FIFO '%s'"), output_path);
return SYSPROF_CB_OK;
}
SysprofCaptureStackUser *ev = (SysprofCaptureStackUser *)frame;
uint8_t *tail_ptr = (uint8_t *)ev;
tail_ptr += sizeof(SysprofCaptureStackUser) + ev->size;
SysprofCaptureUserRegs *regs = (SysprofCaptureUserRegs *)tail_ptr;
if (show_frames)
fprintf(stderr, "\n"); /* extra newline for padding */
Elf *elf = NULL;
Dwfl *dwfl = sysprof_find_dwfl (sui, ev, regs, &elf);
if (dwfl == NULL)
{
if (show_summary)
{
dwfltab_ent *dwfl_ent = dwfltab_find(frame->pid);
dwfl_ent->total_samples++;
dwfl_ent->lost_samples++;
}
if (show_failures)
fprintf(stderr, "sysprof_find_dwfl pid %lld (%s) (failed)\n",
(long long)frame->pid, comm);
return SYSPROF_CB_OK;
}
sui->n_addrs = 0;
sui->last_abi = regs->abi;
#ifdef DEBUG_MODULES
sui->last_dwfl = dwfl;
#endif
sui->last_pid = frame->pid;
uint64_t regs_mask = ebl_perf_frame_regs_mask (default_ebl);
int rc = dwflst_perf_sample_getframes (dwfl, elf, frame->pid, ev->tid,
(uint8_t *)&ev->data, ev->size,
regs->regs, regs->n_regs,
regs_mask, regs->abi,
sysprof_unwind_frame_cb, sui);
if (rc < 0)
{
if (show_failures)
fprintf(stderr, "dwflst_perf_sample_getframes pid %lld: %s\n",
(long long)frame->pid, dwfl_errmsg(-1));
}
if (show_samples)
{
bool is_abi32 = (regs->abi == PERF_SAMPLE_REGS_ABI_32);
fprintf(stderr, N_("sysprof_unwind_cb pid %lld (%s)%s: unwound %d frames\n"),
(long long)frame->pid, comm, is_abi32 ? " (32-bit)" : "", sui->n_addrs);
}
if (show_summary)
{
/* For final diagnostics. */
dwfltab_ent *dwfl_ent = dwfltab_find(frame->pid);
if (dwfl_ent != NULL && sui->n_addrs > dwfl_ent->max_frames)
dwfl_ent->max_frames = sui->n_addrs;
dwfl_ent->total_samples++;
if (sui->n_addrs <= 2)
dwfl_ent->lost_samples ++;
}
/* Assemble and output callchain frame. */
/* XXX assert(sizeof(Dwarf_Addr) == sizeof(SysprofCaptureAddress)); */
SysprofCaptureSample *ev_callchain;
size_t len = sizeof *ev_callchain + (sui->n_addrs * sizeof(Dwarf_Addr));
ev_callchain = (SysprofCaptureSample *)sui->outbuf;
if (len > USHRT_MAX)
{
if (show_failures)
fprintf(stderr, N_("sysprof_unwind_cb frame size %ld is too large (max %d)\n"),
len, USHRT_MAX);
return SYSPROF_CB_OK;
}
SysprofCaptureFrame *out_frame = (SysprofCaptureFrame *)ev_callchain;
out_frame->len = len;
out_frame->cpu = ev->frame.cpu;
out_frame->pid = ev->frame.pid;
out_frame->time = ev->frame.time;
out_frame->type = SYSPROF_CAPTURE_FRAME_SAMPLE;
out_frame->padding1 = 0;
out_frame->padding2 = 0;
ev_callchain->n_addrs = sui->n_addrs;
ev_callchain->tid = ev->tid;
memcpy (ev_callchain->addrs, sui->addrs, (sui->n_addrs * sizeof(SysprofCaptureAddress)));
n_write = write (sui->output_fd, ev_callchain, len);
if (n_write < 0)
error (EXIT_BAD, errno, N_("Write error to file or FIFO '%s'"), output_path);
return SYSPROF_CB_OK;
}
#endif /* HAVE_SYSPROF_HEADERS */
/****************
* Main program *
****************/
/* Required to match our signal handling with that of a sysprof parent process. */
static void sigint_handler (int /* signo */)
{
if (signal_count >= 2)
{
exit(1);
}
if (signal_count == 0)
{
fprintf (stderr, "%s\n", N_("Waiting for input to finish. Press twice more ^C to force exit."));
}
signal_count ++;
}
static error_t
parse_opt (int key, char *arg __attribute__ ((unused)),
struct argp_state *state)
{
switch (key)
{
case 'i':
input_path = arg;
break;
case 'o':
output_path = arg;
break;
case 'm':
if (strcmp (arg, "none") == 0)
{
processing_mode = MODE_NONE;
}
else if (strcmp (arg, "passthru") == 0)
{
processing_mode = MODE_PASSTHRU;
}
else if (strcmp (arg, "naive") == 0)
{
processing_mode = MODE_NAIVE;
}
else
{
argp_error (state, N_("Unsupported -m '%s', should be " MODE_OPTS "."), arg);
}
break;
case 'f':
if (strcmp (arg, "sysprof") == 0)
{
input_format = FORMAT_SYSPROF;
}
else
{
argp_error (state, N_("Unsupported -f '%s', should be " FORMAT_OPTS "."), arg);
}
break;
case OPT_DEBUG:
show_frames = true;
FALLTHROUGH;
case 'v':
show_samples = true;
show_failures = true;
show_summary = true;
break;
case ARGP_KEY_END:
if (input_path == NULL)
input_path = "-"; /* default to stdin */
if (output_path == NULL)
output_path = "-"; /* default to stdout */
if (processing_mode == 0)
processing_mode = MODE_NAIVE;
if (input_format == 0)
input_format = FORMAT_SYSPROF;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
int
main (int argc, char **argv)
{
/* Set locale. */
(void) setlocale (LC_ALL, "");
const struct argp_option options[] =
{
{ NULL, 0, NULL, 0, N_("Input and output selection options:"), 0 },
{ "input", 'i', "PATH", 0,
N_("File or FIFO to read stack samples from"), 0 },
/* TODO: Should also support taking an FD for fork/exec pipes. */
{ "output", 'o', "PATH", 0,
N_("File or FIFO to send stack traces to"), 0 },
{ NULL, 0, NULL, 0, N_("Processing options:"), 0 },
{ "mode", 'm', MODE_OPTS, 0,
N_("Processing mode, default 'naive'"), 0 },
/* TODO: Should also support 'naive', 'caching'. */
/* TODO: Add an option to control stack-stitching. */
{ "verbose", 'v', NULL, 0,
N_("Show additional information for each unwound sample"), 0 },
{ "debug", OPT_DEBUG, NULL, 0,
N_("Show additional information for each unwound frame"), 0 },
/* TODO: Add a 'quiet' option suppressing summaries + errors.
Perhaps also allow -v, -vv, -vvv in SystemTap style? */
{ "format", 'f', FORMAT_OPTS, 0,
N_("Input data format, default 'sysprof'"), 0 },
/* TODO: Add an option to control output data format separately,
shift to I/O selection section. */
{ NULL, 0, NULL, 0, NULL, 0 }
};
const struct argp argp =
{
.options = options,
.parser = parse_opt,
.doc = N_("Process a stream of stack samples into stack traces.\n\
\n\
Experimental tool, see README.eu-stacktrace in the development branch:\n\
https://sourceware.org/cgit/elfutils/tree/README.eu-stacktrace?h=users/serhei/eu-stacktrace\n")
};
argp_parse(&argp, argc, argv, 0, NULL, NULL);
/* Also handle ELFUTILS_STACKTRACE_VERBOSE_ENV_VAR: */
char *env_verbose = getenv(ELFUTILS_STACKTRACE_VERBOSE_ENV_VAR);
if (env_verbose == NULL || strlen(env_verbose) == 0)
; /* nop, use command line options */
else if (strcmp(env_verbose, "false") == 0
|| strcmp(env_verbose, "0") == 0)
; /* nop, use command line options */
else if (strcmp(env_verbose, "true") == 0
|| strcmp(env_verbose, "verbose") == 0
|| strcmp(env_verbose, "1") == 0)
{
show_samples = true;
show_failures = true;
show_summary = true;
}
else if (strcmp(env_verbose, "debug") == 0
|| strcmp(env_verbose, "2") == 0)
{
show_frames = true;
show_samples = true;
show_failures = true;
show_summary = true;
}
else
fprintf (stderr, N_("WARNING: Unknown value '%s' in environment variable %s, ignoring\n"),
env_verbose, ELFUTILS_STACKTRACE_VERBOSE_ENV_VAR);
/* TODO: Also handle common expansions e.g. ~/foo instead of /home/user/foo. */
if (strcmp (input_path, "-") == 0)
input_fd = STDIN_FILENO;
else
input_fd = open (input_path, O_RDONLY);
if (input_fd < 0)
error (EXIT_BAD, errno, N_("Cannot open input file or FIFO '%s'"), input_path);
if (strcmp (output_path, "-") == 0)
output_fd = STDOUT_FILENO;
else
output_fd = open (output_path, O_CREAT | O_WRONLY, 0640);
if (output_fd < 0)
error (EXIT_BAD, errno, N_("Cannot open output file or FIFO '%s'"), output_path);
/* TODO: Only really needed if launched from sysprof and inheriting its signals. */
if (signal (SIGINT, sigint_handler) == SIG_ERR)
error (EXIT_BAD, errno, N_("Cannot set signal handler for SIGINT"));
#if !(HAVE_SYSPROF_HEADERS)
/* TODO: Should hide corresponding command line options when this is the case? */
error (EXIT_BAD, 0, N_("Sysprof support is not available in this version."));
/* XXX: The following are not specific to the Sysprof backend;
avoid unused-variable warnings while it is the only backend. */
(void)sample_thread_callbacks;
(void)output_format;
(void)maxframes;
#else
fprintf(stderr, "\n=== starting eu-stacktrace ===\n");
tracker = dwflst_tracker_begin (&sample_callbacks);
/* TODO: For now, code the processing loop for sysprof only; generalize later. */
assert (input_format == FORMAT_SYSPROF);
assert (output_format == FORMAT_SYSPROF);
SysprofReader *reader = sysprof_reader_begin (input_fd);
ssize_t n_write = write (output_fd, &reader->header, sizeof reader->header);
if (n_write < 0)
error (EXIT_BAD, errno, N_("Write error to file or FIFO '%s'"), output_path);
ptrdiff_t offset;
unsigned long int output_pos = 0;
if (processing_mode == MODE_NONE)
{
struct sysprof_passthru_info sni = { output_fd, reader, sizeof reader->header };
offset = sysprof_reader_getframes (reader, &sysprof_none_cb, &sni);
output_pos = sni.pos;
}
else if (processing_mode == MODE_PASSTHRU)
{
struct sysprof_passthru_info spi = { output_fd, reader, sizeof reader->header };
offset = sysprof_reader_getframes (reader, &sysprof_passthru_cb, &spi);
output_pos = spi.pos;
}
else /* processing_mode == MODE_NAIVE */
{
if (!dwfltab_init())
error (EXIT_BAD, errno, N_("Could not initialize Dwfl table"));
/* TODO: Generalize to other architectures. */
default_ebl = ebl_openbackend_machine(EM_X86_64);
struct sysprof_unwind_info sui;
sui.output_fd = output_fd;
sui.reader = reader;
sui.pos = sizeof reader->header;
sui.n_addrs = 0;
sui.last_base = 0;
sui.last_sp = 0;
sui.last_abi = PERF_SAMPLE_REGS_ABI_NONE;
sui.max_addrs = UNWIND_ADDR_INCREMENT;
sui.addrs = (Dwarf_Addr *)malloc (sui.max_addrs * sizeof(Dwarf_Addr));
sui.outbuf = (void *)malloc (USHRT_MAX * sizeof(uint8_t));
offset = sysprof_reader_getframes (reader, &sysprof_unwind_cb, &sui);
if (show_summary)
{
/* Final diagnostics. */
#define PERCENT(x,tot) ((x+tot == 0)?0.0:((double)x)/((double)tot)*100.0)
int total_samples = 0;
int total_lost_samples = 0;
fprintf(stderr, "\n=== final summary ===\n");
for (unsigned idx = 1; idx < default_table.size; idx++)
{
dwfltab_ent *t = default_table.table;
if (!t[idx].used)
continue;
/* XXX worst_unwound gives least preferred unwind method used for this process
(i.e. eh_frame is preferred to dwarf is preferred to ebl) */
fprintf(stderr, N_("%d %s -- max %d frames, received %d samples, lost %d samples (%.1f%%) (last %s, worst %s)\n"),
t[idx].pid, t[idx].comm, t[idx].max_frames,
t[idx].total_samples, t[idx].lost_samples,
PERCENT(t[idx].lost_samples, t[idx].total_samples),
dwfl_unwound_source_str(t[idx].last_unwound),
dwfl_unwound_source_str(t[idx].worst_unwound));
total_samples += t[idx].total_samples;
total_lost_samples += t[idx].lost_samples;
}
fprintf(stderr, "===\n");
fprintf(stderr, N_("TOTAL -- received %d samples, lost %d samples, loaded %ld processes\n"),
total_samples, total_lost_samples,
default_table.filled /* TODO: after implementing LRU eviction, need to maintain a separate count, e.g. htab->filled + htab->evicted */);
}
output_pos = sui.pos;
free(sui.addrs);
free(sui.outbuf);
}
if (offset < 0 && output_pos <= sizeof reader->header)
error (EXIT_BAD, errno, N_("No frames in file or FIFO '%s'"), input_path);
else if (offset < 0)
error (EXIT_BAD, errno, N_("Error processing file or FIFO '%s' at input offset %ld, output offset %ld"),
input_path, reader->pos, output_pos);
sysprof_reader_end (reader);
#endif
if (input_fd != -1)
close (input_fd);
if (output_fd != -1)
close (output_fd);
dwflst_tracker_end (tracker);
return EXIT_OK;
}
|