From c59772dca3bcea7d53e527bbe24dbf9dd2ed76b4 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 6 Jul 2020 15:34:49 +0800 Subject: [PATCH] Exception handling: Implemented RPC exception. --- examples/exception.py | 28 +++++++++++++++++++++++----- src/runtime/src/comms.rs | 4 +++- src/runtime/src/kernel.rs | 28 ++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/examples/exception.py b/examples/exception.py index ec06741..1745220 100644 --- a/examples/exception.py +++ b/examples/exception.py @@ -1,20 +1,38 @@ from artiq.experiment import * +from artiq.language.core import TerminationRequested class ExceptionDemo(EnvExperiment): def build(self): self.setattr_device("core") self.setattr_device("led0") + def foo(self): + print("raise error") + raise Exception + + def termination(self): + raise TerminationRequested + + @rpc + def remote(self): + raise Exception + @kernel def run(self): self.core.reset() - print("OK!") try: try: - raise Exception + self.foo() except ValueError as e: - print("re-raise") - raise e + print("should not trigger this") except: - print("error") + print("catch all") + + try: + self.remote() + except: + print("Error!") + + print("Uncaught error at last") + self.termination() diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 5642ad3..db9445e 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -171,7 +171,9 @@ async fn handle_run_kernel(stream: &TcpStream, control: &Rc { error!("unexpected RPC request from host: {:?}", host_request); diff --git a/src/runtime/src/kernel.rs b/src/runtime/src/kernel.rs index 5a96324..8539b21 100644 --- a/src/runtime/src/kernel.rs +++ b/src/runtime/src/kernel.rs @@ -1,7 +1,7 @@ use core::{ptr, mem}; use log::{debug, info, error}; -use alloc::{vec::Vec, sync::Arc}; -use cslice::CSlice; +use alloc::{vec::Vec, sync::Arc, string::String}; +use cslice::{CSlice, AsCSlice}; use libcortex_a9::{enable_fpu, cache::dcci_slice, mutex::Mutex, sync_channel::{self, sync_channel}}; use libsupport_zynq::boot::Core1; @@ -11,6 +11,16 @@ use crate::eh_artiq; use crate::rpc; use crate::rtio; +#[derive(Debug)] +pub struct RPCException { + pub name: String, + pub message: String, + pub param: [i64; 3], + pub file: String, + pub line: i32, + pub column: i32, + pub function: String +} #[derive(Debug)] pub enum Message { @@ -22,7 +32,7 @@ pub enum Message { KernelException(&'static eh_artiq::Exception<'static>, &'static [usize]), RpcSend { is_async: bool, data: Arc> }, RpcRecvRequest(*mut ()), - RpcRecvReply(Result), + RpcRecvReply(Result), } static CHANNEL_0TO1: Mutex>> = Mutex::new(None); @@ -150,7 +160,17 @@ extern fn rpc_recv(slot: *mut ()) -> usize { let reply = core1_rx.recv(); match *reply { Message::RpcRecvReply(Ok(alloc_size)) => alloc_size, - Message::RpcRecvReply(Err(_)) => unimplemented!(), + Message::RpcRecvReply(Err(exception)) => unsafe { + eh_artiq::raise(&eh_artiq::Exception { + name: exception.name.as_bytes().as_c_slice(), + file: exception.file.as_bytes().as_c_slice(), + line: exception.line as u32, + column: exception.column as u32, + function: exception.function.as_bytes().as_c_slice(), + message: exception.message.as_bytes().as_c_slice(), + param: exception.param + }) + }, _ => panic!("received unexpected reply to RpcRecvRequest: {:?}", reply) } }