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
|
/*
* Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License (not later!)
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define _LARGEFILE64_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <glob.h>
#include "trace-cmd-local.h"
#include "list.h"
#include "version.h"
/* We can't depend on the host size for size_t, all must be 64 bit */
typedef unsigned long long tsize_t;
typedef long long stsize_t;
static struct tracecmd_event_list all_event_list = {
.next = NULL,
.glob = "all"
};
struct tracecmd_option {
unsigned short id;
int size;
void *data;
tsize_t offset;
struct list_head list;
};
struct tracecmd_output {
int fd;
int page_size;
int cpus;
struct pevent *pevent;
char *tracing_dir;
int options_written;
int nr_options;
struct list_head options;
};
struct list_event {
struct list_event *next;
char *name;
char *file;
};
struct list_event_system {
struct list_event_system *next;
struct list_event *events;
char *name;
};
static stsize_t
do_write_check(struct tracecmd_output *handle, const void *data, tsize_t size)
{
return __do_write_check(handle->fd, data, size);
}
static short convert_endian_2(struct tracecmd_output *handle, short val)
{
if (!handle->pevent)
return val;
return __data2host2(handle->pevent, val);
}
static int convert_endian_4(struct tracecmd_output *handle, int val)
{
if (!handle->pevent)
return val;
return __data2host4(handle->pevent, val);
}
static unsigned long long convert_endian_8(struct tracecmd_output *handle,
unsigned long long val)
{
if (!handle->pevent)
return val;
return __data2host8(handle->pevent, val);
}
void tracecmd_output_free(struct tracecmd_output *handle)
{
struct tracecmd_option *option;
if (!handle)
return;
if (handle->tracing_dir)
free(handle->tracing_dir);
if (handle->pevent)
pevent_unref(handle->pevent);
while (!list_empty(&handle->options)) {
option = container_of(handle->options.next,
struct tracecmd_option, list);
list_del(&option->list);
free(option->data);
free(option);
}
free(handle);
}
void tracecmd_output_close(struct tracecmd_output *handle)
{
if (!handle)
return;
if (handle->fd >= 0) {
close(handle->fd);
handle->fd = -1;
}
tracecmd_output_free(handle);
}
static unsigned long get_size_fd(int fd)
{
unsigned long long size = 0;
char buf[BUFSIZ];
int r;
do {
r = read(fd, buf, BUFSIZ);
if (r > 0)
size += r;
} while (r > 0);
lseek(fd, 0, SEEK_SET);
return size;
}
static unsigned long get_size(const char *file)
{
unsigned long long size = 0;
int fd;
fd = open(file, O_RDONLY);
if (fd < 0)
die("Can't read '%s'", file);
size = get_size_fd(fd);
close(fd);
return size;
}
static tsize_t copy_file_fd(struct tracecmd_output *handle, int fd)
{
tsize_t size = 0;
char buf[BUFSIZ];
stsize_t r;
do {
r = read(fd, buf, BUFSIZ);
if (r > 0) {
size += r;
if (do_write_check(handle, buf, r))
return 0;
}
} while (r > 0);
return size;
}
static tsize_t copy_file(struct tracecmd_output *handle,
const char *file)
{
tsize_t size = 0;
int fd;
fd = open(file, O_RDONLY);
if (fd < 0)
die("Can't read '%s'", file);
size = copy_file_fd(handle, fd);
close(fd);
return size;
}
/*
* Finds the path to the debugfs/tracing
* Allocates the string and stores it.
*/
static const char *find_tracing_dir(struct tracecmd_output *handle)
{
if (!handle->tracing_dir)
handle->tracing_dir = tracecmd_find_tracing_dir();
return handle->tracing_dir;
}
static char *get_tracing_file(struct tracecmd_output *handle, const char *name)
{
const char *tracing;
char *file;
tracing = find_tracing_dir(handle);
if (!tracing)
return NULL;
file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
if (!file)
return NULL;
sprintf(file, "%s/%s", tracing, name);
return file;
}
static void put_tracing_file(char *file)
{
free(file);
}
int tracecmd_ftrace_enable(int set)
{
struct stat buf;
char *path = "/proc/sys/kernel/ftrace_enabled";
int fd;
char *val = set ? "1" : "0";
int ret = 0;
/* if ftace_enable does not exist, simply ignore it */
fd = stat(path, &buf);
if (fd < 0)
return ENODEV;
fd = open(path, O_WRONLY);
if (fd < 0)
die ("Can't %s ftrace", set ? "enable" : "disable");
if (write(fd, val, 1) < 0)
ret = -1;
close(fd);
return ret;
}
static int read_header_files(struct tracecmd_output *handle)
{
tsize_t size, check_size, endian8;
struct stat st;
char *path;
int fd;
int ret;
path = get_tracing_file(handle, "events/header_page");
if (!path)
return -1;
ret = stat(path, &st);
if (ret < 0) {
/* old style did not show this info, just add zero */
put_tracing_file(path);
if (do_write_check(handle, "header_page", 12))
return -1;
size = 0;
if (do_write_check(handle, &size, 8))
return -1;
if (do_write_check(handle, "header_event", 13))
return -1;
if (do_write_check(handle, &size, 8))
return -1;
return 0;
}
fd = open(path, O_RDONLY);
if (fd < 0) {
warning("can't read '%s'", path);
return -1;
}
/* unfortunately, you can not stat debugfs files for size */
size = get_size_fd(fd);
if (do_write_check(handle, "header_page", 12))
goto out_close;
endian8 = convert_endian_8(handle, size);
if (do_write_check(handle, &endian8, 8))
goto out_close;
check_size = copy_file_fd(handle, fd);
close(fd);
if (size != check_size) {
warning("wrong size for '%s' size=%lld read=%lld",
path, size, check_size);
errno = EINVAL;
return -1;
}
put_tracing_file(path);
path = get_tracing_file(handle, "events/header_event");
if (!path)
return -1;
fd = open(path, O_RDONLY);
if (fd < 0)
die("can't read '%s'", path);
size = get_size_fd(fd);
if (do_write_check(handle, "header_event", 13))
goto out_close;
endian8 = convert_endian_8(handle, size);
if (do_write_check(handle, &endian8, 8))
goto out_close;
check_size = copy_file_fd(handle, fd);
close(fd);
if (size != check_size) {
warning("wrong size for '%s'", path);
return -1;
}
put_tracing_file(path);
return 0;
out_close:
close(fd);
return -1;
}
static int copy_event_system(struct tracecmd_output *handle,
struct list_event_system *slist)
{
struct list_event *elist;
unsigned long long size, check_size, endian8;
struct stat st;
char *format;
int endian4;
int count = 0;
int ret;
for (elist = slist->events; elist; elist = elist->next)
count++;
endian4 = convert_endian_4(handle, count);
if (do_write_check(handle, &endian4, 4))
return -1;
for (elist = slist->events; elist; elist = elist->next) {
format = elist->file;
ret = stat(format, &st);
if (ret >= 0) {
/* unfortunately, you can not stat debugfs files for size */
size = get_size(format);
endian8 = convert_endian_8(handle, size);
if (do_write_check(handle, &endian8, 8))
return -1;
check_size = copy_file(handle, format);
if (size != check_size) {
warning("error in size of file '%s'", format);
return -1;
}
}
}
return 0;
}
static void add_list_event_system(struct list_event_system **systems,
const char *system,
const char *event,
const char *path)
{
struct list_event_system *slist;
struct list_event *elist;
for (slist = *systems; slist; slist = slist->next)
if (strcmp(slist->name, system) == 0)
break;
if (!slist) {
slist = malloc_or_die(sizeof(*slist));
slist->name = strdup(system);
slist->next = *systems;
slist->events = NULL;
*systems = slist;
}
for (elist = slist->events; elist; elist = elist->next)
if (strcmp(elist->name, event) == 0)
break;
if (!elist) {
elist = malloc_or_die(sizeof(*elist));
elist->name = strdup(event);
elist->file = strdup(path);
elist->next = slist->events;
slist->events = elist;
}
}
static void free_list_events(struct list_event_system *list)
{
struct list_event_system *slist;
struct list_event *elist;
while (list) {
slist = list;
list = list->next;
while (slist->events) {
elist = slist->events;
slist->events = elist->next;
free(elist->name);
free(elist->file);
free(elist);
}
free(slist->name);
free(slist);
}
}
static void glob_events(struct tracecmd_output *handle,
struct list_event_system **systems,
const char *str)
{
glob_t globbuf;
char *events_path;
char *system;
char *event;
char *path;
char *file;
char *ptr;
int do_ftrace = 0;
int events_len;
int ret;
int i;
if (strncmp(str, "ftrace/", 7) == 0)
do_ftrace = 1;
events_path = get_tracing_file(handle, "events");
events_len = strlen(events_path);
path = malloc_or_die(events_len + strlen(str) +
strlen("/format") + 2);
path[0] = '\0';
strcat(path, events_path);
strcat(path, "/");
strcat(path, str);
strcat(path, "/format");
put_tracing_file(events_path);
globbuf.gl_offs = 0;
ret = glob(path, 0, NULL, &globbuf);
free(path);
if (ret < 0)
return;
for (i = 0; i < globbuf.gl_pathc; i++) {
file = globbuf.gl_pathv[i];
system = strdup(file + events_len + 1);
system = strtok_r(system, "/", &ptr);
if (!ptr) {
/* ?? should we warn? */
free(system);
continue;
}
if (!do_ftrace && strcmp(system, "ftrace") == 0) {
free(system);
continue;
}
event = strtok_r(NULL, "/", &ptr);
if (!ptr) {
/* ?? should we warn? */
free(system);
continue;
}
add_list_event_system(systems, system, event, file);
free(system);
}
globfree(&globbuf);
}
static void
create_event_list_item(struct tracecmd_output *handle,
struct list_event_system **systems,
struct tracecmd_event_list *list)
{
char *ptr;
char *str;
str = strdup(list->glob);
if (!str)
die("strdup - no memory");
/* system and event names are separated by a ':' */
ptr = strchr(str, ':');
if (ptr)
*ptr = '/';
else
/* system and event may also be separated by a '/' */
ptr = strchr(str, '/');
if (ptr) {
glob_events(handle, systems, str);
free(str);
return;
}
ptr = str;
str = malloc_or_die(strlen(ptr) + 3);
str[0] = '\0';
strcat(str, ptr);
strcat(str, "/*");
glob_events(handle, systems, str);
str[0] = '\0';
strcat(str, "*/");
strcat(str, ptr);
glob_events(handle, systems, str);
free(ptr);
free(str);
}
static int read_ftrace_files(struct tracecmd_output *handle)
{
struct list_event_system *systems = NULL;
struct tracecmd_event_list list = { .glob = "ftrace/*" };
int ret;
create_event_list_item(handle, &systems, &list);
ret = copy_event_system(handle, systems);
free_list_events(systems);
return ret;
}
static struct list_event_system *
create_event_list(struct tracecmd_output *handle,
struct tracecmd_event_list *event_list)
{
struct list_event_system *systems = NULL;
struct tracecmd_event_list *list;
for (list = event_list; list; list = list->next)
create_event_list_item(handle, &systems, list);
return systems;
}
static int read_event_files(struct tracecmd_output *handle,
struct tracecmd_event_list *event_list)
{
struct list_event_system *systems;
struct list_event_system *slist;
struct tracecmd_event_list *list;
struct tracecmd_event_list all_events = { .glob = "*/*" };
int count = 0;
int endian4;
int ret;
/*
* If any of the list is the special keyword "all" then
* just do all files.
*/
for (list = event_list; list; list = list->next) {
if (strcmp(list->glob, "all") == 0)
break;
}
/* all events are listed, use a global glob */
if (list)
event_list = &all_events;
systems = create_event_list(handle, event_list);
for (slist = systems; slist; slist = slist->next)
count++;
ret = -1;
endian4 = convert_endian_4(handle, count);
if (do_write_check(handle, &endian4, 4))
goto out_free;
ret = 0;
for (slist = systems; !ret && slist; slist = slist->next) {
if (do_write_check(handle, slist->name,
strlen(slist->name) + 1)) {
ret = -1;
continue;
}
ret = copy_event_system(handle, slist);
}
out_free:
free_list_events(systems);
return ret;
}
static int read_proc_kallsyms(struct tracecmd_output *handle,
const char *kallsyms)
{
unsigned int size, check_size, endian4;
const char *path = "/proc/kallsyms";
struct stat st;
int ret;
if (kallsyms)
path = kallsyms;
ret = stat(path, &st);
if (ret < 0) {
/* not found */
size = 0;
endian4 = convert_endian_4(handle, size);
if (do_write_check(handle, &endian4, 4))
return -1;
return 0;
}
size = get_size(path);
endian4 = convert_endian_4(handle, size);
if (do_write_check(handle, &endian4, 4))
return -1;
check_size = copy_file(handle, path);
if (size != check_size) {
errno = EINVAL;
warning("error in size of file '%s'", path);
return -1;
}
return 0;
}
static int read_ftrace_printk(struct tracecmd_output *handle)
{
unsigned int size, check_size, endian4;
struct stat st;
char *path;
int ret;
path = get_tracing_file(handle, "printk_formats");
if (!path)
return -1;
ret = stat(path, &st);
if (ret < 0) {
/* not found */
size = 0;
endian4 = convert_endian_4(handle, size);
if (do_write_check(handle, &endian4, 4))
goto fail;
goto out;
}
size = get_size(path);
endian4 = convert_endian_4(handle, size);
if (do_write_check(handle, &endian4, 4))
goto fail;
check_size = copy_file(handle, path);
if (size != check_size) {
errno = EINVAL;
warning("error in size of file '%s'", path);
goto fail;
}
out:
put_tracing_file(path);
return 0;
fail:
put_tracing_file(path);
return -1;
}
static int save_tracing_file_data(struct tracecmd_output *handle,
const char *filename)
{
unsigned long long endian8;
char *file = NULL;
struct stat st;
off64_t check_size;
off64_t size;
int ret = -1;
file = get_tracing_file(handle, filename);
if (!file)
return -1;
ret = stat(file, &st);
if (ret >= 0) {
size = get_size(file);
endian8 = convert_endian_8(handle, size);
if (do_write_check(handle, &endian8, 8))
goto out_free;
check_size = copy_file(handle, file);
if (size != check_size) {
errno = EINVAL;
warning("error in size of file '%s'", file);
goto out_free;
}
} else {
size = 0;
endian8 = convert_endian_8(handle, size);
if (do_write_check(handle, &endian8, 8))
goto out_free;
}
ret = 0;
out_free:
put_tracing_file(file);
return ret;
}
static struct tracecmd_output *
create_file_fd(int fd, struct tracecmd_input *ihandle,
const char *tracing_dir,
const char *kallsyms,
struct tracecmd_event_list *list)
{
struct tracecmd_output *handle;
struct pevent *pevent;
char buf[BUFSIZ];
int endian4;
handle = malloc(sizeof(*handle));
if (!handle)
return NULL;
memset(handle, 0, sizeof(*handle));
handle->fd = fd;
if (tracing_dir) {
handle->tracing_dir = strdup(tracing_dir);
if (!handle->tracing_dir)
goto out_free;
}
list_head_init(&handle->options);
buf[0] = 23;
buf[1] = 8;
buf[2] = 68;
memcpy(buf + 3, "tracing", 7);
if (do_write_check(handle, buf, 10))
goto out_free;
if (do_write_check(handle, FILE_VERSION_STRING, strlen(FILE_VERSION_STRING) + 1))
goto out_free;
/* get endian and page size */
if (ihandle) {
pevent = tracecmd_get_pevent(ihandle);
/* Use the pevent of the ihandle for later writes */
handle->pevent = tracecmd_get_pevent(ihandle);
pevent_ref(pevent);
if (pevent->file_bigendian)
buf[0] = 1;
else
buf[0] = 0;
handle->page_size = tracecmd_page_size(ihandle);
} else {
if (tracecmd_host_bigendian())
buf[0] = 1;
else
buf[0] = 0;
handle->page_size = getpagesize();
}
if (do_write_check(handle, buf, 1))
goto out_free;
/* save size of long (this may not be what the kernel is) */
buf[0] = sizeof(long);
if (do_write_check(handle, buf, 1))
goto out_free;
endian4 = convert_endian_4(handle, handle->page_size);
if (do_write_check(handle, &endian4, 4))
goto out_free;
if (ihandle)
return handle;
if (read_header_files(handle))
goto out_free;
if (read_ftrace_files(handle))
goto out_free;
if (read_event_files(handle, list))
goto out_free;
if (read_proc_kallsyms(handle, kallsyms))
goto out_free;
if (read_ftrace_printk(handle))
goto out_free;
/*
* Save the command lines;
*/
if (save_tracing_file_data(handle, "saved_cmdlines") < 0)
goto out_free;
return handle;
out_free:
tracecmd_output_close(handle);
return NULL;
}
static struct tracecmd_output *create_file(const char *output_file,
struct tracecmd_input *ihandle,
const char *tracing_dir,
const char *kallsyms,
struct tracecmd_event_list *list)
{
struct tracecmd_output *handle;
int fd;
fd = open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
if (fd < 0)
return NULL;
handle = create_file_fd(fd, ihandle, tracing_dir, kallsyms, list);
if (!handle) {
close(fd);
unlink(output_file);
}
return handle;
}
/**
* tracecmd_add_option - add options to the file
* @handle: the output file handle name
* @id: the id of the option
* @size: the size of the option data
* @data: the data to write to the file.
*
* Returns handle to update option if needed.
* Just the content can be updated, with smaller or equal to
* content than the specified size.
*/
struct tracecmd_option *
tracecmd_add_option(struct tracecmd_output *handle,
unsigned short id, int size, const void *data)
{
struct tracecmd_option *option;
/*
* We can only add options before they were written.
* This may change in the future.
*/
if (handle->options_written)
return NULL;
handle->nr_options++;
option = malloc(sizeof(*option));
if (!option)
die("Could not allocate space for option");
option->id = id;
option->size = size;
option->data = malloc_or_die(size);
memcpy(option->data, data, size);
list_add_tail(&option->list, &handle->options);
return option;
}
static int add_options(struct tracecmd_output *handle)
{
struct tracecmd_option *options;
unsigned short option;
unsigned short endian2;
unsigned int endian4;
/* If already written, ignore */
if (handle->options_written)
return 0;
if (do_write_check(handle, "options ", 10))
return -1;
list_for_each_entry(options, &handle->options, list) {
endian2 = convert_endian_2(handle, options->id);
if (do_write_check(handle, &endian2, 2))
return -1;
endian4 = convert_endian_4(handle, options->size);
if (do_write_check(handle, &endian4, 4))
return -1;
/* Save the data location in case it needs to be updated */
options->offset = lseek64(handle->fd, 0, SEEK_CUR);
if (do_write_check(handle, options->data,
options->size))
return -1;
}
option = TRACECMD_OPTION_DONE;
if (do_write_check(handle, &option, 2))
return -1;
handle->options_written = 1;
return 0;
}
int tracecmd_update_option(struct tracecmd_output *handle,
struct tracecmd_option *option, int size,
const void *data)
{
tsize_t offset;
stsize_t ret;
if (size > option->size) {
warning("Can't update option with more data than allocated");
return -1;
}
if (!handle->options_written) {
/* Hasn't been written yet. Just update current pointer */
option->size = size;
memcpy(option->data, data, size);
return 0;
}
/* Save current offset */
offset = lseek64(handle->fd, 0, SEEK_CUR);
ret = lseek64(handle->fd, option->offset, SEEK_SET);
if (ret == (off64_t)-1) {
warning("could not seek to %lld\n", option->offset);
return -1;
}
if (do_write_check(handle, data, size))
return -1;
ret = lseek64(handle->fd, offset, SEEK_SET);
if (ret == (off64_t)-1) {
warning("could not seek to %lld\n", offset);
return -1;
}
return 0;
}
struct tracecmd_option *
tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name)
{
struct tracecmd_option *option;
char *buf;
int size = 8 + strlen(name) + 1;
buf = malloc(size);
if (!buf) {
warning("Failed to malloc buffer");
return NULL;
}
*(tsize_t *)buf = 0;
strcpy(buf + 8, name);
option = tracecmd_add_option(handle, TRACECMD_OPTION_BUFFER, size, buf);
free(buf);
return option;
}
struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, int cpus)
{
struct tracecmd_output *handle;
char *path;
handle = create_file(output_file, NULL, NULL, NULL, &all_event_list);
if (!handle)
return NULL;
cpus = convert_endian_4(handle, cpus);
if (do_write_check(handle, &cpus, 4))
goto out_free;
if (add_options(handle) < 0)
goto out_free;
if (do_write_check(handle, "latency ", 10))
goto out_free;
path = get_tracing_file(handle, "trace");
if (!path)
goto out_free;
copy_file(handle, path);
put_tracing_file(path);
return handle;
out_free:
tracecmd_output_close(handle);
return NULL;
}
static int __tracecmd_append_cpu_data(struct tracecmd_output *handle,
int cpus, char * const *cpu_data_files)
{
off64_t *offsets = NULL;
unsigned long long *sizes = NULL;
off64_t offset;
unsigned long long endian8;
off64_t check_size;
char *file;
struct stat st;
int ret;
int i;
if (do_write_check(handle, "flyrecord", 10))
goto out_free;
offsets = malloc_or_die(sizeof(*offsets) * cpus);
if (!offsets)
goto out_free;
sizes = malloc_or_die(sizeof(*sizes) * cpus);
if (!sizes)
goto out_free;
offset = lseek64(handle->fd, 0, SEEK_CUR);
/* hold any extra data for data */
offset += cpus * (16);
/*
* Unfortunately, the trace_clock data was placed after the
* cpu data, and wasn't accounted for with the offsets.
* We need to save room for the trace_clock file. This means
* we need to find the size of it before we define the final
* offsets.
*/
file = get_tracing_file(handle, "trace_clock");
if (!file)
goto out_free;
/* Save room for storing the size */
offset += 8;
ret = stat(file, &st);
if (ret >= 0)
offset += get_size(file);
put_tracing_file(file);
/* Page align offset */
offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1);
for (i = 0; i < cpus; i++) {
file = cpu_data_files[i];
ret = stat(file, &st);
if (ret < 0) {
warning("can not stat '%s'", file);
goto out_free;
}
offsets[i] = offset;
sizes[i] = st.st_size;
offset += st.st_size;
offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1);
endian8 = convert_endian_8(handle, offsets[i]);
if (do_write_check(handle, &endian8, 8))
goto out_free;
endian8 = convert_endian_8(handle, sizes[i]);
if (do_write_check(handle, &endian8, 8))
goto out_free;
}
if (save_tracing_file_data(handle, "trace_clock") < 0)
goto out_free;
for (i = 0; i < cpus; i++) {
fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n",
i, (unsigned long long) offsets[i]);
offset = lseek64(handle->fd, offsets[i], SEEK_SET);
if (offset == (off64_t)-1) {
warning("could not seek to %lld\n", offsets[i]);
goto out_free;
}
check_size = copy_file(handle, cpu_data_files[i]);
if (check_size != sizes[i]) {
errno = EINVAL;
warning("did not match size of %lld to %lld",
check_size, sizes[i]);
goto out_free;
}
fprintf(stderr, " %llu bytes in size\n", (unsigned long long)check_size);
}
free(offsets);
free(sizes);
return 0;
out_free:
free(offsets);
free(sizes);
return -1;
}
int tracecmd_append_cpu_data(struct tracecmd_output *handle,
int cpus, char * const *cpu_data_files)
{
int endian4;
endian4 = convert_endian_4(handle, cpus);
if (do_write_check(handle, &endian4, 4))
return -1;
if (add_options(handle) < 0)
return -1;
return __tracecmd_append_cpu_data(handle, cpus, cpu_data_files);
}
int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
struct tracecmd_option *option,
int cpus, char * const *cpu_data_files)
{
tsize_t offset;
stsize_t ret;
offset = lseek64(handle->fd, 0, SEEK_CUR);
/* Go to the option data, where will write the offest */
ret = lseek64(handle->fd, option->offset, SEEK_SET);
if (ret == (off64_t)-1) {
warning("could not seek to %lld\n", option->offset);
return -1;
}
if (do_write_check(handle, &offset, 8))
return -1;
/* Go back to end of file */
ret = lseek64(handle->fd, offset, SEEK_SET);
if (ret == (off64_t)-1) {
warning("could not seek to %lld\n", offset);
return -1;
}
return __tracecmd_append_cpu_data(handle, cpus, cpu_data_files);
}
int tracecmd_attach_cpu_data_fd(int fd, int cpus, char * const *cpu_data_files)
{
struct tracecmd_input *ihandle;
struct tracecmd_output *handle;
struct pevent *pevent;
int ret = -1;
/* Move the file descriptor to the beginning */
if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
return -1;
/* get a input handle from this */
ihandle = tracecmd_alloc_fd(fd);
if (!ihandle)
return -1;
/* move the file descriptor to the end */
if (lseek(fd, 0, SEEK_END) == (off_t)-1)
goto out_free;
/* create a partial output handle */
handle = malloc(sizeof(*handle));
if (!handle)
goto out_free;
memset(handle, 0, sizeof(*handle));
handle->fd = fd;
/* get endian and page size */
pevent = tracecmd_get_pevent(ihandle);
/* Use the pevent of the ihandle for later writes */
handle->pevent = tracecmd_get_pevent(ihandle);
pevent_ref(pevent);
handle->page_size = tracecmd_page_size(ihandle);
list_head_init(&handle->options);
if (tracecmd_append_cpu_data(handle, cpus, cpu_data_files) >= 0)
ret = 0;
tracecmd_output_close(handle);
out_free:
tracecmd_close(ihandle);
return ret;
}
int tracecmd_attach_cpu_data(char *file, int cpus, char * const *cpu_data_files)
{
int fd;
fd = open(file, O_RDWR);
if (fd < 0)
return -1;
return tracecmd_attach_cpu_data_fd(fd, cpus, cpu_data_files);
}
struct tracecmd_output *
tracecmd_create_file_glob(const char *output_file,
int cpus, char * const *cpu_data_files,
struct tracecmd_event_list *list)
{
struct tracecmd_output *handle;
handle = create_file(output_file, NULL, NULL, NULL, list);
if (!handle)
return NULL;
if (tracecmd_append_cpu_data(handle, cpus, cpu_data_files) < 0) {
tracecmd_output_close(handle);
return NULL;
}
return handle;
}
struct tracecmd_output *tracecmd_create_file(const char *output_file,
int cpus, char * const *cpu_data_files)
{
return tracecmd_create_file_glob(output_file, cpus,
cpu_data_files, &all_event_list);
}
struct tracecmd_output *tracecmd_create_init_fd(int fd)
{
return create_file_fd(fd, NULL, NULL, NULL, &all_event_list);
}
struct tracecmd_output *
tracecmd_create_init_fd_glob(int fd, struct tracecmd_event_list *list)
{
return create_file_fd(fd, NULL, NULL, NULL, list);
}
struct tracecmd_output *
tracecmd_create_init_file_glob(const char *output_file,
struct tracecmd_event_list *list)
{
return create_file(output_file, NULL, NULL, NULL, list);
}
struct tracecmd_output *tracecmd_create_init_file(const char *output_file)
{
return create_file(output_file, NULL, NULL, NULL, &all_event_list);
}
struct tracecmd_output *tracecmd_create_init_file_override(const char *output_file,
const char *tracing_dir,
const char *kallsyms)
{
return create_file(output_file, NULL, tracing_dir, kallsyms, &all_event_list);
}
/**
* tracecmd_copy - copy the headers of one trace.dat file for another
* @ihandle: input handle of the trace.dat file to copy
* @file: the trace.dat file to create
*
* Reads the header information and creates a new trace data file
* with the same characteristics (events and all) and returns
* tracecmd_output handle to this new file.
*/
struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle,
const char *file)
{
struct tracecmd_output *handle;
handle = create_file(file, ihandle, NULL, NULL, &all_event_list);
if (!handle)
return NULL;
if (tracecmd_copy_headers(ihandle, handle->fd) < 0)
goto out_free;
/* The file is all ready to have cpu data attached */
return handle;
out_free:
tracecmd_output_close(handle);
return NULL;
}
|