2 * Licensed to the .NET Foundation under one or more agreements.
3 * The .NET Foundation licenses this file to you under the MIT license.
4 * See the LICENSE file in the project root for more information.
9 #include <mono/metadata/abi-details.h>
10 #include <mono/metadata/mono-debug.h>
12 #include "interp/interp.h"
19 mini_profiler_emit_instrumentation_call (MonoCompile *cfg, void *func, gboolean entry, MonoInst **ret, MonoType *rtype)
21 gboolean instrument, capture;
24 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE;
25 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE_CONTEXT;
27 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE;
28 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE_CONTEXT;
34 g_assert (cfg->current_method == cfg->method);
38 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
40 if (capture && !cfg->llvm_only) {
41 cfg->flags |= MONO_CFG_HAS_ALLOCA;
43 MonoInst *size, *fill_ctx;
45 EMIT_NEW_ICONST (cfg, size, sizeof (MonoProfilerCallContext));
46 MONO_INST_NEW (cfg, iargs [1], OP_LOCALLOC);
47 iargs [1]->dreg = alloc_preg (cfg);
48 iargs [1]->sreg1 = size->dreg;
49 iargs [1]->flags |= MONO_INST_INIT;
50 MONO_ADD_INS (cfg->cbb, iargs [1]);
51 MONO_INST_NEW (cfg, fill_ctx, OP_FILL_PROF_CALL_CTX);
52 fill_ctx->sreg1 = iargs [1]->dreg;
53 MONO_ADD_INS (cfg->cbb, fill_ctx);
54 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, method), iargs [0]->dreg);
56 if (rtype && rtype->type != MONO_TYPE_VOID) {
57 MonoInst *var = mono_compile_create_var (cfg, rtype, OP_LOCAL);
59 MonoInst *store, *addr;
61 EMIT_NEW_TEMPSTORE (cfg, store, var->inst_c0, *ret);
62 EMIT_NEW_VARLOADA (cfg, addr, var, NULL);
63 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, return_value), addr->dreg);
66 EMIT_NEW_PCONST (cfg, iargs [1], NULL);
68 mono_emit_jit_icall (cfg, func, iargs);
74 mini_profiler_context_enable (void)
76 if (!mono_debug_enabled ())
77 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
81 memdup_with_type (gpointer data, MonoType *t)
85 return g_memdup (data, mono_type_size (t, &dummy));
89 get_int_reg (MonoContext *ctx, guint32 reg)
91 return (guint8 *) mono_arch_context_get_int_reg (ctx, reg);
95 get_variable_buffer (MonoDebugMethodJitInfo *jit, MonoDebugVarInfo *var, MonoContext *ctx)
97 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
98 guint32 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
101 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER: {
103 * This is kind of a special case: All other address modes ultimately
104 * produce an address to where the actual value is located, but this
105 * address mode gets us the value itself as an mgreg_t value.
107 mgreg_t value = (mgreg_t) get_int_reg (ctx, reg);
109 return memdup_with_type (&value, var->type);
111 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
112 return memdup_with_type (get_int_reg (ctx, reg) + (gint32) var->offset, var->type);
113 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
114 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
115 return memdup_with_type (*(guint8 **) (get_int_reg (ctx, reg) + (gint32) var->offset), var->type);
116 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL: {
119 MonoDebugVarInfo *info_var = jit->gsharedvt_info_var;
121 flags = info_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
122 reg = info_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
124 MonoGSharedVtMethodRuntimeInfo *info;
127 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
128 info = (MonoGSharedVtMethodRuntimeInfo *) get_int_reg (ctx, reg);
130 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
131 info = *(MonoGSharedVtMethodRuntimeInfo **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
134 g_assert_not_reached ();
137 MonoDebugVarInfo *locals_var = jit->gsharedvt_locals_var;
139 flags = locals_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
140 reg = locals_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
145 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
146 locals = get_int_reg (ctx, reg);
148 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
149 locals = *(guint8 **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
152 g_assert_not_reached ();
155 return memdup_with_type (locals + (gsize) info->entries [idx], var->type);
158 g_assert_not_reached ();
164 mini_profiler_context_get_this (MonoProfilerCallContext *ctx)
166 if (!mono_method_signature (ctx->method)->hasthis)
169 if (ctx->interp_frame)
170 return memdup_with_type (mono_interp_frame_get_this (ctx->interp_frame), &ctx->method->klass->this_arg);
172 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
177 return get_variable_buffer (info, info->this_var, &ctx->context);
181 mini_profiler_context_get_argument (MonoProfilerCallContext *ctx, guint32 pos)
183 MonoMethodSignature *sig = mono_method_signature (ctx->method);
185 if (pos >= sig->param_count)
188 if (ctx->interp_frame)
189 return memdup_with_type (mono_interp_frame_get_arg (ctx->interp_frame, pos), sig->params [pos]);
191 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
196 return get_variable_buffer (info, &info->params [pos], &ctx->context);
200 mini_profiler_context_get_local (MonoProfilerCallContext *ctx, guint32 pos)
203 MonoMethodHeader *header = mono_method_get_header_checked (ctx->method, &error);
204 mono_error_assert_ok (&error); // Must be a valid method at this point.
206 if (pos >= header->num_locals) {
207 mono_metadata_free_mh (header);
211 MonoType *t = header->locals [pos];
213 mono_metadata_free_mh (header);
215 if (ctx->interp_frame)
216 return memdup_with_type (mono_interp_frame_get_local (ctx->interp_frame, pos), t);
218 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
223 return get_variable_buffer (info, &info->locals [pos], &ctx->context);
227 mini_profiler_context_get_result (MonoProfilerCallContext *ctx)
229 if (!ctx->return_value)
232 return memdup_with_type (ctx->return_value, mono_method_signature (ctx->method)->ret);
236 mini_profiler_context_free_buffer (void *buffer)