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.
7 #include <mono/metadata/abi-details.h>
8 #include <mono/metadata/mono-debug.h>
10 #include "interp/interp.h"
15 mini_profiler_emit_instrumentation_call (MonoCompile *cfg, void *func, gboolean entry, MonoInst **ret, MonoType *rtype)
17 gboolean instrument, capture;
20 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE;
21 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE_CONTEXT;
23 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE;
24 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE_CONTEXT;
30 g_assert (cfg->current_method == cfg->method);
34 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
36 if (capture && !cfg->llvm_only) {
37 cfg->flags |= MONO_CFG_HAS_ALLOCA;
39 MonoInst *size, *fill_ctx;
41 EMIT_NEW_ICONST (cfg, size, sizeof (MonoProfilerCallContext));
42 MONO_INST_NEW (cfg, iargs [1], OP_LOCALLOC);
43 iargs [1]->dreg = alloc_preg (cfg);
44 iargs [1]->sreg1 = size->dreg;
45 iargs [1]->flags |= MONO_INST_INIT;
46 MONO_ADD_INS (cfg->cbb, iargs [1]);
47 MONO_INST_NEW (cfg, fill_ctx, OP_FILL_PROF_CALL_CTX);
48 fill_ctx->sreg1 = iargs [1]->dreg;
49 MONO_ADD_INS (cfg->cbb, fill_ctx);
50 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, method), iargs [0]->dreg);
52 if (rtype && rtype->type != MONO_TYPE_VOID) {
53 MonoInst *var = mono_compile_create_var (cfg, rtype, OP_LOCAL);
55 MonoInst *store, *addr;
57 EMIT_NEW_TEMPSTORE (cfg, store, var->inst_c0, *ret);
58 EMIT_NEW_VARLOADA (cfg, addr, var, NULL);
59 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, return_value), addr->dreg);
62 EMIT_NEW_PCONST (cfg, iargs [1], NULL);
64 mono_emit_jit_icall (cfg, func, iargs);
68 mini_profiler_context_enable (void)
70 if (!mono_debug_enabled ())
71 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
75 memdup_with_type (gpointer data, MonoType *t)
79 return g_memdup (data, mono_type_size (t, &dummy));
83 get_int_reg (MonoContext *ctx, guint32 reg)
85 return (guint8 *) mono_arch_context_get_int_reg (ctx, reg);
89 get_variable_buffer (MonoDebugMethodJitInfo *jit, MonoDebugVarInfo *var, MonoContext *ctx)
91 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
92 guint32 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
95 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER: {
97 * This is kind of a special case: All other address modes ultimately
98 * produce an address to where the actual value is located, but this
99 * address mode gets us the value itself as an mgreg_t value.
101 mgreg_t value = (mgreg_t) get_int_reg (ctx, reg);
103 return memdup_with_type (&value, var->type);
105 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
106 return memdup_with_type (get_int_reg (ctx, reg) + (gint32) var->offset, var->type);
107 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
108 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
109 return memdup_with_type (*(guint8 **) (get_int_reg (ctx, reg) + (gint32) var->offset), var->type);
110 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL: {
113 MonoDebugVarInfo *info_var = jit->gsharedvt_info_var;
115 flags = info_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
116 reg = info_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
118 MonoGSharedVtMethodRuntimeInfo *info;
121 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
122 info = (MonoGSharedVtMethodRuntimeInfo *) get_int_reg (ctx, reg);
124 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
125 info = *(MonoGSharedVtMethodRuntimeInfo **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
128 g_assert_not_reached ();
131 MonoDebugVarInfo *locals_var = jit->gsharedvt_locals_var;
133 flags = locals_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
134 reg = locals_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
139 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
140 locals = get_int_reg (ctx, reg);
142 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
143 locals = *(guint8 **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
146 g_assert_not_reached ();
149 return memdup_with_type (locals + (gsize) info->entries [idx], var->type);
152 g_assert_not_reached ();
158 mini_profiler_context_get_this (MonoProfilerCallContext *ctx)
160 if (!mono_method_signature (ctx->method)->hasthis)
163 if (ctx->interp_frame)
164 return memdup_with_type (mono_interp_frame_get_this (ctx->interp_frame), &ctx->method->klass->this_arg);
166 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
171 return get_variable_buffer (info, info->this_var, &ctx->context);
175 mini_profiler_context_get_argument (MonoProfilerCallContext *ctx, guint32 pos)
177 MonoMethodSignature *sig = mono_method_signature (ctx->method);
179 if (pos >= sig->param_count)
182 if (ctx->interp_frame)
183 return memdup_with_type (mono_interp_frame_get_arg (ctx->interp_frame, pos), sig->params [pos]);
185 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
190 return get_variable_buffer (info, &info->params [pos], &ctx->context);
194 mini_profiler_context_get_local (MonoProfilerCallContext *ctx, guint32 pos)
197 MonoMethodHeader *header = mono_method_get_header_checked (ctx->method, &error);
198 mono_error_assert_ok (&error); // Must be a valid method at this point.
200 if (pos >= header->num_locals) {
201 mono_metadata_free_mh (header);
205 MonoType *t = header->locals [pos];
207 mono_metadata_free_mh (header);
209 if (ctx->interp_frame)
210 return memdup_with_type (mono_interp_frame_get_local (ctx->interp_frame, pos), t);
212 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
217 return get_variable_buffer (info, &info->locals [pos], &ctx->context);
221 mini_profiler_context_get_result (MonoProfilerCallContext *ctx)
223 if (!ctx->return_value)
226 return memdup_with_type (ctx->return_value, mono_method_signature (ctx->method)->ret);
230 mini_profiler_context_free_buffer (void *buffer)