1
0
Fork 0

Exception handling: Implemented RPC exception.

This commit is contained in:
pca006132 2020-07-06 15:34:49 +08:00
parent 1d975dd8bf
commit c59772dca3
3 changed files with 50 additions and 10 deletions

View File

@ -1,20 +1,38 @@
from artiq.experiment import * from artiq.experiment import *
from artiq.language.core import TerminationRequested
class ExceptionDemo(EnvExperiment): class ExceptionDemo(EnvExperiment):
def build(self): def build(self):
self.setattr_device("core") self.setattr_device("core")
self.setattr_device("led0") self.setattr_device("led0")
def foo(self):
print("raise error")
raise Exception
def termination(self):
raise TerminationRequested
@rpc
def remote(self):
raise Exception
@kernel @kernel
def run(self): def run(self):
self.core.reset() self.core.reset()
print("OK!")
try: try:
try: try:
raise Exception self.foo()
except ValueError as e: except ValueError as e:
print("re-raise") print("should not trigger this")
raise e
except: except:
print("error") print("catch all")
try:
self.remote()
except:
print("Error!")
print("Uncaught error at last")
self.termination()

View File

@ -171,7 +171,9 @@ async fn handle_run_kernel(stream: &TcpStream, control: &Rc<RefCell<kernel::Cont
let line = read_i32(stream).await?; let line = read_i32(stream).await?;
let column = read_i32(stream).await?; let column = read_i32(stream).await?;
let function = read_string(stream, 16384).await?; let function = read_string(stream, 16384).await?;
control.tx.async_send(kernel::Message::RpcRecvReply(Err(()))).await; control.tx.async_send(kernel::Message::RpcRecvReply(Err(kernel::RPCException {
name, message, param, file, line, column, function
}))).await;
}, },
_ => { _ => {
error!("unexpected RPC request from host: {:?}", host_request); error!("unexpected RPC request from host: {:?}", host_request);

View File

@ -1,7 +1,7 @@
use core::{ptr, mem}; use core::{ptr, mem};
use log::{debug, info, error}; use log::{debug, info, error};
use alloc::{vec::Vec, sync::Arc}; use alloc::{vec::Vec, sync::Arc, string::String};
use cslice::CSlice; use cslice::{CSlice, AsCSlice};
use libcortex_a9::{enable_fpu, cache::dcci_slice, mutex::Mutex, sync_channel::{self, sync_channel}}; use libcortex_a9::{enable_fpu, cache::dcci_slice, mutex::Mutex, sync_channel::{self, sync_channel}};
use libsupport_zynq::boot::Core1; use libsupport_zynq::boot::Core1;
@ -11,6 +11,16 @@ use crate::eh_artiq;
use crate::rpc; use crate::rpc;
use crate::rtio; 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)] #[derive(Debug)]
pub enum Message { pub enum Message {
@ -22,7 +32,7 @@ pub enum Message {
KernelException(&'static eh_artiq::Exception<'static>, &'static [usize]), KernelException(&'static eh_artiq::Exception<'static>, &'static [usize]),
RpcSend { is_async: bool, data: Arc<Vec<u8>> }, RpcSend { is_async: bool, data: Arc<Vec<u8>> },
RpcRecvRequest(*mut ()), RpcRecvRequest(*mut ()),
RpcRecvReply(Result<usize, ()>), RpcRecvReply(Result<usize, RPCException>),
} }
static CHANNEL_0TO1: Mutex<Option<sync_channel::Receiver<Message>>> = Mutex::new(None); static CHANNEL_0TO1: Mutex<Option<sync_channel::Receiver<Message>>> = Mutex::new(None);
@ -150,7 +160,17 @@ extern fn rpc_recv(slot: *mut ()) -> usize {
let reply = core1_rx.recv(); let reply = core1_rx.recv();
match *reply { match *reply {
Message::RpcRecvReply(Ok(alloc_size)) => alloc_size, 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) _ => panic!("received unexpected reply to RpcRecvRequest: {:?}", reply)
} }
} }