Rizin
unix-like reverse engineering framework and cli tools
esil_interrupt.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018 condret <condr3t@protonmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_analysis.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 
8 static void interrupt_free(HtUPKv *kv) {
11 }
12 
14  rz_return_if_fail(esil);
15  esil->interrupts = ht_up_new(NULL, interrupt_free, NULL);
16 }
17 
19  rz_return_val_if_fail(esil && ih && ih->cb, NULL);
21  if (!intr) {
22  return NULL;
23  }
24  intr->esil = esil;
25  intr->handler = ih;
26  if (ih->init && ih->fini) {
27  intr->user = ih->init(esil);
28  }
29  intr->src_id = src_id;
30  rz_analysis_esil_claim_source(esil, src_id);
31  return intr;
32 }
33 
35  rz_return_if_fail(esil);
36  if (intr) {
37  if (intr->user) {
38  intr->handler->fini(intr->user); // fini must exist when user is !NULL
39  }
41  free(intr);
42  }
43 }
44 
46  rz_return_val_if_fail(esil && esil->interrupts && intr && intr->handler && intr->handler->cb, false);
47  return ht_up_update(esil->interrupts, intr->handler->num, intr);
48 }
49 
51  rz_return_val_if_fail(esil, false);
52 
53  if (esil->cmd && esil->cmd(esil, esil->cmd_intr, intr_num, 0)) { // compatibility
54  return true;
55  }
56 
57  if (!esil->interrupts) {
58  RZ_LOG_ERROR("no interrupts initialized\n");
59  return false;
60  }
61  RzAnalysisEsilInterrupt *intr = ht_up_find(esil->interrupts, intr_num, NULL);
62 #if 0
63  // we don't want this warning
64  if (!intr) {
65  RZ_LOG_WARN("no interrupt handler registered for 0x%x\n", intr_num);
66  }
67 #endif
68  return (intr && intr->handler && intr->handler->cb) ? intr->handler->cb(esil, intr_num, intr->user) : false;
69 }
70 
73  ut32 i = 0;
74 
75  rz_return_val_if_fail(esil && esil->interrupts && handlers, false);
76 
77  while (handlers[i]) {
78  intr = rz_analysis_esil_interrupt_new(esil, src_id, handlers[i]);
79  if (!intr) {
80  return false;
81  }
82  if (!rz_analysis_esil_set_interrupt(esil, intr)) {
83  free(intr);
84  }
85  i++;
86  }
87 
88  return true;
89 }
90 
92  rz_return_val_if_fail(esil, false);
93  ut32 src_id = rz_analysis_esil_load_source(esil, path);
94  if (!src_id) { // why id=0 is invalid?
95  return false;
96  }
98  rz_lib_dl_sym(rz_analysis_esil_get_source(esil, src_id), "interrupts");
99  if (!handlers) {
100  rz_analysis_esil_release_source(esil, src_id); // unload
101  return false;
102  }
103  return rz_analysis_esil_load_interrupts(esil, handlers, src_id);
104 }
105 
107  rz_return_if_fail(esil && esil->interrupts);
108  ht_up_free(esil->interrupts);
109  esil->interrupts = NULL;
110 }
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
uint32_t ut32
RZ_API bool rz_analysis_esil_load_interrupts(RzAnalysisEsil *esil, RzAnalysisEsilInterruptHandler *handlers[], ut32 src_id)
RZ_API RzAnalysisEsilInterrupt * rz_analysis_esil_interrupt_new(RzAnalysisEsil *esil, ut32 src_id, RzAnalysisEsilInterruptHandler *ih)
RZ_API bool rz_analysis_esil_load_interrupts_from_lib(RzAnalysisEsil *esil, const char *path)
RZ_API void rz_analysis_esil_interrupts_init(RzAnalysisEsil *esil)
static void interrupt_free(HtUPKv *kv)
Definition: esil_interrupt.c:8
RZ_API bool rz_analysis_esil_set_interrupt(RzAnalysisEsil *esil, RzAnalysisEsilInterrupt *intr)
RZ_API int rz_analysis_esil_fire_interrupt(RzAnalysisEsil *esil, ut32 intr_num)
RZ_API void rz_analysis_esil_interrupt_free(RzAnalysisEsil *esil, RzAnalysisEsilInterrupt *intr)
RZ_API void rz_analysis_esil_interrupts_fini(RzAnalysisEsil *esil)
RZ_API void rz_analysis_esil_release_source(RzAnalysisEsil *esil, ut32 src_id)
Definition: esil_sources.c:60
RZ_API void * rz_analysis_esil_get_source(RzAnalysisEsil *esil, ut32 src_id)
Definition: esil_sources.c:44
RZ_API bool rz_analysis_esil_claim_source(RzAnalysisEsil *esil, ut32 src_id)
Definition: esil_sources.c:50
RZ_API ut32 rz_analysis_esil_load_source(RzAnalysisEsil *esil, const char *path)
Definition: esil_sources.c:14
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
RzAnalysisEsilInterruptCB cb
Definition: rz_analysis.h:998
void *(* init)(ANALYSIS_ESIL *esil)
Definition: rz_analysis.h:997
RzAnalysisEsilInterruptHandler * handler
Definition: rz_analysis.h:1171
bool(* cmd)(ANALYSIS_ESIL *esil, const char *name, ut64 a0, ut64 a1)
Definition: rz_analysis.h:1089