Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / zero.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  */
20
21 #include <error.h>
22 #include <errno.h>
23
24 #include "zero.h"
25 #include "common.h"
26 #include "type.h"
27 #include "value.h"
28 #include "expr.h"
29
30 static int
31 zero_callback_max(struct value *ret_value, struct value *lhs,
32                   struct value_dict *arguments,
33                   size_t max, void *data)
34 {
35         size_t i;
36         for (i = 0; i < max; ++i) {
37                 struct value element;
38                 if (value_init_element(&element, lhs, i) < 0)
39                         return -1;
40
41                 int zero = value_is_zero(&element, arguments);
42
43                 value_destroy(&element);
44
45                 if (zero)
46                         break;
47         }
48
49         struct arg_type_info *long_type = type_get_simple(ARGTYPE_LONG);
50         value_init_detached(ret_value, NULL, long_type, 0);
51         value_set_word(ret_value, i);
52         return 0;
53 }
54
55 /* LHS->zero(RHS).  Looks for a length of zero-terminated array, but
56  * looks no further than first RHS bytes.  */
57 static int
58 zero_callback(struct value *ret_value, struct value *lhs,
59               struct value *rhs, struct value_dict *arguments, void *data)
60 {
61         long l;
62         if (value_extract_word(rhs, &l, arguments) < 0)
63                 return -1;
64         if (l < 0)
65                 /* It might just be a positive value >2GB, but that's
66                  * not likely.  */
67                 report_global_error("maximum array length seems negative");
68         size_t max = (size_t)l;
69         return zero_callback_max(ret_value, lhs, arguments, max, data);
70 }
71
72 /* LHS->zero.  Looks for a length of zero-terminated array, without
73  * limit.  */
74 static int
75 zero1_callback(struct value *ret_value, struct value *lhs,
76                struct value_dict *arguments, void *data)
77 {
78         return zero_callback_max(ret_value, lhs, arguments, (size_t)-1, data);
79 }
80
81 struct expr_node *
82 build_zero_w_arg(struct expr_node *expr, int own)
83 {
84         struct expr_node *e_z = malloc(sizeof(*e_z));
85         if (e_z == NULL)
86                 return NULL;
87
88         expr_init_cb2(e_z, &zero_callback,
89                       expr_self(), 0, expr, own, NULL);
90         return e_z;
91 }
92
93 struct expr_node *
94 expr_node_zero(void)
95 {
96         static struct expr_node *node = NULL;
97         if (node == NULL) {
98                 node = malloc(sizeof(*node));
99                 if (node == NULL)
100                         error(1, errno, "malloc expr_node_zero");
101                 expr_init_cb1(node, &zero1_callback,
102                               expr_self(), 0, (void *)-1);
103         }
104         return node;
105 }