From: Nahim El Atmani <[email protected]> * strace.c: alloctcb() when no tcbs are available (first call and subsequent calls when tcbtab is full) call expand_tcbtab(). Now, expand_tcbtab() allocate the minimum required amount of memory to create the tcbtab if needed and expend otherwise instead of relying on the preallocation of a large amount of tcbs and expand if needed.
Signed-off-by: Nahim El Atmani <[email protected]> Reviewed-By: Gabriel Laskar <[email protected]> Reported-by: haris iqbal <[email protected]> --- strace.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/strace.c b/strace.c index 785cc60..e97fe9e 100644 --- a/strace.c +++ b/strace.c @@ -691,16 +691,26 @@ newoutf(struct tcb *tcp) static void expand_tcbtab(void) { - /* Allocate some more TCBs and expand the table. + /* Allocate some (more) TCBs (and expand the table). We don't want to relocate the TCBs because our callers have pointers and it would be a pain. So tcbtab is a table of pointers. Since we never free the TCBs, we allocate a single chunk of many. */ - unsigned int i = tcbtabsize; - struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0])); - struct tcb **newtab = xreallocarray(tcbtab, tcbtabsize * 2, - sizeof(tcbtab[0])); - tcbtabsize *= 2; + unsigned int i, new_tcbtabsize, alloc_tcbtabsize; + struct tcb *newtcbs; + struct tcb **newtab; + + if (tcbtabsize) { + alloc_tcbtabsize = tcbtabsize; + new_tcbtabsize = tcbtabsize * 2; + } else { + new_tcbtabsize = alloc_tcbtabsize = 1; + } + + i = tcbtabsize; + newtcbs = xcalloc(alloc_tcbtabsize, sizeof(newtcbs[0])); + newtab = xreallocarray(tcbtab, new_tcbtabsize, sizeof(tcbtab[0])); + tcbtabsize = new_tcbtabsize; tcbtab = newtab; while (i < tcbtabsize) tcbtab[i++] = newtcbs++; @@ -1484,10 +1494,8 @@ get_os_release(void) static void ATTRIBUTE_NOINLINE init(int argc, char *argv[]) { - struct tcb *tcp; int c, i; int optF = 0; - unsigned int tcbi; struct sigaction sa; progname = argv[0] ? argv[0] : "strace"; @@ -1503,13 +1511,6 @@ init(int argc, char *argv[]) os_release = get_os_release(); - /* Allocate the initial tcbtab. */ - tcbtabsize = argc; /* Surely enough for all -p args. */ - tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0])); - tcp = xcalloc(tcbtabsize, sizeof(*tcp)); - for (tcbi = 0; tcbi < tcbtabsize; tcbi++) - tcbtab[tcbi] = tcp++; - shared_log = stderr; set_sortby(DEFAULT_SORTBY); set_personality(DEFAULT_PERSONALITY); -- Nahim El Atmani <[email protected]> http://naam.me/ ------------------------------------------------------------------------------ Site24x7 APM Insight: Get Deep Visibility into Application Performance APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month Monitor end-to-end web transactions and take corrective actions now Troubleshoot faster and improve end-user experience. Signup Now! http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140 _______________________________________________ Strace-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/strace-devel
