| 1 |
/****************************************************************************** |
| 2 |
* tools/xentrace/xentrace.c |
| 3 |
* |
| 4 |
* Tool for collecting trace buffer data from Xen. |
| 5 |
* |
| 6 |
* Copyright (C) 2004 by Intel Research Cambridge |
| 7 |
* |
| 8 |
* Author: Mark Williamson, [email protected] |
| 9 |
* Date: February 2004 |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <time.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <sys/mman.h> |
| 16 |
#include <sys/stat.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <errno.h> |
| 21 |
#include <signal.h> |
| 22 |
#include <inttypes.h> |
| 23 |
#include <string.h> |
| 24 |
#include <getopt.h> |
| 25 |
#include <assert.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <poll.h> |
| 28 |
#include <sys/statvfs.h> |
| 29 |
|
| 30 |
#include <xen/xen.h> |
| 31 |
#include <xen/trace.h> |
| 32 |
|
| 33 |
#define XC_WANT_COMPAT_MAP_FOREIGN_API |
| 34 |
#include <xenevtchn.h> |
| 35 |
#include <xenctrl.h> |
| 36 |
|
| 37 |
#define PERROR(_m, _a...) \ |
| 38 |
do { \ |
| 39 |
int __saved_errno = errno; \ |
| 40 |
fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a , \ |
| 41 |
__saved_errno, strerror(__saved_errno)); \ |
| 42 |
errno = __saved_errno; \ |
| 43 |
} while (0) |
| 44 |
|
| 45 |
|
| 46 |
/***** Compile time configuration of defaults ********************************/ |
| 47 |
|
| 48 |
/* sleep for this long (milliseconds) between checking the trace buffers */ |
| 49 |
#define POLL_SLEEP_MILLIS 100 |
| 50 |
|
| 51 |
#define DEFAULT_TBUF_SIZE 32 |
| 52 |
/***** The code **************************************************************/ |
| 53 |
|
| 54 |
typedef struct settings_st { |
| 55 |
char *outfile; |
| 56 |
unsigned long poll_sleep; /* milliseconds to sleep between polls */ |
| 57 |
uint32_t evt_mask; |
| 58 |
char *cpu_mask_str; |
| 59 |
unsigned long tbuf_size; |
| 60 |
unsigned long disk_rsvd; |
| 61 |
unsigned long timeout; |
| 62 |
unsigned long memory_buffer; |
| 63 |
uint8_t discard:1, |
| 64 |
disable_tracing:1, |
| 65 |
start_disabled:1; |
| 66 |
} settings_t; |
| 67 |
|
| 68 |
struct t_struct { |
| 69 |
const struct t_info *t_info; /* Structure with information about individual buffers */ |
| 70 |
struct t_buf **meta; /* Pointers to trace buffer metadata */ |
| 71 |
unsigned char **data; /* Pointers to trace buffer data areas */ |
| 72 |
}; |
| 73 |
|
| 74 |
settings_t opts; |
| 75 |
|
| 76 |
int interrupted = 0; /* gets set if we get a SIGHUP */ |
| 77 |
|
| 78 |
static xc_interface *xc_handle; |
| 79 |
static xenevtchn_handle *xce_handle = NULL; |
| 80 |
static int virq_port = -1; |
| 81 |
static int outfd = 1; |
| 82 |
|
| 83 |
static void close_handler(int signal) |
| 84 |
{ |
| 85 |
interrupted = 1; |
| 86 |
} |
| 87 |
|
| 88 |
static struct { |
| 89 |
char * buf; |
| 90 |
unsigned long prod, cons, size; |
| 91 |
unsigned long pending_size, pending_prod; |
| 92 |
} membuf = { 0 }; |
| 93 |
|
| 94 |
#define MEMBUF_INDEX_RESET_THRESHOLD (1<<29) |
| 95 |
|
| 96 |
/* FIXME -- make a power of 2 so we can mask instead. */ |
| 97 |
#define MEMBUF_POINTER(_i) (membuf.buf + ((_i) % membuf.size)) |
| 98 |
#define MEMBUF_CONS_INCREMENT(_n) \ |
| 99 |
do { \ |
| 100 |
membuf.cons += (_n); \ |
| 101 |
} while(0) |
| 102 |
#define MEMBUF_PROD_SET(_x) \ |
| 103 |
do { \ |
| 104 |
if ( (_x) < membuf.prod ) { \ |
| 105 |
fprintf(stderr, "%s: INTERNAL_ERROR: prod %lu, trying to set to %lu!\n", \ |
| 106 |
__func__, membuf.prod, (unsigned long)(_x)); \ |
| 107 |
exit(1); \ |
| 108 |
} \ |
| 109 |
membuf.prod = (_x); \ |
| 110 |
if ( (_x) > MEMBUF_INDEX_RESET_THRESHOLD ) \ |
| 111 |
{ \ |
| 112 |
membuf.prod %= membuf.size; \ |
| 113 |
membuf.cons %= membuf.size; \ |
| 114 |
if( membuf.prod < membuf.cons ) \ |
| 115 |
membuf.prod += membuf.size; \ |
| 116 |
} \ |
| 117 |
} while(0) |
| 118 |
|
| 119 |
struct cpu_change_record { |
| 120 |
uint32_t header; |
| 121 |
struct { |
| 122 |
int cpu; |
| 123 |
unsigned window_size; |
| 124 |
} data; |
| 125 |
}; |
| 126 |
|
| 127 |
#define CPU_CHANGE_HEADER \ |
| 128 |
(TRC_TRACE_CPU_CHANGE \ |
| 129 |
| (((sizeof(struct cpu_change_record)/sizeof(uint32_t)) - 1) \ |
| 130 |
<< TRACE_EXTRA_SHIFT) ) |
| 131 |
|
| 132 |
void membuf_alloc(unsigned long size) |
| 133 |
{ |
| 134 |
membuf.buf = malloc(size); |
| 135 |
|
| 136 |
if(!membuf.buf) |
| 137 |
{ |
| 138 |
fprintf(stderr, "%s: Couldn't malloc %lu bytes!\n", |
| 139 |
__func__, size); |
| 140 |
exit(1); |
| 141 |
} |
| 142 |
|
| 143 |
membuf.prod = membuf.cons = 0; |
| 144 |
membuf.size = size; |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* Reserve a new window in the buffer. Move the 'consumer' forward size |
| 149 |
* bytes, re-adjusting the cpu window sizes as necessary, and insert a |
| 150 |
* cpu_change record. |
| 151 |
*/ |
| 152 |
void membuf_reserve_window(unsigned cpu, unsigned long window_size) |
| 153 |
{ |
| 154 |
struct cpu_change_record *rec; |
| 155 |
long need_to_consume, free, freed; |
| 156 |
int last_cpu = -1; |
| 157 |
|
| 158 |
if ( membuf.pending_size > 0 ) |
| 159 |
{ |
| 160 |
fprintf(stderr, "%s: INTERNAL_ERROR: pending_size %lu\n", |
| 161 |
__func__, membuf.pending_size); |
| 162 |
exit(1); |
| 163 |
} |
| 164 |
|
| 165 |
need_to_consume = window_size + sizeof(*rec); |
| 166 |
|
| 167 |
if ( window_size > membuf.size ) |
| 168 |
{ |
| 169 |
fprintf(stderr, "%s: reserve size %lu larger than buffer size %lu!\n", |
| 170 |
__func__, window_size, membuf.size); |
| 171 |
exit(1); |
| 172 |
} |
| 173 |
|
| 174 |
/* Subtract free space already in buffer. */ |
| 175 |
free = membuf.size - (membuf.prod - membuf.cons); |
| 176 |
if( need_to_consume < free) |
| 177 |
goto start_window; |
| 178 |
|
| 179 |
need_to_consume -= free; |
| 180 |
|
| 181 |
/* |
| 182 |
* "Free" up full windows until we have enough for this window. |
| 183 |
* It's a bit wasteful to throw away partial buffers, but the only |
| 184 |
* other option is to scan throught he buffer headers. Since the |
| 185 |
* common case is that it's going to be thrown away next anyway, I |
| 186 |
* think minimizing the overall impact is more important. |
| 187 |
*/ |
| 188 |
do { |
| 189 |
rec = (struct cpu_change_record *)MEMBUF_POINTER(membuf.cons); |
| 190 |
if( rec->header != CPU_CHANGE_HEADER ) |
| 191 |
{ |
| 192 |
fprintf(stderr, "%s: INTERNAL ERROR: no cpu_change record at consumer!\n", |
| 193 |
__func__); |
| 194 |
exit(EXIT_FAILURE); |
| 195 |
} |
| 196 |
|
| 197 |
freed = sizeof(*rec) + rec->data.window_size; |
| 198 |
|
| 199 |
if ( need_to_consume > 0 ) |
| 200 |
{ |
| 201 |
last_cpu = rec->data.cpu; |
| 202 |
MEMBUF_CONS_INCREMENT(freed); |
| 203 |
need_to_consume -= freed; |
| 204 |
} |
| 205 |
} while( need_to_consume > 0 ); |
| 206 |
|
| 207 |
/* For good tsc consistency, we need to start at a low-cpu buffer. Keep |
| 208 |
* skipping until the cpu goes down or stays the same. */ |
| 209 |
rec = (struct cpu_change_record *)MEMBUF_POINTER(membuf.cons); |
| 210 |
while ( rec->data.cpu > last_cpu ) |
| 211 |
{ |
| 212 |
last_cpu = rec->data.cpu; |
| 213 |
|
| 214 |
freed = sizeof(*rec) + rec->data.window_size; |
| 215 |
|
| 216 |
MEMBUF_CONS_INCREMENT(freed); |
| 217 |
rec = (struct cpu_change_record *)MEMBUF_POINTER(membuf.cons); |
| 218 |
} |
| 219 |
|
| 220 |
start_window: |
| 221 |
/* |
| 222 |
* Start writing "pending" data. Update prod once all this data is |
| 223 |
* written. |
| 224 |
*/ |
| 225 |
membuf.pending_prod = membuf.prod; |
| 226 |
membuf.pending_size = window_size; |
| 227 |
|
| 228 |
rec = (struct cpu_change_record *)MEMBUF_POINTER(membuf.pending_prod); |
| 229 |
|
| 230 |
rec->header = CPU_CHANGE_HEADER; |
| 231 |
rec->data.cpu = cpu; |
| 232 |
rec->data.window_size = window_size; |
| 233 |
|
| 234 |
membuf.pending_prod += sizeof(*rec); |
| 235 |
} |
| 236 |
|
| 237 |
void membuf_write(void *start, unsigned long size) { |
| 238 |
char * p; |
| 239 |
unsigned long wsize; |
| 240 |
|
| 241 |
if( (membuf.size - (membuf.prod - membuf.cons)) < size ) |
| 242 |
{ |
| 243 |
fprintf(stderr, "%s: INTERNAL ERROR: need %lu bytes, only have %lu!\n", |
| 244 |
__func__, size, membuf.prod - membuf.cons); |
| 245 |
exit(1); |
| 246 |
} |
| 247 |
|
| 248 |
if( size > membuf.pending_size ) |
| 249 |
{ |
| 250 |
fprintf(stderr, "%s: INTERNAL ERROR: size %lu, pending %lu!\n", |
| 251 |
__func__, size, membuf.pending_size); |
| 252 |
exit(1); |
| 253 |
} |
| 254 |
|
| 255 |
wsize = size; |
| 256 |
p = MEMBUF_POINTER(membuf.pending_prod); |
| 257 |
|
| 258 |
/* If the buffer overlaps the "wrap", do an extra write */ |
| 259 |
if ( p + size > membuf.buf + membuf.size ) |
| 260 |
{ |
| 261 |
int usize = ( membuf.buf + membuf.size ) - p; |
| 262 |
|
| 263 |
memcpy(p, start, usize); |
| 264 |
|
| 265 |
start += usize; |
| 266 |
wsize -= usize; |
| 267 |
p = membuf.buf; |
| 268 |
} |
| 269 |
|
| 270 |
memcpy(p, start, wsize); |
| 271 |
|
| 272 |
membuf.pending_prod += size; |
| 273 |
membuf.pending_size -= size; |
| 274 |
|
| 275 |
if ( membuf.pending_size == 0 ) |
| 276 |
{ |
| 277 |
MEMBUF_PROD_SET(membuf.pending_prod); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void membuf_dump(void) { |
| 282 |
/* Dump circular memory buffer */ |
| 283 |
int cons, prod, wsize, written; |
| 284 |
char * wstart; |
| 285 |
|
| 286 |
fprintf(stderr, "Dumping memory buffer.\n"); |
| 287 |
|
| 288 |
cons = membuf.cons % membuf.size; |
| 289 |
prod = membuf.prod % membuf.size; |
| 290 |
|
| 291 |
if(prod > cons) |
| 292 |
{ |
| 293 |
/* Write in one go */ |
| 294 |
wstart = membuf.buf + cons; |
| 295 |
wsize = prod - cons; |
| 296 |
|
| 297 |
written = write(outfd, wstart, wsize); |
| 298 |
if ( written != wsize ) |
| 299 |
goto fail; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
/* Write in two pieces: cons->end, beginning->prod. */ |
| 304 |
wstart = membuf.buf + cons; |
| 305 |
wsize = membuf.size - cons; |
| 306 |
|
| 307 |
written = write(outfd, wstart, wsize); |
| 308 |
if ( written != wsize ) |
| 309 |
{ |
| 310 |
fprintf(stderr, "Write failed! (size %d, returned %d)\n", |
| 311 |
wsize, written); |
| 312 |
goto fail; |
| 313 |
} |
| 314 |
|
| 315 |
wstart = membuf.buf; |
| 316 |
wsize = prod; |
| 317 |
|
| 318 |
written = write(outfd, wstart, wsize); |
| 319 |
if ( written != wsize ) |
| 320 |
{ |
| 321 |
fprintf(stderr, "Write failed! (size %d, returned %d)\n", |
| 322 |
wsize, written); |
| 323 |
goto fail; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
membuf.cons = membuf.prod = 0; |
| 328 |
|
| 329 |
return; |
| 330 |
fail: |
| 331 |
exit(1); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* write_buffer - write a section of the trace buffer |
| 337 |
* @cpu - source buffer CPU ID |
| 338 |
* @start |
| 339 |
* @size - size of write (may be less than total window size) |
| 340 |
* @total_size - total size of the window (0 on 2nd write of wrapped windows) |
| 341 |
* @out - output stream |
| 342 |
* |
| 343 |
* Outputs the trace buffer to a filestream, prepending the CPU and size |
| 344 |
* of the buffer write. |
| 345 |
*/ |
| 346 |
static void write_buffer(unsigned int cpu, unsigned char *start, int size, |
| 347 |
int total_size) |
| 348 |
{ |
| 349 |
struct statvfs stat; |
| 350 |
size_t written = 0; |
| 351 |
|
| 352 |
if ( opts.memory_buffer == 0 && opts.disk_rsvd != 0 ) |
| 353 |
{ |
| 354 |
unsigned long long freespace; |
| 355 |
|
| 356 |
/* Check that filesystem has enough space. */ |
| 357 |
if ( fstatvfs (outfd, &stat) ) |
| 358 |
{ |
| 359 |
fprintf(stderr, "Statfs failed!\n"); |
| 360 |
goto fail; |
| 361 |
} |
| 362 |
|
| 363 |
freespace = stat.f_frsize * (unsigned long long)stat.f_bfree; |
| 364 |
|
| 365 |
if ( total_size ) |
| 366 |
freespace -= total_size; |
| 367 |
else |
| 368 |
freespace -= size; |
| 369 |
|
| 370 |
freespace >>= 20; /* Convert to MB */ |
| 371 |
|
| 372 |
if ( freespace <= opts.disk_rsvd ) |
| 373 |
{ |
| 374 |
fprintf(stderr, "Disk space limit reached (free space: %lluMB, limit: %luMB).\n", freespace, opts.disk_rsvd); |
| 375 |
exit (EXIT_FAILURE); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
/* Write a CPU_BUF record on each buffer "window" written. Wrapped |
| 380 |
* windows may involve two writes, so only write the record on the |
| 381 |
* first write. */ |
| 382 |
if ( total_size != 0 ) |
| 383 |
{ |
| 384 |
if ( opts.memory_buffer ) |
| 385 |
{ |
| 386 |
membuf_reserve_window(cpu, total_size); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
struct cpu_change_record rec; |
| 391 |
|
| 392 |
rec.header = CPU_CHANGE_HEADER; |
| 393 |
rec.data.cpu = cpu; |
| 394 |
rec.data.window_size = total_size; |
| 395 |
|
| 396 |
written = write(outfd, &rec, sizeof(rec)); |
| 397 |
if ( written != sizeof(rec) ) |
| 398 |
{ |
| 399 |
fprintf(stderr, "Cannot write cpu change (write returned %zd)\n", |
| 400 |
written); |
| 401 |
goto fail; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if ( opts.memory_buffer ) |
| 407 |
{ |
| 408 |
membuf_write(start, size); |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
written = write(outfd, start, size); |
| 413 |
if ( written != size ) |
| 414 |
{ |
| 415 |
fprintf(stderr, "Write failed! (size %d, returned %zd)\n", |
| 416 |
size, written); |
| 417 |
goto fail; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
return; |
| 422 |
|
| 423 |
fail: |
| 424 |
PERROR("Failed to write trace data"); |
| 425 |
exit(EXIT_FAILURE); |
| 426 |
} |
| 427 |
|
| 428 |
static void disable_tbufs(void) |
| 429 |
{ |
| 430 |
xc_interface *xc_handle = xc_interface_open(0,0,0); |
| 431 |
|
| 432 |
if ( !xc_handle ) |
| 433 |
{ |
| 434 |
perror("Couldn't open xc handle to disable tbufs."); |
| 435 |
return; |
| 436 |
} |
| 437 |
|
| 438 |
if ( xc_tbuf_disable(xc_handle) != 0 ) |
| 439 |
{ |
| 440 |
perror("Couldn't disable trace buffers"); |
| 441 |
} |
| 442 |
|
| 443 |
xc_interface_close(xc_handle); |
| 444 |
} |
| 445 |
|
| 446 |
static void get_tbufs(unsigned long *mfn, unsigned long *size) |
| 447 |
{ |
| 448 |
int ret; |
| 449 |
|
| 450 |
if(!opts.tbuf_size) |
| 451 |
opts.tbuf_size = DEFAULT_TBUF_SIZE; |
| 452 |
|
| 453 |
ret = xc_tbuf_enable(xc_handle, opts.tbuf_size, mfn, size); |
| 454 |
|
| 455 |
if ( ret != 0 ) |
| 456 |
{ |
| 457 |
perror("Couldn't enable trace buffers"); |
| 458 |
exit(1); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* map_tbufs - memory map Xen trace buffers into user space |
| 464 |
* @tbufs_mfn: mfn of the trace buffers |
| 465 |
* @num: number of trace buffers to map |
| 466 |
* @size: size of each trace buffer |
| 467 |
* |
| 468 |
* Maps the Xen trace buffers them into process address space. |
| 469 |
*/ |
| 470 |
static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num, |
| 471 |
unsigned long tinfo_size) |
| 472 |
{ |
| 473 |
static struct t_struct tbufs = { 0 }; |
| 474 |
int i; |
| 475 |
|
| 476 |
/* Map t_info metadata structure */ |
| 477 |
tbufs.t_info = xc_map_foreign_range(xc_handle, DOMID_XEN, tinfo_size, |
| 478 |
PROT_READ, tbufs_mfn); |
| 479 |
|
| 480 |
if ( tbufs.t_info == 0 ) |
| 481 |
{ |
| 482 |
PERROR("Failed to mmap trace buffers"); |
| 483 |
exit(EXIT_FAILURE); |
| 484 |
} |
| 485 |
|
| 486 |
if ( tbufs.t_info->tbuf_size == 0 ) |
| 487 |
{ |
| 488 |
fprintf(stderr, "%s: tbuf_size 0!\n", __func__); |
| 489 |
exit(EXIT_FAILURE); |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
* Map per-cpu buffers. NB that if a cpu is offline, it may have |
| 494 |
* no trace buffers. In this case, the respective mfn_offset will |
| 495 |
* be 0, and the index should be ignored. |
| 496 |
*/ |
| 497 |
tbufs.meta = (struct t_buf **)calloc(num, sizeof(struct t_buf *)); |
| 498 |
tbufs.data = (unsigned char **)calloc(num, sizeof(unsigned char *)); |
| 499 |
if ( tbufs.meta == NULL || tbufs.data == NULL ) |
| 500 |
{ |
| 501 |
PERROR( "Failed to allocate memory for buffer pointers\n"); |
| 502 |
exit(EXIT_FAILURE); |
| 503 |
} |
| 504 |
|
| 505 |
for(i=0; i<num; i++) |
| 506 |
{ |
| 507 |
const uint32_t *mfn_list; |
| 508 |
int j; |
| 509 |
xen_pfn_t pfn_list[tbufs.t_info->tbuf_size]; |
| 510 |
|
| 511 |
if ( !tbufs.t_info->mfn_offset[i] ) |
| 512 |
continue; |
| 513 |
|
| 514 |
mfn_list = (const uint32_t *)tbufs.t_info + tbufs.t_info->mfn_offset[i]; |
| 515 |
for ( j=0; j<tbufs.t_info->tbuf_size; j++) |
| 516 |
pfn_list[j] = (xen_pfn_t)mfn_list[j]; |
| 517 |
|
| 518 |
tbufs.meta[i] = xc_map_foreign_pages(xc_handle, DOMID_XEN, |
| 519 |
PROT_READ | PROT_WRITE, |
| 520 |
pfn_list, |
| 521 |
tbufs.t_info->tbuf_size); |
| 522 |
if ( tbufs.meta[i] == NULL ) |
| 523 |
{ |
| 524 |
PERROR("Failed to map cpu buffer!"); |
| 525 |
exit(EXIT_FAILURE); |
| 526 |
} |
| 527 |
tbufs.data[i] = (unsigned char *)(tbufs.meta[i]+1); |
| 528 |
} |
| 529 |
|
| 530 |
return &tbufs; |
| 531 |
} |
| 532 |
|
| 533 |
void print_cpu_mask(xc_cpumap_t map) |
| 534 |
{ |
| 535 |
unsigned int v, had_printed = 0; |
| 536 |
int i; |
| 537 |
|
| 538 |
fprintf(stderr, "change cpumask to 0x"); |
| 539 |
|
| 540 |
for ( i = xc_get_cpumap_size(xc_handle); i >= 0; i-- ) |
| 541 |
{ |
| 542 |
v = map[i]; |
| 543 |
if ( v || had_printed || !i ) { |
| 544 |
if (had_printed) |
| 545 |
fprintf(stderr,"%02x", v); |
| 546 |
else |
| 547 |
fprintf(stderr,"%x", v); |
| 548 |
had_printed = 1; |
| 549 |
} |
| 550 |
} |
| 551 |
fprintf(stderr, "\n"); |
| 552 |
} |
| 553 |
|
| 554 |
static int set_cpu_mask(xc_cpumap_t map) |
| 555 |
{ |
| 556 |
int ret = xc_tbuf_set_cpu_mask(xc_handle, map); |
| 557 |
|
| 558 |
if ( ret == 0 ) |
| 559 |
{ |
| 560 |
print_cpu_mask(map); |
| 561 |
return 0; |
| 562 |
} |
| 563 |
PERROR("Failure to get trace buffer pointer from Xen and set the new mask"); |
| 564 |
return EXIT_FAILURE; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* set_mask - set the event mask in HV |
| 569 |
* @mask: the new mask |
| 570 |
* @type: the new mask type,0-event mask, 1-cpu mask |
| 571 |
* |
| 572 |
*/ |
| 573 |
static void set_evt_mask(uint32_t mask) |
| 574 |
{ |
| 575 |
int ret = 0; |
| 576 |
|
| 577 |
ret = xc_tbuf_set_evt_mask(xc_handle, mask); |
| 578 |
fprintf(stderr, "change evtmask to 0x%x\n", mask); |
| 579 |
|
| 580 |
if ( ret != 0 ) |
| 581 |
{ |
| 582 |
PERROR("Failure to get trace buffer pointer from Xen and set the new mask"); |
| 583 |
exit(EXIT_FAILURE); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* get_num_cpus - get the number of logical CPUs |
| 589 |
*/ |
| 590 |
static unsigned int get_num_cpus(void) |
| 591 |
{ |
| 592 |
xc_physinfo_t physinfo; |
| 593 |
int ret; |
| 594 |
|
| 595 |
ret = xc_physinfo(xc_handle, &physinfo); |
| 596 |
|
| 597 |
if ( ret != 0 ) |
| 598 |
{ |
| 599 |
PERROR("Failure to get logical CPU count from Xen"); |
| 600 |
exit(EXIT_FAILURE); |
| 601 |
} |
| 602 |
|
| 603 |
return physinfo.max_cpu_id + 1; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* event_init - setup to receive the VIRQ_TBUF event |
| 608 |
*/ |
| 609 |
static void event_init(void) |
| 610 |
{ |
| 611 |
int rc; |
| 612 |
|
| 613 |
xce_handle = xenevtchn_open(NULL, 0); |
| 614 |
if (xce_handle == NULL) { |
| 615 |
perror("event channel open"); |
| 616 |
exit(EXIT_FAILURE); |
| 617 |
} |
| 618 |
|
| 619 |
rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF); |
| 620 |
if (rc == -1) { |
| 621 |
PERROR("failed to bind to VIRQ port"); |
| 622 |
exit(EXIT_FAILURE); |
| 623 |
} |
| 624 |
virq_port = rc; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* wait_for_event_or_timeout - sleep for the specified number of milliseconds, |
| 629 |
* or until an VIRQ_TBUF event occurs |
| 630 |
*/ |
| 631 |
static void wait_for_event_or_timeout(unsigned long milliseconds) |
| 632 |
{ |
| 633 |
int rc; |
| 634 |
struct pollfd fd = { .fd = xenevtchn_fd(xce_handle), |
| 635 |
.events = POLLIN | POLLERR }; |
| 636 |
int port; |
| 637 |
|
| 638 |
rc = poll(&fd, 1, milliseconds); |
| 639 |
if (rc == -1) { |
| 640 |
if (errno == EINTR) |
| 641 |
return; |
| 642 |
PERROR("poll exitted with an error"); |
| 643 |
exit(EXIT_FAILURE); |
| 644 |
} |
| 645 |
|
| 646 |
if (rc == 1) { |
| 647 |
port = xenevtchn_pending(xce_handle); |
| 648 |
if (port == -1) { |
| 649 |
PERROR("failed to read port from evtchn"); |
| 650 |
exit(EXIT_FAILURE); |
| 651 |
} |
| 652 |
if (port != virq_port) { |
| 653 |
fprintf(stderr, |
| 654 |
"unexpected port returned from evtchn (got %d vs expected %d)\n", |
| 655 |
port, virq_port); |
| 656 |
exit(EXIT_FAILURE); |
| 657 |
} |
| 658 |
rc = xenevtchn_unmask(xce_handle, port); |
| 659 |
if (rc == -1) { |
| 660 |
PERROR("failed to write port to evtchn"); |
| 661 |
exit(EXIT_FAILURE); |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
/** |
| 668 |
* monitor_tbufs - monitor the contents of tbufs and output to a file |
| 669 |
* @logfile: the FILE * representing the file to log to |
| 670 |
*/ |
| 671 |
static void monitor_tbufs(void) |
| 672 |
{ |
| 673 |
int i; |
| 674 |
|
| 675 |
struct t_struct *tbufs; /* Pointer to hypervisor maps */ |
| 676 |
struct t_buf **meta; /* pointers to the trace buffer metadata */ |
| 677 |
unsigned char **data; /* pointers to the trace buffer data areas |
| 678 |
* where they are mapped into user space. */ |
| 679 |
unsigned long tbufs_mfn; /* mfn of the tbufs */ |
| 680 |
unsigned int num; /* number of trace buffers / logical CPUS */ |
| 681 |
unsigned long tinfo_size; /* size of t_info metadata map */ |
| 682 |
unsigned long size; /* size of a single trace buffer */ |
| 683 |
|
| 684 |
unsigned long data_size; |
| 685 |
|
| 686 |
int last_read = 1; |
| 687 |
|
| 688 |
/* prepare to listen for VIRQ_TBUF */ |
| 689 |
event_init(); |
| 690 |
|
| 691 |
/* get number of logical CPUs (and therefore number of trace buffers) */ |
| 692 |
num = get_num_cpus(); |
| 693 |
|
| 694 |
/* setup access to trace buffers */ |
| 695 |
get_tbufs(&tbufs_mfn, &tinfo_size); |
| 696 |
|
| 697 |
if ( opts.start_disabled ) |
| 698 |
disable_tbufs(); |
| 699 |
|
| 700 |
tbufs = map_tbufs(tbufs_mfn, num, tinfo_size); |
| 701 |
|
| 702 |
size = tbufs->t_info->tbuf_size * XC_PAGE_SIZE; |
| 703 |
|
| 704 |
data_size = size - sizeof(struct t_buf); |
| 705 |
|
| 706 |
meta = tbufs->meta; |
| 707 |
data = tbufs->data; |
| 708 |
|
| 709 |
if ( opts.discard ) |
| 710 |
for ( i = 0; i < num; i++ ) |
| 711 |
if ( meta[i] ) |
| 712 |
meta[i]->cons = meta[i]->prod; |
| 713 |
|
| 714 |
/* now, scan buffers for events */ |
| 715 |
while ( 1 ) |
| 716 |
{ |
| 717 |
for ( i = 0; i < num; i++ ) |
| 718 |
{ |
| 719 |
unsigned long start_offset, end_offset, window_size, cons, prod; |
| 720 |
|
| 721 |
if ( !meta[i] ) |
| 722 |
continue; |
| 723 |
|
| 724 |
/* Read window information only once. */ |
| 725 |
cons = meta[i]->cons; |
| 726 |
prod = meta[i]->prod; |
| 727 |
xen_rmb(); /* read prod, then read item. */ |
| 728 |
|
| 729 |
if ( cons == prod ) |
| 730 |
continue; |
| 731 |
|
| 732 |
assert(cons < 2*data_size); |
| 733 |
assert(prod < 2*data_size); |
| 734 |
|
| 735 |
// NB: if (prod<cons), then (prod-cons)%data_size will not yield |
| 736 |
// the correct answer because data_size is not a power of 2. |
| 737 |
if ( prod < cons ) |
| 738 |
window_size = (prod + 2*data_size) - cons; |
| 739 |
else |
| 740 |
window_size = prod - cons; |
| 741 |
assert(window_size > 0); |
| 742 |
assert(window_size <= data_size); |
| 743 |
|
| 744 |
start_offset = cons % data_size; |
| 745 |
end_offset = prod % data_size; |
| 746 |
|
| 747 |
if ( end_offset > start_offset ) |
| 748 |
{ |
| 749 |
/* If window does not wrap, write in one big chunk */ |
| 750 |
write_buffer(i, data[i]+start_offset, |
| 751 |
window_size, |
| 752 |
window_size); |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
/* If wrapped, write in two chunks: |
| 757 |
* - first, start to the end of the buffer |
| 758 |
* - second, start of buffer to end of window |
| 759 |
*/ |
| 760 |
write_buffer(i, data[i] + start_offset, |
| 761 |
data_size - start_offset, |
| 762 |
window_size); |
| 763 |
write_buffer(i, data[i], |
| 764 |
end_offset, |
| 765 |
0); |
| 766 |
} |
| 767 |
|
| 768 |
xen_mb(); /* read buffer, then update cons. */ |
| 769 |
meta[i]->cons = prod; |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
if ( interrupted ) |
| 774 |
{ |
| 775 |
if ( last_read ) |
| 776 |
{ |
| 777 |
/* Disable tracing, then read through all the buffers one last time */ |
| 778 |
if ( opts.disable_tracing ) |
| 779 |
disable_tbufs(); |
| 780 |
last_read = 0; |
| 781 |
continue; |
| 782 |
} |
| 783 |
else |
| 784 |
break; |
| 785 |
} |
| 786 |
|
| 787 |
wait_for_event_or_timeout(opts.poll_sleep); |
| 788 |
} |
| 789 |
|
| 790 |
if ( opts.memory_buffer ) |
| 791 |
membuf_dump(); |
| 792 |
|
| 793 |
/* cleanup */ |
| 794 |
free(meta); |
| 795 |
free(data); |
| 796 |
/* don't need to munmap - cleanup is automatic */ |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
/****************************************************************************** |
| 801 |
* Command line handling |
| 802 |
*****************************************************************************/ |
| 803 |
|
| 804 |
#define xstr(x) str(x) |
| 805 |
#define str(x) #x |
| 806 |
|
| 807 |
const char *program_version = "xentrace v1.2"; |
| 808 |
const char *program_bug_address = "<[email protected]>"; |
| 809 |
|
| 810 |
static void usage(int status) |
| 811 |
{ |
| 812 |
#define USAGE_STR \ |
| 813 |
"Usage: xentrace [OPTION...] [output file]\n" \ |
| 814 |
"Tool to capture Xen trace buffer data\n" \ |
| 815 |
"\n" \ |
| 816 |
" -c, --cpu-mask=c Set cpu-mask, using either hex, CPU ranges, or\n" \ |
| 817 |
" for all CPUs\n" \ |
| 818 |
" -e, --evt-mask=e Set evt-mask\n" \ |
| 819 |
" -s, --poll-sleep=p Set sleep time, p, in milliseconds between\n" \ |
| 820 |
" polling the trace buffer for new data\n" \ |
| 821 |
" (default " xstr(POLL_SLEEP_MILLIS) ").\n" \ |
| 822 |
" -S, --trace-buf-size=N Set trace buffer size in pages (default " \ |
| 823 |
xstr(DEFAULT_TBUF_SIZE) ").\n" \ |
| 824 |
" N.B. that the trace buffer cannot be resized.\n" \ |
| 825 |
" if it has already been set this boot cycle,\n" \ |
| 826 |
" this argument will be ignored.\n" \ |
| 827 |
" -D --discard-buffers Discard all records currently in the trace\n" \ |
| 828 |
" buffers before beginning.\n" \ |
| 829 |
" -x --dont-disable-tracing\n" \ |
| 830 |
" By default, xentrace will disable tracing when\n" \ |
| 831 |
" it exits. Selecting this option will tell it to\n" \ |
| 832 |
" keep tracing on. Traces will be collected in\n" \ |
| 833 |
" Xen's trace buffers until they become full.\n" \ |
| 834 |
" -X --start-disabled Setup trace buffers and listen, but don't enable\n" \ |
| 835 |
" tracing. (Useful if tracing will be enabled by\n" \ |
| 836 |
" else.)\n" \ |
| 837 |
" -T --time-interval=s Run xentrace for s seconds and quit.\n" \ |
| 838 |
" -?, --help Show this message\n" \ |
| 839 |
" -V, --version Print program version\n" \ |
| 840 |
" -M, --memory-buffer=b Copy trace records to a circular memory buffer.\n" \ |
| 841 |
" Dump to file on exit.\n" \ |
| 842 |
" -r --reserve-disk-space=n Before writing trace records to disk, check to see\n" \ |
| 843 |
" that after the write there will be at least n space\n" \ |
| 844 |
" left on the disk.\n" \ |
| 845 |
"\n" \ |
| 846 |
"This tool is used to capture trace buffer data from Xen. The\n" \ |
| 847 |
"data is output in a binary format, in the following order:\n" \ |
| 848 |
"\n" \ |
| 849 |
" CPU(uint) TSC(uint64_t) EVENT(uint32_t) D1 D2 D3 D4 D5 (all uint32_t)\n" \ |
| 850 |
"\n" \ |
| 851 |
"The output should be parsed using the tool xenalyze,\n" \ |
| 852 |
"which can produce human-readable output in ASCII format.\n" |
| 853 |
|
| 854 |
printf(USAGE_STR); |
| 855 |
printf("\nReport bugs to %s\n", program_bug_address); |
| 856 |
|
| 857 |
exit(status); |
| 858 |
} |
| 859 |
|
| 860 |
/* convert the argument string pointed to by arg to a long int representation, |
| 861 |
* including suffixes such as 'M' and 'k'. */ |
| 862 |
#define MB (1024*1024) |
| 863 |
#define KB (1024) |
| 864 |
long sargtol(const char *restrict arg, int base) |
| 865 |
{ |
| 866 |
char *endp; |
| 867 |
long val; |
| 868 |
|
| 869 |
errno = 0; |
| 870 |
val = strtol(arg, &endp, base); |
| 871 |
|
| 872 |
if ( errno != 0 ) |
| 873 |
{ |
| 874 |
fprintf(stderr, "Invalid option argument: %s\n", arg); |
| 875 |
fprintf(stderr, "Error: %s\n\n", strerror(errno)); |
| 876 |
usage(EXIT_FAILURE); |
| 877 |
} |
| 878 |
else if (endp == arg) |
| 879 |
{ |
| 880 |
goto invalid; |
| 881 |
} |
| 882 |
|
| 883 |
switch(*endp) |
| 884 |
{ |
| 885 |
case '\0': |
| 886 |
break; |
| 887 |
case 'M': |
| 888 |
val *= MB; |
| 889 |
break; |
| 890 |
case 'K': |
| 891 |
case 'k': |
| 892 |
val *= KB; |
| 893 |
break; |
| 894 |
default: |
| 895 |
fprintf(stderr, "Unknown suffix %c\n", *endp); |
| 896 |
exit(1); |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
return val; |
| 901 |
|
| 902 |
invalid: |
| 903 |
fprintf(stderr, "Invalid option argument: %s\n\n", arg); |
| 904 |
usage(EXIT_FAILURE); |
| 905 |
return 0; /* not actually reached */ |
| 906 |
} |
| 907 |
|
| 908 |
/* convert the argument string pointed to by arg to a long int representation */ |
| 909 |
static long argtol(const char *restrict arg, int base) |
| 910 |
{ |
| 911 |
char *endp; |
| 912 |
long val; |
| 913 |
|
| 914 |
errno = 0; |
| 915 |
val = strtol(arg, &endp, base); |
| 916 |
|
| 917 |
if (errno != 0) { |
| 918 |
fprintf(stderr, "Invalid option argument: %s\n", arg); |
| 919 |
fprintf(stderr, "Error: %s\n\n", strerror(errno)); |
| 920 |
usage(EXIT_FAILURE); |
| 921 |
} else if (endp == arg || *endp != '\0') { |
| 922 |
fprintf(stderr, "Invalid option argument: %s\n\n", arg); |
| 923 |
usage(EXIT_FAILURE); |
| 924 |
} |
| 925 |
|
| 926 |
return val; |
| 927 |
} |
| 928 |
|
| 929 |
static int parse_evtmask(char *arg) |
| 930 |
{ |
| 931 |
/* search filtering class */ |
| 932 |
if (strcmp(arg, "gen") == 0){ |
| 933 |
opts.evt_mask |= TRC_GEN; |
| 934 |
} else if(strcmp(arg, "sched") == 0){ |
| 935 |
opts.evt_mask |= TRC_SCHED; |
| 936 |
} else if(strcmp(arg, "dom0op") == 0){ |
| 937 |
opts.evt_mask |= TRC_DOM0OP; |
| 938 |
} else if(strcmp(arg, "hvm") == 0){ |
| 939 |
opts.evt_mask |= TRC_HVM; |
| 940 |
} else if(strcmp(arg, "all") == 0){ |
| 941 |
opts.evt_mask |= TRC_ALL; |
| 942 |
} else { |
| 943 |
opts.evt_mask = argtol(arg, 0); |
| 944 |
} |
| 945 |
|
| 946 |
return 0; |
| 947 |
} |
| 948 |
|
| 949 |
#define ZERO_DIGIT '0' |
| 950 |
|
| 951 |
#define is_terminator(c) ((c)=='\0' || (c)==',') |
| 952 |
|
| 953 |
static int parse_cpumask_range(const char *mask_str, xc_cpumap_t map) |
| 954 |
{ |
| 955 |
unsigned int a, b; |
| 956 |
int nmaskbits; |
| 957 |
unsigned char c; |
| 958 |
int in_range; |
| 959 |
const char *s; |
| 960 |
|
| 961 |
nmaskbits = xc_get_max_cpus(xc_handle); |
| 962 |
if ( nmaskbits <= 0 ) |
| 963 |
{ |
| 964 |
fprintf(stderr, "Failed to get max number of CPUs! rc: %d\n", nmaskbits); |
| 965 |
return EXIT_FAILURE; |
| 966 |
} |
| 967 |
|
| 968 |
c = 0; |
| 969 |
s = mask_str; |
| 970 |
do { |
| 971 |
in_range = 0; |
| 972 |
a = b = 0; |
| 973 |
|
| 974 |
/* Process until we find a range terminator */ |
| 975 |
for ( c=*s++; !is_terminator(c); c=*s++ ) |
| 976 |
{ |
| 977 |
if ( c == '-' ) |
| 978 |
{ |
| 979 |
if ( in_range ) |
| 980 |
goto err_out; |
| 981 |
b = 0; |
| 982 |
in_range = 1; |
| 983 |
continue; |
| 984 |
} |
| 985 |
|
| 986 |
if ( !isdigit(c) ) |
| 987 |
{ |
| 988 |
fprintf(stderr, "Invalid character in cpumask: %s\n", mask_str); |
| 989 |
goto err_out; |
| 990 |
} |
| 991 |
|
| 992 |
b = b * 10 + (c - ZERO_DIGIT); |
| 993 |
if ( !in_range ) |
| 994 |
a = b; |
| 995 |
} |
| 996 |
|
| 997 |
/* Syntax: <digit>-[,] - expand to number of CPUs. */ |
| 998 |
if ( b == 0 && in_range ) |
| 999 |
b = nmaskbits-1; |
| 1000 |
|
| 1001 |
if ( a > b ) |
| 1002 |
{ |
| 1003 |
fprintf(stderr, "Wrong order of %d and %d\n", a, b); |
| 1004 |
goto err_out; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if ( b >= nmaskbits ) |
| 1008 |
{ |
| 1009 |
fprintf(stderr, "Specified higher value then there are CPUS!\n"); |
| 1010 |
goto err_out; |
| 1011 |
} |
| 1012 |
|
| 1013 |
while ( a <= b ) |
| 1014 |
{ |
| 1015 |
xc_cpumap_setcpu(a, map); |
| 1016 |
a++; |
| 1017 |
} |
| 1018 |
} while ( c ); |
| 1019 |
|
| 1020 |
return 0; |
| 1021 |
err_out: |
| 1022 |
errno = EINVAL; |
| 1023 |
return EXIT_FAILURE; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/** |
| 1027 |
* Figure out which of the CPU types the user has provided - either the hex |
| 1028 |
* variant, the cpu-list, or 'all'. Once done set the CPU mask. |
| 1029 |
*/ |
| 1030 |
static int parse_cpu_mask(void) |
| 1031 |
{ |
| 1032 |
int i, ret = EXIT_FAILURE; |
| 1033 |
xc_cpumap_t map; |
| 1034 |
|
| 1035 |
map = xc_cpumap_alloc(xc_handle); |
| 1036 |
if ( !map ) |
| 1037 |
goto out; |
| 1038 |
|
| 1039 |
if ( strlen(opts.cpu_mask_str) < 1 ) |
| 1040 |
{ |
| 1041 |
errno = ENOSPC; |
| 1042 |
goto out; |
| 1043 |
} |
| 1044 |
|
| 1045 |
ret = 0; |
| 1046 |
if ( strncmp("0x", opts.cpu_mask_str, 2) == 0 ) |
| 1047 |
{ |
| 1048 |
uint32_t v; |
| 1049 |
|
| 1050 |
v = argtol(opts.cpu_mask_str, 0); |
| 1051 |
/* |
| 1052 |
* If mask is set, copy the bits out of it. This still works for |
| 1053 |
* systems with more than 32 cpus, as the shift will just shift |
| 1054 |
* mask down to zero. |
| 1055 |
*/ |
| 1056 |
for ( i = 0; i < sizeof(uint32_t); i++ ) |
| 1057 |
map[i] = (v >> (i * 8)) & 0xff; |
| 1058 |
} |
| 1059 |
else if ( strcmp("all", opts.cpu_mask_str) == 0 ) |
| 1060 |
{ |
| 1061 |
for ( i = 0; i < xc_get_cpumap_size(xc_handle); i++ ) |
| 1062 |
map[i] = 0xff; |
| 1063 |
} |
| 1064 |
else |
| 1065 |
ret = parse_cpumask_range(opts.cpu_mask_str, map); |
| 1066 |
|
| 1067 |
if ( !ret ) |
| 1068 |
ret = set_cpu_mask(map); |
| 1069 |
out: |
| 1070 |
/* We don't use them pass this point. */ |
| 1071 |
free(map); |
| 1072 |
free(opts.cpu_mask_str); |
| 1073 |
opts.cpu_mask_str = NULL; |
| 1074 |
return ret; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* parse command line arguments */ |
| 1078 |
static void parse_args(int argc, char **argv) |
| 1079 |
{ |
| 1080 |
int option; |
| 1081 |
static struct option long_options[] = { |
| 1082 |
{ "log-thresh", required_argument, 0, 't' }, |
| 1083 |
{ "poll-sleep", required_argument, 0, 's' }, |
| 1084 |
{ "cpu-mask", required_argument, 0, 'c' }, |
| 1085 |
{ "evt-mask", required_argument, 0, 'e' }, |
| 1086 |
{ "trace-buf-size", required_argument, 0, 'S' }, |
| 1087 |
{ "reserve-disk-space", required_argument, 0, 'r' }, |
| 1088 |
{ "time-interval", required_argument, 0, 'T' }, |
| 1089 |
{ "memory-buffer", required_argument, 0, 'M' }, |
| 1090 |
{ "discard-buffers", no_argument, 0, 'D' }, |
| 1091 |
{ "dont-disable-tracing", no_argument, 0, 'x' }, |
| 1092 |
{ "start-disabled", no_argument, 0, 'X' }, |
| 1093 |
{ "help", no_argument, 0, 'h' }, |
| 1094 |
{ "version", no_argument, 0, 'V' }, |
| 1095 |
{ 0, 0, 0, 0 } |
| 1096 |
}; |
| 1097 |
|
| 1098 |
while ( (option = getopt_long(argc, argv, "t:s:c:e:S:r:T:M:DxX?V", |
| 1099 |
long_options, NULL)) != -1) |
| 1100 |
{ |
| 1101 |
switch ( option ) |
| 1102 |
{ |
| 1103 |
case 's': /* set sleep time (given in milliseconds) */ |
| 1104 |
opts.poll_sleep = argtol(optarg, 0); |
| 1105 |
break; |
| 1106 |
|
| 1107 |
case 'c': /* set new cpu mask for filtering (when xch is set). */ |
| 1108 |
free(opts.cpu_mask_str); |
| 1109 |
opts.cpu_mask_str = strdup(optarg); |
| 1110 |
break; |
| 1111 |
|
| 1112 |
case 'e': /* set new event mask for filtering*/ |
| 1113 |
parse_evtmask(optarg); |
| 1114 |
break; |
| 1115 |
|
| 1116 |
case 'S': /* set tbuf size (given in pages) */ |
| 1117 |
opts.tbuf_size = argtol(optarg, 0); |
| 1118 |
break; |
| 1119 |
|
| 1120 |
case 'V': /* print program version */ |
| 1121 |
printf("%s\n", program_version); |
| 1122 |
exit(EXIT_SUCCESS); |
| 1123 |
break; |
| 1124 |
|
| 1125 |
case 'D': /* Discard traces currently in buffer */ |
| 1126 |
opts.discard = 1; |
| 1127 |
break; |
| 1128 |
|
| 1129 |
case 'r': /* Disk-space reservation */ |
| 1130 |
opts.disk_rsvd = argtol(optarg, 0); |
| 1131 |
break; |
| 1132 |
|
| 1133 |
case 'x': /* Don't disable tracing */ |
| 1134 |
opts.disable_tracing = 0; |
| 1135 |
break; |
| 1136 |
|
| 1137 |
case 'X': /* Start disabled */ |
| 1138 |
opts.start_disabled = 1; |
| 1139 |
break; |
| 1140 |
|
| 1141 |
case 'T': |
| 1142 |
opts.timeout = argtol(optarg, 0); |
| 1143 |
break; |
| 1144 |
|
| 1145 |
case 'M': |
| 1146 |
opts.memory_buffer = sargtol(optarg, 0); |
| 1147 |
break; |
| 1148 |
|
| 1149 |
case 'h': |
| 1150 |
usage(EXIT_SUCCESS); |
| 1151 |
break; |
| 1152 |
|
| 1153 |
default: |
| 1154 |
usage(EXIT_FAILURE); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* get outfile (optional last argument) */ |
| 1159 |
if (argc > optind) |
| 1160 |
opts.outfile = argv[optind]; |
| 1161 |
} |
| 1162 |
|
| 1163 |
/* *BSD has no O_LARGEFILE */ |
| 1164 |
#ifndef O_LARGEFILE |
| 1165 |
#define O_LARGEFILE 0 |
| 1166 |
#endif |
| 1167 |
|
| 1168 |
int main(int argc, char **argv) |
| 1169 |
{ |
| 1170 |
struct sigaction act; |
| 1171 |
|
| 1172 |
opts.outfile = 0; |
| 1173 |
opts.poll_sleep = POLL_SLEEP_MILLIS; |
| 1174 |
opts.evt_mask = 0; |
| 1175 |
opts.cpu_mask_str = NULL; |
| 1176 |
opts.disk_rsvd = 0; |
| 1177 |
opts.disable_tracing = 1; |
| 1178 |
opts.start_disabled = 0; |
| 1179 |
opts.timeout = 0; |
| 1180 |
|
| 1181 |
parse_args(argc, argv); |
| 1182 |
|
| 1183 |
xc_handle = xc_interface_open(0,0,0); |
| 1184 |
if ( !xc_handle ) |
| 1185 |
{ |
| 1186 |
perror("xenctrl interface open"); |
| 1187 |
exit(EXIT_FAILURE); |
| 1188 |
} |
| 1189 |
|
| 1190 |
if ( opts.evt_mask != 0 ) |
| 1191 |
set_evt_mask(opts.evt_mask); |
| 1192 |
|
| 1193 |
if ( opts.cpu_mask_str ) |
| 1194 |
{ |
| 1195 |
if ( parse_cpu_mask() ) |
| 1196 |
exit(EXIT_FAILURE); |
| 1197 |
} |
| 1198 |
|
| 1199 |
if ( opts.timeout != 0 ) |
| 1200 |
alarm(opts.timeout); |
| 1201 |
|
| 1202 |
if ( opts.outfile ) |
| 1203 |
outfd = open(opts.outfile, |
| 1204 |
O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, |
| 1205 |
0644); |
| 1206 |
|
| 1207 |
if ( outfd < 0 ) |
| 1208 |
{ |
| 1209 |
perror("Could not open output file"); |
| 1210 |
exit(EXIT_FAILURE); |
| 1211 |
} |
| 1212 |
|
| 1213 |
if ( isatty(outfd) ) |
| 1214 |
{ |
| 1215 |
fprintf(stderr, "Cannot output to a TTY, specify a log file.\n"); |
| 1216 |
exit(EXIT_FAILURE); |
| 1217 |
} |
| 1218 |
|
| 1219 |
if ( opts.memory_buffer > 0 ) |
| 1220 |
membuf_alloc(opts.memory_buffer); |
| 1221 |
|
| 1222 |
/* ensure that if we get a signal, we'll do cleanup, then exit */ |
| 1223 |
act.sa_handler = close_handler; |
| 1224 |
act.sa_flags = 0; |
| 1225 |
sigemptyset(&act.sa_mask); |
| 1226 |
sigaction(SIGHUP, &act, NULL); |
| 1227 |
sigaction(SIGTERM, &act, NULL); |
| 1228 |
sigaction(SIGINT, &act, NULL); |
| 1229 |
sigaction(SIGALRM, &act, NULL); |
| 1230 |
|
| 1231 |
monitor_tbufs(); |
| 1232 |
|
| 1233 |
close(outfd); |
| 1234 |
return 0; |
| 1235 |
} |
| 1236 |
|
| 1237 |
/* |
| 1238 |
* Local variables: |
| 1239 |
* mode: C |
| 1240 |
* c-file-style: "BSD" |
| 1241 |
* c-basic-offset: 4 |
| 1242 |
* tab-width: 4 |
| 1243 |
* indent-tabs-mode: nil |
| 1244 |
* End: |
| 1245 |
*/ |