* strace.c: add create_tcbtab() that allocate the minimum required amount of
memory to create the tcbtab. Add a tcbtab existance check in expand_tcbtab()
that will be called by alloctcb() when no tcb are available (first call and
subsequent calls when tcbtab is full). Remove the always done at first tcbtab
allocation in init(), now replaced by create_tcbtab() and called when needed.

Signed-off-by: Nahim El Atmani <[email protected]>
Reviewed-By: Gabriel Laskar <[email protected]>
Reported-by: haris iqbal <[email protected]>
---
 strace.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/strace.c b/strace.c
index 785cc60..d18aa7e 100644
--- a/strace.c
+++ b/strace.c
@@ -689,6 +689,18 @@ newoutf(struct tcb *tcp)
 }
 
 static void
+create_tcbtab(void)
+{
+       struct tcb *tcp;
+
+       /* Allocate the initial tcbtab.  */
+       tcbtabsize = 1; /* We don't need much more at first */
+       tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
+       tcp = xcalloc(tcbtabsize, sizeof(*tcp));
+       tcbtab[0] = tcp;
+}
+
+static void
 expand_tcbtab(void)
 {
        /* Allocate some more TCBs and expand the table.
@@ -696,10 +708,16 @@ expand_tcbtab(void)
           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]));
+       unsigned int i;
+       struct tcb *newtcbs;
+       struct tcb **newtab;
+
+       if (!tcbtabsize)
+               create_tcbtab();
+
+       i = tcbtabsize;
+       newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
+       newtab = xreallocarray(tcbtab, tcbtabsize * 2, sizeof(tcbtab[0]));
        tcbtabsize *= 2;
        tcbtab = newtab;
        while (i < tcbtabsize)
@@ -1484,10 +1502,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 +1519,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

Reply via email to