Skip to content

Commit 8a730f3

Browse files
author
Petr Machata
committed
Consider exec and exit events an end of outstanding calls
- This cleans up a lot of stuff. The actual substance is addition of account_current_callstack in handle_event.c (which however uses those cleaned-up interfaces). - trace-exec.exp was extended to check that the exec syscall can be seen in -c output. That's one of the symptoms of what this fixes. - This hides dict_opt_c in summary.c. It also gets rid of the global variable current_time_spent--instead, the information is passed via a function argument in a struct timedelta (so that we never confuse absolute time with relative). callstack_element.time_spent was renamed to .enter_time, as that is what it has always been.
1 parent 6415034 commit 8a730f3

12 files changed

+299
-183
lines changed

Makefile.am

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ ltrace_LDADD = \
5454

5555
noinst_HEADERS = bits.h backend.h breakpoint.h common.h debug.h \
5656
defs.h demangle.h dict.h forward.h ltrace-elf.h ltrace.h \
57-
options.h output.h proc.h read_config_file.h library.h \
58-
filter.h glob.h vect.h type.h value.h value_dict.h callback.h \
59-
expr.h fetch.h vect.h param.h printf.h zero.h lens.h \
60-
lens_default.h lens_enum.h memstream.h prototype.h
57+
options.h output.h proc.h read_config_file.h summary.h \
58+
library.h filter.h glob.h vect.h type.h value.h value_dict.h \
59+
callback.h expr.h fetch.h vect.h param.h printf.h zero.h \
60+
lens.h lens_default.h lens_enum.h memstream.h prototype.h
6161

6262
dist_man1_MANS = ltrace.1
6363
dist_man5_MANS = ltrace.conf.5

common.h

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ extern void handle_event(Event * event);
5454

5555
extern pid_t execute_program(const char * command, char ** argv);
5656

57-
extern void show_summary(void);
58-
5957
struct breakpoint;
6058
struct library_symbol;
6159

forward.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct param_enum;
3434
struct process;
3535
struct protolib;
3636
struct prototype;
37+
struct timedelta;
3738
struct value;
3839
struct value_dict;
3940
struct vect;

handle_event.c

+135-90
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <stdio.h>
3333
#include <stdlib.h>
3434
#include <string.h>
35-
#include <sys/time.h>
3635
#include <stdbool.h>
3736

3837
#include "backend.h"
@@ -41,8 +40,9 @@
4140
#include "fetch.h"
4241
#include "library.h"
4342
#include "proc.h"
44-
#include "value_dict.h"
4543
#include "prototype.h"
44+
#include "summary.h"
45+
#include "value_dict.h"
4646

4747
static void handle_signal(Event *event);
4848
static void handle_exit(Event *event);
@@ -419,32 +419,11 @@ handle_signal(Event *event) {
419419
continue_after_signal(event->proc->pid, event->e_un.signum);
420420
}
421421

422-
static void
423-
handle_exit(Event *event) {
424-
debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
425-
if (event->proc->state != STATE_IGNORED) {
426-
output_line(event->proc, "+++ exited (status %d) +++",
427-
event->e_un.ret_val);
428-
}
429-
remove_process(event->proc);
430-
}
431-
432-
static void
433-
handle_exit_signal(Event *event) {
434-
debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
435-
if (event->proc->state != STATE_IGNORED) {
436-
output_line(event->proc, "+++ killed by %s +++",
437-
shortsignal(event->proc, event->e_un.signum));
438-
}
439-
remove_process(event->proc);
440-
}
441-
442-
static void
443-
output_syscall(struct process *proc, const char *name, enum tof tof,
444-
void (*output)(enum tof, struct process *,
445-
struct library_symbol *))
422+
static int
423+
init_syscall_symbol(struct library_symbol *libsym, const char *name)
446424
{
447425
static struct library syscall_lib;
426+
448427
if (syscall_lib.protolib == NULL) {
449428
struct protolib *protolib
450429
= protolib_cache_load(&g_protocache, "syscalls", 0, 1);
@@ -475,28 +454,111 @@ output_syscall(struct process *proc, const char *name, enum tof tof,
475454
syscall_lib.protolib = protolib;
476455
}
477456

457+
if (library_symbol_init(libsym, 0, name, 0, LS_TOPLT_NONE) < 0)
458+
return -1;
459+
460+
libsym->lib = &syscall_lib;
461+
return 0;
462+
}
463+
464+
/* Account the unfinished functions on the call stack. */
465+
static void
466+
account_current_callstack(struct process *proc)
467+
{
468+
if (! options.summary)
469+
return;
470+
471+
struct timedelta spent[proc->callstack_depth];
472+
473+
size_t i;
474+
for (i = 0; i < proc->callstack_depth; ++i) {
475+
struct callstack_element *elem = &proc->callstack[i];
476+
spent[i] = calc_time_spent(elem->enter_time);
477+
}
478+
479+
for (i = 0; i < proc->callstack_depth; ++i) {
480+
struct callstack_element *elem = &proc->callstack[i];
481+
struct library_symbol syscall, *libsym = NULL;
482+
if (elem->is_syscall) {
483+
const char *name = sysname(proc, elem->c_un.syscall);
484+
if (init_syscall_symbol(&syscall, name) >= 0)
485+
libsym = &syscall;
486+
487+
} else {
488+
libsym = elem->c_un.libfunc;
489+
}
490+
491+
if (libsym != NULL) {
492+
summary_account_call(libsym, spent[i]);
493+
494+
if (elem->is_syscall)
495+
library_symbol_destroy(&syscall);
496+
}
497+
}
498+
}
499+
500+
static void
501+
handle_exit(Event *event) {
502+
debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
503+
if (event->proc->state != STATE_IGNORED) {
504+
output_line(event->proc, "+++ exited (status %d) +++",
505+
event->e_un.ret_val);
506+
}
507+
508+
account_current_callstack(event->proc);
509+
remove_process(event->proc);
510+
}
511+
512+
static void
513+
handle_exit_signal(Event *event) {
514+
debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
515+
if (event->proc->state != STATE_IGNORED) {
516+
output_line(event->proc, "+++ killed by %s +++",
517+
shortsignal(event->proc, event->e_un.signum));
518+
}
519+
520+
account_current_callstack(event->proc);
521+
remove_process(event->proc);
522+
}
523+
524+
static void
525+
output_syscall(struct process *proc, const char *name, enum tof tof,
526+
bool left, struct timedelta *spent)
527+
{
528+
if (left)
529+
assert(spent == NULL);
530+
478531
struct library_symbol syscall;
479-
if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
480-
syscall.lib = &syscall_lib;
481-
(*output)(tof, proc, &syscall);
532+
if (init_syscall_symbol(&syscall, name) >= 0) {
533+
if (left) {
534+
if (! options.summary)
535+
output_left(tof, proc, &syscall);
536+
} else if (options.summary) {
537+
summary_account_call(&syscall, *spent);
538+
} else {
539+
output_right(tof, proc, &syscall, spent);
540+
}
541+
482542
library_symbol_destroy(&syscall);
483543
}
484544
}
485545

486546
static void
487547
output_syscall_left(struct process *proc, const char *name)
488548
{
489-
output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
549+
output_syscall(proc, name, LT_TOF_SYSCALL, true, NULL);
490550
}
491551

492552
static void
493-
output_syscall_right(struct process *proc, const char *name)
553+
output_syscall_right(struct process *proc, const char *name,
554+
struct timedelta *spent)
494555
{
495-
output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
556+
output_syscall(proc, name, LT_TOF_SYSCALLR, false, spent);
496557
}
497558

498559
static void
499-
handle_syscall(Event *event) {
560+
handle_syscall(Event *event)
561+
{
500562
debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
501563
if (event->proc->state != STATE_IGNORED) {
502564
callstack_push_syscall(event->proc, event->e_un.sysnum);
@@ -526,6 +588,8 @@ handle_exec(Event *event)
526588
}
527589
output_line(proc, "--- Called exec() ---");
528590

591+
account_current_callstack(proc);
592+
529593
if (process_exec(proc) < 0) {
530594
fprintf(stderr,
531595
"couldn't reinitialize process %d after exec\n", pid);
@@ -549,74 +613,58 @@ handle_arch_syscall(Event *event) {
549613
continue_process(event->proc->pid);
550614
}
551615

552-
struct timeval current_time_spent;
553-
554616
static void
555-
calc_time_spent(struct process *proc)
617+
handle_x_sysret(Event *event, char *(*name_cb)(struct process *, int))
556618
{
557-
struct timeval tv;
558-
struct timezone tz;
559-
struct timeval diff;
560-
struct callstack_element *elem;
561-
562-
debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
563-
elem = &proc->callstack[proc->callstack_depth - 1];
564-
565-
gettimeofday(&tv, &tz);
619+
debug(DEBUG_FUNCTION, "handle_x_sysret(pid=%d, sysnum=%d)",
620+
event->proc->pid, event->e_un.sysnum);
566621

567-
diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
568-
if (tv.tv_usec >= elem->time_spent.tv_usec) {
569-
diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
570-
} else {
571-
diff.tv_sec--;
572-
diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
573-
}
574-
current_time_spent = diff;
575-
}
622+
unsigned d = event->proc->callstack_depth;
623+
assert(d > 0);
624+
struct callstack_element *elem = &event->proc->callstack[d - 1];
625+
assert(elem->is_syscall);
576626

577-
static void
578-
handle_sysret(Event *event) {
579-
debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
580627
if (event->proc->state != STATE_IGNORED) {
581-
if (opt_T || options.summary) {
582-
calc_time_spent(event->proc);
583-
}
628+
struct timedelta spent = calc_time_spent(elem->enter_time);
584629
if (options.syscalls)
585630
output_syscall_right(event->proc,
586-
sysname(event->proc,
587-
event->e_un.sysnum));
631+
name_cb(event->proc,
632+
event->e_un.sysnum),
633+
&spent);
588634

589-
assert(event->proc->callstack_depth > 0);
590-
unsigned d = event->proc->callstack_depth - 1;
591-
assert(event->proc->callstack[d].is_syscall);
592635
callstack_pop(event->proc);
593636
}
594637
continue_after_syscall(event->proc, event->e_un.sysnum, 1);
595638
}
596639

597640
static void
598-
handle_arch_sysret(Event *event) {
599-
debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
600-
if (event->proc->state != STATE_IGNORED) {
601-
if (opt_T || options.summary) {
602-
calc_time_spent(event->proc);
603-
}
604-
if (options.syscalls)
605-
output_syscall_right(event->proc,
606-
arch_sysname(event->proc,
607-
event->e_un.sysnum));
608-
callstack_pop(event->proc);
609-
}
610-
continue_process(event->proc->pid);
641+
handle_sysret(Event *event)
642+
{
643+
handle_x_sysret(event, &sysname);
644+
}
645+
646+
static void
647+
handle_arch_sysret(Event *event)
648+
{
649+
handle_x_sysret(event, &arch_sysname);
611650
}
612651

613652
static void
614653
output_right_tos(struct process *proc)
615654
{
616655
size_t d = proc->callstack_depth;
656+
assert(d > 0);
617657
struct callstack_element *elem = &proc->callstack[d - 1];
618-
if (proc->state != STATE_IGNORED)
619-
output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
658+
assert(! elem->is_syscall);
659+
660+
if (proc->state != STATE_IGNORED) {
661+
struct timedelta spent = calc_time_spent(elem->enter_time);
662+
if (options.summary)
663+
summary_account_call(elem->c_un.libfunc, spent);
664+
else
665+
output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc,
666+
&spent);
667+
}
620668
}
621669

622670
#ifndef ARCH_HAVE_SYMBOL_RET
@@ -645,14 +693,8 @@ handle_breakpoint(Event *event)
645693

646694
for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
647695
if (brk_addr == event->proc->callstack[i].return_addr) {
648-
for (j = event->proc->callstack_depth - 1; j > i; j--) {
696+
for (j = event->proc->callstack_depth - 1; j > i; j--)
649697
callstack_pop(event->proc);
650-
}
651-
if (event->proc->state != STATE_IGNORED) {
652-
if (opt_T || options.summary) {
653-
calc_time_spent(event->proc);
654-
}
655-
}
656698

657699
struct library_symbol *libsym =
658700
event->proc->callstack[i].c_un.libfunc;
@@ -705,11 +747,14 @@ handle_breakpoint(Event *event)
705747
/* breakpoint_on_hit may delete its own breakpoint, so we have
706748
* to look it up again. */
707749
if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
750+
708751
if (event->proc->state != STATE_IGNORED
709752
&& sbp->libsym != NULL) {
710753
event->proc->stack_pointer = get_stack_pointer(event->proc);
711754
callstack_push_symfunc(event->proc, sbp);
712-
output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
755+
if (! options.summary)
756+
output_left(LT_TOF_FUNCTION, event->proc,
757+
sbp->libsym);
713758
}
714759

715760
breakpoint_on_continue(sbp, event->proc);
@@ -743,7 +788,7 @@ callstack_push_syscall(struct process *proc, int sysnum)
743788
proc->callstack_depth++;
744789
if (opt_T || options.summary) {
745790
struct timezone tz;
746-
gettimeofday(&elem->time_spent, &tz);
791+
gettimeofday(&elem->enter_time, &tz);
747792
}
748793
}
749794

@@ -781,7 +826,7 @@ callstack_push_symfunc(struct process *proc, struct breakpoint *bp)
781826

782827
if (opt_T || options.summary) {
783828
struct timezone tz;
784-
gettimeofday(&elem->time_spent, &tz);
829+
gettimeofday(&elem->enter_time, &tz);
785830
}
786831
}
787832

libltrace.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
#include <string.h>
3333
#include <unistd.h>
3434

35+
#include "backend.h"
3536
#include "common.h"
3637
#include "proc.h"
37-
#include "read_config_file.h"
38-
#include "backend.h"
3938
#include "prototype.h"
39+
#include "read_config_file.h"
40+
#include "summary.h"
4041

4142
char *command = NULL;
4243

0 commit comments

Comments
 (0)