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 * Do not instrument an inlined method - it becomes
25 * part of the current method.
27 if (cfg->current_method != cfg->method)
31 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE;
32 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE_CONTEXT;
34 instrument = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE;
35 capture = cfg->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE_CONTEXT;
43 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
45 if (capture && !cfg->llvm_only) {
46 cfg->flags |= MONO_CFG_HAS_ALLOCA;
48 MonoInst *size, *fill_ctx;
50 EMIT_NEW_ICONST (cfg, size, sizeof (MonoProfilerCallContext));
51 MONO_INST_NEW (cfg, iargs [1], OP_LOCALLOC);
52 iargs [1]->dreg = alloc_preg (cfg);
53 iargs [1]->sreg1 = size->dreg;
54 iargs [1]->flags |= MONO_INST_INIT;
55 MONO_ADD_INS (cfg->cbb, iargs [1]);
56 MONO_INST_NEW (cfg, fill_ctx, OP_FILL_PROF_CALL_CTX);
57 fill_ctx->sreg1 = iargs [1]->dreg;
58 MONO_ADD_INS (cfg->cbb, fill_ctx);
59 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, method), iargs [0]->dreg);
61 if (rtype && rtype->type != MONO_TYPE_VOID) {
62 MonoInst *var = mono_compile_create_var (cfg, rtype, OP_LOCAL);
64 MonoInst *store, *addr;
66 EMIT_NEW_TEMPSTORE (cfg, store, var->inst_c0, *ret);
67 EMIT_NEW_VARLOADA (cfg, addr, var, NULL);
68 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, iargs [1]->dreg, MONO_STRUCT_OFFSET (MonoProfilerCallContext, return_value), addr->dreg);
71 EMIT_NEW_PCONST (cfg, iargs [1], NULL);
73 mono_emit_jit_icall (cfg, func, iargs);
79 mini_profiler_context_enable (void)
81 if (!mono_debug_enabled ())
82 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
86 memdup_with_type (gpointer data, MonoType *t)
90 return g_memdup (data, mono_type_size (t, &dummy));
94 get_int_reg (MonoContext *ctx, guint32 reg)
96 return (guint8 *) mono_arch_context_get_int_reg (ctx, reg);
100 get_variable_buffer (MonoDebugMethodJitInfo *jit, MonoDebugVarInfo *var, MonoContext *ctx)
102 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
103 guint32 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
106 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER: {
108 * This is kind of a special case: All other address modes ultimately
109 * produce an address to where the actual value is located, but this
110 * address mode gets us the value itself as an mgreg_t value.
112 mgreg_t value = (mgreg_t) get_int_reg (ctx, reg);
114 return memdup_with_type (&value, var->type);
116 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
117 return memdup_with_type (get_int_reg (ctx, reg) + (gint32) var->offset, var->type);
118 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
119 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
120 return memdup_with_type (*(guint8 **) (get_int_reg (ctx, reg) + (gint32) var->offset), var->type);
121 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL: {
124 MonoDebugVarInfo *info_var = jit->gsharedvt_info_var;
126 flags = info_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
127 reg = info_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
129 MonoGSharedVtMethodRuntimeInfo *info;
132 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
133 info = (MonoGSharedVtMethodRuntimeInfo *) get_int_reg (ctx, reg);
135 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
136 info = *(MonoGSharedVtMethodRuntimeInfo **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
139 g_assert_not_reached ();
142 MonoDebugVarInfo *locals_var = jit->gsharedvt_locals_var;
144 flags = locals_var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
145 reg = locals_var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
150 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
151 locals = get_int_reg (ctx, reg);
153 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
154 locals = *(guint8 **) (get_int_reg (ctx, reg) + (gint32) info_var->offset);
157 g_assert_not_reached ();
160 return memdup_with_type (locals + (gsize) info->entries [idx], var->type);
163 g_assert_not_reached ();
169 mini_profiler_context_get_this (MonoProfilerCallContext *ctx)
171 if (!mono_method_signature (ctx->method)->hasthis)
174 if (ctx->interp_frame)
175 return memdup_with_type (mono_interp_frame_get_this (ctx->interp_frame), &ctx->method->klass->this_arg);
177 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
182 return get_variable_buffer (info, info->this_var, &ctx->context);
186 mini_profiler_context_get_argument (MonoProfilerCallContext *ctx, guint32 pos)
188 MonoMethodSignature *sig = mono_method_signature (ctx->method);
190 if (pos >= sig->param_count)
193 if (ctx->interp_frame)
194 return memdup_with_type (mono_interp_frame_get_arg (ctx->interp_frame, pos), sig->params [pos]);
196 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
201 return get_variable_buffer (info, &info->params [pos], &ctx->context);
205 mini_profiler_context_get_local (MonoProfilerCallContext *ctx, guint32 pos)
208 MonoMethodHeader *header = mono_method_get_header_checked (ctx->method, &error);
209 mono_error_assert_ok (&error); // Must be a valid method at this point.
211 if (pos >= header->num_locals) {
212 mono_metadata_free_mh (header);
216 MonoType *t = header->locals [pos];
218 mono_metadata_free_mh (header);
220 if (ctx->interp_frame)
221 return memdup_with_type (mono_interp_frame_get_local (ctx->interp_frame, pos), t);
223 MonoDebugMethodJitInfo *info = mono_debug_find_method (ctx->method, mono_domain_get ());
228 return get_variable_buffer (info, &info->locals [pos], &ctx->context);
232 mini_profiler_context_get_result (MonoProfilerCallContext *ctx)
234 if (!ctx->return_value)
237 return memdup_with_type (ctx->return_value, mono_method_signature (ctx->method)->ret);
241 mini_profiler_context_free_buffer (void *buffer)